diff --git a/src/sfxd.c b/src/sfxd.c index ca278b1..13751c4 100644 --- a/src/sfxd.c +++ b/src/sfxd.c @@ -16,6 +16,9 @@ #define MAX_SOUNDS_PER_KEY 32 #define MAX_SOURCE_DEPTH 32 +// Set initial offset to size to distinguish start and end +#define FOR_OFFSET_PROBE(base, offset, index, size) for (int offset = (size) | 1, index = (base + offset) % (size); offset > 0; offset = (offset << 1L) % ((size) | 1), offset = offset == (size) ? (size) - 1 /* n * 2 = n - 1 (mod n + 1) */ : offset, index = (base + offset) % (size)) + struct msg_target { FILE *file_handle; int sock_fd; @@ -361,8 +364,7 @@ void sfx_pool_grow(int size) { for (int i = 0; i < sounds_pool.cap; i++) { if (sounds_pool.sounds[i].key[0] == '\0') continue; uint32_t new_hash = adler32(sounds_pool.sounds[i].key, strlen(sounds_pool.sounds[i].key)); - for (int offset = 0; offset < size; offset++) { - int index = (new_hash + offset) % size; + FOR_OFFSET_PROBE (new_hash, offset, index, size) { #ifndef NO_COUNTERS global_counters.pool_write++; if (offset != 0) { @@ -403,8 +405,7 @@ struct sfx_pool_item *sfx_pool_lookup(const char *key) { struct sfx_pool_item *sfx_pool_find_place_for(const char *key) { uint32_t hash = adler32(key, strlen(key)); - for (int offset = 0; offset < sounds_pool.cap; offset++) { - int index = (hash + offset) % sounds_pool.cap; + FOR_OFFSET_PROBE (hash, offset, index, sounds_pool.cap) { if (sounds_pool.sounds[index].key[0] == '\0' || 0 == strncmp(sounds_pool.sounds[index].key, key, KEY_LENGTH)) { #ifndef NO_COUNTERS global_counters.pool_write++;