ObjFW  Check-in [59f16e8a3d]

Overview
Comment:Implementation for OFBigArray.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 59f16e8a3d010e4387800c45c029bc3b9da8002c808cd41097845ce92c22cfc9
User & Date: js on 2008-11-07 17:25:08
Other Links: manifest | tags
Context
2008-11-07
20:25
Don't allocate 2 pages if we need exactly pagesize. check-in: 252ecc91a1 user: js tags: trunk
17:25
Implementation for OFBigArray. check-in: 59f16e8a3d user: js tags: trunk
14:29
Everything should be documented now. check-in: e8e45975df user: js tags: trunk
Changes

Modified TODO from [e12a5a7d6a] to [1c6f68e944].

1
2
3

4
5
6
7
8
9
10
Tests for OFFile.

OFArray

OFDictionary
OFSortedArray
OFSocket
OFThread
OFAutoreleasePool

OFStack



>







1
2
3
4
5
6
7
8
9
10
11
Tests for OFFile.

OFArray
OFDirectory
OFDictionary
OFSortedArray
OFSocket
OFThread
OFAutoreleasePool

OFStack

Modified src/OFArray.m from [97782e7ade] to [c474c9a944].

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18


19
20
21
22
23
24
25
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <stdio.h>
#import <string.h>


#import "OFArray.h"

#import "OFExceptions.h"
#import "OFMacros.h"



@implementation OFArray
+ newWithItemSize: (size_t)is
{
	return [[OFArray alloc] initWithItemSize: is];
}














>





>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <stdio.h>
#import <string.h>
#import <unistd.h>

#import "OFArray.h"

#import "OFExceptions.h"
#import "OFMacros.h"

static size_t pagesize = 0;

@implementation OFArray
+ newWithItemSize: (size_t)is
{
	return [[OFArray alloc] initWithItemSize: is];
}

60
61
62
63
64
65
66



67
68
69
70
71
72
73
- (void*)last
{
	return data + (items - 1) * itemsize;
}

- add: (void*)item
{



	data = [self resizeMem: data
		      toNItems: items + 1
			ofSize: itemsize];

	memcpy(data + items++ * itemsize, item, itemsize);

	return self;







>
>
>







63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
- (void*)last
{
	return data + (items - 1) * itemsize;
}

- add: (void*)item
{
	if (SIZE_MAX - items < 1)
		[[OFOutOfRangeException newWithObject: self] raise];

	data = [self resizeMem: data
		      toNItems: items + 1
			ofSize: itemsize];

	memcpy(data + items++ * itemsize, item, itemsize);

	return self;
108
109
110
111
112
113
114



115
116
117
118
119
120
121
122
123

124


125

126



127







128
129

130


131

132









133
+ newWithItemSize: (size_t)is
{
	return [[OFBigArray alloc] initWithItemSize: is];
}

- initWithItemSize: (size_t)is
{



	if ((self = [super init]))
		size = 0;

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{

	/* FIXME */


	OF_NOT_IMPLEMENTED(self)

}











- removeNItems: (size_t)nitems
{

	/* FIXME */


	OF_NOT_IMPLEMENTED(self)

}









@end







>
>
>
|








>
|
>
>
|
>
|
>
>
>

>
>
>
>
>
>
>


>
|
>
>
|
>
|
>
>
>
>
>
>
>
>
>

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
+ newWithItemSize: (size_t)is
{
	return [[OFBigArray alloc] initWithItemSize: is];
}

- initWithItemSize: (size_t)is
{
	if (pagesize == 0)
		pagesize = getpagesize();

	if ((self = [super initWithItemSize: is]))
		size = 0;

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	size_t nsize;

	if (nitems > SIZE_MAX - items || items + nitems > SIZE_MAX / itemsize)
		[[OFOutOfRangeException newWithObject: self] raise];

	nsize = (((items + nitems) * itemsize) / pagesize) + 1;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	memcpy(data + items * itemsize, carray, nitems * itemsize);
	items += nitems;
	size = nsize;

	return self;
}

- removeNItems: (size_t)nitems
{
	size_t nsize;

	if (nitems > items)
		[[OFOutOfRangeException newWithObject: self] raise];

	nsize = (((items - nitems) * itemsize) / pagesize) + 1;

	if (size != nsize)
		data = [self resizeMem: data
				toSize: nsize];

	items -= nitems;
	size = nsize;

	return self;
}
@end

Modified src/OFObject.m from [014c4c85fd] to [f326f71c26].

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
		  ofSize: (size_t)size
{
	size_t memsize;
	
	if (nitems == 0 || size == 0)
		return NULL;

	if (size > SIZE_MAX / nitems)
		[[OFOutOfRangeException newWithObject: self] raise];

	memsize = nitems * size;
	return [self getMemWithSize: memsize];
}

- (void*)resizeMem: (void*)ptr







|







80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
		  ofSize: (size_t)size
{
	size_t memsize;
	
	if (nitems == 0 || size == 0)
		return NULL;

	if (nitems > SIZE_MAX / size)
		[[OFOutOfRangeException newWithObject: self] raise];

	memsize = nitems * size;
	return [self getMemWithSize: memsize];
}

- (void*)resizeMem: (void*)ptr
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
				      ofSize: size];
	
	if (nitems == 0 || size == 0) {
		[self freeMem: ptr];
		return NULL;
	}

	if (size > SIZE_MAX / nitems)
		[[OFOutOfRangeException newWithObject: self] raise];

	memsize = nitems * size;
	return [self resizeMem: ptr
			toSize: memsize];
}








|







131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
				      ofSize: size];
	
	if (nitems == 0 || size == 0) {
		[self freeMem: ptr];
		return NULL;
	}

	if (nitems > SIZE_MAX / size)
		[[OFOutOfRangeException newWithObject: self] raise];

	memsize = nitems * size;
	return [self resizeMem: ptr
			toSize: memsize];
}

Modified tests/OFArray/OFArray.m from [d6db94dbe6] to [ad07c5ffd2].

29
30
31
32
33
34
35














































































36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}

const char *str = "Hallo!";















































































int
main()
{
	BOOL caught;
	OFArray *a;
	void *p, *q;
	size_t i;

	puts("Trying to add too much to an array...");
	a = [OFArray newWithItemSize: 4096];
	CATCH_EXCEPTION([a addNItems: SIZE_MAX
			  fromCArray: NULL],
	    OFOutOfRangeException)

	puts("Trying to add something after that error...");
	p = [a getMemWithSize: 4096];
	memset(p, 255, 4096);
	[a add: p];
	if (!memcmp([a last], p, 4096))
		puts("[a last] matches with p!");
	else {
		puts("[a last] does not match p!");
		abort();
	}
	[a freeMem: p];

	puts("Adding more data...");
	q = [a getMemWithSize: 4096];
	memset(q, 42, 4096);
	[a add: q];
	if (!memcmp([a last], q, 4096))
		puts("[a last] matches with q!");
	else {
		puts("[a last] does not match q!");
		abort();
	}
	[a freeMem: q];

	puts("Adding multiple items at once...");
	p = [a getMemWithSize: 8192];
	memset(p, 64, 8192);
	[a addNItems: 2
	  fromCArray: p];
	if (!memcmp([a last], [a item: [a items] - 2], 4096) &&
	    !memcmp([a item: [a items] - 2], p, 4096))
		puts("[a last], [a item: [a items] - 2] and p match!");
	else {
		puts("[a last], [a item: [a items] - 2] and p did not match!");
		abort();
	}
	[a freeMem: p];

	i = [a items];
	puts("Removing 2 items...");
	[a removeNItems: 2];
	if ([a items] + 2 != i) {
		puts("[a items] + 2 != i!");
		abort();
	}

	puts("Trying to remove more data than we added...");
	CATCH_EXCEPTION([a removeNItems: [a items] + 1], OFOutOfRangeException);

	puts("Trying to access an index that does not exist...");
	CATCH_EXCEPTION([a item: [a items]], OFOutOfRangeException);

	[a free];

	puts("Creating new array and using it to build a string...");
	a = [OFArray newWithItemSize: 1];

	for (i = 0; i < strlen(str); i++)
		[a add: (void*)(str + i)];
	[a add: ""];

	if (!strcmp([a data], str))
		puts("Built string matches!");
	else {
		puts("Built string does not match!");
		abort();
	}

	[a free];

	return 0;
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|



<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
|
<
|
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121





122




























123






124

125







126























127
128
129
	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}

const char *str = "Hallo!";

#define TEST(type) \
	puts("Trying to add too much to an array...");			\
	a = [type newWithItemSize: 4096];				\
	CATCH_EXCEPTION([a addNItems: SIZE_MAX				\
			  fromCArray: NULL],				\
	    OFOutOfRangeException)					\
									\
	puts("Trying to add something after that error...");		\
	p = [a getMemWithSize: 4096];					\
	memset(p, 255, 4096);						\
	[a add: p];							\
	if (!memcmp([a last], p, 4096))					\
		puts("[a last] matches with p!");			\
	else {								\
		puts("[a last] does not match p!");			\
		abort();						\
	}								\
	[a freeMem: p];							\
									\
	puts("Adding more data...");					\
	q = [a getMemWithSize: 4096];					\
	memset(q, 42, 4096);						\
	[a add: q];							\
	if (!memcmp([a last], q, 4096))					\
		puts("[a last] matches with q!");			\
	else {								\
		puts("[a last] does not match q!");			\
		abort();						\
	}								\
	[a freeMem: q];							\
									\
	puts("Adding multiple items at once...");			\
	p = [a getMemWithSize: 8192];					\
	memset(p, 64, 8192);						\
	[a addNItems: 2							\
	  fromCArray: p];						\
	if (!memcmp([a last], [a item: [a items] - 2], 4096) &&		\
	    !memcmp([a item: [a items] - 2], p, 4096))			\
		puts("[a last], [a item: [a items] - 2] and p match!");	\
	else {								\
		puts("[a last], [a item: [a items] - 2] and p did not match!");\
		abort();						\
	}								\
	[a freeMem: p];							\
									\
	i = [a items];							\
	puts("Removing 2 items...");					\
	[a removeNItems: 2];						\
	if ([a items] + 2 != i) {					\
		puts("[a items] + 2 != i!");				\
		abort();						\
	}								\
									\
	puts("Trying to remove more data than we added...");		\
	CATCH_EXCEPTION([a removeNItems: [a items] + 1],		\
	    OFOutOfRangeException);					\
									\
	puts("Trying to access an index that does not exist...");	\
	CATCH_EXCEPTION([a item: [a items]], OFOutOfRangeException);	\
									\
	[a free];							\
									\
	puts("Creating new array and using it to build a string...");	\
	a = [type newWithItemSize: 1];				\
									\
	for (i = 0; i < strlen(str); i++)				\
		[a add: (void*)(str + i)];				\
	[a add: ""];							\
									\
	if (!strcmp([a data], str))					\
		puts("Built string matches!");				\
	else {								\
		puts("Built string does not match!");			\
		abort();						\
	}								\
									\
	[a free];

int
main()
{
	BOOL caught;
	id a;
	void *p, *q;
	size_t i;






	puts("== TESTING OFArray ==");




























	TEST(OFArray)








	puts("== TESTING OFBigArray ==");







	TEST(OFBigArray)
























	return 0;
}

Modified tests/OFObject/OFObject.m from [2848a193bd] to [49c951c9b1].

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	puts("Now trying to free them again...");
	CATCH_EXCEPTION([obj freeMem: p], OFMemNotPartOfObjException)
	CATCH_EXCEPTION([obj freeMem: q], OFMemNotPartOfObjException)
	CATCH_EXCEPTION([obj freeMem: r], OFMemNotPartOfObjException)
	puts("Got all 3!");
	
	puts("Trying to allocate more memory than possible...");
	CATCH_EXCEPTION(p = [obj getMemWithSize: 4294967295U], OFNoMemException)

	puts("Allocating 1 byte...");
	p = [obj getMemWithSize: 1];

	puts("Trying to resize that 1 byte to more than possible...");
	CATCH_EXCEPTION(p = [obj resizeMem: p
				    toSize: 4294967295U],
	    OFNoMemException)

	puts("Trying to resize NULL to 1024 bytes...");
	p = [obj resizeMem: NULL
		    toSize: 1024];
	[obj freeMem: p];
	







|






|







74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	puts("Now trying to free them again...");
	CATCH_EXCEPTION([obj freeMem: p], OFMemNotPartOfObjException)
	CATCH_EXCEPTION([obj freeMem: q], OFMemNotPartOfObjException)
	CATCH_EXCEPTION([obj freeMem: r], OFMemNotPartOfObjException)
	puts("Got all 3!");
	
	puts("Trying to allocate more memory than possible...");
	CATCH_EXCEPTION(p = [obj getMemWithSize: SIZE_MAX], OFNoMemException)

	puts("Allocating 1 byte...");
	p = [obj getMemWithSize: 1];

	puts("Trying to resize that 1 byte to more than possible...");
	CATCH_EXCEPTION(p = [obj resizeMem: p
				    toSize: SIZE_MAX],
	    OFNoMemException)

	puts("Trying to resize NULL to 1024 bytes...");
	p = [obj resizeMem: NULL
		    toSize: 1024];
	[obj freeMem: p];