ObjFW  Check-in [f675979cf0]

Overview
Comment:Add of_random().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f675979cf0f40ec8c9ffb0ffd496fe5fbfde4b05e87d3964630c0d5a23479874
User & Date: js on 2012-12-06 11:00:50
Other Links: manifest | tags
Context
2012-12-06
11:00
OFMapTable: Rotate hash by a random number of bits check-in: 4857107479 user: js tags: trunk
11:00
Add of_random(). check-in: f675979cf0 user: js tags: trunk
01:19
Don't inline runtime functions. check-in: 7fe9b2e320 user: js tags: trunk
Changes

Modified src/OFMapTable.m from [06127d91bb] to [ecdbc341ea].

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
		minCapacity = capacity;

		buckets = [self allocMemoryWithSize: sizeof(*buckets)
					      count: capacity];

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

		if (of_hash_seed != 0) {
#if defined(OF_HAVE_ARC4RANDOM)
			seed = arc4random();
#elif defined(OF_HAVE_RANDOM)
			seed = random();
#else
			seed = rand();
#endif
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}







|
<
<
<
|
<
<
<
<







149
150
151
152
153
154
155
156



157




158
159
160
161
162
163
164
		minCapacity = capacity;

		buckets = [self allocMemoryWithSize: sizeof(*buckets)
					      count: capacity];

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

		if (of_hash_seed != 0)



			seed = of_random();




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

	return self;
}
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
	newBuckets = [self allocMemoryWithSize: sizeof(*newBuckets)
					 count: newCapacity];

	for (i = 0; i < newCapacity; i++)
		newBuckets[i] = NULL;

	if (of_hash_seed != 0) {
#if defined(OF_HAVE_ARC4RANDOM)
		newSeed = arc4random();
#elif defined(OF_HAVE_RANDOM)
		newSeed = random();
#else
		newSeed = rand();
#endif
		seedUpdate = seed ^ newSeed;
	}

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








<
<
<
<
<
|
<







309
310
311
312
313
314
315





316

317
318
319
320
321
322
323
	newBuckets = [self allocMemoryWithSize: sizeof(*newBuckets)
					 count: newCapacity];

	for (i = 0; i < newCapacity; i++)
		newBuckets[i] = NULL;

	if (of_hash_seed != 0) {





		newSeed = of_random();

		seedUpdate = seed ^ newSeed;
	}

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

Modified src/OFObject.h from [c663f1ca8f] to [b44a7c59ae].

873
874
875
876
877
878
879

880
881
882
883
884
885
#import "OFObject+Serialization.h"

#ifdef __cplusplus
extern "C" {
#endif
extern id of_alloc_object(Class class_, size_t extraSize, size_t extraAlignment,
    void **extra);

extern size_t of_pagesize;
extern size_t of_num_cpus;
extern uint32_t of_hash_seed;
#ifdef __cplusplus
}
#endif







>






873
874
875
876
877
878
879
880
881
882
883
884
885
886
#import "OFObject+Serialization.h"

#ifdef __cplusplus
extern "C" {
#endif
extern id of_alloc_object(Class class_, size_t extraSize, size_t extraAlignment,
    void **extra);
extern uint32_t of_random(void);
extern size_t of_pagesize;
extern size_t of_num_cpus;
extern uint32_t of_hash_seed;
#ifdef __cplusplus
}
#endif

Modified src/OFObject.m from [e989a30df6] to [af5fd55d36].

224
225
226
227
228
229
230
































231
232
233
234
235
236
237
	}

	if OF_UNLIKELY (extra != NULL)
		*extra = (char*)instance + instanceSize + extraAlignment;

	return instance;
}

































const char*
_NSPrintForDebugger(id object)
{
	return [[object description]
	    cStringWithEncoding: OF_STRING_ENCODING_NATIVE];
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
	}

	if OF_UNLIKELY (extra != NULL)
		*extra = (char*)instance + instanceSize + extraAlignment;

	return instance;
}

uint32_t
of_random()
{
#if defined(OF_HAVE_ARC4RANDOM)
	return arc4random();
#elif defined(OF_HAVE_RANDOM)
	static BOOL initialized = NO;

	if (!initialized) {
		struct timeval t;

		gettimeofday(&t, NULL);
		srandom(t.tv_sec ^ t.tv_usec);
		initialized = YES;
	}

	return (random() << 16) | (random() & 0xFFFF);
#else
	static BOOL initialized = NO;

	if (!initialized) {
		struct timeval t;

		gettimeofday(&t, NULL);
		srand(t.tv_sec ^ t.tv_usec);
		initialized = YES;
	}

	return (random() << 16) | (random() & 0xFFFF);
#endif
}

const char*
_NSPrintForDebugger(id object)
{
	return [[object description]
	    cStringWithEncoding: OF_STRING_ENCODING_NATIVE];
}
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
		of_pagesize = 4096;
# ifdef _SC_NPROCESSORS_CONF
	if ((of_num_cpus = sysconf(_SC_NPROCESSORS_CONF)) < 1)
# endif
		of_num_cpus = 1;
#endif

#if defined(OF_HAVE_ARC4RANDOM)
	of_hash_seed = arc4random();
#elif defined(OF_HAVE_RANDOM)
	struct timeval t;
	gettimeofday(&t, NULL);
	srandom(t.tv_usec);
	of_hash_seed = random();
#else
	struct timeval t;
	gettimeofday(&t, NULL);
	srand(t.tv_usec);
	of_hash_seed = rand();
#endif
}

+ (void)initialize
{
}

+ alloc







<
<
<
<
<
<
<
<
<
<
<
|
<







307
308
309
310
311
312
313











314

315
316
317
318
319
320
321
		of_pagesize = 4096;
# ifdef _SC_NPROCESSORS_CONF
	if ((of_num_cpus = sysconf(_SC_NPROCESSORS_CONF)) < 1)
# endif
		of_num_cpus = 1;
#endif












	of_hash_seed = of_random();

}

+ (void)initialize
{
}

+ alloc