snapraid/tommyds/tommyarray.c

123 lines
3.5 KiB
C
Raw Permalink Normal View History

// SPDX-License-Identifier: BSD-2-Clause
// Copyright (C) 2011 Andrea Mazzoleni
2011-03-01 22:21:30 +01:00
#include "tommyarray.h"
2026-10-07 18:44:12 +02:00
#include <string.h> /* for memset */
2011-03-01 22:21:30 +01:00
/******************************************************************************/
/* array */
2025-12-03 19:10:13 +01:00
TOMMY_API void tommy_array_init(tommy_array* array)
2011-03-01 22:21:30 +01:00
{
/* fixed initial size */
array->bucket_bit = TOMMY_ARRAY_BIT;
2018-04-01 22:36:42 +02:00
array->bucket_max = (tommy_size_t)1 << array->bucket_bit;
2014-09-26 23:50:01 +02:00
array->bucket[0] = tommy_cast(void**, tommy_calloc(array->bucket_max, sizeof(void*)));
2026-10-07 18:44:12 +02:00
for (tommy_uint_t i = 1; i < TOMMY_ARRAY_BIT; ++i)
2014-09-26 23:50:01 +02:00
array->bucket[i] = array->bucket[0];
2013-06-22 17:27:03 +02:00
2014-10-25 17:28:41 +02:00
array->count = 0;
2011-03-01 22:21:30 +01:00
}
2025-12-03 19:10:13 +01:00
TOMMY_API void tommy_array_done(tommy_array* array)
2011-03-01 22:21:30 +01:00
{
2014-09-26 23:50:01 +02:00
tommy_free(array->bucket[0]);
2026-10-07 18:44:12 +02:00
for (tommy_uint_t i = TOMMY_ARRAY_BIT; i < array->bucket_bit; ++i) {
2014-09-26 23:50:01 +02:00
void** segment = array->bucket[i];
2018-04-01 22:36:42 +02:00
tommy_free(&segment[(tommy_ptrdiff_t)1 << i]);
2014-09-26 23:50:01 +02:00
}
2011-03-01 22:21:30 +01:00
}
2026-10-07 18:44:12 +02:00
TOMMY_API void tommy_array_reserve(tommy_array* array, tommy_size_t size)
2011-03-01 22:21:30 +01:00
{
2026-10-07 18:44:12 +02:00
while (size > array->bucket_max) {
2014-09-26 23:50:01 +02:00
/* allocate one more segment */
2026-10-07 18:44:12 +02:00
void** segment = tommy_cast(void**, tommy_calloc(array->bucket_max, sizeof(void*)));
2014-09-26 23:50:01 +02:00
/* store it adjusting the offset */
2014-09-30 23:26:32 +02:00
/* cast to ptrdiff_t to ensure to get a negative value */
2014-10-25 17:28:41 +02:00
array->bucket[array->bucket_bit] = &segment[-(tommy_ptrdiff_t)array->bucket_max];
2013-06-22 17:27:03 +02:00
2011-03-01 22:21:30 +01:00
++array->bucket_bit;
2018-04-01 22:36:42 +02:00
array->bucket_max = (tommy_size_t)1 << array->bucket_bit;
2011-03-01 22:21:30 +01:00
}
}
2026-10-07 18:44:12 +02:00
TOMMY_API void tommy_array_resize(tommy_array* array, tommy_size_t size)
{
if (size >= array->count) {
tommy_array_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_uint_t bsr = tommy_ilog2(pos | 1);
tommy_size_t seg_end = (bsr < TOMMY_ARRAY_BIT) ? ((tommy_size_t)1 << TOMMY_ARRAY_BIT) : ((tommy_size_t)1 << (bsr + 1));
tommy_size_t chunk_end = array->count < seg_end ? array->count : seg_end;
memset(&array->bucket[bsr][pos], 0, (chunk_end - pos) * sizeof(void*));
pos = chunk_end;
}
array->count = size;
}
TOMMY_API void tommy_array_shrink(tommy_array* array)
{
tommy_uint_t target_bucket_bit = TOMMY_ARRAY_BIT;
if (array->count > (tommy_size_t)1 << TOMMY_ARRAY_BIT)
target_bucket_bit = tommy_ilog2(array->count - 1) + 1;
while (array->bucket_bit > target_bucket_bit) {
--array->bucket_bit;
void** segment = array->bucket[array->bucket_bit];
tommy_free(&segment[(tommy_ptrdiff_t)1 << array->bucket_bit]);
}
array->bucket_max = (tommy_size_t)1 << array->bucket_bit;
}
TOMMY_API void tommy_array_foreach(tommy_array* array, tommy_foreach_func* func)
{
tommy_size_t pos = 0;
while (pos < array->count) {
tommy_uint_t bsr = tommy_ilog2(pos | 1);
tommy_size_t seg_end = (bsr < TOMMY_ARRAY_BIT) ? ((tommy_size_t)1 << TOMMY_ARRAY_BIT) : ((tommy_size_t)1 << (bsr + 1));
tommy_size_t chunk_end = array->count < seg_end ? array->count : seg_end;
void** ptr = array->bucket[bsr];
while (pos < chunk_end) {
func(ptr[pos]);
++pos;
}
}
}
TOMMY_API void tommy_array_foreach_arg(tommy_array* array, tommy_foreach_arg_func* func, void* arg)
{
tommy_size_t pos = 0;
while (pos < array->count) {
tommy_uint_t bsr = tommy_ilog2(pos | 1);
tommy_size_t seg_end = (bsr < TOMMY_ARRAY_BIT) ? ((tommy_size_t)1 << TOMMY_ARRAY_BIT) : ((tommy_size_t)1 << (bsr + 1));
tommy_size_t chunk_end = array->count < seg_end ? array->count : seg_end;
void** ptr = array->bucket[bsr];
while (pos < chunk_end) {
func(arg, ptr[pos]);
++pos;
}
}
}
TOMMY_API tommy_size_t tommy_array_memory_usage(const tommy_array* array)
2011-03-01 22:21:30 +01:00
{
2013-06-23 21:49:36 +02:00
return array->bucket_max * (tommy_size_t)sizeof(void*);
2011-03-01 22:21:30 +01:00
}