Skip to content

Commit

Permalink
mem: use new re_atomic helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Jul 21, 2022
1 parent e7be13d commit b0447df
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/mem/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ void *mem_alloc(size_t size, mem_destroy_h *dh)
list_append(&meml, &m->le, m);
mem_unlock();
#endif

re_atomic_store(&m->nrefs, 1u, re_memory_order_relaxed);
re_atomic_weak_set(&m->nrefs, 1u);
m->dh = dh;

STAT_ALLOC(m, size);
Expand Down Expand Up @@ -230,7 +229,7 @@ void *mem_realloc(void *data, size_t size)

MAGIC_CHECK(m);

if (re_atomic_load(&m->nrefs, re_memory_order_acquire) > 1u) {
if (re_atomic_strong(&m->nrefs) > 1u) {
void* p = mem_alloc(size, m->dh);
if (p) {
memcpy(p, data, m->size);
Expand Down Expand Up @@ -342,7 +341,7 @@ void *mem_ref(void *data)

MAGIC_CHECK(m);

re_atomic_fetch_add(&m->nrefs, 1u, re_memory_order_relaxed);
re_atomic_weak_add(&m->nrefs, 1u);

return data;
}
Expand All @@ -369,16 +368,15 @@ void *mem_deref(void *data)

MAGIC_CHECK(m);

if (re_atomic_fetch_sub(
&m->nrefs, 1u, re_memory_order_acq_rel) > 1u) {
if (re_atomic_strong_sub(&m->nrefs, 1u) > 1u) {
return NULL;
}

if (m->dh)
m->dh(data);

/* NOTE: check if the destructor called mem_ref() */
if (re_atomic_load(&m->nrefs, re_memory_order_relaxed) > 0u)
if (re_atomic_weak(&m->nrefs) > 0u)
return NULL;

#if MEM_DEBUG
Expand Down Expand Up @@ -413,7 +411,7 @@ uint32_t mem_nrefs(const void *data)

MAGIC_CHECK(m);

return (uint32_t)re_atomic_load(&m->nrefs, re_memory_order_acquire);
return (uint32_t)re_atomic_strong(&m->nrefs);
}


Expand All @@ -427,7 +425,7 @@ static bool debug_handler(struct le *le, void *arg)
(void)arg;

(void)re_fprintf(stderr, " %p: nrefs=%-2u", p,
(uint32_t)re_atomic_load(&m->nrefs, re_memory_order_relaxed));
(uint32_t)re_atomic_weak(&m->nrefs));

(void)re_fprintf(stderr, " size=%-7u", m->size);

Expand Down

0 comments on commit b0447df

Please sign in to comment.