diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-02-22 23:10:25 +0100 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-02-23 11:43:43 +0900 |
commit | ff3f1464ec2dd40c9d8eb92e1474cb4d1c8c676b (patch) | |
tree | a0d437665b0e0b35eef3f137b6b9ceb7d5441e13 | |
parent | c7d941c52771e09f14cdfff969e7e9a00e2c8a2f (diff) | |
download | systemd-ff3f1464ec2dd40c9d8eb92e1474cb4d1c8c676b.tar.gz |
memory-util: add a concept for gcc cleanup attribute based array destruction
-rw-r--r-- | src/basic/alloc-util.h | 1 | ||||
-rw-r--r-- | src/basic/memory-util.h | 34 | ||||
-rw-r--r-- | src/test/test-memory-util.c | 37 |
3 files changed, 72 insertions, 0 deletions
diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index bf783b15a2..9a62381df1 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -15,6 +15,7 @@ typedef void (*free_func_t)(void *p); typedef void* (*mfree_func_t)(void *p); +typedef void (*free_array_func_t)(void *p, size_t n); /* If for some reason more than 4M are allocated on the stack, let's abort immediately. It's better than * proceeding and smashing the stack limits. Note that by default RLIMIT_STACK is 8M on Linux. */ diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 428ccc210c..d03d52cd43 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -111,3 +111,37 @@ static inline void erase_and_freep(void *p) { static inline void erase_char(char *p) { explicit_bzero_safe(p, sizeof(char)); } + +/* An automatic _cleanup_-like logic for destroy arrays (i.e. pointers + size) when leaving scope */ +struct ArrayCleanup { + void **parray; + size_t *pn; + free_array_func_t pfunc; +}; + +static inline void array_cleanup(struct ArrayCleanup *c) { + assert(c); + + assert(!c->parray == !c->pn); + + if (!c->parray) + return; + + if (*c->parray) { + assert(c->pfunc); + c->pfunc(*c->parray, *c->pn); + *c->parray = NULL; + } + + *c->pn = 0; +} + +#define CLEANUP_ARRAY(array, n, func) \ + _cleanup_(array_cleanup) _unused_ struct ArrayCleanup CONCATENATE(_cleanup_array_, UNIQ) = { \ + .parray = (void**) &(array), \ + .pn = &(n), \ + .pfunc = (free_array_func_t) ({ \ + void (*_f)(typeof(array[0]) *a, size_t b) = func; \ + _f; \ + }), \ + } diff --git a/src/test/test-memory-util.c b/src/test/test-memory-util.c index 241f46c0d0..2f8384ac09 100644 --- a/src/test/test-memory-util.c +++ b/src/test/test-memory-util.c @@ -15,4 +15,41 @@ TEST(eqzero) { assert_se(!eqzero(longer)); } +static void my_destructor(struct iovec *iov, size_t n) { + /* not really a destructor, just something we can use to check if the destruction worked */ + memset(iov, 'y', sizeof(struct iovec) * n); +} + +TEST(cleanup_array) { + struct iovec *iov, *saved_iov; + size_t n, saved_n; + + n = 7; + iov = new(struct iovec, n); + assert_se(iov); + + memset(iov, 'x', sizeof(struct iovec) * n); + + saved_iov = iov; + saved_n = n; + + { + assert_se(memeqbyte('x', saved_iov, sizeof(struct iovec) * saved_n)); + assert_se(iov); + assert_se(n > 0); + + CLEANUP_ARRAY(iov, n, my_destructor); + + assert_se(memeqbyte('x', saved_iov, sizeof(struct iovec) * saved_n)); + assert_se(iov); + assert_se(n > 0); + } + + assert_se(memeqbyte('y', saved_iov, sizeof(struct iovec) * saved_n)); + assert_se(!iov); + assert_se(n == 0); + + free(saved_iov); +} + DEFINE_TEST_MAIN(LOG_INFO); |