diff --git a/examples/guards/cleanup.c b/examples/guards/cleanup.c index 14cfaee8..fbfd2444 100644 --- a/examples/guards/cleanup.c +++ b/examples/guards/cleanup.c @@ -5,6 +5,7 @@ #include #include +#include #include BFDEV_DEFINE_CLEAN(malloc, void *, @@ -15,7 +16,7 @@ BFDEV_DEFINE_CLEAN(malloc, void *, ) static void -test_clean_gc(void) +test_malloc_gc(void) { BFDEV_CLEAN(malloc) void *block; block = malloc(8); @@ -23,11 +24,11 @@ test_clean_gc(void) } static void * -test_clean_keep(void) +test_malloc_keep(void) { BFDEV_CLEAN(malloc) void *block; block = malloc(8); - return bfdev_taken(block); + return bfdev_taken_ptr(block); } int @@ -35,8 +36,8 @@ main(int argc, const char *argv[]) { void *block; - test_clean_gc(); - block = test_clean_keep(); + test_malloc_gc(); + block = test_malloc_keep(); free(block); return 0; diff --git a/include/bfdev/guards.h b/include/bfdev/guards.h index 2d7b402f..9b949d4f 100644 --- a/include/bfdev/guards.h +++ b/include/bfdev/guards.h @@ -54,12 +54,15 @@ __bfdev_class_##name##_destructor(type *p) \ __bfdev_cleanup(__bfdev_class_##name##_destructor) = \ __bfdev_class_##name##_constructor -#define bfdev_taken(object) ({ \ - __auto_type __ptr = (object); \ - (object) = BFDEV_NULL; \ - __ptr; \ +#define bfdev_taken(freed, object) ({ \ + __auto_type __object = (object); \ + (object) = (freed); \ + __object; \ }) +#define bfdev_taken_ptr(object) \ + bfdev_taken(BFDEV_NULL, object) + BFDEV_END_DECLS #endif /* _BFDEV_GUARDS_H_ */