forked from hkc/sfxd
1
0
Fork 0

Create a macro for exponential probe loop

This commit is contained in:
Vftdan 2024-02-24 23:51:45 +01:00
parent f95224db34
commit 1e8bad5dd2
1 changed files with 5 additions and 4 deletions

View File

@ -16,6 +16,9 @@
#define MAX_SOUNDS_PER_KEY 32 #define MAX_SOUNDS_PER_KEY 32
#define MAX_SOURCE_DEPTH 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 { struct msg_target {
FILE *file_handle; FILE *file_handle;
int sock_fd; int sock_fd;
@ -361,8 +364,7 @@ void sfx_pool_grow(int size) {
for (int i = 0; i < sounds_pool.cap; i++) { for (int i = 0; i < sounds_pool.cap; i++) {
if (sounds_pool.sounds[i].key[0] == '\0') continue; 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)); uint32_t new_hash = adler32(sounds_pool.sounds[i].key, strlen(sounds_pool.sounds[i].key));
for (int offset = 0; offset < size; offset++) { FOR_OFFSET_PROBE (new_hash, offset, index, size) {
int index = (new_hash + offset) % size;
#ifndef NO_COUNTERS #ifndef NO_COUNTERS
global_counters.pool_write++; global_counters.pool_write++;
if (offset != 0) { 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) { struct sfx_pool_item *sfx_pool_find_place_for(const char *key) {
uint32_t hash = adler32(key, strlen(key)); uint32_t hash = adler32(key, strlen(key));
for (int offset = 0; offset < sounds_pool.cap; offset++) { FOR_OFFSET_PROBE (hash, offset, index, sounds_pool.cap) {
int index = (hash + offset) % sounds_pool.cap;
if (sounds_pool.sounds[index].key[0] == '\0' || 0 == strncmp(sounds_pool.sounds[index].key, key, KEY_LENGTH)) { if (sounds_pool.sounds[index].key[0] == '\0' || 0 == strncmp(sounds_pool.sounds[index].key, key, KEY_LENGTH)) {
#ifndef NO_COUNTERS #ifndef NO_COUNTERS
global_counters.pool_write++; global_counters.pool_write++;