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 2b0cf26 commit 5844b8f
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 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 @@ -378,7 +377,7 @@ void *mem_deref(void *data)
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 +412,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 +426,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 5844b8f

Please sign in to comment.