snapraid/tommyds/tommyarrayblkof.c

60 lines
1.5 KiB
C
Raw Normal View History

// SPDX-License-Identifier: BSD-2-Clause
// Copyright (C) 2013 Andrea Mazzoleni
#include "tommyarrayblkof.h"
/******************************************************************************/
/* array */
2025-12-03 19:10:13 +01:00
TOMMY_API void tommy_arrayblkof_init(tommy_arrayblkof* array, tommy_size_t element_size)
{
tommy_array_init(&array->block);
2014-10-25 17:28:41 +02:00
array->element_size = element_size;
2014-10-25 17:28:41 +02:00
array->count = 0;
}
2025-12-03 19:10:13 +01:00
TOMMY_API void tommy_arrayblkof_done(tommy_arrayblkof* array)
{
2018-04-01 22:36:42 +02:00
tommy_size_t i;
2014-09-26 23:50:01 +02:00
for (i = 0; i < tommy_array_size(&array->block); ++i)
tommy_free(tommy_array_get(&array->block, i));
tommy_array_done(&array->block);
}
2025-12-03 19:10:13 +01:00
TOMMY_API void tommy_arrayblkof_grow(tommy_arrayblkof* array, tommy_size_t count)
{
2018-04-01 22:36:42 +02:00
tommy_size_t block_max;
tommy_size_t block_mac;
2014-09-26 23:50:01 +02:00
2014-10-25 17:28:41 +02:00
if (array->count >= count)
2014-09-26 23:50:01 +02:00
return;
2014-10-25 17:28:41 +02:00
array->count = count;
2014-09-26 23:50:01 +02:00
2015-07-13 21:46:36 +02:00
block_max = (count + TOMMY_ARRAYBLKOF_SIZE - 1) / TOMMY_ARRAYBLKOF_SIZE;
2014-09-26 23:50:01 +02:00
block_mac = tommy_array_size(&array->block);
if (block_mac < block_max) {
/* grow the block array */
tommy_array_grow(&array->block, block_max);
/* allocate new blocks */
while (block_mac < block_max) {
2015-07-13 21:46:36 +02:00
void** ptr = tommy_cast(void**, tommy_calloc(TOMMY_ARRAYBLKOF_SIZE, array->element_size));
/* set the new block */
tommy_array_set(&array->block, block_mac, ptr);
++block_mac;
}
}
}
2025-12-03 19:10:13 +01:00
TOMMY_API tommy_size_t tommy_arrayblkof_memory_usage(tommy_arrayblkof* array)
{
return tommy_array_memory_usage(&array->block) + tommy_array_size(&array->block) * TOMMY_ARRAYBLKOF_SIZE * array->element_size;
}