docs/src/features.md

changeset 745
c99abca90d21
parent 733
2ed01495f838
child 746
b4bd0155f03f
equal deleted inserted replaced
744:937f8b5d4a3f 745:c99abca90d21
43 CxAllocator create_my_allocator(size_t n) { 43 CxAllocator create_my_allocator(size_t n) {
44 CxAllocator alloc; 44 CxAllocator alloc;
45 alloc.cl = &my_allocator_class; 45 alloc.cl = &my_allocator_class;
46 alloc.data = calloc(1, sizeof(struct my_allocator_state) + n); 46 alloc.data = calloc(1, sizeof(struct my_allocator_state) + n);
47 return alloc; 47 return alloc;
48 }
49
50 void free_my_allocator(CxAllocator *alloc) {
51 free(alloc.data);
52 free(alloc);
53 } 48 }
54 ``` 49 ```
55 50
56 ## String 51 ## String
57 52
131 // create a buffer using the memory pool for destruction 126 // create a buffer using the memory pool for destruction
132 CxBuffer *content = cxBufferCreate(NULL, 256, pool->allocator, CX_BUFFER_AUTO_EXTEND); 127 CxBuffer *content = cxBufferCreate(NULL, 256, pool->allocator, CX_BUFFER_AUTO_EXTEND);
133 128
134 // read the file into the buffer and turn it into a string 129 // read the file into the buffer and turn it into a string
135 cx_stream_copy(f, content, (cx_read_func) fread, (cx_write_func) cxBufferWrite); 130 cx_stream_copy(f, content, (cx_read_func) fread, (cx_write_func) cxBufferWrite);
131 fclose(f);
136 cxstring contentstr = cx_strn(content->space, content->size); 132 cxstring contentstr = cx_strn(content->space, content->size);
137 133
138 // split the string into lines - use the mempool for allocating the target array 134 // split the string into lines - use the mempool for allocating the target array
139 cxstring* lines; 135 cxstring* lines;
140 size_t lc = cx_strsplit_a(pool->allocator, contentstr, 136 size_t lc = cx_strsplit_a(pool->allocator, contentstr,
233 struct cx_list_s { 229 struct cx_list_s {
234 CX_COLLECTION_MEMBERS // size, capacity, etc. 230 CX_COLLECTION_MEMBERS // size, capacity, etc.
235 cx_list_class const *cl; // The list class definition 231 cx_list_class const *cl; // The list class definition
236 }; 232 };
237 ``` 233 ```
238 The actual structure contains one more class pointer that is used when wrapping a list into a pointer aware list 234 The actual structure contains one more class pointer that is used when wrapping a list into a pointer-aware list
239 with `cxListStorePointers()`. What this means, is that - if you want to implement your own list structure - you 235 with `cxListStorePointers()`. What this means, is that - if you want to implement your own list structure - you
240 only need to cover the case where the list is storing copies of your objects. 236 only need to cover the case where the list is storing copies of your objects.
241 237
242 UCX comes with two common list implementations (linked list and array list) that should cover most use cases. 238 UCX comes with two common list implementations (linked list and array list) that should cover most use cases.
243 But if you feel the need to implement an own list, the only thing you need to do is to define a struct where 239 But if you feel the need to implement an own list, the only thing you need to do is to define a struct where

mercurial