docs/src/features.md

changeset 722
adc3c48dd3af
parent 720
f73adc75e50a
child 723
c33e0ac069dd
equal deleted inserted replaced
721:b79340dee0b4 722:adc3c48dd3af
13 </div> 13 </div>
14 14
15 ## Allocator 15 ## Allocator
16 16
17 *Header file:* [allocator.h](api/allocator_8h.html) 17 *Header file:* [allocator.h](api/allocator_8h.html)
18
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`.
23
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:
28
29 ```c
30 struct my_allocator_state {
31 size_t total;
32 size_t avail;
33 char[] mem;
34 };
35
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 };
42
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 }
49
50 void free_my_allocator(CxAllocator *alloc) {
51 free(alloc.data);
52 free(alloc);
53 }
54 ```
18 55
19 ## String 56 ## String
20 57
21 *Header file:* [string.h](api/string_8h.html) 58 *Header file:* [string.h](api/string_8h.html)
22 59

mercurial