--- libaitio/src/Attic/mem.c 2012/05/23 11:49:35 1.1.4.3 +++ libaitio/src/Attic/mem.c 2012/07/03 08:51:05 1.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: mem.c,v 1.1.4.3 2012/05/23 11:49:35 misho Exp $ +* $Id: mem.c,v 1.2 2012/07/03 08:51:05 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -402,11 +402,13 @@ mpool_free(mpool_t * __restrict mp, void * __restrict int idx; struct tagAlloc *m, *tmp; + assert(data); if (!mp) { io_SetErr(EINVAL, "Pool not specified"); return -1; } /* check address range & sentinel */ + assert(!MEM_BADADDR(data) && !MEM_CORRUPT(data)); if (MEM_BADADDR(data) || MEM_CORRUPT(data)) { io_SetErr(EFAULT, "Corrupted memory address"); return -2; @@ -497,6 +499,39 @@ mpool_free2(mpool_t * __restrict mp, u_int size, const } /* + * mpool_strdup() - String duplicate + * + * @mp = Memory pool + * @str = String + * @memname = Memory name + * return: NULL error or !=NULL new string + */ +char * +mpool_strdup(mpool_t * __restrict mp, const char *str, const char *memname) +{ + char *s = NULL; + u_int len; + + if (!mp) { + io_SetErr(EINVAL, "Pool not specified"); + return NULL; + } + if (!str) { + io_SetErr(EINVAL, "String is NULL"); + return NULL; + } else + len = strlen(str) + 1; + + s = mpool_malloc(mp, len, memname); + if (!s) + return NULL; + else + memcpy(s, str, len); + + return s; +} + +/* * mpool_getmembynam() Find allocated memory block by size and memory name * * @mp = Memory pool @@ -628,4 +663,68 @@ mpool_statistics(mpool_t * __restrict mp, mpool_stat_c cb(1 << (i + MEM_MIN_BUCKET), act, inact); } +} + +/* ----------------------------------------------------------- */ + +/* + * mpool_xmalloc() - malloc wrapper + * + * @size = Size + * return: NULL error or !=NULL ok allocated memory + */ +void * +mpool_xmalloc(size_t size) +{ + return mpool_malloc(io_mpool, size, NULL); +} + +/* + * mpool_xcalloc() - calloc wrapper + * + * @num = number of elements + * @size = Size of element + * return: NULL error or !=NULL ok allocated memory + */ +void * +mpool_xcalloc(size_t num, size_t size) +{ + return mpool_malloc(io_mpool, num * size, NULL); +} + +/* + * mpool_xrealloc() - realloc wrapper + * + * @data = Allocated memory data + * @newsize = New size of memory block + * return: NULL error or !=NULL new reallocated memory block + */ +void * +mpool_xrealloc(void * __restrict data, size_t newsize) +{ + return mpool_realloc(io_mpool, data, newsize, NULL); +} + +/* + * mpool_xfree() - free wrapper + * + * @data = Allocated memory data + * return: none + */ +void +mpool_xfree(void * __restrict data) +{ + mpool_free(io_mpool, data, 0); +} + +/* + * mpool_xstrdup() - strdup wrapper + * + * @str = string + * return: =NULL error or !=NULL new allocated string + */ +char * +mpool_xstrdup(const char *str) +{ + return mpool_strdup(io_mpool, str, NULL); }