ObjFW  Diff

Differences From Artifact [878b273561]:

To Artifact [717a250a20]:


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







-
+


















-
+



-
+







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

		if (_capacity < MIN_CAPACITY)
			_capacity = MIN_CAPACITY;

		_buckets = of_alloc_zeroed(_capacity, sizeof(*_buckets));
		_buckets = OFAllocZeroedMemory(_capacity, sizeof(*_buckets));

		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]);
			OFFreeMemory(_buckets[i]);
		}
	}

	free(_buckets);
	OFFreeMemory(_buckets);

	[super dealloc];
}

static void
resizeForCount(OFMapTable *self, unsigned long count)
{
206
207
208
209
210
211
212
213

214
215
216
217
218
219
220
206
207
208
209
210
211
212

213
214
215
216
217
218
219
220







-
+







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

	buckets = of_alloc_zeroed(capacity, sizeof(*buckets));
	buckets = OFAllocZeroedMemory(capacity, sizeof(*buckets));

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

			last = capacity;
233
234
235
236
237
238
239
240

241
242
243
244
245
246
247
233
234
235
236
237
238
239

240
241
242
243
244
245
246
247







-
+







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

			buckets[j] = self->_buckets[i];
		}
	}

	free(self->_buckets);
	OFFreeMemory(self->_buckets);
	self->_buckets = buckets;
	self->_capacity = capacity;
}

static void
setObject(OFMapTable *restrict self, void *key, void *object,
    unsigned long hash)
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
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







-
+




-
+







-
+







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

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

		bucket = of_alloc(1, sizeof(*bucket));
		bucket = OFAllocMemory(1, sizeof(*bucket));

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

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

		bucket->hash = hash;

		self->_buckets[i] = bucket;
		self->_count++;
461
462
463
464
465
466
467
468

469
470
471
472
473
474
475
461
462
463
464
465
466
467

468
469
470
471
472
473
474
475







-
+








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

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

			free(_buckets[i]);
			OFFreeMemory(_buckets[i]);
			_buckets[i] = &deleted;

			_count--;
			resizeForCount(self, _count);

			return;
		}
485
486
487
488
489
490
491
492

493
494
495
496
497
498
499
485
486
487
488
489
490
491

492
493
494
495
496
497
498
499







-
+







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

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

			free(_buckets[i]);
			OFFreeMemory(_buckets[i]);
			_buckets[i] = &deleted;

			_count--;
			_mutations++;
			resizeForCount(self, _count);

			return;
509
510
511
512
513
514
515
516

517
518
519
520
521
522
523

524
525
526
527
528
529
530
509
510
511
512
513
514
515

516
517
518
519
520
521
522

523
524
525
526
527
528
529
530







-
+






-
+







				_buckets[i] = NULL;
				continue;
			}

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

			free(_buckets[i]);
			OFFreeMemory(_buckets[i]);
			_buckets[i] = NULL;
		}
	}

	_count = 0;
	_capacity = MIN_CAPACITY;
	_buckets = of_realloc(_buckets, _capacity, sizeof(*_buckets));
	_buckets = OFResizeMemory(_buckets, _capacity, sizeof(*_buckets));

	/*
	 * 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;