array_allocator.[hc] add parameter: need_lock

pull/37/merge
YuQing 2021-12-14 21:15:30 +08:00
parent 64ae0757d7
commit 0c437d3799
2 changed files with 27 additions and 9 deletions

View File

@ -16,9 +16,10 @@
#include "shared_func.h" #include "shared_func.h"
#include "array_allocator.h" #include "array_allocator.h"
int array_allocator_init(ArrayAllocatorContext *ctx, int array_allocator_init_ex(ArrayAllocatorContext *ctx,
const char *name_prefix, const int element_size, const char *name_prefix, const int element_size,
const int min_bits, const int max_bits) const int min_bits, const int max_bits,
const bool need_lock)
{ {
const int reclaim_interval = 0; const int reclaim_interval = 0;
char name[32]; char name[32];
@ -48,7 +49,7 @@ int array_allocator_init(ArrayAllocatorContext *ctx,
snprintf(name, sizeof(name), "%s-array", name_prefix); snprintf(name, sizeof(name), "%s-array", name_prefix);
return fast_allocator_init_ex(&ctx->allocator, return fast_allocator_init_ex(&ctx->allocator,
name, regions, region - regions, 0, name, regions, region - regions, 0,
0.9999, reclaim_interval, true); 0.9999, reclaim_interval, need_lock);
} }
VoidArray *array_allocator_alloc(ArrayAllocatorContext *ctx, VoidArray *array_allocator_alloc(ArrayAllocatorContext *ctx,

View File

@ -57,9 +57,10 @@ typedef struct array_allocator_context
extern "C" { extern "C" {
#endif #endif
int array_allocator_init(ArrayAllocatorContext *ctx, int array_allocator_init_ex(ArrayAllocatorContext *ctx,
const char *name_prefix, const int element_size, const char *name_prefix, const int element_size,
const int min_bits, const int max_bits); const int min_bits, const int max_bits,
const bool need_lock);
VoidArray *array_allocator_alloc(ArrayAllocatorContext *ctx, VoidArray *array_allocator_alloc(ArrayAllocatorContext *ctx,
const int target_count); const int target_count);
@ -84,8 +85,17 @@ extern "C" {
int array_compare_element_id_name(const id_name_pair_t *pair1, int array_compare_element_id_name(const id_name_pair_t *pair1,
const id_name_pair_t *pair2); const id_name_pair_t *pair2);
#define array_allocator_init(ctx, name_prefix, \
element_size, min_bits, max_bits) \
array_allocator_init_ex(ctx, name_prefix, \
element_size, min_bits, max_bits, true)
#define i64_array_allocator_init_ex(ctx, min_bits, max_bits, need_lock) \
array_allocator_init_ex(ctx, "i64", sizeof(int64_t), \
min_bits, max_bits, need_lock)
#define i64_array_allocator_init(ctx, min_bits, max_bits) \ #define i64_array_allocator_init(ctx, min_bits, max_bits) \
array_allocator_init(ctx, "i64", sizeof(int64_t), min_bits, max_bits) i64_array_allocator_init_ex(ctx, min_bits, max_bits, true)
#define i64_array_allocator_alloc(ctx, target_count) \ #define i64_array_allocator_alloc(ctx, target_count) \
(I64Array *)array_allocator_alloc(ctx, target_count) (I64Array *)array_allocator_alloc(ctx, target_count)
@ -98,8 +108,12 @@ extern "C" {
array_allocator_free(ctx, (VoidArray *)array) array_allocator_free(ctx, (VoidArray *)array)
#define i32_array_allocator_init_ex(ctx, min_bits, max_bits, need_lock) \
array_allocator_init_ex(ctx, "i32", sizeof(int32_t), \
min_bits, max_bits, need_lock)
#define i32_array_allocator_init(ctx, min_bits, max_bits) \ #define i32_array_allocator_init(ctx, min_bits, max_bits) \
array_allocator_init(ctx, "i32", sizeof(int32_t), min_bits, max_bits) i32_array_allocator_init_ex(ctx, min_bits, max_bits, true)
#define i32_array_allocator_alloc(ctx, target_count) \ #define i32_array_allocator_alloc(ctx, target_count) \
(I32Array *)array_allocator_alloc(ctx, target_count) (I32Array *)array_allocator_alloc(ctx, target_count)
@ -112,9 +126,12 @@ extern "C" {
array_allocator_free(ctx, (VoidArray *)array) array_allocator_free(ctx, (VoidArray *)array)
#define id_name_array_allocator_init_ex(ctx, min_bits, max_bits, need_lock) \
array_allocator_init_ex(ctx, "id_name", sizeof(id_name_pair_t), \
min_bits, max_bits, need_lock)
#define id_name_array_allocator_init(ctx, min_bits, max_bits) \ #define id_name_array_allocator_init(ctx, min_bits, max_bits) \
array_allocator_init(ctx, "id_name", sizeof(id_name_pair_t), \ id_name_array_allocator_init_ex(ctx, min_bits, max_bits, true)
min_bits, max_bits)
#define id_name_array_allocator_alloc(ctx, target_count) \ #define id_name_array_allocator_alloc(ctx, target_count) \
(IdNameArray *)array_allocator_alloc(ctx, target_count) (IdNameArray *)array_allocator_alloc(ctx, target_count)