Skip to content

Commit

Permalink
primitive: remove pointer indirection in SqshRcMap.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Jun 27, 2023
1 parent edd96b1 commit 4f287ae
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
5 changes: 2 additions & 3 deletions include/sqsh_primitive_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,10 @@ bool sqsh__rc_map_is_empty(struct SqshRcMap *array, sqsh_index_t index);
* @param array The array to set the value in.
* @param index The index to set the value at.
* @param data The data to set.
* @param span The number of indices to span. (Use 1 for a single index.)
* @return 0 on success, a negative value on error.
*/
const void *sqsh__rc_map_set(
struct SqshRcMap *array, sqsh_index_t index, void *data, int span);
struct SqshRcMap *array, sqsh_index_t index, void *data);

/**
* @internal
Expand All @@ -249,7 +248,7 @@ size_t sqsh__rc_map_size(const struct SqshRcMap *array);
* @param index The index of the data.
* @return A pointer to the retained data.
*/
const void *sqsh__rc_map_retain(struct SqshRcMap *array, sqsh_index_t *index);
const void *sqsh__rc_map_retain(struct SqshRcMap *array, sqsh_index_t index);

/**
* @internal
Expand Down
7 changes: 3 additions & 4 deletions lib/mapper/map_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,13 @@ sqsh__map_manager_get(
const struct SqshMapSlice **target) {
int rv = 0;
assert(span == 1);
sqsh_index_t real_index = index;

rv = sqsh_mutex_lock(&manager->lock);
if (rv < 0) {
goto out;
}

*target = sqsh__rc_map_retain(&manager->maps, &real_index);
*target = sqsh__rc_map_retain(&manager->maps, index);

if (*target == NULL) {
struct SqshMapSlice mapping = {0};
Expand All @@ -158,9 +157,9 @@ sqsh__map_manager_get(
goto out;
}

*target = sqsh__rc_map_set(&manager->maps, index, &mapping, span);
*target = sqsh__rc_map_set(&manager->maps, index, &mapping);
}
rv = sqsh__lru_touch(&manager->lru, real_index);
rv = sqsh__lru_touch(&manager->lru, index);

out:
sqsh_mutex_unlock(&manager->lock);
Expand Down
5 changes: 2 additions & 3 deletions lib/primitive/rc_hash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ sqsh__rc_hash_map_put(

found:
keys[index] = key;
return sqsh__rc_map_set(values, index, data, 1);
return sqsh__rc_map_set(values, index, data);
}

size_t
Expand All @@ -159,8 +159,7 @@ sqsh__rc_hash_map_retain(
struct SqshRcMap *values = &hash_map->hash_maps[j].values;
sqsh_rc_map_key_t *keys = hash_map->hash_maps[j].keys;
if (keys[index] == key) {
sqsh_index_t real_index = index;
return sqsh__rc_map_retain(values, &real_index);
return sqsh__rc_map_retain(values, index);
}
}
}
Expand Down
19 changes: 8 additions & 11 deletions lib/primitive/rc_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,16 @@ sqsh__rc_map_is_empty(struct SqshRcMap *array, sqsh_index_t index) {

const void *
sqsh__rc_map_set(
struct SqshRcMap *array, sqsh_index_t index, void *data, int span) {
(void)span;
struct SqshRcMap *array, sqsh_index_t index, void *data) {
void *target;

assert(span == 1);

target = get_element(array, index);

/* If the element is already in the array, cleanup the new data and retain
* the old. */
if (sqsh__rc_map_is_empty(array, index) == false) {
array->cleanup(data);
return sqsh__rc_map_retain(array, &index);
return sqsh__rc_map_retain(array, index);
}

memcpy(target, data, array->element_size);
Expand All @@ -154,13 +151,13 @@ sqsh__rc_map_set(
}

const void *
sqsh__rc_map_retain(struct SqshRcMap *array, sqsh_index_t *index) {
sqsh__rc_map_retain(struct SqshRcMap *array, sqsh_index_t index) {
void *data = NULL;

if (sqsh__rc_map_is_empty(array, *index) == false) {
retain_rc(array, *index);
debug_print(array, *index, '+');
data = get_element(array, *index);
if (sqsh__rc_map_is_empty(array, index) == false) {
retain_rc(array, index);
debug_print(array, index, '+');
data = get_element(array, index);
}

return data;
Expand Down Expand Up @@ -236,7 +233,7 @@ sqsh__rc_map_cleanup(struct SqshRcMap *array) {

static const void *
lru_rc_map_retain(void *backend, sqsh_index_t index) {
return sqsh__rc_map_retain(backend, &index);
return sqsh__rc_map_retain(backend, index);
}

static int
Expand Down
4 changes: 2 additions & 2 deletions test/primitive/rc_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ set_and_get_element(void) {
assert(rv == 0);

sqsh_index_t index = 42;
const uint8_t *set_ptr = sqsh__rc_map_set(&map, index, &data, 1);
const uint8_t *set_ptr = sqsh__rc_map_set(&map, index, &data);
assert(rv == 0);
assert(set_ptr != &data);

const uint8_t *get_ptr = sqsh__rc_map_retain(&map, &index);
const uint8_t *get_ptr = sqsh__rc_map_retain(&map, index);
assert(rv == 0);
assert(get_ptr != &data);
assert(get_ptr == get_ptr);
Expand Down

0 comments on commit 4f287ae

Please sign in to comment.