From b4343b17479151d438d32530cdd2541262e3088e Mon Sep 17 00:00:00 2001 From: "Andrew J. Schorr" Date: Sat, 8 Mar 2014 14:41:00 -0500 Subject: Add memory allocation functions to gawk API. --- gawkapi.h | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'gawkapi.h') diff --git a/gawkapi.h b/gawkapi.h index b26ee24c..e84f2dea 100644 --- a/gawkapi.h +++ b/gawkapi.h @@ -30,7 +30,6 @@ * * FILE - * NULL - - * malloc() - * memset(), memcpy() - * size_t - * struct stat - @@ -62,7 +61,7 @@ * * Additional important information: * - * 1. ALL string values in awk_value_t objects need to come from malloc(). + * 1. ALL string values in awk_value_t objects need to come from api_malloc(). * Gawk will handle releasing the storage if necessary. This is slightly * awkward, in that you can't take an awk_value_t that you got from gawk * and reuse it directly, even for something that is conceptually pass @@ -264,7 +263,7 @@ typedef struct awk_two_way_processor { /* Current version of the API. */ enum { GAWK_API_MAJOR_VERSION = 1, - GAWK_API_MINOR_VERSION = 0 + GAWK_API_MINOR_VERSION = 1 }; /* A number of typedefs related to different types of values. */ @@ -665,6 +664,16 @@ typedef struct gawk_api { awk_bool_t (*api_release_flattened_array)(awk_ext_id_t id, awk_array_t a_cookie, awk_flat_array_t *data); + + /* + * Hooks to provide access to gawk's memory allocation functions. + * This ensures that memory passed between gawk and the extension + * is allocated and released by the same library. + */ + void *(*api_malloc)(size_t size); + void *(*api_calloc)(size_t nmemb, size_t size); + void *(*api_realloc)(void *ptr, size_t size); + void (*api_free)(void *ptr); } gawk_api_t; #ifndef GAWK /* these are not for the gawk code itself! */ @@ -736,6 +745,11 @@ typedef struct gawk_api { #define release_flattened_array(array, data) \ (api->api_release_flattened_array(ext_id, array, data)) +#define gawk_malloc(size) (api->api_malloc(size)) +#define gawk_calloc(nmemb, size) (api->api_calloc(nmemb, size)) +#define gawk_realloc(ptr, size) (api->api_realloc(ptr, size)) +#define gawk_free(ptr) (api->api_free(ptr)) + #define create_value(value, result) \ (api->api_create_value(ext_id, value,result)) @@ -747,13 +761,13 @@ typedef struct gawk_api { #define emalloc(pointer, type, size, message) \ do { \ - if ((pointer = (type) malloc(size)) == 0) \ + if ((pointer = (type) gawk_malloc(size)) == 0) \ fatal(ext_id, "%s: malloc of %d bytes failed\n", message, size); \ } while(0) #define erealloc(pointer, type, size, message) \ do { \ - if ((pointer = (type) realloc(pointer, size)) == 0) \ + if ((pointer = (type) gawk_realloc(pointer, size)) == 0) \ fatal(ext_id, "%s: realloc of %d bytes failed\n", message, size); \ } while(0) -- cgit v1.2.3