From f45db64106b25b5bde94dbad72134497aa5f395a Mon Sep 17 00:00:00 2001 From: Tushar Gohad Date: Fri, 18 Sep 2015 05:53:00 +0000 Subject: Implement alloc/free fns local to pyeclib c_ext ... stop using helpers internal to liberasurecode --- src/c/pyeclib_c/pyeclib_c.c | 46 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/c/pyeclib_c/pyeclib_c.c b/src/c/pyeclib_c/pyeclib_c.c index e2d98c3..5250709 100644 --- a/src/c/pyeclib_c/pyeclib_c.c +++ b/src/c/pyeclib_c/pyeclib_c.c @@ -30,7 +30,6 @@ #include "capsulethunk.h" #include -#include #include #include #include @@ -87,6 +86,51 @@ static PyObject *import_class(const char *module, const char *cls) return (PyObject *) PyObject_GetAttrString(s, cls); } +/** + * Allocate a buffer of a specific size and set its' contents + * to the specified value. + * + * @param size integer size in bytes of buffer to allocate + * @param value + * @return pointer to start of allocated buffer or NULL on error + */ +void * alloc_and_set_buffer(int size, int value) { + void * buf = NULL; /* buffer to allocate and return */ + + /* Allocate and zero the buffer, or set the appropriate error */ + buf = malloc((size_t) size); + if (buf) { + buf = memset(buf, value, (size_t) size); + } + return buf; +} + +/** + * Allocate a zero-ed buffer of a specific size. + * + * @param size integer size in bytes of buffer to allocate + * @return pointer to start of allocated buffer or NULL on error + */ +void * alloc_zeroed_buffer(int size) +{ + return alloc_and_set_buffer(size, 0); +} + +/** + * Deallocate memory buffer if it's not NULL. This methods returns NULL so + * that you can free and reset a buffer using a single line as follows: + * + * my_ptr = check_and_free_buffer(my_ptr); + * + * @return NULL + */ +void * check_and_free_buffer(void * buf) +{ + if (buf) + free(buf); + return NULL; +} + void pyeclib_c_seterr(int ret, const char * prefix) { -- cgit v1.2.1