mirror of
https://github.com/amadvance/snapraid.git
synced 2026-12-01 20:36:38 -05:00
Update to TommyDS v4.0
This commit is contained in:
parent
a39fb132f8
commit
9505499e15
21 changed files with 2220 additions and 959 deletions
|
|
@ -3,12 +3,16 @@
|
|||
|
||||
#include "tommyarrayblkof.h"
|
||||
|
||||
#include <string.h> /* for memset */
|
||||
|
||||
/******************************************************************************/
|
||||
/* array */
|
||||
|
||||
TOMMY_API void tommy_arrayblkof_init(tommy_arrayblkof* array, tommy_size_t element_size)
|
||||
{
|
||||
tommy_array_init(&array->block);
|
||||
array->block_capacity = (tommy_size_t)1 << TOMMY_ARRAYBLKOF_BIT;
|
||||
array->block = tommy_cast(unsigned char**, tommy_malloc(array->block_capacity * sizeof(array->block[0])));
|
||||
array->block_count = 0;
|
||||
|
||||
array->element_size = element_size;
|
||||
array->count = 0;
|
||||
|
|
@ -16,44 +20,127 @@ TOMMY_API void tommy_arrayblkof_init(tommy_arrayblkof* array, tommy_size_t eleme
|
|||
|
||||
TOMMY_API void tommy_arrayblkof_done(tommy_arrayblkof* array)
|
||||
{
|
||||
tommy_size_t i;
|
||||
for (tommy_size_t i = 0; i < array->block_count; ++i)
|
||||
tommy_free(array->block[i]);
|
||||
|
||||
for (i = 0; i < tommy_array_size(&array->block); ++i)
|
||||
tommy_free(tommy_array_get(&array->block, i));
|
||||
|
||||
tommy_array_done(&array->block);
|
||||
tommy_free(array->block);
|
||||
}
|
||||
|
||||
TOMMY_API void tommy_arrayblkof_grow(tommy_arrayblkof* array, tommy_size_t count)
|
||||
TOMMY_API void tommy_arrayblkof_reserve(tommy_arrayblkof* array, tommy_size_t size)
|
||||
{
|
||||
tommy_size_t block_max;
|
||||
tommy_size_t block_mac;
|
||||
|
||||
if (array->count >= count)
|
||||
if (size <= array->block_count * TOMMY_ARRAYBLKOF_SIZE)
|
||||
return;
|
||||
array->count = count;
|
||||
|
||||
block_max = (count + TOMMY_ARRAYBLKOF_SIZE - 1) / TOMMY_ARRAYBLKOF_SIZE;
|
||||
block_mac = tommy_array_size(&array->block);
|
||||
tommy_size_t block_max = (size - 1) / TOMMY_ARRAYBLKOF_SIZE + 1;
|
||||
|
||||
if (block_mac < block_max) {
|
||||
/* grow the block array */
|
||||
tommy_array_grow(&array->block, block_max);
|
||||
if (array->block_capacity < block_max) {
|
||||
/* only the directory moves; addresses inside existing blocks stay valid */
|
||||
tommy_size_t capacity = array->block_capacity;
|
||||
while (capacity < block_max)
|
||||
capacity *= 2;
|
||||
array->block = tommy_cast(unsigned char**, tommy_realloc(array->block, capacity * sizeof(array->block[0])));
|
||||
array->block_capacity = capacity;
|
||||
}
|
||||
|
||||
/* allocate new blocks */
|
||||
while (block_mac < block_max) {
|
||||
void** ptr = tommy_cast(void**, tommy_calloc(TOMMY_ARRAYBLKOF_SIZE, array->element_size));
|
||||
/* allocate new blocks */
|
||||
while (array->block_count < block_max) {
|
||||
unsigned char* ptr = tommy_cast(unsigned char*, tommy_calloc(TOMMY_ARRAYBLKOF_SIZE, array->element_size));
|
||||
|
||||
/* set the new block */
|
||||
tommy_array_set(&array->block, block_mac, ptr);
|
||||
|
||||
++block_mac;
|
||||
}
|
||||
array->block[array->block_count] = ptr;
|
||||
++array->block_count;
|
||||
}
|
||||
}
|
||||
|
||||
TOMMY_API tommy_size_t tommy_arrayblkof_memory_usage(tommy_arrayblkof* array)
|
||||
TOMMY_API void tommy_arrayblkof_resize(tommy_arrayblkof* array, tommy_size_t size)
|
||||
{
|
||||
return tommy_array_memory_usage(&array->block) + tommy_array_size(&array->block) * TOMMY_ARRAYBLKOF_SIZE * array->element_size;
|
||||
if (size >= array->count) {
|
||||
tommy_arrayblkof_grow(array, size);
|
||||
return;
|
||||
}
|
||||
|
||||
/* clear the unused elements to maintain the invariant that unused slots are zero */
|
||||
tommy_size_t pos = size;
|
||||
while (pos < array->count) {
|
||||
tommy_size_t blk_idx = pos / TOMMY_ARRAYBLKOF_SIZE;
|
||||
tommy_size_t blk_offset = pos % TOMMY_ARRAYBLKOF_SIZE;
|
||||
tommy_size_t blk_rem = TOMMY_ARRAYBLKOF_SIZE - blk_offset;
|
||||
tommy_size_t count_rem = array->count - pos;
|
||||
tommy_size_t chunk = blk_rem < count_rem ? blk_rem : count_rem;
|
||||
|
||||
memset(array->block[blk_idx] + blk_offset * array->element_size, 0, chunk * array->element_size);
|
||||
pos += chunk;
|
||||
}
|
||||
|
||||
array->count = size;
|
||||
}
|
||||
|
||||
TOMMY_API void tommy_arrayblkof_shrink(tommy_arrayblkof* array)
|
||||
{
|
||||
tommy_size_t block_max = (array->count == 0) ? 0 : ((array->count - 1) / TOMMY_ARRAYBLKOF_SIZE + 1);
|
||||
|
||||
while (array->block_count > block_max) {
|
||||
--array->block_count;
|
||||
tommy_free(array->block[array->block_count]);
|
||||
}
|
||||
|
||||
tommy_size_t min_capacity = (tommy_size_t)1 << TOMMY_ARRAYBLKOF_BIT;
|
||||
tommy_size_t capacity = min_capacity;
|
||||
while (capacity < block_max)
|
||||
capacity *= 2;
|
||||
if (capacity < array->block_capacity) {
|
||||
array->block = tommy_cast(unsigned char**, tommy_realloc(array->block, capacity * sizeof(array->block[0])));
|
||||
array->block_capacity = capacity;
|
||||
}
|
||||
}
|
||||
|
||||
TOMMY_API void tommy_arrayblkof_foreach(tommy_arrayblkof* array, tommy_foreach_func* func)
|
||||
{
|
||||
/* cache the element size to avoid reloading it after each callback */
|
||||
tommy_size_t element_size = array->element_size;
|
||||
tommy_size_t pos = 0;
|
||||
|
||||
while (pos < array->count) {
|
||||
tommy_size_t blk_idx = pos / TOMMY_ARRAYBLKOF_SIZE;
|
||||
tommy_size_t chunk = array->count - pos;
|
||||
unsigned char* ptr = array->block[blk_idx];
|
||||
|
||||
if (chunk > TOMMY_ARRAYBLKOF_SIZE)
|
||||
chunk = TOMMY_ARRAYBLKOF_SIZE;
|
||||
|
||||
for (tommy_size_t i = 0; i < chunk; ++i) {
|
||||
func(ptr);
|
||||
ptr += element_size;
|
||||
}
|
||||
|
||||
pos += chunk;
|
||||
}
|
||||
}
|
||||
|
||||
TOMMY_API void tommy_arrayblkof_foreach_arg(tommy_arrayblkof* array, tommy_foreach_arg_func* func, void* arg)
|
||||
{
|
||||
/* cache the element size to avoid reloading it after each callback */
|
||||
tommy_size_t element_size = array->element_size;
|
||||
tommy_size_t pos = 0;
|
||||
|
||||
while (pos < array->count) {
|
||||
tommy_size_t blk_idx = pos / TOMMY_ARRAYBLKOF_SIZE;
|
||||
tommy_size_t chunk = array->count - pos;
|
||||
unsigned char* ptr = array->block[blk_idx];
|
||||
|
||||
if (chunk > TOMMY_ARRAYBLKOF_SIZE)
|
||||
chunk = TOMMY_ARRAYBLKOF_SIZE;
|
||||
|
||||
for (tommy_size_t i = 0; i < chunk; ++i) {
|
||||
func(arg, ptr);
|
||||
ptr += element_size;
|
||||
}
|
||||
|
||||
pos += chunk;
|
||||
}
|
||||
}
|
||||
|
||||
TOMMY_API tommy_size_t tommy_arrayblkof_memory_usage(const tommy_arrayblkof* array)
|
||||
{
|
||||
return array->block_capacity * sizeof(array->block[0]) + array->block_count * TOMMY_ARRAYBLKOF_SIZE * array->element_size;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue