summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-05-12 21:38:38 -0700
committerVito Caputo <vcaputo@pengaru.com>2020-05-12 21:38:38 -0700
commit3eb8ca2284ad42a80c3fd6edae33c1dbe20b94e5 (patch)
tree64ff7d305fa18948cec923df2fd5f10009ff5e3c
parent5cc37b5bed7c8a6539871483b49aa35a18f3bef6 (diff)
libpad: pad_free() accept and return NULLHEADmaster
Minor ergonimics change, originally I liked the idea of policing programming errors like attempting to free a NULL pad. But in practice it's just more convenient to be free-like ignoring NULL inputs, and while at it return a NULL pointer in case a reused pointer needs to be NULL'd on free.
-rw-r--r--src/pad.c16
-rw-r--r--src/pad.h2
2 files changed, 10 insertions, 8 deletions
diff --git a/src/pad.c b/src/pad.c
index e7a1ad4..48186d3 100644
--- a/src/pad.c
+++ b/src/pad.c
@@ -196,18 +196,20 @@ void pad_reset(pad_t *pad)
/* Free a pad and it's associated allocations. */
-void pad_free(pad_t *pad)
+pad_t * pad_free(pad_t *pad)
{
- chunk_t *chunk, *_chunk;
+ if (pad) {
+ chunk_t *chunk, *_chunk;
- assert(pad);
+ pad_reset(pad);
- pad_reset(pad);
+ list_for_each_entry_safe(chunk, _chunk, &pad->free_chunks, chunks)
+ free(chunk);
- list_for_each_entry_safe(chunk, _chunk, &pad->free_chunks, chunks)
- free(chunk);
+ free(pad);
+ }
- free(pad);
+ return NULL;
}
diff --git a/src/pad.h b/src/pad.h
index e9927e8..7a7a4dc 100644
--- a/src/pad.h
+++ b/src/pad.h
@@ -23,7 +23,7 @@ typedef struct pad_t pad_t;
pad_t * pad_new(unsigned chunk_size, unsigned flags);
void pad_reset(pad_t *pad);
-void pad_free(pad_t *pad);
+pad_t * pad_free(pad_t *pad);
void * pad_get(pad_t *pad, unsigned size);
void pad_put(void *mem);
© All Rights Reserved