diff options
author | Kinglong Mee <mijinlong@open-fs.com> | 2017-08-30 17:54:09 +0800 |
---|---|---|
committer | Jeff Darcy <jeff@pl.atyp.us> | 2017-09-01 15:32:55 +0000 |
commit | d7ccdb33c2e84bab25bf0898866104f8a85b4217 (patch) | |
tree | 451568625f26dc732e52c318f414e777c8b2e19a /api/src/glfs-internal.h | |
parent | 75223c0a1b3c7bd65bb0c59449ed1fb0663cfdd3 (diff) |
gfapi: adds a glfs_mem_header for exported memory
glfs_free releases different types of data depends on memory type.
Drop the depends of memory type of memory accounting,
new macro GLFS_CALLOC/GLFS_MALLOC/GLFS_REALLOC/GLFS_FREE are added
to support assign release function dynamically, it adds a separate
memory header named glfs_mem_header for gfapi.
Updates: #312
Change-Id: Ie608e5227cbaa05d3f4681a515e83a50d5b17c3f
Signed-off-by: Kinglong Mee <mijinlong@open-fs.com>
Reviewed-on: https://review.gluster.org/18092
Smoke: Gluster Build System <jenkins@build.gluster.org>
Reviewed-by: Niels de Vos <ndevos@redhat.com>
Tested-by: Niels de Vos <ndevos@redhat.com>
Reviewed-by: Jeff Darcy <jeff@pl.atyp.us>
CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
Diffstat (limited to 'api/src/glfs-internal.h')
-rw-r--r-- | api/src/glfs-internal.h | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/api/src/glfs-internal.h b/api/src/glfs-internal.h index 180981830d7..be2d60a38e0 100644 --- a/api/src/glfs-internal.h +++ b/api/src/glfs-internal.h @@ -258,6 +258,114 @@ struct glfs_xreaddirp_stat { #define GF_MEMPOOL_COUNT_OF_LRU_BUF_T 256 +typedef void (glfs_mem_release_t) (void *ptr); + +struct glfs_mem_header { + uint32_t magic; + size_t nmemb; + size_t size; + glfs_mem_release_t *release; +}; + +#define GLFS_MEM_HEADER_SIZE (sizeof (struct glfs_mem_header)) +#define GLFS_MEM_HEADER_MAGIC 0x20170830 + +static inline void * +__glfs_calloc (size_t nmemb, size_t size, glfs_mem_release_t release, + uint32_t type, const char *typestr) +{ + struct glfs_mem_header *header = NULL; + + header = __gf_calloc(nmemb, (size + GLFS_MEM_HEADER_SIZE), + type, typestr); + if (!header) + return NULL; + + header->magic = GLFS_MEM_HEADER_MAGIC; + header->nmemb = nmemb; + header->size = size; + header->release = release; + + return header + 1; +} + +static inline void * +__glfs_malloc (size_t size, glfs_mem_release_t release, + uint32_t type, const char *typestr) +{ + struct glfs_mem_header *header = NULL; + + header = __gf_malloc((size + GLFS_MEM_HEADER_SIZE), type, typestr); + if (!header) + return NULL; + + header->magic = GLFS_MEM_HEADER_MAGIC; + header->nmemb = 1; + header->size = size; + header->release = release; + + return header + 1; +} + +static inline void * +__glfs_realloc (void *ptr, size_t size) +{ + struct glfs_mem_header *old_header = NULL; + struct glfs_mem_header *new_header = NULL; + struct glfs_mem_header tmp_header; + void *new_ptr = NULL; + + GF_ASSERT (NULL != ptr); + + old_header = (struct glfs_mem_header *) (ptr - GLFS_MEM_HEADER_SIZE); + GF_ASSERT (old_header->magic == GLFS_MEM_HEADER_MAGIC); + tmp_header = *old_header; + + new_ptr = __gf_realloc (old_header, (size + GLFS_MEM_HEADER_SIZE)); + if (!new_ptr) + return NULL; + + new_header = (struct glfs_mem_header *) new_ptr; + *new_header = tmp_header; + new_header->size = size; + + return new_header + 1; +} + +static inline void +__glfs_free (void *free_ptr) +{ + struct glfs_mem_header *header = NULL; + void *release_ptr = NULL; + int i = 0; + + if (!free_ptr) + return; + + header = (struct glfs_mem_header *) (free_ptr - GLFS_MEM_HEADER_SIZE); + GF_ASSERT (header->magic == GLFS_MEM_HEADER_MAGIC); + + if (header->release) { + release_ptr = free_ptr; + for (i = 0; i < header->nmemb; i++) { + header->release (release_ptr); + release_ptr += header->size; + } + } + + __gf_free (header); +} + +#define GLFS_CALLOC(nmemb, size, release, type) \ + __glfs_calloc (nmemb, size, release, type, #type) + +#define GLFS_MALLOC(size, release, type) \ + __glfs_malloc (size, release, type, #type) + +#define GLFS_REALLOC(ptr, size) __glfs_realloc(ptr, size) + +#define GLFS_FREE(free_ptr) __glfs_free(free_ptr) + int glfs_mgmt_init (struct glfs *fs); void glfs_init_done (struct glfs *fs, int ret) GFAPI_PRIVATE(glfs_init_done, 3.4.0); |