diff --git a/main.cpp b/main.cpp index b7cff44..7ef6806 100644 --- a/main.cpp +++ b/main.cpp @@ -7,8 +7,8 @@ void test_basics(amap mapdo, void *map) { assert(NULL == mapdo(map, AMAP_GET, "asdf", NULL)); int i = 42; int *iptr; - assert(NULL != mapdo(map, AMAP_SET, "meanging", &i)); - assert(NULL != (iptr = (int *)mapdo(map, AMAP_GET, "asdf", NULL))); + assert(NULL != mapdo(map, AMAP_SET, "meaning", &i)); + assert(NULL != (iptr = (int *)mapdo(map, AMAP_GET, "meaning", NULL))); assert(*iptr == 42); assert(iptr == &i); } diff --git a/simap.h b/simap.h index d02927c..bdeff42 100644 --- a/simap.h +++ b/simap.h @@ -132,8 +132,12 @@ static inline simap_ptr64 *simap_search_internal(simap_instance *map, const char /** Gets padding bytes for a size to be padded to divisible alignment */ static inline unsigned int get_size_padding(unsigned int size, unsigned int alignment) { - // return (alignment - (size % alignment)) % alignment; - return (size + alignment - 1) / alignment * alignment; + /* Would ensure returned value divisible by alignment */ + /* return (size + alignment - 1) / alignment * alignment; */ + /* same: return (alignment - (size % alignment)) % alignment; */ + + /* Substracting size leads to padding */ + return ((size + alignment - 1) / alignment) * alignment - size; } /** Returns the size of the storage needed for the given key */ @@ -151,8 +155,8 @@ static inline uint32_t simap_elem_storage_size(const char *key) { /** Force-add the (key,value) to the end of the map */ static inline void *simap_force_add_internal(simap_instance *map, const char *key, void *ptr) { uint32_t storage_needed = simap_elem_storage_size(key); - assert((storage_needed & 8) == 0); - if(map->end - map->usage_end > storage_needed) { + assert((storage_needed % 8) == 0); + if(map->end - map->usage_end < storage_needed) { /* Need storage */ aralloc(&(map->a), sizeof(uint8_t)/*esize*/,