Remove 'count' parameters (avoid to call free for alloc blocks: clear do everything
in one go). Add contract.
This commit is contained in:
1 parent
2d9ee1b930
commit
6d29968e34
1 file changed
+12
-7
+12
-7
@@ -40,7 +40,6 @@
|
|||||||
mempool_uint_clear(m); // Give back memory to system
|
mempool_uint_clear(m); // Give back memory to system
|
||||||
*/
|
*/
|
||||||
// NOTE: Can not use m-list since it may be expanded from LIST_DEF
|
// NOTE: Can not use m-list since it may be expanded from LIST_DEF
|
||||||
// FIXME: Shall this use standard memory function (malloc) or M_MEMORY_ALLOC?
|
|
||||||
|
|
||||||
#define MEMPOOL_DEF(name, type) \
|
#define MEMPOOL_DEF(name, type) \
|
||||||
\
|
\
|
||||||
@@ -58,7 +57,6 @@
|
|||||||
typedef struct { \
|
typedef struct { \
|
||||||
M_C3(mempool_,name,_union_t) *free_list; \
|
M_C3(mempool_,name,_union_t) *free_list; \
|
||||||
M_C3(mempool_,name,_segment_t) *current_segment; \
|
M_C3(mempool_,name,_segment_t) *current_segment; \
|
||||||
size_t count; \
|
|
||||||
} M_C3(mempool_,name,_t)[1]; \
|
} M_C3(mempool_,name,_t)[1]; \
|
||||||
\
|
\
|
||||||
static inline void \
|
static inline void \
|
||||||
@@ -73,20 +71,19 @@
|
|||||||
} \
|
} \
|
||||||
mem->current_segment->next = NULL; \
|
mem->current_segment->next = NULL; \
|
||||||
mem->current_segment->count = 0; \
|
mem->current_segment->count = 0; \
|
||||||
mem->count = 0; \
|
MEMPOOLI_CONTRACT(mem, type); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static inline void \
|
static inline void \
|
||||||
M_C3(mempool_,name,_clear)(M_C3(mempool_,name,_t) mem) \
|
M_C3(mempool_,name,_clear)(M_C3(mempool_,name,_t) mem) \
|
||||||
{ \
|
{ \
|
||||||
assert(mem -> count == 0); \
|
MEMPOOLI_CONTRACT(mem, type); \
|
||||||
M_C3(mempool_,name,_segment_t) *segment = mem->current_segment; \
|
M_C3(mempool_,name,_segment_t) *segment = mem->current_segment; \
|
||||||
while (segment != NULL) { \
|
while (segment != NULL) { \
|
||||||
M_C3(mempool_,name,_segment_t) *next = segment->next; \
|
M_C3(mempool_,name,_segment_t) *next = segment->next; \
|
||||||
free(segment); \
|
free(segment); \
|
||||||
segment = next; \
|
segment = next; \
|
||||||
} \
|
} \
|
||||||
mem->count = 0; \
|
|
||||||
mem->free_list = NULL; \
|
mem->free_list = NULL; \
|
||||||
mem->current_segment = NULL; \
|
mem->current_segment = NULL; \
|
||||||
} \
|
} \
|
||||||
@@ -94,7 +91,7 @@
|
|||||||
static inline type * \
|
static inline type * \
|
||||||
M_C3(mempool_,name,_alloc)(M_C3(mempool_,name,_t) mem) \
|
M_C3(mempool_,name,_alloc)(M_C3(mempool_,name,_t) mem) \
|
||||||
{ \
|
{ \
|
||||||
mem->count++; \
|
MEMPOOLI_CONTRACT(mem, type); \
|
||||||
M_C3(mempool_,name,_union_t) *ret = mem->free_list; \
|
M_C3(mempool_,name,_union_t) *ret = mem->free_list; \
|
||||||
if (ret != NULL) { \
|
if (ret != NULL) { \
|
||||||
mem->free_list = ret->next; \
|
mem->free_list = ret->next; \
|
||||||
@@ -118,22 +115,30 @@
|
|||||||
} \
|
} \
|
||||||
ret = &segment->tab[count]; \
|
ret = &segment->tab[count]; \
|
||||||
segment->count = count + 1; \
|
segment->count = count + 1; \
|
||||||
|
MEMPOOLI_CONTRACT(mem, type); \
|
||||||
return &ret->t; \
|
return &ret->t; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static inline void \
|
static inline void \
|
||||||
M_C3(mempool_,name,_free)(M_C3(mempool_,name,_t) mem, type *ptr) \
|
M_C3(mempool_,name,_free)(M_C3(mempool_,name,_t) mem, type *ptr) \
|
||||||
{ \
|
{ \
|
||||||
|
MEMPOOLI_CONTRACT(mem, type); \
|
||||||
/* NOTE: Unsafe cast: suppose that the given pointer \
|
/* NOTE: Unsafe cast: suppose that the given pointer \
|
||||||
was allocated by the previous alloc function. */ \
|
was allocated by the previous alloc function. */ \
|
||||||
M_C3(mempool_,name,_union_t) *ret = (M_C3(mempool_,name,_union_t) *)(uintptr_t)ptr; \
|
M_C3(mempool_,name,_union_t) *ret = (M_C3(mempool_,name,_union_t) *)(uintptr_t)ptr; \
|
||||||
ret->next = mem->free_list; \
|
ret->next = mem->free_list; \
|
||||||
mem->free_list = ret; \
|
mem->free_list = ret; \
|
||||||
mem->count--; \
|
MEMPOOLI_CONTRACT(mem, type); \
|
||||||
} \
|
} \
|
||||||
|
|
||||||
/********************************** INTERNAL ************************************/
|
/********************************** INTERNAL ************************************/
|
||||||
|
|
||||||
#define MEMPOOLI_MAX_PER_SEGMENT(type) M_MAX((16*1024-sizeof(unsigned int) - 2*sizeof(void*)) / sizeof (type), 256U)
|
#define MEMPOOLI_MAX_PER_SEGMENT(type) M_MAX((16*1024-sizeof(unsigned int) - 2*sizeof(void*)) / sizeof (type), 256U)
|
||||||
|
|
||||||
|
#define MEMPOOLI_CONTRACT(mempool, type) do { \
|
||||||
|
assert((mempool) != NULL); \
|
||||||
|
assert((mempool)->current_segment != NULL); \
|
||||||
|
assert((mempool)->current_segment->count <= MEMPOOLI_MAX_PER_SEGMENT(type)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user