Index: src/OFMapTable.h ================================================================== --- src/OFMapTable.h +++ src/OFMapTable.h @@ -48,12 +48,10 @@ { of_map_table_functions_t keyFunctions, valueFunctions; struct of_map_table_bucket **buckets; uint32_t minCapacity, capacity, count; unsigned long mutations; - uint32_t seed; - uint8_t rotate; } /*! * @brief Creates a new OFMapTable with the specified key and value functions. * Index: src/OFMapTable.m ================================================================== --- src/OFMapTable.m +++ src/OFMapTable.m @@ -14,17 +14,14 @@ * file. */ #include "config.h" -#include #include #include -#include - #import "OFMapTable.h" #import "OFEnumerator.h" #import "OFEnumerationMutationException.h" #import "OFInvalidArgumentException.h" @@ -150,15 +147,10 @@ buckets = [self allocMemoryWithSize: sizeof(*buckets) count: capacity]; memset(buckets, 0, capacity * sizeof(*buckets)); - - if (of_hash_seed != 0) { - seed = of_random(); - rotate = of_random() & 0x1F; - } } @catch (id e) { [self release]; @throw e; } @@ -232,13 +224,11 @@ for (i = 0; i < capacity; i++) if (buckets[i] != NULL && buckets[i] != &deleted) [copy OF_setValue: buckets[i]->value forKey: buckets[i]->key - hash: OF_ROR( - buckets[i]->hash ^ seed, - rotate)]; + hash: buckets[i]->hash]; } @catch (id e) { [copy release]; @throw e; } @@ -259,11 +249,11 @@ if (key == NULL) @throw [OFInvalidArgumentException exceptionWithClass: [self class] selector: _cmd]; - hash = OF_ROL(keyFunctions.hash(key) ^ seed, rotate); + hash = keyFunctions.hash(key); last = capacity; for (i = hash & (capacity - 1); i < last && buckets[i] != NULL; i++) { if (buckets[i] == &deleted) continue; @@ -289,13 +279,12 @@ return NULL; } - (void)OF_resizeForCount: (uint32_t)newCount { - uint32_t i, fullness, newCapacity, newSeed = 0, seedUpdate = 0; + uint32_t i, fullness, newCapacity; struct of_map_table_bucket **newBuckets; - uint8_t newRotate = 0; if (newCount > UINT32_MAX || newCount > UINT32_MAX / sizeof(*buckets) || newCount > UINT32_MAX / 8) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; @@ -315,25 +304,14 @@ count: newCapacity]; for (i = 0; i < newCapacity; i++) newBuckets[i] = NULL; - if (of_hash_seed != 0) { - newSeed = of_random(); - seedUpdate = seed ^ newSeed; - - newRotate = of_random() & 0x1F; - } - for (i = 0; i < capacity; i++) { if (buckets[i] != NULL && buckets[i] != &deleted) { uint32_t j, last; - buckets[i]->hash = OF_ROL( - OF_ROR(buckets[i]->hash, rotate) ^ seedUpdate, - newRotate); - last = newCapacity; j = buckets[i]->hash & (newCapacity - 1); for (; j < last && newBuckets[j] != NULL; j++); @@ -352,41 +330,37 @@ } [self freeMemory: buckets]; buckets = newBuckets; capacity = newCapacity; - seed = newSeed; - rotate = newRotate; } - (void)OF_setValue: (void*)value forKey: (void*)key hash: (uint32_t)hash { - uint32_t i, last, seededHash; + uint32_t i, last; void *old; if (key == NULL || value == NULL) @throw [OFInvalidArgumentException exceptionWithClass: [self class] selector: _cmd]; last = capacity; - seededHash = OF_ROL(hash ^ seed, rotate); - for (i = seededHash & (capacity - 1); i < last && buckets[i] != NULL; - i++) { + for (i = hash & (capacity - 1); i < last && buckets[i] != NULL; i++) { if (buckets[i] == &deleted) continue; if (keyFunctions.equal(buckets[i]->key, key)) break; } /* In case the last bucket is already used */ if (i >= last) { - last = seededHash & (capacity - 1); + last = hash & (capacity - 1); for (i = 0; i < last && buckets[i] != NULL; i++) { if (buckets[i] == &deleted) continue; @@ -399,21 +373,20 @@ if (i >= last || buckets[i] == NULL || buckets[i] == &deleted || !keyFunctions.equal(buckets[i]->key, key)) { struct of_map_table_bucket *bucket; [self OF_resizeForCount: count + 1]; - seededHash = OF_ROL(hash ^ seed, rotate); mutations++; last = capacity; - for (i = seededHash & (capacity - 1); i < last && + for (i = hash & (capacity - 1); i < last && buckets[i] != NULL && buckets[i] != &deleted; i++); /* In case the last bucket is already used */ if (i >= last) { - last = seededHash & (capacity - 1); + last = hash & (capacity - 1); for (i = 0; i < last && buckets[i] != NULL && buckets[i] != &deleted; i++); } @@ -436,11 +409,11 @@ keyFunctions.release(key); [self freeMemory: bucket]; @throw e; } - bucket->hash = seededHash; + bucket->hash = hash; buckets[i] = bucket; count++; return; @@ -466,11 +439,11 @@ if (key == NULL) @throw [OFInvalidArgumentException exceptionWithClass: [self class] selector: _cmd]; - hash = OF_ROL(keyFunctions.hash(key) ^ seed, rotate); + hash = keyFunctions.hash(key); last = capacity; for (i = hash & (capacity - 1); i < last && buckets[i] != NULL; i++) { if (buckets[i] == &deleted) continue; Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -875,12 +875,11 @@ #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 Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -227,42 +227,10 @@ *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((unsigned)(t.tv_sec ^ t.tv_usec)); - initialized = YES; - } - - return (uint32_t)((random() << 16) | (random() & 0xFFFF)); -#else - static BOOL initialized = NO; - - if (!initialized) { - struct timeval t; - - gettimeofday(&t, NULL); - srand((unsigned)(t.tv_sec ^ t.tv_usec)); - initialized = YES; - } - - return (rand() << 16) | (rand() & 0xFFFF); -#endif -} - const char* _NSPrintForDebugger(id object) { return [[object description] cStringWithEncoding: OF_STRING_ENCODING_NATIVE]; @@ -309,11 +277,23 @@ if ((of_num_cpus = sysconf(_SC_NPROCESSORS_CONF)) < 1) # endif of_num_cpus = 1; #endif - of_hash_seed = of_random(); +#if defined(OF_HAVE_ARC4RANDOM) + of_hash_seed = arc4random(); +#elif defined(OF_HAVE_RANDOM) + struct timeval t; + gettimeofday(&t, NULL); + srandom((unsigned)(t.tv_sec ^ t.tv_usec)); + of_hash_seed = (uint32_t)((random() << 16) | (random() & 0xFFFF)); +#else + struct timeval t; + gettimeofday(&t, NULL); + srand((unsigned)(t.tv_sec ^ t.tv_usec)); + of_hash_seed = (uint32_t)((rand() << 16) | (rand() & 0xFFFF)); +#endif } + (void)initialize { } Index: src/macros.h ================================================================== --- src/macros.h +++ src/macros.h @@ -323,13 +323,10 @@ #endif #define OF_ROL(value, bits) \ (((value) << ((bits) % (sizeof(value) * 8))) | \ (value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) -#define OF_ROR(value, bits) \ - (((value) >> ((bits) % (sizeof(value) * 8))) | \ - (value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) #define OF_HASH_INIT(hash) hash = of_hash_seed #define OF_HASH_ADD(hash, byte) \ { \ hash += (uint8_t)(byte); \