summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-11-18 21:40:58 -0800
committerVito Caputo <vcaputo@pengaru.com>2020-11-18 21:40:58 -0800
commit58563f46c53d4677f8db57bb0689614834fdc43b (patch)
treea960c38a48d9814be60c76bc4bfabeb89fe3435d
parent7c196762e4cabf5582116091bcf0818f4c2d612d (diff)
thunk: clear thunk instance payloads
Since thunk instances are utilized via initializer functions which always initialize all the environment members, it made sense to use plain malloc(). But with the addition of payloads, there's a component of the allocation which gets split out for the caller to make use of in a much more ad-hoc fashion. In my personal programming style, I tend to take advantage of the assumption that allocations are zeroed. The returned payload is essentially used as a heap allocation, just coupled to the thunk instance in terms of life cycle. So I prefer the payload space be zeroed on allocation. Since it still seems sensible to leave the thunk's environment part of the instance exclusively initialized by it's init function, this commit leaves the plain malloc and just memset's the payload when present.
-rw-r--r--thunk.h9
1 files changed, 7 insertions, 2 deletions
diff --git a/thunk.h b/thunk.h
index 7fbdfbd..262137f 100644
--- a/thunk.h
+++ b/thunk.h
@@ -21,6 +21,7 @@
#include <assert.h>
#include <stdlib.h>
+#include <string.h>
typedef struct thunk_t thunk_t;
@@ -488,8 +489,10 @@ struct thunk_t {
env = malloc(sizeof(*env) + payload_size); \
assert(env); \
\
- if (payload_ptr) \
+ if (payload_ptr) { \
+ memset(env->__payload, 0, payload_size); \
*payload_ptr = env->__payload; \
+ } \
\
env->__thunk.dispatch = __thunk_dispatch_##_name; \
\
@@ -566,8 +569,10 @@ struct thunk_t {
env = malloc(sizeof(*env) + payload_size); \
assert(env); \
\
- if (payload_ptr) \
+ if (payload_ptr) { \
+ memset(env->__payload, 0, payload_size); \
*payload_ptr = env->__payload; \
+ } \
\
env->__thunk.dispatch = __thunk_dispatch_##_name; \
\
© 2021 All Rights Reserved