CARMA C++
list.h
1 #ifndef list_h
2 #define list_h
3 
4 /*
5  * List containers and nodes are small objects. Separately allocating
6  * many such objects would normally cause memory fragmentation. To
7  * counter this, ListMemory objects are used. These contain
8  * dedicated free-lists formed from large dynamically allocated arrays
9  * of objects. One ListMemory object can be shared between multiple lists
10  * (within a single thread).
11  */
12 typedef struct ListMemory ListMemory;
13 
14 /* Create a free-list for allocation of lists */
15 
16 ListMemory *new_ListMemory(int list_count, int node_count);
17 
18 /*
19  * Display the number of active lists and list nodes that are
20  * currently allocated from a given freelist
21  */
22 void show_ListMemory(ListMemory *mem);
23 
24 /*
25  * Return all allocated lists and their contents to the freelist.
26  * This function should not be called unless if it is known that none
27  * of the allocated lists are still in use.
28  */
29 
30 void rst_ListMemory(ListMemory *mem);
31 
32 /* Delete a redundant free-list if not being used */
33 
34 ListMemory *del_ListMemory(ListMemory *mem, int force);
35 
36 /*
37  * Declare the contents of a generic list node.
38  */
39 typedef struct ListNode ListNode;
40 struct ListNode {
41  void *data; /* Application supplied data */
42  ListNode *next; /* The next node in the list, or NULL */
43 };
44 
45 /*
46  * Declare a list container.
47  */
48 typedef struct {
49  unsigned ref; /* Reference count */
50  unsigned nnode; /* The number of nodes in the list */
51  ListMemory *mem; /* Memory allocation container */
52  int internal_mem; /* True if mem was allocated by new_List() */
53  ListNode *head; /* The first node of the list */
54  ListNode *tail; /* The last node of the list */
55 } List;
56 
57 /* Create a new list using memory from a given ListMemory container */
58 
59 List *new_List(ListMemory *mem);
60 
61 /* Delete a reference to a list */
62 
63 List *del_List(List *list);
64 
65 /* Increment the reference count of a list to prevent untimely deletion */
66 
67 List *ref_List(List *list);
68 
69 /* Discard the contents of a list via calls to del_ListNode() */
70 
71 List *clr_List(List *list);
72 
73 /* Append a node to the end of a list */
74 
75 ListNode *append_ListNode(List *list, void *data);
76 
77 /* Prepend a node to the head of a list */
78 
79 ListNode *prepend_ListNode(List *list, void *data);
80 
81 /* Insert a node into a list after a given node */
82 
83 ListNode *insert_ListNode(List *list, ListNode *prev, void *data);
84 
85 /*
86  * Delete a list node container and return its 'data' pointer.
87  * The node can be identified either by its node pointer or its data pointer.
88  */
89 
90 void *del_ListNode(List *list, ListNode *node, void *data);
91 
92 /* Find the first list node that contains a given data pointer */
93 
94 ListNode *find_ListNode(List *list, void *data);
95 
96 #endif