[PATCH 4/5] rtth: maintain nr_allocated atomically

From: Hugh Dickins
Date: Sat Dec 17 2011 - 02:57:58 EST


Someone has been worrying about radix_tree_node leaks, and inserted a
lot of nr_allocated printfs; but it often came out negative because it
was not manipulated atomically. We're already using userspace-rcu, so
use its uatomic implementation on nr_allocated (but the count still
doesn't go down to zero at the end).

Signed-off-by: Hugh Dickins <hughd@xxxxxxxxxx>
---

linux.c | 5 +++--
main.c | 4 +++-
2 files changed, 6 insertions(+), 3 deletions(-)

--- rtth3/linux.c 2010-08-25 13:30:45.000000000 -0700
+++ rtth4/linux.c 2011-12-16 18:44:05.587897075 -0800
@@ -6,6 +6,7 @@

#include <linux/mempool.h>
#include <linux/slab.h>
+#include <urcu/uatomic_arch.h>

int nr_allocated;

@@ -35,14 +36,14 @@ void *kmem_cache_alloc(struct kmem_cache
void *ret = malloc(cachep->size);
if (cachep->ctor)
cachep->ctor(ret);
- nr_allocated++;
+ uatomic_inc(&nr_allocated);
return ret;
}

void kmem_cache_free(struct kmem_cache *cachep, void *objp)
{
assert(objp);
- nr_allocated--;
+ uatomic_dec(&nr_allocated);
memset(objp, 0, cachep->size);
free(objp);
}
--- rtth3/main.c 2011-01-24 22:12:10.000000000 -0800
+++ rtth4/main.c 2011-12-16 18:44:05.587897075 -0800
@@ -261,8 +261,10 @@ int main()

regression1_test();
regression2_test();
-
single_thread_tests();

+ sleep(1);
+ printf("after sleep(1): %d allocated\n", nr_allocated);
+
exit(0);
}
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/