10 #include <portable.h>
\r
11 #include <xbt/xbt_os_thread.h>
\r
14 #ifndef __DEFAULT_BLOCK_CAPACITY
\r
15 #define __DEFAULT_BLOCK_CAPACITY ((int)512)
\r
18 #ifndef __DEFAULT_TABLE_SIZE
\r
19 #define __DEFAULT_TABLE_SIZE ((int)256)
\r
23 #ifndef __BYTE_T_DEFINED
\r
24 typedef unsigned char byte;
\r
25 #define __BYTE_T_DEFINED
\r
36 static xbt_os_mutex_t
\r
40 static unsigned long
\r
41 hfunc(const void* key)
\r
43 return ((unsigned long)(void*)(unsigned long)key) >> 4;
\r
47 cmp_key(const void* k1, const void* k2)
\r
54 xmalloc_mod_init(void)
\r
56 /*allocator_node_t cur, next;
\r
57 allocator_block_t block;
\r
58 int block_capacity, type_size, node_size;
\r
64 _mutex = xbt_os_mutex_init();
\r
67 if(!(_heap = (htable_t)calloc(1, sizeof(s_htable_t))))
\r
70 if(!(_heap->content = (hassoc_t*)calloc(__DEFAULT_TABLE_SIZE, sizeof(hassoc_t))))
\r
76 if(!(_heap->allocator = (allocator_t)calloc(1,sizeof(s_allocator_t))))
\r
78 free(_heap->content);
\r
83 _heap->allocator->block_capacity = __DEFAULT_BLOCK_CAPACITY;
\r
84 _heap->allocator->type_size = sizeof(s_hassoc_t);
\r
88 block_capacity = __DEFAULT_BLOCK_CAPACITY;
\r
89 type_size = sizeof(s_hassoc_t);
\r
90 node_size = type_size + sizeof(s_allocator_node_t);
\r
92 if(!(block = (allocator_block_t)calloc(1,sizeof(s_allocator_block_t) + (block_capacity * node_size))))
\r
94 free(_heap->content);
\r
95 free(_heap->allocator);
\r
100 block->next = _heap->allocator->head;
\r
101 block->allocator = _heap->allocator;
\r
102 _heap->allocator->head = block;
\r
104 cur = (allocator_node_t)(((byte*)(block + 1)) + ((block_capacity - 1) * node_size));
\r
106 for(pos = block_capacity - 1; pos >= 0; pos--, cur = (allocator_node_t)(((byte*)next) - node_size))
\r
109 cur->block = block;
\r
113 _heap->allocator->free = _heap->allocator->first = next;
\r
115 _heap->allocator->capacity += block_capacity;
\r
116 _heap->allocator->fn_finalize = NULL;
\r
119 _heap->size = __DEFAULT_TABLE_SIZE;
\r
120 _heap->fn_hfunc = hfunc;
\r
121 _heap->fn_cmp_key = cmp_key;
\r
122 _heap->fn_finalize = NULL;
\r
136 xmalloc_mod_exit(void)
\r
138 /*hassoc_t* content;
\r
139 register hassoc_t hassoc;
\r
140 allocator_block_t cur, next;
\r
151 if(!htable_is_empty(_heap))
\r
154 fprintf(stderr,"WARNING : Memory leak detected - automaticaly release the memory...\n");
\r
156 fprintf(stderr,"System aborted - Automaticaly release the memory...\n");
\r
160 content = _heap->content;
\r
161 size = _heap->size;
\r
163 for(pos = 0; pos < size; pos++)
\r
165 for(hassoc = content[pos]; hassoc; hassoc = hassoc->next)
\r
167 val = (void*)hassoc->val;
\r
168 if(xfree(&val) < 0)
\r
173 free(_heap->content);
\r
175 cur = _heap->allocator->head;
\r
185 free(_heap->allocator);
\r
190 xbt_os_mutex_destroy(_mutex);
\r
196 xmalloc(unsigned int size)
\r
200 if(!(p = (byte*)calloc(size + 1, sizeof(byte))))
\r
205 /*if(htable_set(_heap, p, p))
\r
215 xcalloc(unsigned int count, unsigned int size)
\r
219 if(!(p = (byte*)calloc((size * count) + 1, sizeof(byte))))
\r
224 /*if(htable_set(_heap, p, p))
\r
244 _ptr = (byte*)ptr - 1;
\r
246 if(XMAGIC != _ptr[0])
\r
252 /*if(!htable_lookup(_heap, _ptr))
\r
255 if(!htable_remove(_heap, _ptr))
\r
265 xrealloc(void *ptr, unsigned int size)
\r
267 byte* _ptr,* _ptr_r ;
\r
271 /* If ptr is NULL, realloc() is identical to a call
\r
272 * to malloc() for size byte
\r
274 _ptr = (byte*)calloc(size + 1, sizeof(byte));
\r
286 /* If size is zero and ptr is not NULL, the allocated
\r
293 _ptr = (byte*)ptr - 1;
\r
295 if(XMAGIC != _ptr[0])
\r
301 if((_ptr_r = realloc(_ptr, size)))
\r
308 xstrdup(const char* s1)
\r
315 if((d1 = xmalloc((unsigned int)strlen(s1) +1 )))
\r