docs/Writerside/topics/mempool.h.md

Sat, 25 Jan 2025 13:44:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jan 2025 13:44:24 +0100
branch
docs/3.1
changeset 1146
151c057faf7c
parent 1143
0559812df10c
permissions
-rw-r--r--

add marker to every incomplete page

relates to #451

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # Memory Pool
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
1146
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
3 <warning>
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
4 Outdated - Rewrite!
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
5 </warning>
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
6
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 A memory pool is providing an allocator implementation that automatically deallocates the memory upon its destruction.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 It also allows you to register destructor functions for the allocated memory, which are automatically called before
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 the memory is deallocated.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 Additionally, you may also register _independent_ destructor functions within a pool in case some external library
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 allocated memory for you, which should be freed together with this pool.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 Many UCX features support the use of an allocator.
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
14 The [strings](string.h.md), for instance, provide several functions suffixed with `_a` that allow specifying an allocator.
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 You can use this to keep track of the memory occupied by dynamically allocated strings and cleanup everything with
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 just a single call to `cxMempoolFree()`.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 The following code illustrates this on the example of reading a CSV file into memory.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 ```C
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 #include <stdio.h>
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 #include <cx/mempool.h>
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 #include <cx/linked_list.h>
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 #include <cx/string.h>
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 #include <cx/buffer.h>
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 #include <cx/utils.h>
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 typedef struct {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 cxstring column_a;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 cxstring column_b;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 cxstring column_c;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 } CSVData;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 int main(void) {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 CxMempool* pool = cxBasicMempoolCreate(128);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 FILE *f = fopen("test.csv", "r");
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 if (!f) {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 perror("Cannot open file");
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 return 1;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 }
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 // close the file automatically at pool destruction
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 cxMempoolRegister(pool, f, (cx_destructor_func) fclose);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 // create a buffer using the memory pool for destruction
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 CxBuffer *content = cxBufferCreate(NULL, 256, pool->allocator, CX_BUFFER_AUTO_EXTEND);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 // read the file into the buffer and turn it into a string
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 cx_stream_copy(f, content, (cx_read_func) fread, cxBufferWriteFunc);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 fclose(f);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 cxstring contentstr = cx_strn(content->space, content->size);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 // split the string into lines - use the mempool for allocating the target array
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 cxstring* lines;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 size_t lc = cx_strsplit_a(pool->allocator, contentstr,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 CX_STR("\n"), SIZE_MAX, &lines);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 // skip the header and parse the remaining data into a linked list
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 // the nodes of the linked list shall also be allocated by the mempool
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 CxList* datalist = cxLinkedListCreate(pool->allocator, NULL, sizeof(CSVData));
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 for (size_t i = 1 ; i < lc ; i++) {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 if (lines[i].length == 0) continue;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 cxstring fields[3];
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 size_t fc = cx_strsplit(lines[i], CX_STR(";"), 3, fields);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 if (fc != 3) {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 fprintf(stderr, "Syntax error in line %zu.\n", i);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 cxMempoolFree(pool);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 return 1;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 }
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 CSVData data;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 data.column_a = fields[0];
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 data.column_b = fields[1];
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 data.column_c = fields[2];
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 cxListAdd(datalist, &data);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 }
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 // iterate through the list and output the data
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 CxIterator iter = cxListIterator(datalist);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 cx_foreach(CSVData*, data, iter) {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 printf("Column A: %.*s | "
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 "Column B: %.*s | "
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 "Column C: %.*s\n",
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 (int)data->column_a.length, data->column_a.ptr,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 (int)data->column_b.length, data->column_b.ptr,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 (int)data->column_c.length, data->column_c.ptr
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 );
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 }
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 // cleanup everything, no manual free() needed
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 cxMempoolFree(pool);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 return 0;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 }
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 ```
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
95 ## Undocumented Symbols (TODO)
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
96 ### cxMempoolCreate
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
97 ### cxMempoolFree
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
98 ### cxMempoolRegister
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
99 ### cxMempoolRemoveDestructor
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
100 ### cxMempoolSetDestructor

mercurial