Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.10] value's mempool cleanup - [MOD-7132] #4653

Merged
merged 2 commits into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions src/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,29 @@ static size_t RSValue_NumToString(double dd, char *buf) {
}
}

typedef struct {
mempool_t *values;
mempool_t *fieldmaps;
} mempoolThreadPool;

static void mempoolThreadPoolDtor(void *p) {
mempoolThreadPool *tp = p;
if (tp->values) {
mempool_destroy(tp->values);
}
if (tp->fieldmaps) {
mempool_destroy(tp->fieldmaps);
}
rm_free(tp);
}

pthread_key_t mempoolKey_g;

static void *_valueAlloc() {
return rm_malloc(sizeof(RSValue));
}

static void _valueFree(void *p) {
rm_free(p);
}

static void __attribute__((constructor)) initKey() {
pthread_key_create(&mempoolKey_g, mempoolThreadPoolDtor);
pthread_key_create(&mempoolKey_g, (void (*)(void *))mempool_destroy);
}

static inline mempoolThreadPool *getPoolInfo() {
mempoolThreadPool *tp = pthread_getspecific(mempoolKey_g);
static inline mempool_t *getPool() {
mempool_t *tp = pthread_getspecific(mempoolKey_g);
if (tp == NULL) {
tp = rm_calloc(1, sizeof(*tp));
mempool_options opts = {
.initialCap = 0, .maxCap = 1000, .alloc = _valueAlloc, .free = _valueFree};
tp->values = mempool_new(&opts);
const mempool_options opts = {
.initialCap = 0, .maxCap = 1000, .alloc = _valueAlloc, .free = rm_free};
tp = mempool_new(&opts);
pthread_setspecific(mempoolKey_g, tp);
}
return tp;
}

RSValue *RS_NewValue(RSValueType t) {
RSValue *v = mempool_get(getPoolInfo()->values);
RSValue *v = mempool_get(getPool());
v->t = t;
v->refcount = 1;
v->allocated = 1;
Expand Down Expand Up @@ -133,7 +112,7 @@ void RSValue_Clear(RSValue *v) {
void RSValue_Free(RSValue *v) {
RSValue_Clear(v);
if (v->allocated) {
mempool_release(getPoolInfo()->values, v);
mempool_release(getPool(), v);
}
}

Expand Down
Loading