+++ /dev/null
-#include <xalloc.h>\r
-#include <htable.h>\r
-\r
-#include <stdlib.h>\r
-#include <errno.h>\r
-#include <string.h>\r
-\r
-#include <stdio.h>\r
-\r
-#include <portable.h>\r
-#include <xbt/xbt_os_thread.h>\r
-\r
-\r
-#ifndef __DEFAULT_BLOCK_CAPACITY\r
-#define __DEFAULT_BLOCK_CAPACITY ((int)512)\r
-#endif\r
-\r
-#ifndef __DEFAULT_TABLE_SIZE\r
-#define __DEFAULT_TABLE_SIZE ((int)256)\r
-#endif\r
-\r
-\r
-#ifndef __BYTE_T_DEFINED\r
- typedef unsigned char byte;\r
- #define __BYTE_T_DEFINED\r
-#endif\r
-\r
-\r
-static int\r
-_aborded = 0;\r
-\r
-/*\r
-static htable_t\r
-_heap = NULL;\r
-\r
-static xbt_os_mutex_t\r
-_mutex = NULL;\r
-\r
-\r
-static unsigned long \r
-hfunc(const void* key)\r
-{\r
- return ((unsigned long)(void*)(unsigned long)key) >> 4;\r
-}\r
-\r
-static int\r
-cmp_key(const void* k1, const void* k2)\r
-{\r
- return (k1 == k2);\r
-}\r
-*/\r
-\r
-int\r
-xmalloc_mod_init(void)\r
-{\r
- /*allocator_node_t cur, next;\r
- allocator_block_t block;\r
- int block_capacity, type_size, node_size;\r
- register int pos;\r
- \r
- if(_heap)\r
- return EALREADY;\r
- \r
- _mutex = xbt_os_mutex_init();\r
- \r
- \r
- if(!(_heap = (htable_t)calloc(1, sizeof(s_htable_t))))\r
- return -1;\r
- \r
- if(!(_heap->content = (hassoc_t*)calloc(__DEFAULT_TABLE_SIZE, sizeof(hassoc_t))))\r
- {\r
- free(_heap);\r
- return -1;\r
- }\r
- \r
- if(!(_heap->allocator = (allocator_t)calloc(1,sizeof(s_allocator_t))))\r
- {\r
- free(_heap->content);\r
- free(_heap);\r
- return -1;\r
- }\r
-\r
- _heap->allocator->block_capacity = __DEFAULT_BLOCK_CAPACITY;\r
- _heap->allocator->type_size = sizeof(s_hassoc_t);\r
- \r
- \r
- next = NULL;\r
- block_capacity = __DEFAULT_BLOCK_CAPACITY;\r
- type_size = sizeof(s_hassoc_t);\r
- node_size = type_size + sizeof(s_allocator_node_t);\r
- \r
- if(!(block = (allocator_block_t)calloc(1,sizeof(s_allocator_block_t) + (block_capacity * node_size))))\r
- {\r
- free(_heap->content);\r
- free(_heap->allocator);\r
- free(_heap);\r
- return -1;\r
- }\r
- \r
- block->next = _heap->allocator->head;\r
- block->allocator = _heap->allocator;\r
- _heap->allocator->head = block;\r
- \r
- cur = (allocator_node_t)(((byte*)(block + 1)) + ((block_capacity - 1) * node_size));\r
- \r
- for(pos = block_capacity - 1; pos >= 0; pos--, cur = (allocator_node_t)(((byte*)next) - node_size))\r
- {\r
- cur->next = next;\r
- cur->block = block;\r
- next = cur;\r
- }\r
- \r
- _heap->allocator->free = _heap->allocator->first = next;\r
- \r
- _heap->allocator->capacity += block_capacity;\r
- _heap->allocator->fn_finalize = NULL;\r
- \r
- \r
- _heap->size = __DEFAULT_TABLE_SIZE;\r
- _heap->fn_hfunc = hfunc;\r
- _heap->fn_cmp_key = cmp_key;\r
- _heap->fn_finalize = NULL;\r
- \r
- */\r
- return 0;\r
-}\r
-\r
-void\r
-xabort(void)\r
-{\r
- _aborded = 1;\r
- abort();\r
-}\r
-\r
-int\r
-xmalloc_mod_exit(void)\r
-{\r
- /*hassoc_t* content;\r
- register hassoc_t hassoc;\r
- allocator_block_t cur, next;\r
- register int pos;\r
- int size;\r
- void* val;\r
- \r
- if(_heap)\r
- {\r
- errno = EPERM;\r
- return -1;\r
- }\r
- \r
- if(!htable_is_empty(_heap))\r
- {\r
- if(!_aborded)\r
- fprintf(stderr,"WARNING : Memory leak detected - automaticaly release the memory...\n");\r
- else\r
- fprintf(stderr,"System aborted - Automaticaly release the memory...\n"); \r
- }\r
- \r
-\r
- content = _heap->content;\r
- size = _heap->size;\r
-\r
- for(pos = 0; pos < size; pos++)\r
- {\r
- for(hassoc = content[pos]; hassoc; hassoc = hassoc->next)\r
- {\r
- val = (void*)hassoc->val;\r
- if(xfree(&val) < 0)\r
- return -1;\r
- }\r
- }\r
- \r
- free(_heap->content);\r
- \r
- cur = _heap->allocator->head;\r
- \r
- while(cur)\r
- {\r
- next = cur->next;\r
- xfree(cur);\r
- cur = next;\r
- }\r
- \r
- \r
- free(_heap->allocator);\r
- \r
- free(_heap);\r
- _heap = NULL;\r
- \r
- xbt_os_mutex_destroy(_mutex);\r
- */\r
- return 0;\r
-}\r
-\r
-void*\r
-xmalloc(unsigned int size)\r
-{\r
- byte* p;\r
- \r
- if(!(p = (byte*)calloc(size + 1, sizeof(byte))))\r
- return NULL;\r
- \r
- p[0] = XMAGIC;\r
- \r
- /*if(htable_set(_heap, p, p))\r
- {\r
- xfree(p);\r
- return NULL;\r
- }*/\r
- \r
- return p + 1;\r
-}\r
-\r
-void*\r
-xcalloc(unsigned int count, unsigned int size)\r
-{\r
- byte* p;\r
- \r
- if(!(p = (byte*)calloc((size * count) + 1, sizeof(byte))))\r
- return NULL;\r
- \r
- p[0] = XMAGIC;\r
- \r
- /*if(htable_set(_heap, p, p))\r
- {\r
- xfree(p);\r
- return NULL;\r
- }*/\r
- \r
- return p + 1;\r
-}\r
-\r
-int\r
-xfree(void* ptr)\r
-{\r
- byte* _ptr;\r
- \r
- if(!ptr)\r
- {\r
- errno = EINVAL;\r
- return -1;\r
- }\r
- \r
- _ptr = (byte*)ptr - 1;\r
- \r
- if(XMAGIC != _ptr[0])\r
- {\r
- errno = EINVAL;\r
- return -1;\r
- }\r
- \r
- /*if(!htable_lookup(_heap, _ptr))\r
- return -1;\r
- \r
- if(!htable_remove(_heap, _ptr))\r
- return -1;\r
- */\r
- \r
- free(_ptr);\r
- \r
- return 0;\r
-}\r
-\r
-void*\r
-xrealloc(void *ptr, unsigned int size)\r
-{\r
- byte* _ptr,* _ptr_r ;\r
- \r
- if(!ptr)\r
- {\r
- /* If ptr is NULL, realloc() is identical to a call\r
- * to malloc() for size byte\r
- */\r
- _ptr = (byte*)calloc(size + 1, sizeof(byte));\r
- \r
- if(!_ptr)\r
- return NULL;\r
- \r
- _ptr[0] = XMAGIC;\r
- \r
- return _ptr + 1;\r
- }\r
- \r
- if(ptr && !size)\r
- {\r
- /* If size is zero and ptr is not NULL, the allocated \r
- * object is freed.\r
- */\r
- xfree(ptr);\r
- return NULL;\r
- }\r
- \r
- _ptr = (byte*)ptr - 1;\r
- \r
- if(XMAGIC != _ptr[0])\r
- {\r
- errno = EINVAL;\r
- return NULL;\r
- }\r
- \r
- if((_ptr_r = realloc(_ptr, size)))\r
- return _ptr_r + 1;\r
- \r
- return NULL;\r
-}\r
-\r
-char*\r
-xstrdup(const char* s1)\r
-{\r
- char* d1;\r
- \r
- if(!s1)\r
- return NULL;\r
- \r
- if((d1 = xmalloc((unsigned int)strlen(s1) +1 )))\r
- strcpy(d1, s1);\r
- \r
- return d1;\r
-}\r
-\r