ObjFW  Check-in [aacb5897a0]

Overview
Comment:Add -[OFObject allocZeroedMemoryWithSize:(count:)]
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: aacb5897a040184da8a25906a011da9139c86c532d3857dc24e8d7d30fec3419
User & Date: js on 2018-06-03 23:31:17
Other Links: manifest | tags
Context
2018-06-04
22:05
OFLHAArchive: Verify CRC16 checksum check-in: fca5e53e12 user: js tags: trunk
2018-06-03
23:31
Add -[OFObject allocZeroedMemoryWithSize:(count:)] check-in: aacb5897a0 user: js tags: trunk
23:08
runtime/linklib: Do not pass exit() to Amiga lib check-in: a37a78d51b user: js tags: trunk
Changes

Modified src/OFInflateStream.m from [cd213df087] to [2f25e4e657].

190
191
192
193
194
195
196
197

198
199
200
201
202
203
204
205
206
207
190
191
192
193
194
195
196

197
198


199
200
201
202
203
204
205







-
+

-
-







		_bitIndex = 8;

#ifdef INFLATE64
		_slidingWindowMask = 0xFFFF;
#else
		_slidingWindowMask = 0x7FFF;
#endif
		_slidingWindow = [self allocMemoryWithSize:
		_slidingWindow = [self allocZeroedMemoryWithSize:
		    _slidingWindowMask + 1];
		/* Avoid leaking data */
		memset(_slidingWindow, 0, _slidingWindowMask + 1);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
370
371
372
373
374
375
376
377
378
379



380
381
382
383
384
385
386
387
368
369
370
371
372
373
374



375
376
377

378
379
380
381
382
383
384







-
-
-
+
+
+
-







			if OF_LIKELY (CTX.codeLenCodesCount == 0xFF) {
				if OF_UNLIKELY (!tryReadBits(self, &bits, 4))
					return bytesWritten;

				CTX.codeLenCodesCount = bits;
			}

			if OF_LIKELY (CTX.lengths == NULL) {
				CTX.lengths = [self allocMemoryWithSize: 19];
				memset(CTX.lengths, 0, 19);
			if OF_LIKELY (CTX.lengths == NULL)
				CTX.lengths = [self
				    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;
				}

Modified src/OFLHAArchive.m from [9530d9435c] to [56fa949e3a].

292
293
294
295
296
297
298
299

300
301
302
303
304
305
306
307
308
309
292
293
294
295
296
297
298

299
300


301
302
303
304
305
306
307







-
+

-
-







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

		_distanceBits = distanceBits;
		_dictionaryBits = dictionaryBits;

		_slidingWindowMask = (1 << dictionaryBits) - 1;
		_slidingWindow = [self allocMemoryWithSize:
		_slidingWindow = [self allocZeroedMemoryWithSize:
		    _slidingWindowMask + 1];
		/* Avoid leaking data */
		memset(_slidingWindow, 0, _slidingWindowMask + 1);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
356
357
358
359
360
361
362
363

364
365
366
367
368
369
370
371
354
355
356
357
358
359
360

361

362
363
364
365
366
367
368







-
+
-







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

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

		_state = STATE_CODE_LEN_TREE;
		goto start;
	case STATE_CODE_LEN_TREE:
		while (_codesReceived < _codesCount) {
			if OF_UNLIKELY (_currentIsExtendedLength) {
437
438
439
440
441
442
443
444

445
446
447
448
449
450
451
452
434
435
436
437
438
439
440

441

442
443
444
445
446
447
448







-
+
-








			_state = STATE_LITLEN_TREE_SINGLE;
			goto start;
		}

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

		_treeIter = _codeLenTree;
		_state = STATE_LITLEN_TREE;
		goto start;
	case STATE_LITLEN_TREE:
		while (_codesReceived < _codesCount) {
527
528
529
530
531
532
533
534

535
536
537
538
539
540
541
542
523
524
525
526
527
528
529

530

531
532
533
534
535
536
537







-
+
-







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

		_codesCount = bits;
		_codesReceived = 0;
		_codesLengths = [self allocMemoryWithSize: bits];
		_codesLengths = [self allocZeroedMemoryWithSize: bits];
		memset(_codesLengths, 0, bits);

		_treeIter = _codeLenTree;
		_state = STATE_DIST_TREE;
		goto start;
	case STATE_DIST_TREE:
		while (_codesReceived < _codesCount) {
			if OF_UNLIKELY (_currentIsExtendedLength) {

Modified src/OFMapTable.m from [bdc38c2ac6] to [bcee3b5d39].

157
158
159
160
161
162
163
164
165


166
167
168
169
170
171
172
173
174
157
158
159
160
161
162
163


164
165


166
167
168
169
170
171
172







-
-
+
+
-
-







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

		if (_capacity < MIN_CAPACITY)
			_capacity = MIN_CAPACITY;

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

		memset(_buckets, 0, _capacity * sizeof(*_buckets));

		if (of_hash_seed != 0)
#if defined(HAVE_ARC4RANDOM)
			_rotate = arc4random() & 31;
#elif defined(HAVE_RANDOM)
			_rotate = random() & 31;
#else
324
325
326
327
328
329
330
331
332


333
334
335
336
337
338
339
340
341
322
323
324
325
326
327
328


329
330


331
332
333
334
335
336
337







-
-
+
+
-
-







	/*
	 * 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 allocMemoryWithSize: sizeof(*buckets)
				      count: capacity];
	buckets = [self allocZeroedMemoryWithSize: sizeof(*buckets)
					    count: capacity];

	memset(buckets, 0, capacity * sizeof(*buckets));

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

			last = capacity;

Modified src/OFObject.h from [a6c803a105] to [cdaa2f3d08].

745
746
747
748
749
750
751
752

753
754
755
756
757
758
759
760
761
762
763
764

765
766
767
768
769
770
771
772
773




























774
775
776
777
778
779
780
745
746
747
748
749
750
751

752
753
754
755
756
757
758
759
760
761
762
763

764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808







-
+











-
+









+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







 * @return The method signature for the specified selector
 */
- (nullable OFMethodSignature *)methodSignatureForSelector: (SEL)selector;

/*!
 * @brief Allocates memory and stores it in the object's memory pool.
 *
 * It will be freed automatically when the object is deallocated.
 * It will be free'd automatically when the object is deallocated.
 *
 * @param size The size of the memory to allocate
 * @return A pointer to the allocated memory. May return NULL if the specified
 *	   size is 0.
 */
- (nullable void *)allocMemoryWithSize: (size_t)size OF_WARN_UNUSED_RESULT;

/*!
 * @brief Allocates memory for the specified number of items and stores it in
 *	  the object's memory pool.
 *
 * It will be freed automatically when the object is deallocated.
 * It will be free'd automatically when the object is deallocated.
 *
 * @param size The size of each item to allocate
 * @param count The number of items to allocate
 * @return A pointer to the allocated memory. May return NULL if the specified
 *	   size or count is 0.
 */
- (nullable void *)allocMemoryWithSize: (size_t)size
				 count: (size_t)count OF_WARN_UNUSED_RESULT;

/*!
 * @brief Allocates memory, initializes it with zeros and stores it in the
 *	  object's memory pool.
 *
 * It will be free'd automatically when the object is deallocated.
 *
 * @param size The size of the memory to allocate
 * @return A pointer to the allocated memory. May return NULL if the specified
 *	   size is 0.
 */
- (nullable void *)allocZeroedMemoryWithSize: (size_t)size
    OF_WARN_UNUSED_RESULT;

/*!
 * @brief Allocates memory for the specified number of items, initializes it
 *	  with zeros and stores it in the object's memory pool.
 *
 * It will be free'd automatically when the object is deallocated.
 *
 * @param size The size of each item to allocate
 * @param count The number of items to allocate
 * @return A pointer to the allocated memory. May return NULL if the specified
 *	   size or count is 0.
 */
- (nullable void *)allocZeroedMemoryWithSize: (size_t)size
				       count: (size_t)count
    OF_WARN_UNUSED_RESULT;

/*!
 * @brief Resizes memory in the object's memory pool to the specified size.
 *
 * If the pointer is NULL, this is equivalent to allocating memory.
 * If the size is 0, this is equivalent to freeing memory.
 *
 * @param pointer A pointer to the already allocated memory

Modified src/OFObject.m from [de528ece86] to [aff4f94d44].

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







-
+








-
-












-
-








	instanceSize = class_getInstanceSize(class);

	if OF_UNLIKELY (extraAlignment > 1)
		extraAlignment = ((instanceSize + extraAlignment - 1) &
		    ~(extraAlignment - 1)) - extraAlignment;

	instance = malloc(PRE_IVARS_ALIGN + instanceSize +
	instance = calloc(1, PRE_IVARS_ALIGN + instanceSize +
	    extraAlignment + extraSize);

	if OF_UNLIKELY (instance == nil) {
		allocFailedException.isa = [OFAllocFailedException class];
		@throw (id)&allocFailedException;
	}

	((struct pre_ivar *)instance)->retainCount = 1;
	((struct pre_ivar *)instance)->firstMem = NULL;
	((struct pre_ivar *)instance)->lastMem = NULL;

#if !defined(OF_HAVE_ATOMIC_OPS) && defined(OF_HAVE_THREADS)
	if OF_UNLIKELY (!of_spinlock_new(
	    &((struct pre_ivar *)instance)->retainCountSpinlock)) {
		free(instance);
		@throw [OFInitializationFailedException
		    exceptionWithClass: class];
	}
#endif

	instance = (OFObject *)(void *)((char *)instance + PRE_IVARS_ALIGN);

	memset(instance, 0, instanceSize);

	if (!objc_constructInstance(class, instance)) {
		free((char *)instance - PRE_IVARS_ALIGN);
		@throw [OFInitializationFailedException
		    exceptionWithClass: class];
	}

	if OF_UNLIKELY (extra != NULL)
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
1030
1031
1032
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
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107







-

+









+













+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








	if OF_UNLIKELY (size > SIZE_MAX - PRE_IVARS_ALIGN)
		@throw [OFOutOfRangeException exception];

	if OF_UNLIKELY ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: size];
	preMem = pointer;

	preMem = pointer;
	preMem->owner = self;
	preMem->prev = PRE_IVARS->lastMem;
	preMem->next = NULL;

	if OF_LIKELY (PRE_IVARS->lastMem != NULL)
		PRE_IVARS->lastMem->next = preMem;

	if OF_UNLIKELY (PRE_IVARS->firstMem == NULL)
		PRE_IVARS->firstMem = preMem;

	PRE_IVARS->lastMem = preMem;

	return (char *)pointer + PRE_MEM_ALIGN;
}

- (void *)allocMemoryWithSize: (size_t)size
			count: (size_t)count
{
	if OF_UNLIKELY (count > SIZE_MAX / size)
		@throw [OFOutOfRangeException exception];

	return [self allocMemoryWithSize: size * count];
}

- (void *)allocZeroedMemoryWithSize: (size_t)size
{
	void *pointer;
	struct pre_mem *preMem;

	if OF_UNLIKELY (size == 0)
		return NULL;

	if OF_UNLIKELY (size > SIZE_MAX - PRE_IVARS_ALIGN)
		@throw [OFOutOfRangeException exception];

	if OF_UNLIKELY ((pointer = calloc(1, PRE_MEM_ALIGN + size)) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: size];

	preMem = pointer;
	preMem->owner = self;
	preMem->prev = PRE_IVARS->lastMem;

	if OF_LIKELY (PRE_IVARS->lastMem != NULL)
		PRE_IVARS->lastMem->next = preMem;

	if OF_UNLIKELY (PRE_IVARS->firstMem == NULL)
		PRE_IVARS->firstMem = preMem;

	PRE_IVARS->lastMem = preMem;

	return (char *)pointer + PRE_MEM_ALIGN;
}

- (void *)allocZeroedMemoryWithSize: (size_t)size
			      count: (size_t)count
{
	if OF_UNLIKELY (count > SIZE_MAX / size)
		@throw [OFOutOfRangeException exception];

	return [self allocZeroedMemoryWithSize: size * count];
}

- (void *)resizeMemory: (void *)pointer
		  size: (size_t)size
{
	void *new;
	struct pre_mem *preMem;

Modified src/OFSecureData.m from [8841787590] to [1f55e2f307].

108
109
110
111
112
113
114
115

116
117
118
119
120
121
122
123
124
125
126
127
108
109
110
111
112
113
114

115
116
117
118


119
120
121
122
123
124
125







-
+



-
-







	struct page *lastPage;
#endif

	if ((page = malloc(sizeof(*page))) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: sizeof(*page)];

	if ((page->map = malloc(mapSize)) == NULL)
	if ((page->map = calloc(1, mapSize)) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: mapSize];

	of_explicit_memset(page->map, 0, mapSize);

	page->page = mapPages(1);
	of_explicit_memset(page->page, 0, pageSize);

#if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS)
	lastPage = of_tlskey_get(lastPageKey);
#endif

Modified src/runtime/exception.m from [690202f93c] to [a80746f2fd].

716
717
718
719
720
721
722
723

724
725
726
727
728
729
730
716
717
718
719
720
721
722

723
724
725
726
727
728
729
730







-
+







		OBJC_ERROR("Cannot unlock spinlock!");
#endif
}

void
objc_exception_throw(id object)
{
	struct objc_exception *e = malloc(sizeof(*e));
	struct objc_exception *e = calloc(1, sizeof(*e));
	bool emergency = false;

	if (e == NULL) {
#ifdef OF_HAVE_THREADS
		if (!of_spinlock_lock(&emergency_exceptions_spinlock))
			OBJC_ERROR("Cannot lock spinlock!");
#endif
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
744
745
746
747
748
749
750

751
752
753
754
755
756
757







-







			OBJC_ERROR("Cannot lock spinlock!");
#endif
	}

	if (e == NULL)
		OBJC_ERROR("Not enough memory to allocate exception!")

	memset(e, 0, sizeof(*e));
	e->exception.class = GNUCOBJC_EXCEPTION_CLASS;
	e->exception.cleanup = (emergency ? cleanup_emergency : cleanup);
	e->object = object;

	if (_Unwind_RaiseException(&e->exception) == _URC_END_OF_STACK &&
	    uncaught_exception_handler != NULL)
		uncaught_exception_handler(object);