ObjFW  Check-in [da1fb6b21c]

Overview
Comment:Further reduce usage of -[allocMemoryWithSize:]
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: da1fb6b21c4f39eff720e320db57c7fdfd1ae8819447de74ca9aa87def7cec53
User & Date: js on 2020-11-05 02:27:35
Other Links: manifest | tags
Context
2020-11-05
02:35
Remove -[allocZeroedMemoryWithSize:] check-in: 2bb3136a7f user: js tags: trunk
02:27
Further reduce usage of -[allocMemoryWithSize:] check-in: da1fb6b21c user: js tags: trunk
00:24
Avoid -[allocMemoryWithSize:] for temporary memory check-in: 0ddaac3f5b user: js tags: trunk
Changes

Modified src/OFArray.m from [e881543a8d] to [eac8a2ce35].

236
237
238
239
240
241
242
243
244



245
246
247
248
249
250
251


252
253
254
255
256
257
258
236
237
238
239
240
241
242


243
244
245
246
247





248
249
250
251
252
253
254
255
256







-
-
+
+
+


-
-
-
-
-
+
+







{
	for (size_t i = 0; i < range.length; i++)
		buffer[i] = [self objectAtIndex: range.location + i];
}

- (id const *)objects
{
	OFObject *container;
	size_t count;
	size_t count = self.count;
	OFMutableData *data = [OFMutableData dataWithItemSize: sizeof(id)
						     capacity: count];
	id *buffer;

	container = [[[OFObject alloc] init] autorelease];
	count = self.count;
	buffer = [container allocMemoryWithSize: sizeof(*buffer)
					  count: count];

	[data increaseCountBy: count];
	buffer = data.mutableItems;
	[self getObjects: buffer
		 inRange: of_range(0, count)];

	return buffer;
}

- (id)copy

Modified src/OFBitSetCharacterSet.m from [a3efdbfddb] to [a0186359ac].

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







-
+
-
















+
+
+
+
+
+
+










				if (UINT32_MAX - c < 1)
					@throw [OFOutOfRangeException
					    exception];

				newSize = OF_ROUND_UP_POW2(8, c + 1) / 8;

				_bitset = [self resizeMemory: _bitset
				_bitset = of_realloc(_bitset, newSize, 1);
							size: newSize];
				memset(_bitset + _size, '\0', newSize - _size);

				_size = newSize;
			}

			of_bitset_set(_bitset, c);
		}

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

	return self;
}

- (void)dealloc
{
	free(_bitset);

	[super dealloc];
}

- (bool)characterIsMember: (of_unichar_t)character
{
	if (character / 8 >= _size)
		return false;

	return of_bitset_isset(_bitset, character);
}
@end

Modified src/OFBytesValue.m from [812d7bedad] to [54953a495e].

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







-
+









+
+
+
+
+
+
+










		     objCType: (const char *)objCType
{
	self = [super init];

	@try {
		_size = of_sizeof_type_encoding(objCType);
		_objCType = objCType;
		_bytes = [self allocMemoryWithSize: _size];
		_bytes = of_malloc(1, _size);

		memcpy(_bytes, bytes, _size);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	free(_bytes);

	[super dealloc];
}

- (void)getValue: (void *)value
	    size: (size_t)size
{
	if (size != _size)
		@throw [OFOutOfRangeException exception];

	memcpy(value, _bytes, _size);
}
@end

Modified src/OFDNSResolver.m from [abd3a40827] to [3373845de9].

555
556
557
558
559
560
561

562
563
564
565
566
567
568
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569







+







	[_query release];
	[_ID release];
	[_settings release];
	[_delegate release];
	[_queryData release];
	[_TCPSocket release];
	[_TCPQueryData release];
	free(_TCPBuffer);
	[_cancelTimer release];

	[super dealloc];
}
@end

@implementation OFDNSResolver
1133
1134
1135
1136
1137
1138
1139
1140

1141
1142
1143
1144
1145
1146
1147
1148
1134
1135
1136
1137
1138
1139
1140

1141

1142
1143
1144
1145
1146
1147
1148







-
+
-







		[context->_TCPSocket release];
		context->_TCPSocket = nil;
		context->_responseLength = 0;
		return nil;
	}

	if (context->_TCPBuffer == nil)
		context->_TCPBuffer =
		context->_TCPBuffer = of_malloc(MAX_DNS_RESPONSE_LENGTH, 1);
		    [context allocMemoryWithSize: MAX_DNS_RESPONSE_LENGTH];

	[sock asyncReadIntoBuffer: context->_TCPBuffer
		      exactLength: 2];
	return nil;
}

-      (bool)stream: (OFStream *)stream

Modified src/OFInflateStream.m from [c1d8d6e047] to [b112db9229].

192
193
194
195
196
197
198
199
200

201
202
203
204
205
206
207
208
209
210
211
212
213


214



215
216
217

218
219
220
221
222
223
224
192
193
194
195
196
197
198


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

215
216
217
218
219
220
221
222
223
224
225
226
227
228







-
-
+













+
+
-
+
+
+



+







		_bitIndex = 8;

#ifdef OF_INFLATE64_STREAM_M
		_slidingWindowMask = 0xFFFF;
#else
		_slidingWindowMask = 0x7FFF;
#endif
		_slidingWindow = [self allocZeroedMemoryWithSize:
		    _slidingWindowMask + 1];
		_slidingWindow = of_calloc(_slidingWindowMask + 1, 1);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	if (_stream != nil)
		[self close];

	free(_slidingWindow);

	if (_state == HUFFMAN_TREE)
	if (_state == HUFFMAN_TREE) {
		free(_context.huffmanTree.lengths);

		if (_context.huffmanTree.codeLenTree != NULL)
			of_huffman_tree_release(
			    _context.huffmanTree.codeLenTree);
	}

	if (_state == HUFFMAN_TREE || _state == HUFFMAN_BLOCK) {
		if (_context.huffman.litLenTree != fixedLitLenTree)
			of_huffman_tree_release(_context.huffman.litLenTree);
		if (_context.huffman.distTree != fixedDistTree)
			of_huffman_tree_release(_context.huffman.distTree);
	}
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

397
398
399
400
401
402
403
404


405
406
407
408
409
410
411
376
377
378
379
380
381
382

383

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

399
400
401
402
403
404
405


406
407
408
409
410
411
412
413
414







-
+
-















-
+






-
-
+
+







				if OF_UNLIKELY (!tryReadBits(self, &bits, 4))
					return bytesWritten;

				CTX.codeLenCodesCount = bits;
			}

			if OF_LIKELY (CTX.lengths == NULL)
				CTX.lengths = [self
				CTX.lengths = of_calloc(19, 1);
				    allocZeroedMemoryWithSize: 19];

			for (uint16_t i = CTX.receivedCount;
			    i < CTX.codeLenCodesCount + 4; i++) {
				if OF_UNLIKELY (!tryReadBits(self, &bits, 3)) {
					CTX.receivedCount = i;
					return bytesWritten;
				}

				CTX.lengths[codeLengthsOrder[i]] = bits;
			}

			CTX.codeLenTree = of_huffman_tree_construct(
			    CTX.lengths, 19);
			CTX.treeIter = CTX.codeLenTree;

			[self freeMemory: CTX.lengths];
			free(CTX.lengths);
			CTX.lengths = NULL;
			CTX.receivedCount = 0;
			CTX.value = 0xFF;
		}

		if OF_LIKELY (CTX.lengths == NULL)
			CTX.lengths = [self allocMemoryWithSize:
			    CTX.litLenCodesCount + CTX.distCodesCount + 258];
			CTX.lengths = of_malloc(
			    CTX.litLenCodesCount + CTX.distCodesCount + 258, 1);

		for (uint16_t i = CTX.receivedCount;
		    i < CTX.litLenCodesCount + CTX.distCodesCount + 258;) {
			uint8_t j, count;

			if OF_LIKELY (CTX.value == 0xFF) {
				if OF_UNLIKELY (!of_huffman_tree_walk(self,
480
481
482
483
484
485
486
487

488
489
490
491
492
493
494
483
484
485
486
487
488
489

490
491
492
493
494
495
496
497







-
+








		CTX.litLenTree = of_huffman_tree_construct(CTX.lengths,
		    CTX.litLenCodesCount + 257);
		CTX.distTree = of_huffman_tree_construct(
		    CTX.lengths + CTX.litLenCodesCount + 257,
		    CTX.distCodesCount + 1);

		[self freeMemory: CTX.lengths];
		free(CTX.lengths);

		/*
		 * litLenTree and distTree are at the same location in
		 * _context.huffman and _context.huffmanTree, thus no need to
		 * set them.
		 */
		_state = HUFFMAN_BLOCK;

Modified src/OFLHADecompressingStream.m from [16b7cb0b85] to [548b68c743].

106
107
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
134


135
136
137
138
139
140
141
106
107
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
134
135
136
137
138
139
140
141
142
143
144







-
-
+













+
+







+
+







		/* 0-7 address the bit, 8 means fetch next byte */
		_bitIndex = 8;

		_distanceBits = distanceBits;
		_dictionaryBits = dictionaryBits;

		_slidingWindowMask = (1u << dictionaryBits) - 1;
		_slidingWindow = [self allocMemoryWithSize:
		    _slidingWindowMask + 1];
		_slidingWindow = of_malloc(_slidingWindowMask + 1, 1);
		memset(_slidingWindow, ' ', _slidingWindowMask + 1);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	if (_stream != nil)
		[self close];

	free(_slidingWindow);

	if (_codeLenTree != NULL)
		of_huffman_tree_release(_codeLenTree);
	if (_litLenTree != NULL)
		of_huffman_tree_release(_litLenTree);
	if (_distTree != NULL)
		of_huffman_tree_release(_distTree);

	free(_codesLengths);

	[super dealloc];
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer_
			  length: (size_t)length
{
170
171
172
173
174
175
176
177

178
179
180
181
182
183
184
173
174
175
176
177
178
179

180
181
182
183
184
185
186
187







-
+







		if OF_UNLIKELY (bits == 0) {
			_state = STATE_CODE_LEN_TREE_SINGLE;
			goto start;
		}

		_codesCount = bits;
		_codesReceived = 0;
		_codesLengths = [self allocZeroedMemoryWithSize: bits];
		_codesLengths = of_calloc(bits, 1);
		_skip = true;

		_state = STATE_CODE_LEN_TREE;
		goto start;
	case STATE_CODE_LEN_TREE:
		while (_codesReceived < _codesCount) {
			if OF_UNLIKELY (_currentIsExtendedLength) {
221
222
223
224
225
226
227
228


229
230
231
232
233
234
235
224
225
226
227
228
229
230

231
232
233
234
235
236
237
238
239







-
+
+







				continue;
			} else
				_codesReceived++;
		}

		_codeLenTree = of_huffman_tree_construct(_codesLengths,
		    _codesCount);
		[self freeMemory: _codesLengths];
		free(_codesLengths);
		_codesLengths = NULL;

		_state = STATE_LITLEN_CODES_COUNT;
		goto start;
	case STATE_CODE_LEN_TREE_SINGLE:
		if OF_UNLIKELY (!tryReadBits(self, &bits, 5))
			return bytesWritten;

250
251
252
253
254
255
256
257

258
259
260
261
262
263
264
254
255
256
257
258
259
260

261
262
263
264
265
266
267
268







-
+








			_state = STATE_LITLEN_TREE_SINGLE;
			goto start;
		}

		_codesCount = bits;
		_codesReceived = 0;
		_codesLengths = [self allocZeroedMemoryWithSize: bits];
		_codesLengths = of_calloc(bits, 1);
		_skip = false;

		_treeIter = _codeLenTree;
		_state = STATE_LITLEN_TREE;
		goto start;
	case STATE_LITLEN_TREE:
		while (_codesReceived < _codesCount) {
310
311
312
313
314
315
316
317


318
319
320
321
322
323
324
314
315
316
317
318
319
320

321
322
323
324
325
326
327
328
329







-
+
+







				_skip = true;
			} else
				_codesLengths[_codesReceived++] = value - 2;
		}

		_litLenTree = of_huffman_tree_construct(_codesLengths,
		    _codesCount);
		[self freeMemory: _codesLengths];
		free(_codesLengths);
		_codesLengths = NULL;

		of_huffman_tree_release(_codeLenTree);
		_codeLenTree = NULL;

		_state = STATE_DIST_CODES_COUNT;
		goto start;
	case STATE_LITLEN_TREE_SINGLE:
339
340
341
342
343
344
345
346

347
348
349
350
351
352
353
344
345
346
347
348
349
350

351
352
353
354
355
356
357
358







-
+







		if OF_UNLIKELY (bits == 0) {
			_state = STATE_DIST_TREE_SINGLE;
			goto start;
		}

		_codesCount = bits;
		_codesReceived = 0;
		_codesLengths = [self allocZeroedMemoryWithSize: bits];
		_codesLengths = of_calloc(bits, 1);

		_treeIter = _codeLenTree;
		_state = STATE_DIST_TREE;
		goto start;
	case STATE_DIST_TREE:
		while (_codesReceived < _codesCount) {
			if OF_UNLIKELY (_currentIsExtendedLength) {
374
375
376
377
378
379
380
381


382
383
384
385
386
387
388
379
380
381
382
383
384
385

386
387
388
389
390
391
392
393
394







-
+
+







				continue;
			} else
				_codesReceived++;
		}

		_distTree = of_huffman_tree_construct(_codesLengths,
		    _codesCount);
		[self freeMemory: _codesLengths];
		free(_codesLengths);
		_codesLengths = NULL;

		_treeIter = _litLenTree;
		_state = STATE_BLOCK_LITLEN;
		goto start;
	case STATE_DIST_TREE_SINGLE:
		if OF_UNLIKELY (!tryReadBits(self, &bits, _distanceBits))
			return bytesWritten;

Modified src/OFMapTable.m from [8775708778] to [bb1c57067e].

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







-
+
-

















-
+
+

+
+
+







		if (capacity * 8 / _capacity >= 6)
			if (_capacity <= ULONG_MAX / 2)
				_capacity *= 2;

		if (_capacity < MIN_CAPACITY)
			_capacity = MIN_CAPACITY;

		_buckets = [self allocZeroedMemoryWithSize: sizeof(*_buckets)
		_buckets = of_calloc(_capacity, sizeof(*_buckets));
						     count: _capacity];

		if (of_hash_seed != 0)
			_rotate = of_random16() & 31;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	for (unsigned long i = 0; i < _capacity; i++) {
		if (_buckets[i] != NULL && _buckets[i] != &deleted) {
			_keyFunctions.release(_buckets[i]->key);
			_objectFunctions.release(_buckets[i]->object);
		}

			free(_buckets[i]);
	}
	}

	free(_buckets);

	[super dealloc];
}

- (bool)isEqual: (id)object
{
	OFMapTable *mapTable;
318
319
320
321
322
323
324
325

326
327
328
329
330
331
332
333
321
322
323
324
325
326
327

328

329
330
331
332
333
334
335







-
+
-







	/*
	 * Don't downsize if we have an initial capacity or if we would fall
	 * below the minimum capacity.
	 */
	if ((capacity < _capacity && count > _count) || capacity < MIN_CAPACITY)
		return;

	buckets = [self allocZeroedMemoryWithSize: sizeof(*buckets)
	buckets = of_calloc(capacity, sizeof(*buckets));
					    count: capacity];

	for (unsigned long i = 0; i < _capacity; i++) {
		if (_buckets[i] != NULL && _buckets[i] != &deleted) {
			unsigned long j, last;

			last = capacity;

345
346
347
348
349
350
351
352

353
354
355
356
357
358
359
347
348
349
350
351
352
353

354
355
356
357
358
359
360
361







-
+







			if (j >= last)
				@throw [OFOutOfRangeException exception];

			buckets[j] = _buckets[i];
		}
	}

	[self freeMemory: _buckets];
	free(_buckets);
	_buckets = buckets;
	_capacity = capacity;
}

- (void)of_setObject: (void *)object
	      forKey: (void *)key
		hash: (unsigned long)hash
408
409
410
411
412
413
414
415

416
417
418
419
420

421
422
423
424
425
426
427
428

429
430
431
432
433
434
435
410
411
412
413
414
415
416

417
418
419
420
421

422
423
424
425
426
427
428
429

430
431
432
433
434
435
436
437







-
+




-
+







-
+







			for (i = 0; i < last && _buckets[i] != NULL &&
			    _buckets[i] != &deleted; i++);
		}

		if (i >= last)
			@throw [OFOutOfRangeException exception];

		bucket = [self allocMemoryWithSize: sizeof(*bucket)];
		bucket = of_malloc(1, sizeof(*bucket));

		@try {
			bucket->key = _keyFunctions.retain(key);
		} @catch (id e) {
			[self freeMemory: bucket];
			free(bucket);
			@throw e;
		}

		@try {
			bucket->object = _objectFunctions.retain(object);
		} @catch (id e) {
			_keyFunctions.release(bucket->key);
			[self freeMemory: bucket];
			free(bucket);
			@throw e;
		}

		bucket->hash = hash;

		_buckets[i] = bucket;
		_count++;
466
467
468
469
470
471
472
473

474
475
476
477
478
479
480
468
469
470
471
472
473
474

475
476
477
478
479
480
481
482







-
+








		if (_keyFunctions.equal(_buckets[i]->key, key)) {
			_mutations++;

			_keyFunctions.release(_buckets[i]->key);
			_objectFunctions.release(_buckets[i]->object);

			[self freeMemory: _buckets[i]];
			free(_buckets[i]);
			_buckets[i] = &deleted;

			_count--;
			[self of_resizeForCount: _count];

			return;
		}
490
491
492
493
494
495
496
497

498
499
500
501
502
503
504
492
493
494
495
496
497
498

499
500
501
502
503
504
505
506







-
+







		if (_buckets[i] == &deleted)
			continue;

		if (_keyFunctions.equal(_buckets[i]->key, key)) {
			_keyFunctions.release(_buckets[i]->key);
			_objectFunctions.release(_buckets[i]->object);

			[self freeMemory: _buckets[i]];
			free(_buckets[i]);
			_buckets[i] = &deleted;

			_count--;
			_mutations++;
			[self of_resizeForCount: _count];

			return;
514
515
516
517
518
519
520
521

522
523
524
525
526
527
528
529

530
531
532
533
534
535
536
537
516
517
518
519
520
521
522

523
524
525
526
527
528
529


530

531
532
533
534
535
536
537







-
+






-
-
+
-







				_buckets[i] = NULL;
				continue;
			}

			_keyFunctions.release(_buckets[i]->key);
			_objectFunctions.release(_buckets[i]->object);

			[self freeMemory: _buckets[i]];
			free(_buckets[i]);
			_buckets[i] = NULL;
		}
	}

	_count = 0;
	_capacity = MIN_CAPACITY;
	_buckets = [self resizeMemory: _buckets
				 size: sizeof(*_buckets)
	_buckets = of_realloc(_buckets, _capacity, sizeof(*_buckets));
				count: _capacity];

	/*
	 * Get a new random value for _rotate, so that it is not less secure
	 * than creating a new hash map.
	 */
	if (of_hash_seed != 0)
		_rotate = of_random16() & 31;

Modified src/OFMethodSignature.m from [cf206c5116] to [67a687aa14].

593
594
595
596
597
598
599
600

601
602
603
604
605
606
607
593
594
595
596
597
598
599

600
601
602
603
604
605
606
607







-
+







			@throw [OFInvalidArgumentException exception];

		length = strlen(types);

		if (length == 0)
			@throw [OFInvalidFormatException exception];

		_types = [self allocMemoryWithSize: length + 1];
		_types = of_malloc(length + 1, 1);
		memcpy(_types, types, length);

		_typesPointers = [[OFMutableData alloc]
		    initWithItemSize: sizeof(char *)];
		_offsets = [[OFMutableData alloc]
		    initWithItemSize: sizeof(size_t)];

667
668
669
670
671
672
673

674
675
676
677
678
679
680
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681







+







	}

	return self;
}

- (void)dealloc
{
	free(_types);
	[_typesPointers release];
	[_offsets release];

	[super dealloc];
}

- (size_t)numberOfArguments

Modified src/OFOptionsParser.m from [194fc0cb2f] to [93ab00cf42].

82
83
84
85
86
87
88

89
90
91
92
93
94
95
96
97
98
99
100
101
82
83
84
85
86
87
88
89
90
91
92



93
94
95
96
97
98
99







+



-
-
-







				*iter->isSpecifiedPtr = false;
			if (iter->argumentPtr)
				*iter->argumentPtr = nil;

			count++;
		}

		_options = of_malloc(count + 1, sizeof(*_options));
		_longOptions = [[OFMapTable alloc]
		    initWithKeyFunctions: keyFunctions
			 objectFunctions: objectFunctions];
		_options = [self
		    allocMemoryWithSize: sizeof(*_options)
				  count: count + 1];

		for (iter = options, iter2 = _options;
		    iter->shortOption != '\0' || iter->longOption != nil;
		    iter++, iter2++) {
			iter2->shortOption = iter->shortOption;
			iter2->longOption = nil;
			iter2->hasArgument = iter->hasArgument;
142
143
144
145
146
147
148

149
150
151
152
153
154
155
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154







+







	return self;
}

- (void)dealloc
{
	of_options_parser_option_t *iter;

	free(_options);
	[_longOptions release];

	if (_options != NULL)
		for (iter = _options;
		    iter->shortOption != '\0' || iter->longOption != nil;
		    iter++)
			[iter->longOption release];

Modified src/OFPollKernelEventObserver.m from [5ca69d7bfc] to [4cf3bcee3f].

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







-
-
+











+







		struct pollfd p = { _cancelFD[0], POLLIN, 0 };

		_FDs = [[OFMutableData alloc] initWithItemSize:
		    sizeof(struct pollfd)];
		[_FDs addItem: &p];

		_maxFD = _cancelFD[0];
		_FDToObject = [self allocMemoryWithSize: sizeof(id)
						  count: (size_t)_maxFD + 1];
		_FDToObject = of_malloc((size_t)_maxFD + 1, sizeof(id));
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_FDs release];
	free(_FDToObject);

	[super dealloc];
}

- (void)of_addObject: (id)object
      fileDescriptor: (int)fd
	      events: (short)events OF_DIRECT
94
95
96
97
98
99
100
101

102
103

104
105
106
107
108
109
110
94
95
96
97
98
99
100

101


102
103
104
105
106
107
108
109







-
+
-
-
+







	}

	if (!found) {
		struct pollfd p = { fd, events, 0 };

		if (fd > _maxFD) {
			_maxFD = fd;
			_FDToObject = [self resizeMemory: _FDToObject
			_FDToObject = of_realloc(_FDToObject,
						    size: sizeof(id)
						   count: (size_t)_maxFD + 1];
			    (size_t)_maxFD + 1, sizeof(id));
		}

		_FDToObject[fd] = object;
		[_FDs addItem: &p];
	}
}

Modified src/OFSeekableStream.m from [57a7a2c048] to [b1692ec182].

51
52
53
54
55
56
57
58

59
60
61
62
63
64
51
52
53
54
55
56
57

58
59
60
61
62
63
64







-
+






{
	if (whence == SEEK_CUR)
		offset -= _readBufferLength;

	offset = [self lowlevelSeekToOffset: offset
				     whence: whence];

	[self freeMemory: _readBufferMemory];
	free(_readBufferMemory);
	_readBuffer = _readBufferMemory = NULL;
	_readBufferLength = 0;

	return offset;
}
@end

Modified src/OFStream.m from [631572b875] to [c39ea2325e].

88
89
90
91
92
93
94








95
96
97
98
99
100
101
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109







+
+
+
+
+
+
+
+







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

	return self;
}

- (void)dealloc
{
	free(_readBufferMemory);
	free(_writeBuffer);

	[super dealloc];
}

- (bool)lowlevelIsAtEndOfStream
{
	OF_UNRECOGNIZED_SELECTOR
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
139
140
141
142
143
144
145
146
147

148
149
150
151
152
153
154
147
148
149
150
151
152
153


154
155
156
157
158
159
160
161







-
-
+







			bytesRead = [self
			    lowlevelReadIntoBuffer: tmp
					    length: MIN_READ_SIZE];

			if (bytesRead > length) {
				memcpy(buffer, tmp, length);

				readBuffer = [self allocMemoryWithSize:
				    bytesRead - length];
				readBuffer = of_malloc(bytesRead - length, 1);
				memcpy(readBuffer, tmp + length,
				    bytesRead - length);

				_readBuffer = _readBufferMemory = readBuffer;
				_readBufferLength = bytesRead - length;

				return length;
162
163
164
165
166
167
168
169

170
171
172
173
174
175
176
169
170
171
172
173
174
175

176
177
178
179
180
181
182
183







-
+







					     length: length];
	}

	if (length >= _readBufferLength) {
		size_t ret = _readBufferLength;
		memcpy(buffer, _readBuffer, _readBufferLength);

		[self freeMemory: _readBufferMemory];
		free(_readBufferMemory);
		_readBuffer = _readBufferMemory = NULL;
		_readBufferLength = 0;

		return ret;
	} else {
		memcpy(buffer, _readBuffer, length);

750
751
752
753
754
755
756
757

758
759
760
761
762
763
764
757
758
759
760
761
762
763

764
765
766
767
768
769
770
771







-
+







			if (retLength > 0 && _readBuffer[retLength - 1] == '\r')
				retLength--;

			ret = [OFString stringWithCString: _readBuffer
						 encoding: encoding
						   length: retLength];

			[self freeMemory: _readBufferMemory];
			free(_readBufferMemory);
			_readBuffer = _readBufferMemory = NULL;
			_readBufferLength = 0;

			_waitingForDelimiter = false;
			return ret;
		}

789
790
791
792
793
794
795
796

797
798
799

800
801
802
803
804
805
806
807
808

809
810
811
812
813
814
815
816
817
818
819
820

821
822
823
824
825
826

827
828
829
830
831
832
833
834
835
836
837
838


839
840
841
842
843
844

845
846
847
848
849
850
851
796
797
798
799
800
801
802

803

804

805
806
807
808
809
810
811
812


813
814
815
816
817
818
819
820
821
822
823
824

825

826
827
828
829

830
831
832
833
834
835
836
837
838
839
840


841
842
843
844
845
846
847

848
849
850
851
852
853
854
855







-
+
-

-
+







-
-
+











-
+
-




-
+










-
-
+
+





-
+







						       length: retLength];
				} @catch (id e) {
					if (bufferLength > 0) {
						/*
						 * Append data to _readBuffer
						 * to prevent loss of data.
						 */
						readBuffer = [self
						readBuffer = of_malloc(
						    allocMemoryWithSize:
						    _readBufferLength +
						    bufferLength];
						    bufferLength, 1);

						memcpy(readBuffer, _readBuffer,
						    _readBufferLength);
						memcpy(readBuffer +
						    _readBufferLength,
						    buffer, bufferLength);

						[self freeMemory:
						    _readBufferMemory];
						free(_readBufferMemory);
						_readBuffer = readBuffer;
						_readBufferMemory = readBuffer;
						_readBufferLength +=
						    bufferLength;
					}

					@throw e;
				} @finally {
					free(retCString);
				}

				readBuffer = [self
				readBuffer = of_malloc(bufferLength - i - 1, 1);
				    allocMemoryWithSize: bufferLength - i - 1];
				if (readBuffer != NULL)
					memcpy(readBuffer, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _readBufferMemory];
				free(_readBufferMemory);
				_readBuffer = _readBufferMemory = readBuffer;
				_readBufferLength = bufferLength - i - 1;

				_waitingForDelimiter = false;
				return ret;
			}
		}

		/* There was no newline or \0 */
		if (bufferLength > 0) {
			readBuffer = [self allocMemoryWithSize:
			    _readBufferLength + bufferLength];
			readBuffer = of_malloc(_readBufferLength + bufferLength,
			    1);

			memcpy(readBuffer, _readBuffer, _readBufferLength);
			memcpy(readBuffer + _readBufferLength,
			    buffer, bufferLength);

			[self freeMemory: _readBufferMemory];
			free(_readBufferMemory);
			_readBuffer = _readBufferMemory = readBuffer;
			_readBufferLength += bufferLength;
		}
	} @finally {
		free(buffer);
	}

984
985
986
987
988
989
990
991

992
993
994
995
996
997
998
988
989
990
991
992
993
994

995
996
997
998
999
1000
1001
1002







-
+







				return nil;
			}

			ret = [OFString stringWithCString: _readBuffer
						 encoding: encoding
						   length: _readBufferLength];

			[self freeMemory: _readBufferMemory];
			free(_readBufferMemory);
			_readBuffer = _readBufferMemory = NULL;
			_readBufferLength = 0;

			_waitingForDelimiter = false;
			return ret;
		}

1033
1034
1035
1036
1037
1038
1039
1040

1041
1042
1043

1044
1045
1046
1047
1048
1049
1050
1051
1052

1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065

1066
1067
1068
1069
1070

1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082


1083
1084
1085
1086
1087
1088

1089
1090
1091
1092
1093
1094
1095
1037
1038
1039
1040
1041
1042
1043

1044

1045

1046
1047
1048
1049
1050
1051
1052
1053


1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065


1066
1067
1068
1069
1070

1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081


1082
1083
1084
1085
1086
1087
1088

1089
1090
1091
1092
1093
1094
1095
1096







-
+
-

-
+







-
-
+











-
-
+




-
+










-
-
+
+





-
+







						       length: retLength];
				} @catch (id e) {
					if (bufferLength > 0) {
						/*
						 * Append data to _readBuffer
						 * to prevent loss of data.
						 */
						readBuffer = [self
						readBuffer = of_malloc(
						    allocMemoryWithSize:
						    _readBufferLength +
						    bufferLength];
						    bufferLength, 1);

						memcpy(readBuffer, _readBuffer,
						    _readBufferLength);
						memcpy(readBuffer +
						    _readBufferLength,
						    buffer, bufferLength);

						[self freeMemory:
						    _readBufferMemory];
						free(_readBufferMemory);
						_readBuffer = readBuffer;
						_readBufferMemory = readBuffer;
						_readBufferLength +=
						    bufferLength;
					}

					@throw e;
				} @finally {
					free(retCString);
				}

				readBuffer = [self allocMemoryWithSize:
				    bufferLength - i - 1];
				readBuffer = of_malloc(bufferLength - i - 1, 1);
				if (readBuffer != NULL)
					memcpy(readBuffer, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _readBufferMemory];
				free(_readBufferMemory);
				_readBuffer = _readBufferMemory = readBuffer;
				_readBufferLength = bufferLength - i - 1;

				_waitingForDelimiter = false;
				return ret;
			}
		}

		/* Neither the delimiter nor \0 was found */
		if (bufferLength > 0) {
			readBuffer = [self allocMemoryWithSize:
			    _readBufferLength + bufferLength];
			readBuffer = of_malloc(_readBufferLength + bufferLength,
			    1);

			memcpy(readBuffer, _readBuffer, _readBufferLength);
			memcpy(readBuffer + _readBufferLength,
			    buffer, bufferLength);

			[self freeMemory: _readBufferMemory];
			free(_readBufferMemory);
			_readBuffer = _readBufferMemory = readBuffer;
			_readBufferLength += bufferLength;
		}
	} @finally {
		free(buffer);
	}

1127
1128
1129
1130
1131
1132
1133
1134

1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156


1157
1158
1159
1160
1161
1162
1163
1128
1129
1130
1131
1132
1133
1134

1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155


1156
1157
1158
1159
1160
1161
1162
1163
1164







-
+




















-
-
+
+







{
	if (_writeBuffer == NULL)
		return;

	[self lowlevelWriteBuffer: _writeBuffer
			   length: _writeBufferLength];

	[self freeMemory: _writeBuffer];
	free(_writeBuffer);
	_writeBuffer = NULL;
	_writeBufferLength = 0;
}

- (size_t)writeBuffer: (const void *)buffer
	       length: (size_t)length
{
	if (!_buffersWrites) {
		size_t bytesWritten = [self lowlevelWriteBuffer: buffer
							 length: length];

		if (_canBlock && bytesWritten < length)
			@throw [OFWriteFailedException
			    exceptionWithObject: self
				requestedLength: length
				   bytesWritten: bytesWritten
					  errNo: 0];

		return bytesWritten;
	} else {
		_writeBuffer = [self resizeMemory: _writeBuffer
					     size: _writeBufferLength + length];
		_writeBuffer = of_realloc(_writeBuffer,
		    _writeBufferLength + length, 1);
		memcpy(_writeBuffer + _writeBufferLength, buffer, length);
		_writeBufferLength += length;

		return length;
	}
}

1869
1870
1871
1872
1873
1874
1875
1876

1877
1878
1879
1880

1881
1882
1883
1884
1885
1886
1887

1888
1889
1890
1891

1892
1893
1894
1895
1896
1897
1898
1870
1871
1872
1873
1874
1875
1876

1877
1878
1879
1880

1881
1882
1883
1884
1885
1886
1887

1888
1889
1890
1891

1892
1893
1894
1895
1896
1897
1898
1899







-
+



-
+






-
+



-
+







		  length: (size_t)length
{
	char *readBuffer;

	if (length > SIZE_MAX - _readBufferLength)
		@throw [OFOutOfRangeException exception];

	readBuffer = [self allocMemoryWithSize: _readBufferLength + length];
	readBuffer = of_malloc(_readBufferLength + length, 1);
	memcpy(readBuffer, buffer, length);
	memcpy(readBuffer + length, _readBuffer, _readBufferLength);

	[self freeMemory: _readBufferMemory];
	free(_readBufferMemory);
	_readBuffer = _readBufferMemory = readBuffer;
	_readBufferLength += length;
}

- (void)close
{
	[self freeMemory: _readBufferMemory];
	free(_readBufferMemory);
	_readBuffer = _readBufferMemory = NULL;
	_readBufferLength = 0;

	[self freeMemory: _writeBuffer];
	free(_writeBuffer);
	_writeBuffer = NULL;
	_writeBufferLength = 0;
	_buffersWrites = false;

	_waitingForDelimiter = false;
}
@end

Modified utils/ofhttp/OFHTTP.m from [c172d83732] to [121d87bdd5].

300
301
302
303
304
305
306
307

308
309
310
311
312
313
314
300
301
302
303
304
305
306

307
308
309
310
311
312
313
314







-
+







		_clientHeaders = [[OFMutableDictionary alloc]
		    initWithObject: @"OFHTTP"
			    forKey: @"User-Agent"];

		_HTTPClient = [[OFHTTPClient alloc] init];
		_HTTPClient.delegate = self;

		_buffer = [self allocMemoryWithSize: [OFSystemInfo pageSize]];
		_buffer = of_malloc(1, [OFSystemInfo pageSize]);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}