Index: src/OFMapTable.h ================================================================== --- src/OFMapTable.h +++ src/OFMapTable.h @@ -154,10 +154,15 @@ * * @param key The key whose object should be removed */ - (void)removeValueForKey: (void*)key; +/*! + * @brief Removes all values. + */ +- (void)removeAllValues; + /*! * @brief Checks whether the map table contains a value equal to the specified * value. * * @param value The value which is checked for being in the map table Index: src/OFMapTable.m ================================================================== --- src/OFMapTable.m +++ src/OFMapTable.m @@ -492,10 +492,49 @@ return; } } } + +- (void)removeAllValues +{ + uint32_t i; + + for (i = 0; i < _capacity; i++) { + if (_buckets[i] != NULL) { + if (_buckets[i] == &deleted) { + _buckets[i] = NULL; + continue; + } + + _keyFunctions.release(_buckets[i]->key); + _valueFunctions.release(_buckets[i]->value); + + [self freeMemory: _buckets[i]]; + _buckets[i] = NULL; + } + } + + _count = 0; + _capacity = MIN_CAPACITY; + _buckets = [self resizeMemory: _buckets + size: 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) +#if defined(HAVE_ARC4RANDOM) + _rotate = arc4random() & 31; +#elif defined(HAVE_RANDOM) + _rotate = random() & 31; +#else + _rotate = rand() & 31; +#endif +} - (bool)containsValue: (void*)value { uint32_t i; Index: src/OFMutableDictionary.h ================================================================== --- src/OFMutableDictionary.h +++ src/OFMutableDictionary.h @@ -69,10 +69,15 @@ * * @param key The key whose object should be removed */ - (void)removeObjectForKey: (id)key; +/*! + * @brief Removes all objects. + */ +- (void)removeAllObjects; + #ifdef OF_HAVE_BLOCKS /*! * @brief Replaces each object with the object returned by the block. * * @param block The block which returns a new object for each object Index: src/OFMutableDictionary.m ================================================================== --- src/OFMutableDictionary.m +++ src/OFMutableDictionary.m @@ -18,10 +18,12 @@ #include #import "OFMutableDictionary_hashtable.h" +#import "autorelease.h" + static struct { Class isa; } placeholder; @interface OFMutableDictionary_placeholder: OFDictionary @@ -187,10 +189,23 @@ - (void)removeObjectForKey: (id)key { [self doesNotRecognizeSelector: _cmd]; abort(); } + +- (void)removeAllObjects +{ + void *pool = objc_autoreleasePoolPush(); + OFArray *keys = [self allKeys]; + OFEnumerator *enumerator = [keys objectEnumerator]; + id key; + + while ((key = [enumerator nextObject]) != nil) + [self removeObjectForKey: key]; + + objc_autoreleasePoolPop(pool); +} - copy { return [[OFDictionary alloc] initWithDictionary: self]; } Index: src/OFMutableDictionary_hashtable.m ================================================================== --- src/OFMutableDictionary_hashtable.m +++ src/OFMutableDictionary_hashtable.m @@ -43,10 +43,15 @@ - (void)removeObjectForKey: (id)key { [_mapTable removeValueForKey: key]; } + +- (void)removeAllObjects +{ + [_mapTable removeAllValues]; +} #ifdef OF_HAVE_BLOCKS - (void)replaceObjectsUsingBlock: (of_dictionary_replace_block_t)block { @try {