Skip to content

Commit

Permalink
mapper: Replace CxRcMap with CxRcRadixTree
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Aug 17, 2024
1 parent 449cbf2 commit 70b0338
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 38 deletions.
21 changes: 6 additions & 15 deletions libsqsh/include/sqsh_mapper_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ struct SqshMapSlice {
* @privatesection
*/
struct SqshMapper *mapper;
size_t size;
uint64_t index;
uint8_t *data;
size_t size;
};

/**
Expand Down Expand Up @@ -147,14 +148,15 @@ SQSH_NO_EXPORT int sqsh__map_slice_cleanup(struct SqshMapSlice *mapping);
*
* @param[out] mapping The mapping to store the mapped data.
* @param[in] mapper The mapper to use for the mapping.
* @param[in] address The address in the input data to start the mapping.
* @param[in] offset The offset in the input data to start the mapping.
* @param[in] size The size of the mapped data.
*
* @return 0 on success, a negative value on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__map_slice_init(
struct SqshMapSlice *mapping, struct SqshMapper *mapper,
uint64_t offset, size_t size);
uint64_t address, uint64_t offset, size_t size);

/***************************************
* mapper/map_manager.c
Expand All @@ -169,8 +171,9 @@ struct SqshMapManager {
*/
struct SqshMapper mapper;
struct CxLru lru;
struct CxRcMap maps;
struct CxRcRadixTree maps;
uint64_t archive_offset;
uint64_t block_count;
sqsh__mutex_t lock;
};

Expand Down Expand Up @@ -213,18 +216,6 @@ sqsh__map_manager_size(const struct SqshMapManager *manager);
SQSH_NO_EXPORT size_t
sqsh__map_manager_block_size(const struct SqshMapManager *manager);

/**
* @internal
* @memberof SqshMapManager
* @brief Gets the number of chunks in the file.
*
* @param[in] manager The SqshMapManager instance.
*
* @return Returns the number of chunks in the file.
*/
SQSH_NO_EXPORT size_t
sqsh__map_manager_block_count(const struct SqshMapManager *manager);

/**
* @internal
* @memberof SqshMapManager
Expand Down
34 changes: 12 additions & 22 deletions libsqsh/src/mapper/map_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ load_mapping(
int rv = 0;

const size_t block_size = sqsh_mapper_block_size(&manager->mapper);
const size_t block_count = sqsh__map_manager_block_count(manager);
const uint64_t block_count = manager->block_count;
const uint64_t mapper_size = sqsh__map_manager_size(manager);
size_t size = block_size;
uint64_t offset;
Expand All @@ -69,7 +69,7 @@ load_mapping(
return -SQSH_ERROR_INTEGER_OVERFLOW;
}

rv = sqsh__map_slice_init(mapping, &manager->mapper, offset, size);
rv = sqsh__map_slice_init(mapping, &manager->mapper, index, offset, size);
if (rv < 0) {
goto out;
}
Expand All @@ -83,7 +83,6 @@ sqsh__map_manager_init(
struct SqshMapManager *manager, const void *input,
const struct SqshConfig *config) {
int rv;
uint64_t map_size;
const size_t lru_size = SQSH_CONFIG_DEFAULT(config->mapper_lru_size, 32);
const uint64_t archive_offset = config->archive_offset;

Expand All @@ -102,23 +101,19 @@ sqsh__map_manager_init(
rv = -SQSH_ERROR_OUT_OF_BOUNDS;
goto out;
}
map_size = SQSH_DIVIDE_CEIL(
manager->block_count = SQSH_DIVIDE_CEIL(
mapper_size - archive_offset,
sqsh_mapper_block_size(&manager->mapper));
if (map_size > SIZE_MAX) {
rv = -SQSH_ERROR_INTEGER_OVERFLOW;
goto out;
}

manager->archive_offset = archive_offset;
rv = cx_rc_map_init(
&manager->maps, (size_t)map_size, sizeof(struct SqshMapSlice),
map_cleanup_cb);
rv = cx_rc_radix_tree_init(
&manager->maps, sizeof(struct SqshMapSlice), map_cleanup_cb);
if (rv < 0) {
goto out;
}

rv = cx_lru_init(&manager->lru, lru_size, &cx_lru_rc_map, &manager->maps);
rv = cx_lru_init(
&manager->lru, lru_size, &cx_lru_rc_radix_tree, &manager->maps);
out:
if (rv < 0) {
sqsh__map_manager_cleanup(manager);
Expand All @@ -136,11 +131,6 @@ sqsh__map_manager_block_size(const struct SqshMapManager *manager) {
return sqsh_mapper_block_size(&manager->mapper);
}

size_t
sqsh__map_manager_block_count(const struct SqshMapManager *manager) {
return cx_rc_map_size(&manager->maps);
}

int
sqsh__map_manager_get(
struct SqshMapManager *manager, sqsh_index_t index,
Expand All @@ -152,7 +142,7 @@ sqsh__map_manager_get(
goto out;
}

*target = cx_rc_map_retain(&manager->maps, index);
*target = cx_rc_radix_tree_retain(&manager->maps, index);

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

*target = cx_rc_map_set(&manager->maps, index, &mapping);
*target = cx_rc_radix_tree_put(&manager->maps, index, &mapping);
}
rv = cx_lru_touch(&manager->lru, index);

Expand All @@ -179,15 +169,15 @@ sqsh__map_manager_get(
int
sqsh__map_manager_release(
struct SqshMapManager *manager, const struct SqshMapSlice *mapping) {
if (manager == NULL) {
if (manager == NULL || mapping == NULL) {
return 0;
}
int rv = sqsh__mutex_lock(&manager->lock);
if (rv < 0) {
goto out;
}

rv = cx_rc_map_release(&manager->maps, mapping);
cx_rc_radix_tree_release(&manager->maps, mapping->index);

sqsh__mutex_unlock(&manager->lock);
out:
Expand All @@ -197,7 +187,7 @@ sqsh__map_manager_release(
int
sqsh__map_manager_cleanup(struct SqshMapManager *manager) {
cx_lru_cleanup(&manager->lru);
cx_rc_map_cleanup(&manager->maps);
cx_rc_radix_tree_cleanup(&manager->maps);
sqsh__mapper_cleanup(&manager->mapper);

sqsh__mutex_destroy(&manager->lock);
Expand Down
3 changes: 2 additions & 1 deletion libsqsh/src/mapper/map_slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

int
sqsh__map_slice_init(
struct SqshMapSlice *mapping, struct SqshMapper *mapper,
struct SqshMapSlice *mapping, struct SqshMapper *mapper, uint64_t index,
uint64_t offset, size_t size) {
size_t end_offset;
uint64_t archive_size = sqsh_mapper_size2(mapper);
Expand All @@ -53,6 +53,7 @@ sqsh__map_slice_init(
if (end_offset > archive_size) {
return -SQSH_ERROR_SIZE_MISMATCH;
}
mapping->index = index;
mapping->mapper = mapper;
mapping->size = size;
if (mapper->impl->map == NULL) {
Expand Down

0 comments on commit 70b0338

Please sign in to comment.