88 |
88 |
89 A memory pool is providing an allocator implementation that automatically deallocates the memory upon its destruction. |
89 A memory pool is providing an allocator implementation that automatically deallocates the memory upon its destruction. |
90 It also allows you to register destructor functions for the allocated memory, which are automatically called before |
90 It also allows you to register destructor functions for the allocated memory, which are automatically called before |
91 the memory is deallocated. |
91 the memory is deallocated. |
92 Additionally, you may also register _independent_ destructor functions within a pool in case some external library |
92 Additionally, you may also register _independent_ destructor functions within a pool in case some external library |
93 allocated memory for you, which should be destroyed together with this pool. |
93 allocated memory for you, which should be freed together with this pool. |
94 |
94 |
95 Many UCX features support the use of an allocator. |
95 Many UCX features support the use of an allocator. |
96 The [strings](#string), for instance, provide several functions suffixed with `_a` that allow specifying an allocator. |
96 The [strings](#string), for instance, provide several functions suffixed with `_a` that allow specifying an allocator. |
97 You can use this to keep track of the memory occupied by dynamically allocated strings and cleanup everything with |
97 You can use this to keep track of the memory occupied by dynamically allocated strings and cleanup everything with |
98 just a single call to `cxMempoolDestroy()`. |
98 just a single call to `cxMempoolFree()`. |
99 |
99 |
100 The following code illustrates this on the example of reading a CSV file into memory. |
100 The following code illustrates this on the example of reading a CSV file into memory. |
101 ```C |
101 ```C |
102 #include <stdio.h> |
102 #include <stdio.h> |
103 #include <cx/mempool.h> |
103 #include <cx/mempool.h> |
143 if (lines[i].length == 0) continue; |
143 if (lines[i].length == 0) continue; |
144 cxstring fields[3]; |
144 cxstring fields[3]; |
145 size_t fc = cx_strsplit(lines[i], CX_STR(";"), 3, fields); |
145 size_t fc = cx_strsplit(lines[i], CX_STR(";"), 3, fields); |
146 if (fc != 3) { |
146 if (fc != 3) { |
147 fprintf(stderr, "Syntax error in line %zu.\n", i); |
147 fprintf(stderr, "Syntax error in line %zu.\n", i); |
148 cxMempoolDestroy(pool); |
148 cxMempoolFree(pool); |
149 return 1; |
149 return 1; |
150 } |
150 } |
151 CSVData data; |
151 CSVData data; |
152 data.column_a = fields[0]; |
152 data.column_a = fields[0]; |
153 data.column_b = fields[1]; |
153 data.column_b = fields[1]; |