--- libaitio/src/Attic/mem.c 2012/05/23 10:56:03 1.1.4.2 +++ libaitio/src/Attic/mem.c 2012/07/25 15:21:59 1.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: mem.c,v 1.1.4.2 2012/05/23 10:56:03 misho Exp $ +* $Id: mem.c,v 1.3 2012/07/25 15:21:59 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -170,11 +170,11 @@ mpool_malloc(mpool_t * __restrict mp, u_int size, cons u_int align; if (!mp) { - sess_SetErr(EINVAL, "Pool not specified"); + io_SetErr(EINVAL, "Pool not specified"); return NULL; } if (size > MEM_ALLOC_MAX) { - sess_SetErr(ENOMEM, "Memory size is too large"); + io_SetErr(ENOMEM, "Memory size is too large"); return NULL; } else size = (size + 3) & ~3; /* must align to 4 because needed room for sentinels */ @@ -188,7 +188,7 @@ mpool_malloc(mpool_t * __restrict mp, u_int size, cons /* quota */ if (mp->pool_quota.max && (mp->pool_quota.curr + size) > mp->pool_quota.max) { - sess_SetErr(ENOMEM, "Max.allocate memory quota has been reached"); + io_SetErr(ENOMEM, "Max.allocate memory quota has been reached"); mpool_unlock(mp); return NULL; } @@ -213,8 +213,10 @@ mpool_malloc(mpool_t * __restrict mp, u_int size, cons free(m); mpool_unlock(mp); return NULL; - } else /* quota */ + } else { /* quota */ mp->pool_quota.curr += size; + memset(m->alloc_mem, 0, align + 12); + } } m->alloc_mem[0] = size / sizeof(u_int); @@ -251,12 +253,12 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri return mpool_malloc(mp, newsize, memname); if (!mp) { - sess_SetErr(EINVAL, "Pool not specified"); + io_SetErr(EINVAL, "Pool not specified"); return NULL; } /* check address range & sentinel */ if (MEM_BADADDR(data) || MEM_CORRUPT(data)) { - sess_SetErr(EFAULT, "Corrupted memory address"); + io_SetErr(EFAULT, "Corrupted memory address"); return NULL; } else { osize = ((u_int*)data)[-2] * sizeof(u_int); @@ -264,7 +266,7 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri } /* prepare new size */ if (newsize > MEM_ALLOC_MAX) { - sess_SetErr(ENOMEM, "Memory size is too large"); + io_SetErr(ENOMEM, "Memory size is too large"); return NULL; } else { newsize = (newsize + 3) & ~3; /* must align to 4 because needed room for sentinels */ @@ -276,7 +278,7 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri /* quota */ if (mp->pool_quota.max && (mp->pool_quota.curr + ((u_long) newsize - osize)) > mp->pool_quota.max) { - sess_SetErr(ENOMEM, "Max.allocate memory quota has been reached"); + io_SetErr(ENOMEM, "Max.allocate memory quota has been reached"); mpool_unlock(mp); return NULL; } @@ -297,7 +299,7 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri /* memory block not found! */ if (!m) { mpool_unlock(mp); - sess_SetErr(EFAULT, "Memory block not found"); + io_SetErr(EFAULT, "Memory block not found"); return NULL; } @@ -354,7 +356,7 @@ mpool_purge(mpool_t * __restrict mp, u_int atmost) struct tagAlloc *m, *tmp; if (!mp) { - sess_SetErr(EINVAL, "Pool not specified"); + io_SetErr(EINVAL, "Pool not specified"); return -1; } @@ -402,13 +404,15 @@ mpool_free(mpool_t * __restrict mp, void * __restrict int idx; struct tagAlloc *m, *tmp; + assert(data); if (!mp) { - sess_SetErr(EINVAL, "Pool not specified"); + 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)) { - sess_SetErr(EFAULT, "Corrupted memory address"); + io_SetErr(EFAULT, "Corrupted memory address"); return -2; } else idx = BucketIndex(((u_int*)data)[-2] * sizeof(u_int)); @@ -460,7 +464,7 @@ mpool_free2(mpool_t * __restrict mp, u_int size, const struct tagAlloc *m, *tmp; if (!mp || !memname) { - sess_SetErr(EINVAL, "Pool or memory name is not specified"); + io_SetErr(EINVAL, "Pool or memory name is not specified"); return -1; } else idx = BucketIndex(size); @@ -497,6 +501,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 +665,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); }