docs/Writerside/topics/allocator.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 # Allocator
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 The UCX allocator provides an interface for implementing an own memory allocation mechanism.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 Various function in UCX provide an additional alternative signature that takes an allocator as
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 argument. A default allocator implementation using the stdlib memory management functions is
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 available via the global symbol `cxDefaultAllocator`.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 If you want to define your own allocator, you need to initialize the `CxAllocator` structure
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 with a pointer to an allocator class (containing function pointers for the memory management
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 functions) and an optional pointer to an arbitrary memory region that can be used to store
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 state information for the allocator. An example is shown below:
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 ```c
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 struct my_allocator_state {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 size_t total;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 size_t avail;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 char mem[];
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 };
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 static cx_allocator_class my_allocator_class = {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 my_malloc_impl,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 my_realloc_impl, // all these functions are somewhere defined
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 my_calloc_impl,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 my_free_impl
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 };
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 CxAllocator create_my_allocator(size_t n) {
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 CxAllocator alloc;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 alloc.cl = &my_allocator_class;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 alloc.data = calloc(1, sizeof(struct my_allocator_state) + n);
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 return alloc;
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 }
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 ```
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
39 ## Undocumented Symbols (TODO)
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
40
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
41 ### cxCalloc
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
42 ### cx_default_allocator
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
43 ### cxDefaultAllocator
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
44 ### cxFree
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
45 ### cxMalloc
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
46 ### cxRealloc
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
47 ### cxReallocArray
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
48 ### cx_reallocate_
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
49 ### cxReallocate_
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
50 ### cx_reallocatearray_
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
51 ### cxReallocateArray_

mercurial