ObjFW  Check-in [382ed34d40]

Overview
Comment:Add global variable of_pagesize and use it to reduce code duplication.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 382ed34d4052f704a5f1e1773dc80b1a9aea093037a2e0ed79f94ac012dba9b9
User & Date: js on 2010-03-13 21:24:16
Other Links: manifest | tags
Context
2010-03-13
21:27
Add tests for OFStream. check-in: aa3e784dd8 user: js tags: trunk
21:24
Add global variable of_pagesize and use it to reduce code duplication. check-in: 382ed34d40 user: js tags: trunk
2010-03-05
03:21
Let operations on OFNumbers return a new one. check-in: 5e65237276 user: js tags: trunk
Changes

Modified src/OFDataArray.m from [5f7745e2a8] to [c912fd9350].

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 * the packaging of this file.
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>

#import "OFDataArray.h"
#import "OFExceptions.h"
#import "macros.h"

#ifdef _WIN32
#include <windows.h>
#endif

static int lastpagebyte = 0;

@implementation OFDataArray
+ dataArrayWithItemSize: (size_t)is
{
	return [[[self alloc] initWithItemSize: is] autorelease];
}

- init







<






<
<
<
<
<
<







9
10
11
12
13
14
15

16
17
18
19
20
21






22
23
24
25
26
27
28
 * the packaging of this file.
 */

#include "config.h"

#include <stdio.h>
#include <string.h>

#include <limits.h>

#import "OFDataArray.h"
#import "OFExceptions.h"
#import "macros.h"







@implementation OFDataArray
+ dataArrayWithItemSize: (size_t)is
{
	return [[[self alloc] initWithItemSize: is] autorelease];
}

- init
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

390
391
392
393
394
395
396
	OF_HASH_FINALIZE(hash);

	return hash;
}
@end

@implementation OFBigDataArray
- initWithItemSize: (size_t)is
{
	self = [super initWithItemSize: is];

	if (lastpagebyte == 0) {
#ifndef _WIN32
		if ((lastpagebyte = sysconf(_SC_PAGESIZE)) == -1)
			lastpagebyte = 4096;
		lastpagebyte--;
#else
		SYSTEM_INFO si;
		GetSystemInfo(&si);
		lastpagebyte = si.dwPageSize - 1;
#endif
	}

	return self;
}

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

	if (SIZE_MAX - count < 1 || count + 1 > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];


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

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

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

	count++;
	size = nsize;

	return self;
}

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

	if (nitems > SIZE_MAX - count || count + nitems > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];


	nsize = ((count + nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

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

	memcpy(data + count * itemsize, carray, nitems * itemsize);

	count += nitems;
	size = nsize;

	return self;
}

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

	if (nitems > SIZE_MAX - count || count + nitems > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];


	nsize = ((count + nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMemory: data
				 toNItems: nsize
				 withSize: itemsize];

	memmove(data + (index + nitems) * itemsize, data + index * itemsize,
	    (count - index) * itemsize);
	memcpy(data + index * itemsize, carray, nitems * itemsize);

	count += nitems;
	size = nsize;

	return self;
}

- removeNItems: (size_t)nitems
{
	size_t nsize;

	if (nitems > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	count -= nitems;

	nsize = (count * itemsize + lastpagebyte) & ~lastpagebyte;

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

	return self;
}

- removeNItems: (size_t)nitems
       atIndex: (size_t)index
{
	size_t nsize;

	if (nitems > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	memmove(data + index * itemsize, data + (index + nitems) * itemsize,
	    (count - index - nitems) * itemsize);

	count -= nitems;

	nsize = (count * itemsize + lastpagebyte) & ~lastpagebyte;

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








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


|




>

















|




>


















|




>



















|





>













|








>







258
259
260
261
262
263
264



















265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
	OF_HASH_FINALIZE(hash);

	return hash;
}
@end

@implementation OFBigDataArray



















- addItem: (void*)item
{
	size_t nsize, lastpagebyte;

	if (SIZE_MAX - count < 1 || count + 1 > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];

	lastpagebyte = of_pagesize - 1;
	nsize = ((count + 1) * itemsize + lastpagebyte) & ~lastpagebyte;

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

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

	count++;
	size = nsize;

	return self;
}

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

	if (nitems > SIZE_MAX - count || count + nitems > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];

	lastpagebyte = of_pagesize - 1;
	nsize = ((count + nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

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

	memcpy(data + count * itemsize, carray, nitems * itemsize);

	count += nitems;
	size = nsize;

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
    atIndex: (size_t)index
{
	size_t nsize, lastpagebyte;

	if (nitems > SIZE_MAX - count || count + nitems > SIZE_MAX / itemsize)
		@throw [OFOutOfRangeException newWithClass: isa];

	lastpagebyte = of_pagesize - 1;
	nsize = ((count + nitems) * itemsize + lastpagebyte) & ~lastpagebyte;

	if (size != nsize)
		data = [self resizeMemory: data
				 toNItems: nsize
				 withSize: itemsize];

	memmove(data + (index + nitems) * itemsize, data + index * itemsize,
	    (count - index) * itemsize);
	memcpy(data + index * itemsize, carray, nitems * itemsize);

	count += nitems;
	size = nsize;

	return self;
}

- removeNItems: (size_t)nitems
{
	size_t nsize, lastpagebyte;

	if (nitems > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	count -= nitems;
	lastpagebyte = of_pagesize - 1;
	nsize = (count * itemsize + lastpagebyte) & ~lastpagebyte;

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

	return self;
}

- removeNItems: (size_t)nitems
       atIndex: (size_t)index
{
	size_t nsize, lastpagebyte;

	if (nitems > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	memmove(data + index * itemsize, data + (index + nitems) * itemsize,
	    (count - index - nitems) * itemsize);

	count -= nitems;
	lastpagebyte = of_pagesize - 1;
	nsize = (count * itemsize + lastpagebyte) & ~lastpagebyte;

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

Modified src/OFObject.h from [eab33580e8] to [573c4d5ce8].

341
342
343
344
345
346
347


 */
@protocol OFMutableCopying
/**
 * \return A copy of the object
 */
- (id)mutableCopy;
@end









>
>
341
342
343
344
345
346
347
348
349
 */
@protocol OFMutableCopying
/**
 * \return A copy of the object
 */
- (id)mutableCopy;
@end

extern size_t of_pagesize;

Modified src/OFObject.m from [864eafb36e] to [4bc8df336e].

10
11
12
13
14
15
16

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31




32
33
34
35
36
37
38
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <limits.h>
#include <assert.h>

#import "OFObject.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#import <objc/objc-api.h>
#ifdef OF_APPLE_RUNTIME
# import <objc/runtime.h>
#endif
#ifdef OF_GNU_RUNTIME
# import <objc/sarray.h>
#endif





#ifdef OF_ATOMIC_OPS
# import "atomic.h"
#else
# import "threading.h"
#endif








>















>
>
>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <assert.h>

#import "OFObject.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#import <objc/objc-api.h>
#ifdef OF_APPLE_RUNTIME
# import <objc/runtime.h>
#endif
#ifdef OF_GNU_RUNTIME
# import <objc/sarray.h>
#endif

#ifdef _WIN32
# include <windows.h>
#endif

#ifdef OF_ATOMIC_OPS
# import "atomic.h"
#else
# import "threading.h"
#endif

57
58
59
60
61
62
63


64
65
66
67
68
69
70
/* Hopefully no arch needs more than 16 bytes padding */
#define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + 15) & ~15)
#define PRE_IVAR ((struct pre_ivar*)((char*)self - PRE_IVAR_ALIGN))

static struct {
	Class isa;
} alloc_failed_exception;



#ifdef NEED_OBJC_SYNC_INIT
extern BOOL objc_sync_init();
#endif

#ifdef NEED_OBJC_PROPERTIES_INIT
extern BOOL objc_properties_init();







>
>







62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Hopefully no arch needs more than 16 bytes padding */
#define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + 15) & ~15)
#define PRE_IVAR ((struct pre_ivar*)((char*)self - PRE_IVAR_ALIGN))

static struct {
	Class isa;
} alloc_failed_exception;

size_t of_pagesize;

#ifdef NEED_OBJC_SYNC_INIT
extern BOOL objc_sync_init();
#endif

#ifdef NEED_OBJC_PROPERTIES_INIT
extern BOOL objc_properties_init();
101
102
103
104
105
106
107









108
109
110
111
112
113
114
		abort();
	}
#endif

#ifdef OF_APPLE_RUNTIME
	objc_setEnumerationMutationHandler(enumeration_mutation_handler);
#endif









}

+ (void)initialize
{
}

+ alloc







>
>
>
>
>
>
>
>
>







108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
		abort();
	}
#endif

#ifdef OF_APPLE_RUNTIME
	objc_setEnumerationMutationHandler(enumeration_mutation_handler);
#endif

#ifndef _WIN32
	if ((of_pagesize = sysconf(_SC_PAGESIZE)) < 1)
		of_pagesize = 4096;
#else
	SYSTEM_INFO si;
	GetSystemInfo(&si);
	of_pagesize = si.dwPageSize;
#endif
}

+ (void)initialize
{
}

+ alloc

Modified src/OFStream.m from [4230af470d] to [c5232e5990].

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"

#include <string.h>
#include <unistd.h>
#include <assert.h>

#import "OFStream.h"
#import "OFString.h"
#import "OFExceptions.h"
#import "macros.h"

#ifdef _WIN32
# include <windows.h>
#endif

static int pagesize = 0;

@implementation OFStream
- init
{
	self = [super init];

	if (isa == [OFStream class])
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];

	cache = NULL;

#ifndef _WIN32
	if (pagesize == 0)
		if ((pagesize = sysconf(_SC_PAGESIZE)) == -1)
			pagesize = 4096;
#else
	if (pagesize == 0) {
		SYSTEM_INFO si;
		GetSystemInfo(&si);
		pagesize = si.dwPageSize - 1;
	}
#endif

	return self;
}

- (BOOL)atEndOfStream
{
	if (cache != NULL)
		return NO;







<







<
<
<
<
<
<











<
<
<
<
<
<
<
<
<
<
<
<







8
9
10
11
12
13
14

15
16
17
18
19
20
21






22
23
24
25
26
27
28
29
30
31
32












33
34
35
36
37
38
39
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"

#include <string.h>

#include <assert.h>

#import "OFStream.h"
#import "OFString.h"
#import "OFExceptions.h"
#import "macros.h"







@implementation OFStream
- init
{
	self = [super init];

	if (isa == [OFStream class])
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];

	cache = NULL;













	return self;
}

- (BOOL)atEndOfStream
{
	if (cache != NULL)
		return NO;
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

				return ret;
			}
		}
	}

	/* Read until we get a newline or \0 */
	tmp = [self allocMemoryWithSize: pagesize];

	@try {
		for (;;) {
			if ([self atEndOfStreamWithoutCache]) {
				if (cache == NULL)
					return nil;








|







121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

				return ret;
			}
		}
	}

	/* Read until we get a newline or \0 */
	tmp = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self atEndOfStreamWithoutCache]) {
				if (cache == NULL)
					return nil;

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
				[self freeMemory: cache];
				cache = NULL;
				cache_len = 0;

				return ret;
			}

			len = [self readNBytesWithoutCache: pagesize
						intoBuffer: tmp];

			/* Look if there's a newline or \0 */
			for (i = 0; i < len; i++) {
				if (OF_UNLIKELY(tmp[i] == '\n' ||
				    tmp[i] == '\0')) {
					ret_len = cache_len + i;







|







145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
				[self freeMemory: cache];
				cache = NULL;
				cache_len = 0;

				return ret;
			}

			len = [self readNBytesWithoutCache: of_pagesize
						intoBuffer: tmp];

			/* Look if there's a newline or \0 */
			for (i = 0; i < len; i++) {
				if (OF_UNLIKELY(tmp[i] == '\n' ||
				    tmp[i] == '\0')) {
					ret_len = cache_len + i;
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

				return ret;
			}
		}
	}

	/* Read until we get the delimiter or \0 */
	tmp = [self allocMemoryWithSize: pagesize];

	@try {
		for (;;) {
			if ([self atEndOfStreamWithoutCache]) {
				if (cache == NULL)
					return nil;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: cache_len];

				[self freeMemory: cache];
				cache = NULL;
				cache_len = 0;

				return ret;
			}

			len = [self readNBytesWithoutCache: pagesize
						intoBuffer: tmp];

			/* Look if there's the delimiter or \0 */
			for (i = 0; i < len; i++) {
				if (tmp[i] != delim[j++])
					j = 0;








|


















|







262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

				return ret;
			}
		}
	}

	/* Read until we get the delimiter or \0 */
	tmp = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self atEndOfStreamWithoutCache]) {
				if (cache == NULL)
					return nil;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: cache_len];

				[self freeMemory: cache];
				cache = NULL;
				cache_len = 0;

				return ret;
			}

			len = [self readNBytesWithoutCache: of_pagesize
						intoBuffer: tmp];

			/* Look if there's the delimiter or \0 */
			for (i = 0; i < len; i++) {
				if (tmp[i] != delim[j++])
					j = 0;