summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTushar Gohad <tushar.gohad@intel.com>2015-09-18 05:53:00 +0000
committerTushar Gohad <tushar.gohad@intel.com>2015-09-18 05:53:00 +0000
commitf45db64106b25b5bde94dbad72134497aa5f395a (patch)
tree958065fe53e14668cb5b989a838da856d7f0fd00
parent6d67203bde8681c598e8d5aba60724e5cbcdea38 (diff)
downloadpyeclib-f45db64106b25b5bde94dbad72134497aa5f395a.tar.gz
Implement alloc/free fns local to pyeclib c_ext
... stop using helpers internal to liberasurecode
-rw-r--r--src/c/pyeclib_c/pyeclib_c.c46
1 files changed, 45 insertions, 1 deletions
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 <erasurecode.h>
-#include <erasurecode_helpers.h>
#include <math.h>
#include <pyeclib_c.h>
#include <bytesobject.h>
@@ -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)
{