ObjFW  Diff

Differences From Artifact [9af5fde103]:

To Artifact [bb3cd515fc]:

  • File src/OFDataArray.m — part of check-in [e1e7ffa903] at 2011-09-22 23:25:42 on branch trunk — Exceptions are now autoreleased.

    This is safe as an "exception loop" can't happen, since if allocating
    an exception fails, it throws an OFAllocFailedException which is
    preallocated and can always be thrown.

    So, the worst case would be that an autorelease of an exception fails,
    triggering an OFOutOfMemoryException for which there is no memory,
    resulting in an OFAllocFailedException to be thrown. (user: js, size: 12126) [annotate] [blame] [check-ins using]


82
83
84
85
86
87
88
89
90


91
92
93
94
95
96
97
82
83
84
85
86
87
88


89
90
91
92
93
94
95
96
97







-
-
+
+







- initWithItemSize: (size_t)itemSize_
{
	self = [super init];

	if (itemSize_ == 0) {
		Class c = isa;
		[self release];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: c
							     selector: _cmd];
	}

	itemSize = itemSize_;

	return self;
}

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
189
190
191
192
193
194



195
196
197
198
199
200
201
202


203
204
205
206
207
208
209
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
189
190
191
192


193
194
195
196
197
198
199
200
201
202

203
204
205
206
207
208
209
210
211







-
-
-
+
+
+

















-
+

















-
-
+
+
+







-
+
+







	}

	request = [OFHTTPRequest requestWithURL: URL];
	result = [request perform];

	if ([result statusCode] != 200)
		@throw [OFHTTPRequestFailedException
		    newWithClass: [request class]
		     HTTPRequest: request
			  result: result];
		    exceptionWithClass: [request class]
			   HTTPRequest: request
				result: result];

	self = [[result data] retain];
	[pool release];
	return self;
}

- initWithBase64EncodedString: (OFString*)string
{
	self = [super init];

	itemSize = 1;

	if (!of_base64_decode(self,
	    [string cStringWithEncoding: OF_STRING_ENCODING_ASCII],
	    [string cStringLengthWithEncoding: OF_STRING_ENCODING_ASCII])) {
		Class c = isa;
		[self release];
		@throw [OFInvalidEncodingException newWithClass: c];
		@throw [OFInvalidEncodingException exceptionWithClass: c];
	}

	return self;
}

- initWithSerialization: (OFXMLElement*)element
{
	self = [super init];

	itemSize = 1;

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFString *stringValue;

		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
				      selector: _cmd];

		stringValue = [element stringValue];

		if (!of_base64_decode(self,
		    [stringValue cStringWithEncoding: OF_STRING_ENCODING_ASCII],
		    [stringValue cStringLengthWithEncoding:
		    OF_STRING_ENCODING_ASCII]))
			@throw [OFInvalidEncodingException newWithClass: isa];
			@throw [OFInvalidEncodingException
			    exceptionWithClass: isa];

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}

224
225
226
227
228
229
230
231

232
233
234
235
236
237
238
226
227
228
229
230
231
232

233
234
235
236
237
238
239
240







-
+







{
	return data;
}

- (void*)itemAtIndex: (size_t)index
{
	if (index >= count)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	return data + index * itemSize;
}

- (void*)firstItem
{
	if (data == NULL || count == 0)
248
249
250
251
252
253
254
255

256
257
258
259
260
261
262
250
251
252
253
254
255
256

257
258
259
260
261
262
263
264







-
+








	return data + (count - 1) * itemSize;
}

- (void)addItem: (const void*)item
{
	if (SIZE_MAX - count < 1)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	data = [self resizeMemory: data
			 toNItems: count + 1
			   ofSize: itemSize];

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

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
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







-
+














-
+







		atIndex: index];
}

- (void)addNItems: (size_t)nItems
       fromCArray: (const void*)cArray
{
	if (nItems > SIZE_MAX - count)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	data = [self resizeMemory: data
			 toNItems: count + nItems
			   ofSize: itemSize];

	memcpy(data + count * itemSize, cArray, nItems * itemSize);
	count += nItems;
}

- (void)addNItems: (size_t)nItems
       fromCArray: (const void*)cArray
	  atIndex: (size_t)index
{
	if (nItems > SIZE_MAX - count)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	data = [self resizeMemory: data
			 toNItems: count + nItems
			   ofSize: itemSize];

	memmove(data + (index + nItems) * itemSize, data + index * itemSize,
	    (count - index) * itemSize);
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
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







-
+









-







-
+











-












-







	[self removeNItems: 1
		   atIndex: index];
}

- (void)removeNItems: (size_t)nItems
{
	if (nItems > count)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];


	count -= nItems;
	@try {
		data = [self resizeMemory: data
				 toNItems: count
				   ofSize: itemSize];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- (void)removeNItems: (size_t)nItems
	     atIndex: (size_t)index
{
	if (nItems > count)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	memmove(data + index * itemSize, data + (index + nItems) * itemSize,
	    (count - index - nItems) * itemSize);

	count -= nItems;
	@try {
		data = [self resizeMemory: data
				 toNItems: count
				   ofSize: itemSize];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- (void)removeLastItem
{
	count--;
	@try {
		data = [self resizeMemory: data
				 toNItems: count
				   ofSize: itemSize];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only made it smaller */
		[e release];
	}
}

- copy
{
	OFDataArray *copy = [[isa alloc] initWithItemSize: itemSize];

390
391
392
393
394
395
396
397
398


399
400
401
402
403


404
405
406
407
408
409
410
389
390
391
392
393
394
395


396
397
398
399
400


401
402
403
404
405
406
407
408
409







-
-
+
+



-
-
+
+







- (of_comparison_result_t)compare: (id)object
{
	OFDataArray *otherDataArray;
	int comparison;
	size_t otherCount, minimumCount;

	if (![object isKindOfClass: [OFDataArray class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];
	otherDataArray = object;

	if ([otherDataArray itemSize] != itemSize)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	otherCount = [otherDataArray count];
	minimumCount = (count > otherCount ? otherCount : count);

	if ((comparison = memcmp(data, [otherDataArray cArray],
	    minimumCount * itemSize)) == 0) {
		if (count > otherCount)
453
454
455
456
457
458
459
460
461


462
463
464
465
466
467
468
452
453
454
455
456
457
458


459
460
461
462
463
464
465
466
467







-
-
+
+








- (OFXMLElement*)XMLElementBySerializing
{
	OFAutoreleasePool *pool;
	OFXMLElement *element;

	if (itemSize != 1)
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];
		@throw [OFNotImplementedException exceptionWithClass: isa
							    selector: _cmd];

	pool = [[OFAutoreleasePool alloc] init];
	element = [OFXMLElement
	    elementWithName: [self className]
		  namespace: OF_SERIALIZATION_NS
		stringValue: of_base64_encode(data, count * itemSize)];

476
477
478
479
480
481
482
483

484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504

505
506
507
508
509
510
511
475
476
477
478
479
480
481

482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502

503
504
505
506
507
508
509
510







-
+




















-
+








@implementation OFBigDataArray
- (void)addItem: (const void*)item
{
	size_t newSize, lastPageByte;

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

	lastPageByte = of_pagesize - 1;
	newSize = ((count + 1) * itemSize + lastPageByte) & ~lastPageByte;

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

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

	count++;
	size = newSize;
}

- (void)addNItems: (size_t)nItems
       fromCArray: (const void*)cArray
{
	size_t newSize, lastPageByte;

	if (nItems > SIZE_MAX - count || count + nItems > SIZE_MAX / itemSize)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	lastPageByte = of_pagesize - 1;
	newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte;

	if (size != newSize)
		data = [self resizeMemory: data
				   toSize: newSize];
519
520
521
522
523
524
525
526

527
528
529
530
531
532
533
518
519
520
521
522
523
524

525
526
527
528
529
530
531
532







-
+







- (void)addNItems: (size_t)nItems
       fromCArray: (const void*)cArray
	  atIndex: (size_t)index
{
	size_t newSize, lastPageByte;

	if (nItems > SIZE_MAX - count || count + nItems > SIZE_MAX / itemSize)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	lastPageByte = of_pagesize - 1;
	newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte;

	if (size != newSize)
		data = [self resizeMemory: data
				 toNItems: newSize
542
543
544
545
546
547
548
549

550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

568
569
570
571
572
573
574
541
542
543
544
545
546
547

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565

566
567
568
569
570
571
572
573







-
+

















-
+







}

- (void)removeNItems: (size_t)nItems
{
	size_t newSize, lastPageByte;

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

	count -= nItems;
	lastPageByte = of_pagesize - 1;
	newSize = (count * itemSize + lastPageByte) & ~lastPageByte;

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

- (void)removeNItems: (size_t)nItems
	     atIndex: (size_t)index
{
	size_t newSize, lastPageByte;

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

	memmove(data + index * itemSize, data + (index + nItems) * itemSize,
	    (count - index - nItems) * itemSize);

	count -= nItems;
	lastPageByte = of_pagesize - 1;
	newSize = (count * itemSize + lastPageByte) & ~lastPageByte;