Tue, 27 Jun 2023 18:10:34 +0200
add allocator example
1 ---
2 title: UCX Features
3 ---
5 <div id="modules">
7 ------------------------ ------------------------- ------------------- ---------------------------------
8 [Allocator](#allocator) [String](#string) [Buffer](#buffer) [Memory Pool](#memory-pool)
9 [Iterator](#iterator) [Collection](#collection) [List](#list) [Map](#map)
10 [Utilities](#utilities)
11 ------------------------ ------------------------- ------------------- ---------------------------------
13 </div>
15 ## Allocator
17 *Header file:* [allocator.h](api/allocator_8h.html)
19 The UCX allocator provides an interface for implementing an own memory allocation mechanism.
20 Various function in UCX provide an additional alternative signature that takes an allocator as
21 argument. A default allocator implementation using the stdlib memory management functions is
22 available via the global symbol `cxDefaultAllocator`.
24 If you want to define your own allocator, you need to initialize the `CxAllocator` structure
25 with a pointer to an allocator class (containing function pointers for the memory management
26 functions) and an optional pointer to an arbitrary memory region that can be used to store
27 state information for the allocator. An example is shown below:
29 ```c
30 struct my_allocator_state {
31 size_t total;
32 size_t avail;
33 char[] mem;
34 };
36 static cx_allocator_class my_allocator_class = {
37 my_malloc_impl,
38 my_realloc_impl, // all these functions are somewhere defined
39 my_calloc_impl,
40 my_free_impl
41 };
43 CxAllocator create_my_allocator(size_t n) {
44 CxAllocator alloc;
45 alloc.cl = &my_allocator_class;
46 alloc.data = calloc(1, sizeof(struct my_allocator_state) + n);
47 return alloc;
48 }
50 void free_my_allocator(CxAllocator *alloc) {
51 free(alloc.data);
52 free(alloc);
53 }
54 ```
56 ## String
58 *Header file:* [string.h](api/string_8h.html)
60 ## Buffer
62 *Header file:* [buffer.h](api/buffer_8h.html)
64 ## Memory Pool
66 *Header file:* [mempool.h](api/mempool_8h.html)
68 ### Basic Memory Pool
70 *Header file:* [basic_mempool.h](api/basic__mempool_8h.html)
72 ## Iterator
74 *Header file:* [iterator.h](api/iterator_8h.html)
76 ## Collection
78 *Header file:* [collection.h](api/collection_8h.html)
80 ## List
82 *Header file:* [list.h](api/list_8h.html)
84 ### Linked List
86 *Header file:* [linked_list.h](api/linked__list_8h.html)
88 ### Array List
90 *Header file:* [array_list.h](api/array__list_8h.html)
92 ## Map
94 *Header file:* [map.h](api/map_8h.html)
96 ### Hash Map
98 *Header file:* [hash_map.h](api/hash__map_8h.html)
100 ## Utilities
102 *Header file:* [utils.h](api/utils_8h.html)
104 ### Printf Functions
106 *Header file:* [printf.h](api/printf_8h.html)
108 ### Compare Functions
110 *Header file:* [compare.h](api/compare_8h.html)