ObjFW  Check-in [ae28fbf3ca]

Overview
Comment:Add missing add: for OFBigArray and fix calculation of nsize.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ae28fbf3cafae1b88056537dc9010962df1fbd93623996ee64e96c2d898f1ae2
User & Date: js on 2008-11-08 17:57:22
Other Links: manifest | tags
Context
2008-11-08
20:28
Optimize OFObject. check-in: 14ba9e517b user: js tags: trunk
17:57
Add missing add: for OFBigArray and fix calculation of nsize. check-in: ae28fbf3ca user: js tags: trunk
2008-11-07
20:25
Don't allocate 2 pages if we need exactly pagesize. check-in: 252ecc91a1 user: js tags: trunk
Changes

Modified src/OFArray.h from [1b567eb69c] to [8aff812fdb].

117
118
119
120
121
122
123







124
125
126
127
128
129
130
 * size.
 * 
 * \param is The size of each element in the OFBigArray
 * \return An initialized OFBigArray
 */
- initWithItemSize: (size_t)is;








/**
 * Adds items from a C array to the OFBigArray.
 *
 * \param nitems The number of items to add
 * \param carray A C array containing the items to add
 */
- addNItems: (size_t)nitems







>
>
>
>
>
>
>







117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 * size.
 * 
 * \param is The size of each element in the OFBigArray
 * \return An initialized OFBigArray
 */
- initWithItemSize: (size_t)is;

/**
 * Adds an item to the OFBigArray.
 *
 * \param item An arbitrary item
 */
- add: (void*)item;

/**
 * Adds items from a C array to the OFBigArray.
 *
 * \param nitems The number of items to add
 * \param carray A C array containing the items to add
 */
- addNItems: (size_t)nitems

Modified src/OFArray.m from [84ed4421e5] to [b60d88894a].

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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];
}








|







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#import <unistd.h>

#import "OFArray.h"

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

static size_t lastpagebyte = 0;

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

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) / pagesize;

	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) / pagesize;

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

	items -= nitems;
	size = nsize;

	return 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
+ newWithItemSize: (size_t)is
{
	return [[OFBigArray alloc] initWithItemSize: is];
}

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

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

	return self;
}

- add: (void*)item
{
	size_t nsize;

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

	nsize = ((items + 1) * itemsize + lastpagebyte) & ~lastpagebyte;

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

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

	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 + lastpagebyte) & ~lastpagebyte;

	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 + lastpagebyte) & ~lastpagebyte;

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

	items -= nitems;
	size = nsize;

	return self;
}
@end