]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: memtypes: restore atomicity
authorDavid Lamparter <equinox@opensourcerouting.org>
Wed, 26 Oct 2016 15:19:56 +0000 (17:19 +0200)
committerDavid Lamparter <equinox@opensourcerouting.org>
Fri, 31 Mar 2017 15:59:48 +0000 (17:59 +0200)
the original version of this code already used _Atomic and atomic_*().
Restore this functionality for future multithreading.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/memory.c
lib/memory.h

index ad55366f64a15a040547b41666753f49222bb04f..28e358dfc13e334cc8da40e6812eecbacd92ebc9 100644 (file)
@@ -30,19 +30,22 @@ DEFINE_MTYPE(LIB, TMP, "Temporary memory")
 static inline void
 mt_count_alloc (struct memtype *mt, size_t size)
 {
-  mt->n_alloc++;
+  size_t oldsize;
 
-  if (mt->size == 0)
-    mt->size = size;
-  else if (mt->size != size)
-    mt->size = SIZE_VAR;
+  atomic_fetch_add_explicit(&mt->n_alloc, 1, memory_order_relaxed);
+
+  oldsize = atomic_load_explicit(&mt->size, memory_order_relaxed);
+  if (oldsize == 0)
+    oldsize = atomic_exchange_explicit(&mt->size, size, memory_order_relaxed);
+  if (oldsize != 0 && oldsize != size && oldsize != SIZE_VAR)
+    atomic_store_explicit(&mt->size, SIZE_VAR, memory_order_relaxed);
 }
 
 static inline void
 mt_count_free (struct memtype *mt)
 {
   assert(mt->n_alloc);
-  mt->n_alloc--;
+  atomic_fetch_sub_explicit(&mt->n_alloc, 1, memory_order_relaxed);
 }
 
 static inline void *
index 477a6162dc45960aa0be35c66269a38227af33d6..284a1b13c514fcd7715c170ee23dd5a90f112302 100644 (file)
@@ -18,6 +18,7 @@
 #define _QUAGGA_MEMORY_H
 
 #include <stdlib.h>
+#include <frratomic.h>
 
 #define array_size(ar) (sizeof(ar) / sizeof(ar[0]))
 
@@ -26,8 +27,8 @@ struct memtype
 {
   struct memtype *next, **ref;
   const char *name;
-  size_t n_alloc;
-  size_t size;
+  _Atomic size_t n_alloc;
+  _Atomic size_t size;
 };
 
 struct memgroup