CARMA C++
freelist.h
1 #ifndef freelist_h
2 #define freelist_h
3 
4 #include <stdlib.h>
5 
6 /*
7  * This module provides a memory allocation scheme that helps to
8  * prevent memory fragmentation by allocating large blocks of
9  * fixed sized objects and forming them into a free-list for
10  * subsequent allocations. The free-list is expanded as needed.
11  */
12 typedef struct FreeList FreeList;
13 
14 /*
15  * Allocate a new free-list from blocks of 'blocking_factor' objects of size
16  * node_size. The node_size argument should be determined by applying
17  * the sizeof() operator to the object type that you intend to allocate from
18  * the freelist.
19  */
20 FreeList *new_FreeList(const char *caller, size_t node_size,
21  unsigned blocking_factor);
22 
23 /*
24  * If it is known that none of the nodes currently allocated from
25  * a freelist are still in use, the following function can be called
26  * to return all nodes to the freelist without the overhead of
27  * having to call del_FreeListNode() for every allocated node. The
28  * nodes of the freelist can then be reused by future callers to
29  * new_FreeListNode().
30  */
31 void rst_FreeList(FreeList *fl);
32 
33 /*
34  * Delete a free-list.
35  */
36 FreeList *del_FreeList(const char *caller, FreeList *fl, int force);
37 
38 /*
39  * Determine the number of nodes that are currently allocated.
40  */
41 long busy_FreeListNodes(FreeList *fl);
42 
43 /*
44  * Allocate a new object from a free-list.
45  */
46 void *new_FreeListNode(const char *caller, FreeList *fl);
47 
48 /*
49  * Return an object to the free-list that it was allocated from.
50  */
51 void *del_FreeListNode(const char *caller, FreeList *fl, void *object);
52 
53 /*
54  * Return non-zero if the specified freelist was created for the specified
55  * node size. The node_size argument should be determined by applying
56  * the sizeof() operator to the object type that you intend to allocate from
57  * the freelist.
58  */
59 int compatible_FreeList(FreeList *fl, size_t node_size);
60 
61 #endif