diff options
author | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2014-03-08 14:41:00 -0500 |
---|---|---|
committer | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2014-03-08 14:41:00 -0500 |
commit | b4343b17479151d438d32530cdd2541262e3088e (patch) | |
tree | 59c81427d817fa5c704336313999e4428ec616c1 /gawkapi.h | |
parent | 4c0b1ddb06fd9329fd34db65a93e067d6426a7d1 (diff) | |
download | egawk-b4343b17479151d438d32530cdd2541262e3088e.tar.gz egawk-b4343b17479151d438d32530cdd2541262e3088e.tar.bz2 egawk-b4343b17479151d438d32530cdd2541262e3088e.zip |
Add memory allocation functions to gawk API.
Diffstat (limited to 'gawkapi.h')
-rw-r--r-- | gawkapi.h | 24 |
1 files changed, 19 insertions, 5 deletions
@@ -30,7 +30,6 @@ * * FILE - <stdio.h> * NULL - <stddef.h> - * malloc() - <stdlib.h> * memset(), memcpy() - <string.h> * size_t - <sys/types.h> * struct stat - <sys/stat.h> @@ -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) |