Compare commits

..

3 Commits

Author SHA1 Message Date
Richard Thier
047babeb1b testing the tombstone-ing 2024-10-10 16:38:15 +02:00
Richard Thier
21351fd2b4 fixed edge-case when padding was not there and zero-terminator got overridden 2024-10-10 16:32:07 +02:00
Richard Thier
14052a8421 more meaningful tests for finding the errors: 8-length special case bug found 2024-10-10 16:06:08 +02:00
2 changed files with 9 additions and 6 deletions

View File

@ -15,6 +15,7 @@ void test_basics(amap mapdo, void *map) {
/* Delete / tombstone */
assert(NULL != mapdo(map, AMAP_SET, "meaning", NULL));
assert(NULL == (int *)mapdo(map, AMAP_GET, "meaning", NULL));
/* Check re-adding */
assert(NULL != mapdo(map, AMAP_SET, "meaning", &i));
@ -26,7 +27,7 @@ void test_basics(amap mapdo, void *map) {
assert(NULL != mapdo(map, AMAP_ERASE, NULL, NULL));
/* Check re-adding 3 new things */
assert(NULL != mapdo(map, AMAP_SET, "meaning1", &i));
assert(NULL != mapdo(map, AMAP_SET, "meaningless1", &i));
assert(NULL != mapdo(map, AMAP_SET, "meaning2", &i));
const char *helloworld = "Hello world!";
assert(NULL != mapdo(map, AMAP_SET, "hello", (char *)helloworld)); /* TODO: ugly cast... */
@ -37,7 +38,7 @@ void test_basics(amap mapdo, void *map) {
assert(NULL != (iptr = (int *)mapdo(map, AMAP_GET, "meaning2", NULL)));
assert(*iptr == 42);
assert(iptr == &i);
assert(NULL != (iptr = (int *)mapdo(map, AMAP_GET, "meaning1", NULL)));
assert(NULL != (iptr = (int *)mapdo(map, AMAP_GET, "meaningless1", NULL)));
assert(*iptr == 42);
assert(iptr == &i);
}

10
simap.h
View File

@ -140,6 +140,8 @@ static inline unsigned int get_size_padding(unsigned int size, unsigned int alig
static inline uint32_t simap_elem_storage_size(const char *key) {
uint32_t keysize = strlen(key);
uint32_t padding = get_size_padding(keysize, 8);
/* XXX: The exactly 8byte keys need a zero terminator too (would be overridden without this) */
padding += (keysize == 8) ? 8 : 0;
return keysize +
sizeof(simap_ptr64) +
@ -173,18 +175,17 @@ static inline void *simap_force_add_internal(simap_instance *map, const char *ke
uint32_t usi = map->usage_end;
uint32_t previ = map->prev_usage_end;
/* Save data ptr */
/* 8byte: Save data ptr */
simap_ptr64 *data = (simap_ptr64 *)((uint8_t *)(map->base) + usi);
data->ptr = ptr;
/* Save link to previous */
/* 8byte: Save link to previous and next */
uint32_t *usprev = (uint32_t *)((uint8_t *)(map->base) + usi +
sizeof(simap_ptr64));
*usprev = previ;
/* and nex */
*(usprev + 1) = (uint32_t) -1;
/* First 8 bytes */
/* 8byte: First 8 char */
simap_c64 *start_str = (simap_c64 *)(usprev + 2);
*start_str = first8;
@ -194,6 +195,7 @@ static inline void *simap_force_add_internal(simap_instance *map, const char *ke
char *rem_str = (char *)(start_str + 1);
strcpy(rem_str, key + 8);
}
/* XXX: The "padding" gets automagically added by the movement of the arena here(by junk bytes)! */
/* Update previous with linkage */
if(previ != (uint32_t)-1) {