diff options
author | Dimitri Staessens <[email protected]> | 2016-03-20 10:35:01 +0100 |
---|---|---|
committer | Dimitri Staessens <[email protected]> | 2016-03-20 10:35:01 +0100 |
commit | b3009103bd2b86b2ee0df2a71300bb976442c6a6 (patch) | |
tree | 8a952d8e6eed7fbfbb1431af106d75c5bb82eddd | |
parent | a9bd08bf09c7baa9254a4b63aacb6bbb23f85f07 (diff) | |
download | ouroboros-b3009103bd2b86b2ee0df2a71300bb976442c6a6.tar.gz ouroboros-b3009103bd2b86b2ee0df2a71300bb976442c6a6.zip |
lib: optimization of ringbuffer
Got rid of divisions and modulo operations.
Change in the meaning of a #define.
SHM_DU_MAP_SIZE: is now an exponent of 2. Default is 10, allowing
for 1024 blocks in the map.
-rw-r--r-- | include/ouroboros/shm_du_map.h | 2 | ||||
-rw-r--r-- | src/lib/shm_du_map.c | 29 |
2 files changed, 15 insertions, 16 deletions
diff --git a/include/ouroboros/shm_du_map.h b/include/ouroboros/shm_du_map.h index bfccf60a..b3533fa3 100644 --- a/include/ouroboros/shm_du_map.h +++ b/include/ouroboros/shm_du_map.h @@ -39,7 +39,7 @@ #endif #ifndef SHM_DU_MAP_SIZE -#define SHM_DU_MAP_SIZE (1 << 26) +#define SHM_DU_MAP_SIZE 10 #endif #include "common.h" diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index 630cfa42..d071b0c3 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -35,7 +35,7 @@ #define SHM_DU_BLOCK_DATA_SIZE (SHM_DU_BUFF_BLOCK_SIZE - \ sizeof(struct shm_block)) -#define SHM_BLOCKS_IN_MAP (SHM_DU_MAP_SIZE / SHM_DU_BUFF_BLOCK_SIZE) +#define SHM_BLOCKS_IN_MAP (1 << SHM_DU_MAP_SIZE) #define SHM_BLOCKS_SIZE (SHM_DU_BUFF_BLOCK_SIZE * SHM_BLOCKS_IN_MAP) #define SHM_BUFFS_SIZE (SHM_BLOCKS_IN_MAP * sizeof (struct shm_du_buff)) #define SHM_FILE_SIZE (SHM_BLOCKS_IN_MAP * (SHM_DU_BUFF_BLOCK_SIZE \ @@ -52,9 +52,9 @@ / SHM_DU_BUFF_BLOCK_SIZE) #define shm_map_used(dum) ((*(dum->ptr_head) + SHM_BLOCKS_IN_MAP - \ - *(dum->ptr_tail)) % SHM_BLOCKS_IN_MAP) + *(dum->ptr_tail)) & (SHM_BLOCKS_IN_MAP - 1)) -#define shm_map_free(dum, i)(shm_map_used(dum) + i < SHM_BLOCKS_IN_MAP) +#define shm_map_free(dum, i)(shm_map_used(dum) + i + 1 < SHM_BLOCKS_IN_MAP) struct shm_block { size_t size; @@ -118,8 +118,6 @@ struct shm_du_map * shm_du_map_create() if (shm_base == MAP_FAILED) { LOG_ERR("Failed to map shared memory."); - if (close(shm_fd) == -1) - LOG_ERR("Failed to close invalid shm."); if (shm_unlink(SHM_DU_MAP_FILENAME) == -1) LOG_ERR("Failed to remove invalid shm."); @@ -173,9 +171,6 @@ struct shm_du_map * shm_du_map_open() if (shm_base == MAP_FAILED) { LOG_ERR("Failed to map shared memory."); - if (close(shm_fd) == -1) - LOG_ERR("Failed to close invalid shm."); - if (shm_unlink(SHM_DU_MAP_FILENAME) == -1) LOG_ERR("Failed to unlink invalid shm."); @@ -228,7 +223,8 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, size_t remaining = size; size_t ts = size - (headspace + len); uint8_t * read_pos = data; - size_t blocks; + size_t blocks = 0; + int sz = size; if (dum == NULL || data == NULL) { LOG_DBGF("Bogus input, bugging out."); @@ -252,9 +248,10 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, pthread_mutex_lock(dum->shm_mutex); - blocks = size / SHM_DU_BLOCK_DATA_SIZE; - if (size % SHM_DU_BLOCK_DATA_SIZE > 0) - ++blocks; + while (sz > 0) { + sz -= SHM_DU_BLOCK_DATA_SIZE; + blocks++; + } if (!shm_map_free(dum, blocks)) { pthread_mutex_unlock(dum->shm_mutex); @@ -316,7 +313,8 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, prev_index = *dum->ptr_head; - *(dum->ptr_head) = (*dum->ptr_head + 1) % SHM_BLOCKS_IN_MAP; + *(dum->ptr_head) = (*dum->ptr_head + 1) + & (SHM_BLOCKS_IN_MAP - 1); } pthread_mutex_unlock(dum->shm_mutex); @@ -337,11 +335,12 @@ int shm_release_du_buff(struct shm_du_map * dum) } while (idx_to_block_ptr(dum, *dum->ptr_tail)->next != -1) { - *(dum->ptr_tail) = (*dum->ptr_tail + 1) % SHM_BLOCKS_IN_MAP; + *(dum->ptr_tail) = (*dum->ptr_tail + 1) + & (SHM_BLOCKS_IN_MAP -1); released++; } - *(dum->ptr_tail) = (*dum->ptr_tail + 1) % SHM_BLOCKS_IN_MAP; + *(dum->ptr_tail) = (*dum->ptr_tail + 1) & (SHM_BLOCKS_IN_MAP - 1); pthread_mutex_unlock(dum->shm_mutex); |