docs/src/modules-ucx2.md

Fri, 12 Apr 2024 21:48:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Apr 2024 21:48:12 +0200
changeset 849
edb9f875b7f9
parent 719
034ec7abb83e
permissions
-rw-r--r--

improves interface of cx_sprintf() variants

universe@264 1 ---
universe@390 2 title: UCX 2.1 Modules
universe@264 3 ---
universe@259 4
universe@390 5 UCX 2.1 provided several modules for data structures and algorithms.
universe@717 6 You may choose to use specific modules by including the corresponding header
universe@259 7 file.
universe@390 8 Please note, that some modules make use of other UCX 2.1 modules.
universe@259 9 For instance, the [Allocator](#allocator) module is used by many other modules
universe@259 10 to allow flexible memory allocation.
universe@717 11 By default, the header files are placed into an `ucx` directory within your
universe@282 12 systems include directory. In this case you can use a module by including it
universe@259 13 via `#include <ucx/MODULENAME.h>`.
universe@259 14 Required modules are included automatically.
universe@259 15
universe@719 16 <div id="modules">
universe@267 17
universe@340 18 ----------------------- ---------------------- -------------------------------- ---------------------------
universe@340 19 [String](#string) [Buffer](#buffer)
universe@340 20 [Allocator](#allocator) [Stack](#stack) [Memory&nbsp;Pool](#memory-pool)
universe@340 21 [Array](#array) [List](#list) [Map](#map) [AVL&nbsp;Tree](#avl-tree)
universe@340 22 [Logging](#logging) [Testing](#testing) [Utilities](#utilities) [Properties](#properties)
universe@340 23 ----------------------- ---------------------- -------------------------------- ---------------------------
universe@267 24
universe@267 25 </div>
universe@267 26
universe@259 27 ## Allocator
universe@259 28
universe@390 29 *Header file:* [allocator.h](api-2.1/allocator_8h.html)
universe@259 30 *Required modules:* None.
universe@259 31
universe@259 32 A UCX allocator consists of a pointer to the memory area / pool and four
universe@259 33 function pointers to memory management functions operating on this memory
universe@259 34 area / pool. These functions shall behave equivalent to the standard libc
universe@259 35 functions `malloc`, `calloc`, `realloc` and `free`.
universe@259 36
universe@259 37 The signature of the memory management functions is based on the signature
universe@259 38 of the respective libc function but each of them takes the pointer to the
universe@259 39 memory area / pool as first argument.
universe@259 40
universe@259 41 As the pointer to the memory area / pool can be arbitrarily chosen, any data
universe@259 42 can be provided to the memory management functions. One example is the
universe@280 43 [UCX Memory Pool](#memory-pool).
universe@259 44
universe@340 45 ## Array
universe@340 46
universe@390 47 *Header file:* [array.h](api-2.1/array_8h.html)
universe@340 48 *Required modules:* [Allocator](#allocator)
universe@340 49
universe@340 50 The UCX Array is an implementation of a dynamic array with automatic
universe@340 51 reallocation. The array structure contains a capacity, the current size,
universe@340 52 the size of each element, the raw pointer to the memory area and an allocator.
universe@359 53 Arrays are in most cases much faster than linked list.
universe@359 54 One can decide, whether to create a new array on the heap with `ucx_array_new()`
universe@359 55 or to save one indirection by initializing a `UcxArray` structure on the stack
universe@359 56 with `ucx_array_init()`.
universe@340 57
universe@340 58 ### Remove duplicates from an array of strings
universe@340 59
universe@340 60 The following example shows, how a `UcxArray` can be built with
universe@340 61 a standard dynamic C array (pointer+length) as basis.
universe@340 62
universe@340 63 ```C
universe@370 64 UcxArray* create_unique(sstr_t* array, size_t arrlen) {
universe@340 65 // worst case is no duplicates, hence the capacity is set to arrlen
universe@370 66 UcxArray* result = ucx_array_new(arrlen, sizeof(sstr_t));
universe@340 67 // only append elements, if they are not already present in the array
universe@340 68 for (size_t i = 0 ; i < arrlen ; ++i) {
universe@340 69 if (!ucx_array_contains(result, array+i, ucx_cmp_sstr, NULL)) {
universe@370 70 ucx_array_append_from(result, array+i, 1);
universe@340 71 }
universe@340 72 }
universe@340 73 // make the array as small as possible
universe@370 74 ucx_array_shrink(result);
universe@340 75 return result;
universe@340 76 }
universe@340 77
universe@628 78 // ...
universe@340 79
universe@628 80 sstr_t* array = // some standard array of strings
universe@628 81 size_t arrlen = // the length of the array
universe@340 82
universe@370 83 UcxArray* result = create_unique(array,arrlen);
universe@340 84
universe@628 85 // Iterate over the array and print the elements
universe@370 86 sstr_t* unique = result->data;
universe@370 87 for (size_t i = 0 ; i < result->size ; i++) {
universe@370 88 printf("%" PRIsstr "\n", SFMT(unique[i]));
universe@340 89 }
universe@340 90
universe@628 91 // Free the array.
universe@370 92 ucx_array_free(result);
universe@370 93 ```
universe@370 94 ### Preventing out of bounds writes
universe@370 95
universe@370 96 The functions `ucx_array_reserve()`, `ucx_array_resize()`, `ucx_array_grow()`,
universe@370 97 and `ucx_array_shrink()` allow easy management of the array capacity.
universe@370 98 Imagine you want to add `n` elements to an array. If your `n` elements are
universe@370 99 already somewhere else consecutively in memory, you can use
universe@370 100 `ucx_array_append_from()` and benefit from the autogrow facility in this family
universe@370 101 of functions. Otherwise, you can ask the array to have enough capacity for
universe@370 102 holding additional `n` elements.
universe@370 103
universe@370 104 ```C
universe@370 105 size_t n = // ... elements to add
universe@370 106 if (ucx_array_grow(array, n)) {
universe@370 107 fprintf(stderr, "Cannot add %zu elements to the array.\n", n);
universe@370 108 return 1;
universe@370 109 }
universe@370 110 for (size_t i = 0 ; i < n ; i++) {
universe@370 111 ((int*)array->data)[array->size++] = 80;
universe@370 112 }
universe@340 113 ```
universe@340 114
universe@259 115 ## AVL Tree
universe@259 116
universe@390 117 *Header file:* [avl.h](api-2.1/avl_8h.html)
universe@259 118 *Required modules:* [Allocator](#allocator)
universe@259 119
universe@259 120 This binary search tree implementation allows average O(1) insertion and
universe@259 121 removal of elements (excluding binary search time).
universe@259 122 All common binary tree operations are implemented. Furthermore, this module
universe@259 123 provides search functions via lower and upper bounds.
universe@259 124
universe@287 125 ### Filtering items with a time window
universe@287 126
universe@287 127 Suppose you have a list of items which contain a `time_t` value and your task
universe@287 128 is to find all items within a time window `[t_start, t_end]`.
universe@287 129 With AVL Trees this is easy:
universe@287 130 ```C
universe@628 131 // Somewhere in a header
universe@287 132 typedef struct {
universe@287 133 time_t ts;
universe@628 134 // other important data
universe@287 135 } MyObject;
universe@287 136
universe@628 137 // Source code
universe@628 138 UcxAVLTree* tree = ucx_avl_new(ucx_cmp_longint);
universe@628 139 // ... populate tree with objects, use '& MyObject.ts' as key ...
universe@287 140
universe@287 141
universe@628 142 // Now find every item, with 30 <= ts <= 70
universe@287 143 time_t ts_start = 30;
universe@287 144 time_t ts_end = 70;
universe@287 145
universe@287 146 printf("Values in range:\n");
universe@287 147 for (
universe@287 148 UcxAVLNode* node = ucx_avl_find_node(
universe@287 149 tree, (intptr_t) &ts_start,
universe@314 150 ucx_dist_longint, UCX_AVL_FIND_LOWER_BOUNDED);
universe@287 151 node && (*(time_t*)node->key) <= ts_end;
universe@287 152 node = ucx_avl_succ(node)
universe@287 153 ) {
universe@287 154 printf(" ts: %ld\n", ((MyObject*)node->value)->ts);
universe@287 155 }
universe@287 156
universe@287 157 ucx_avl_free_content(tree, free);
universe@287 158 ucx_avl_free(tree);
universe@287 159 ```
universe@287 160
universe@259 161 ## Buffer
universe@259 162
universe@390 163 *Header file:* [buffer.h](api-2.1/buffer_8h.html)
universe@259 164 *Required modules:* None.
universe@259 165
universe@259 166 Instances of this buffer implementation can be used to read from or to write to
universe@259 167 memory like you would do with a stream. This allows the use of
universe@282 168 `ucx_stream_copy()` from the [Utilities](#utilities) module to copy contents
universe@282 169 from one buffer to another or from file or network streams to the buffer and
universe@259 170 vice-versa.
universe@259 171
universe@259 172 More features for convenient use of the buffer can be enabled, like automatic
universe@259 173 memory management and automatic resizing of the buffer space.
universe@259 174 See the documentation of the macro constants in the header file for more
universe@259 175 information.
universe@259 176
universe@290 177 ### Add line numbers to a file
universe@290 178
universe@290 179 When reading a file line by line, you have three options: first, you could limit
universe@290 180 the maximum supported line length.
universe@290 181 Second, you allocate a god buffer large
universe@290 182 enough for the most lines a text file could have.
universe@290 183 And third, undoubtedly the best option, you start with a small buffer, which
universe@290 184 adjusts on demand.
universe@290 185 An `UcxBuffer` can be created to do just that for you.
universe@290 186 Just pass the `UCX_BUFFER_AUTOEXTEND` option to the initialization function.
universe@290 187 Here is a full working program, which adds line numbers to a file.
universe@290 188 ```C
universe@290 189 #include <stdio.h>
universe@290 190 #include <ucx/buffer.h>
universe@290 191 #include <ucx/utils.h>
universe@290 192
universe@290 193 int main(int argc, char** argv) {
universe@290 194
universe@290 195 if (argc != 2) {
universe@290 196 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
universe@290 197 return 1;
universe@290 198 }
universe@290 199
universe@290 200 FILE* input = fopen(argv[1], "r");
universe@290 201 if (!input) {
universe@290 202 perror("Canno read input");
universe@290 203 return 1;
universe@290 204 }
universe@290 205
universe@290 206 const size_t chunksize = 256;
universe@290 207
universe@290 208 UcxBuffer* linebuf =
universe@290 209 ucx_buffer_new(
universe@628 210 NULL, // the buffer should manage the memory area for us
universe@628 211 2*chunksize, // initial size should be twice the chunk size
universe@628 212 UCX_BUFFER_AUTOEXTEND); // the buffer will grow when necessary
universe@290 213
universe@290 214 size_t lineno = 1;
universe@290 215 do {
universe@628 216 // read line chunk
universe@290 217 size_t read = ucx_stream_ncopy(
universe@290 218 input, linebuf, fread, ucx_buffer_write, chunksize);
universe@290 219 if (read == 0) break;
universe@290 220
universe@628 221 // handle line endings
universe@290 222 do {
universe@290 223 sstr_t bufstr = ucx_buffer_to_sstr(linebuf);
universe@290 224 sstr_t nl = sstrchr(bufstr, '\n');
universe@290 225 if (nl.length == 0) break;
universe@290 226
universe@290 227 size_t linelen = bufstr.length - nl.length;
universe@290 228 sstr_t linestr = sstrsubsl(bufstr, 0, linelen);
universe@290 229
universe@290 230 printf("%zu: %" PRIsstr "\n", lineno++, SFMT(linestr));
universe@290 231
universe@628 232 // shift the buffer to the next line
universe@290 233 ucx_buffer_shift_left(linebuf, linelen+1);
universe@290 234 } while(1);
universe@290 235
universe@290 236 } while(1);
universe@290 237
universe@628 238 // print the 'noeol' line, if any
universe@290 239 sstr_t lastline = ucx_buffer_to_sstr(linebuf);
universe@290 240 if (lastline.length > 0) {
universe@290 241 printf("%zu: %" PRIsstr, lineno, SFMT(lastline));
universe@290 242 }
universe@290 243
universe@290 244 fclose(input);
universe@290 245 ucx_buffer_free(linebuf);
universe@290 246
universe@290 247 return 0;
universe@290 248 }
universe@290 249 ```
universe@290 250
universe@259 251 ## List
universe@259 252
universe@390 253 *Header file:* [list.h](api-2.1/list_8h.html)
universe@259 254 *Required modules:* [Allocator](#allocator)
universe@259 255
universe@259 256 This module provides the data structure and several functions for a doubly
universe@259 257 linked list. Among the common operations like insert, remove, search and sort,
universe@259 258 we allow convenient iteration via a special `UCX_FOREACH` macro.
universe@259 259
universe@294 260 ### Remove duplicates from an array of strings
universe@294 261
universe@294 262 Assume you are given an array of `sstr_t` and want to create a list of these
universe@294 263 strings without duplicates.
universe@340 264 This is a similar example to the one [above](#array), but here we are
universe@340 265 using a `UcxList`.
universe@294 266 ```C
universe@294 267 #include <stdio.h>
universe@294 268 #include <ucx/list.h>
universe@294 269 #include <ucx/string.h>
universe@294 270 #include <ucx/utils.h>
universe@294 271
universe@294 272 UcxList* remove_duplicates(sstr_t* array, size_t arrlen) {
universe@294 273 UcxList* list = NULL;
universe@294 274 for (size_t i = 0 ; i < arrlen ; ++i) {
universe@310 275 if (ucx_list_find(list, array+i, ucx_cmp_sstr, NULL) == -1) {
universe@294 276 sstr_t* s = malloc(sizeof(sstr_t));
universe@294 277 *s = sstrdup(array[i]);
universe@294 278 list = ucx_list_append(list, s);
universe@294 279 }
universe@294 280 }
universe@294 281 return list;
universe@294 282 }
universe@294 283
universe@628 284 // we will need this function to clean up the list contents later
universe@294 285 void free_sstr(void* ptr) {
universe@294 286 sstr_t* s = ptr;
universe@294 287 free(s->ptr);
universe@294 288 free(s);
universe@294 289 }
universe@294 290
universe@628 291 // ...
universe@294 292
universe@628 293 sstr_t* array = // some array of strings
universe@628 294 size_t arrlen = // the length of the array
universe@294 295
universe@294 296 UcxList* list = remove_duplicates(array,arrlen);
universe@294 297
universe@628 298 // Iterate over the list and print the elements
universe@294 299 UCX_FOREACH(elem, list) {
universe@294 300 sstr_t s = *((sstr_t*)elem->data);
universe@294 301 printf("%" PRIsstr "\n", SFMT(s));
universe@294 302 }
universe@294 303
universe@628 304 // Use our free function to free the duplicated strings.
universe@294 305 ucx_list_free_content(list, free_sstr);
universe@294 306 ucx_list_free(list);
universe@294 307 ```
universe@294 308
universe@259 309 ## Logging
universe@259 310
universe@390 311 *Header file:* [logging.h](api-2.1/logging_8h.html)
universe@259 312 *Required modules:* [Map](#map), [String](#string)
universe@259 313
universe@259 314 The logging module comes with some predefined log levels and allows some more
universe@259 315 customization. You may choose if you want to get timestamps or source file and
universe@259 316 line number logged automatically when outputting a message.
universe@295 317 The following function call initializes a debug logger with all of the above
universe@295 318 information:
universe@295 319 ```C
universe@295 320 log = ucx_logger_new(stdout, UCX_LOGGER_DEBUG,
universe@295 321 UCX_LOGGER_LEVEL | UCX_LOGGER_TIMESTAMP | UCX_LOGGER_SOURCE);
universe@295 322 ```
universe@295 323 Afterwards you can use this logger with the predefined macros
universe@295 324 ```C
universe@295 325 ucx_logger_trace(log, "Verbose output");
universe@295 326 ucx_logger_debug(log, "Debug message");
universe@295 327 ucx_logger_info(log, "Information");
universe@295 328 ucx_logger_warn(log, "Warning");
universe@295 329 ucx_logger_error(log, "Error message");
universe@295 330 ```
universe@295 331 or you use
universe@295 332 ```C
universe@295 333 ucx_logger_log(log, CUSTOM_LEVEL, "Some message")
universe@295 334 ```
universe@295 335 When you use your custom log level, don't forget to register it with
universe@295 336 ```C
universe@295 337 ucx_logger_register_level(log, CUSTOM_LEVEL, "CUSTOM")
universe@295 338 ```
universe@295 339 where the last argument must be a string literal.
universe@259 340
universe@259 341 ## Map
universe@259 342
universe@390 343 *Header file:* [map.h](api-2.1/map_8h.html)
universe@259 344 *Required modules:* [Allocator](#allocator), [String](#string)
universe@259 345
universe@259 346 This module provides a hash map implementation using murmur hash 2 and separate
universe@259 347 chaining with linked lists. Similarly to the list module, we provide a
universe@259 348 `UCX_MAP_FOREACH` macro to conveniently iterate through the key/value pairs.
universe@259 349
universe@298 350 ### Parsing command line options
universe@298 351
universe@298 352 Assume you want to parse command line options and record them within a map.
universe@298 353 One way to do this is shown by the following code sample:
universe@298 354 ```C
universe@298 355 UcxMap* options = ucx_map_new(16);
universe@298 356 const char *NOARG = "";
universe@298 357
universe@298 358 char *option = NULL;
universe@298 359 char optchar = 0;
universe@298 360 for(int i=1;i<argc;i++) {
universe@298 361 char *arg = argv[i];
universe@298 362 size_t len = strlen(arg);
universe@298 363 if(len > 1 && arg[0] == '-') {
universe@298 364 for(int c=1;c<len;c++) {
universe@299 365 if(option) {
universe@299 366 fprintf(stderr,
universe@299 367 "Missing argument for option -%c\n", optchar);
universe@299 368 return 1;
universe@299 369 }
universe@298 370 switch(arg[c]) {
universe@298 371 default: {
universe@298 372 fprintf(stderr, "Unknown option -%c\n\n", arg[c]);
universe@298 373 return 1;
universe@298 374 }
universe@298 375 case 'v': {
universe@298 376 ucx_map_cstr_put(options, "verbose", NOARG);
universe@298 377 break;
universe@298 378 }
universe@298 379 case 'o': {
universe@298 380 option = "output";
universe@298 381 optchar = 'o';
universe@298 382 break;
universe@298 383 }
universe@298 384 }
universe@298 385 }
universe@298 386 } else if(option) {
universe@298 387 ucx_map_cstr_put(options, option, arg);
universe@298 388 option = NULL;
universe@298 389 } else {
universe@628 390 // ... handle argument that is not an option ...
universe@298 391 }
universe@298 392 }
universe@298 393 if(option) {
universe@298 394 fprintf(stderr,
universe@298 395 "Missing argument for option -%c\n", optchar);
universe@298 396 return 1;
universe@298 397 }
universe@298 398 ```
universe@298 399 With the following loop, you can access the previously recorded options:
universe@298 400 ```C
universe@298 401 UcxMapIterator iter = ucx_map_iterator(options);
universe@298 402 char *arg;
universe@298 403 UCX_MAP_FOREACH(optkey, arg, iter) {
universe@298 404 char* opt = optkey.data;
universe@298 405 if (*arg) {
universe@298 406 printf("%s = %s\n", opt, arg);
universe@298 407 } else {
universe@298 408 printf("%s active\n", opt);
universe@298 409 }
universe@298 410 }
universe@298 411 ```
universe@298 412 Don't forget to call `ucx_map_free()`, when you are done with the map.
universe@298 413
universe@259 414 ## Memory Pool
universe@259 415
universe@390 416 *Header file:* [mempool.h](api-2.1/mempool_8h.html)
universe@259 417 *Required modules:* [Allocator](#allocator)
universe@259 418
universe@259 419 Here we have a concrete allocator implementation in the sense of a memory pool.
universe@259 420 This pool allows you to register destructor functions for the allocated memory,
universe@259 421 which are automatically called on the destruction of the pool.
universe@259 422 But you may also register *independent* destructor functions within a pool in
universe@302 423 case some external library allocated memory for you, which should be
universe@259 424 destroyed together with this pool.
universe@259 425
universe@302 426 Many UCX modules support the use of an allocator.
universe@302 427 The [String Module](#string), for instance, provides the `sstrdup_a()` function,
universe@302 428 which uses the specified allocator to allocate the memory for the duplicated
universe@302 429 string.
universe@302 430 This way, you can use a `UcxMempool` to keep track of the memory occupied by
universe@302 431 duplicated strings and cleanup everything with just a single call to
universe@302 432 `ucx_mempool_destroy()`.
universe@302 433
universe@302 434 ### Read CSV data into a structure
universe@302 435
universe@302 436 The following code example shows some of the basic memory pool functions and
universe@302 437 how they can be used with other UCX modules.
universe@302 438 ```C
universe@302 439 #include <stdio.h>
universe@302 440 #include <ucx/mempool.h>
universe@302 441 #include <ucx/list.h>
universe@302 442 #include <ucx/string.h>
universe@302 443 #include <ucx/buffer.h>
universe@302 444 #include <ucx/utils.h>
universe@302 445
universe@302 446 typedef struct {
universe@302 447 sstr_t column_a;
universe@302 448 sstr_t column_b;
universe@302 449 sstr_t column_c;
universe@302 450 } CSVData;
universe@302 451
universe@302 452 int main(int argc, char** argv) {
universe@302 453
universe@302 454 UcxMempool* pool = ucx_mempool_new(128);
universe@302 455
universe@302 456 FILE *f = fopen("test.csv", "r");
universe@302 457 if (!f) {
universe@302 458 perror("Cannot open file");
universe@302 459 return 1;
universe@302 460 }
universe@628 461 // close the file automatically at pool destruction
universe@302 462 ucx_mempool_reg_destr(pool, f, (ucx_destructor) fclose);
universe@302 463
universe@628 464 // create a buffer and register it at the memory pool for destruction
universe@302 465 UcxBuffer* content = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
universe@302 466 ucx_mempool_reg_destr(pool, content, (ucx_destructor) ucx_buffer_free);
universe@302 467
universe@628 468 // read the file and split it by lines first
universe@302 469 ucx_stream_copy(f, content, fread, ucx_buffer_write);
universe@302 470 sstr_t contentstr = ucx_buffer_to_sstr(content);
universe@302 471 ssize_t lc = 0;
universe@302 472 sstr_t* lines = sstrsplit_a(pool->allocator, contentstr, S("\n"), &lc);
universe@302 473
universe@628 474 // skip the header and parse the remaining data
universe@302 475 UcxList* datalist = NULL;
universe@302 476 for (size_t i = 1 ; i < lc ; i++) {
universe@302 477 if (lines[i].length == 0) continue;
universe@302 478 ssize_t fc = 3;
universe@302 479 sstr_t* fields = sstrsplit_a(pool->allocator, lines[i], S(";"), &fc);
universe@302 480 if (fc != 3) {
universe@302 481 fprintf(stderr, "Syntax error in line %zu.\n", i);
universe@302 482 ucx_mempool_destroy(pool);
universe@302 483 return 1;
universe@302 484 }
universe@302 485 CSVData* data = ucx_mempool_malloc(pool, sizeof(CSVData));
universe@302 486 data->column_a = fields[0];
universe@302 487 data->column_b = fields[1];
universe@302 488 data->column_c = fields[2];
universe@302 489 datalist = ucx_list_append_a(pool->allocator, datalist, data);
universe@302 490 }
universe@302 491
universe@628 492 // control output
universe@302 493 UCX_FOREACH(elem, datalist) {
universe@302 494 CSVData* data = elem->data;
universe@302 495 printf("Column A: %" PRIsstr " | "
universe@302 496 "Column B: %" PRIsstr " | "
universe@302 497 "Column C: %" PRIsstr "\n",
universe@302 498 SFMT(data->column_a), SFMT(data->column_b), SFMT(data->column_c)
universe@302 499 );
universe@302 500 }
universe@302 501
universe@628 502 // cleanup everything, no manual free() needed
universe@302 503 ucx_mempool_destroy(pool);
universe@302 504
universe@302 505 return 0;
universe@302 506 }
universe@302 507 ```
universe@302 508
universe@302 509 ### Overriding the default destructor
universe@302 510
universe@302 511 Sometimes you need to allocate memory with `ucx_mempool_malloc()`, but the
universe@302 512 memory is not supposed to be freed with a simple call to `free()`.
universe@302 513 In this case, you can overwrite the default destructor as follows:
universe@302 514 ```C
universe@302 515 MyObject* obj = ucx_mempool_malloc(pool, sizeof(MyObject));
universe@302 516
universe@628 517 // some special initialization with own resource management
universe@302 518 my_object_init(obj);
universe@302 519
universe@628 520 // register destructor function
universe@302 521 ucx_mempool_set_destr(obj, (ucx_destructor) my_object_destroy);
universe@302 522 ```
universe@304 523 Be aware, that your destructor function should not free any memory, that is
universe@302 524 also managed by the pool.
universe@717 525 Otherwise, you might be risking a double-free.
universe@326 526 More precisely, a destructor function set with `ucx_mempool_set_destr()` MUST
universe@717 527 NOT call `free()` on the specified pointer whereas a destructor function
universe@326 528 registered with `ucx_mempool_reg_destr()` MAY (and in most cases will) call
universe@326 529 `free()`.
universe@302 530
universe@259 531 ## Properties
universe@259 532
universe@390 533 *Header file:* [properties.h](api-2.1/properties_8h.html)
universe@259 534 *Required modules:* [Map](#map)
universe@259 535
universe@259 536 This module provides load and store function for `*.properties` files.
universe@259 537 The key/value pairs are stored within an UCX Map.
universe@259 538
universe@277 539 ### Example: Loading properties from a file
universe@277 540
universe@277 541 ```C
universe@628 542 // Open the file as usual
universe@277 543 FILE* file = fopen("myprops.properties", "r");
universe@277 544 if (!file) {
universe@277 545 // error handling
universe@277 546 return 1;
universe@277 547 }
universe@277 548
universe@628 549 // Load the properties from the file
universe@277 550 UcxMap* myprops = ucx_map_new(16);
universe@277 551 if (ucx_properties_load(myprops, file)) {
universe@628 552 // ... error handling ...
universe@277 553 fclose(file);
universe@277 554 ucx_map_free(myprops);
universe@277 555 return 1;
universe@277 556 }
universe@277 557
universe@628 558 // Print out the key/value pairs
universe@277 559 char* propval;
universe@277 560 UcxMapIterator propiter = ucx_map_iterator(myprops);
universe@277 561 UCX_MAP_FOREACH(key, propval, propiter) {
universe@277 562 printf("%s = %s\n", (char*)key.data, propval);
universe@277 563 }
universe@277 564
universe@628 565 // Don't forget to free the values before freeing the map
universe@277 566 ucx_map_free_content(myprops, NULL);
universe@277 567 ucx_map_free(myprops);
universe@277 568 fclose(file);
universe@277 569 ```
universe@295 570
universe@259 571 ## Stack
universe@259 572
universe@390 573 *Header file:* [stack.h](api-2.1/stack_8h.html)
universe@259 574 *Required modules:* [Allocator](#allocator)
universe@259 575
universe@259 576 This concrete implementation of an UCX Allocator allows you to grab some amount
universe@259 577 of memory which is then handled as a stack.
universe@259 578 Please note, that the term *stack* only refers to the behavior of this
universe@301 579 allocator. You may still choose to use either stack or heap memory
universe@259 580 for the underlying space.
universe@259 581 A typical use case is an algorithm where you need to allocate and free large
universe@259 582 amounts of memory very frequently.
universe@259 583
universe@301 584 The following code sample shows how to initialize a stack and push and pop
universe@301 585 simple data.
universe@301 586 ```C
universe@301 587 const size_t len = 1024;
universe@301 588 char space[len];
universe@301 589 UcxStack stack;
universe@301 590 ucx_stack_init(&stack, space, len);
universe@301 591
universe@301 592 int i = 42;
universe@301 593 float f = 3.14f;
universe@301 594 const char* str = "Hello!";
universe@301 595 size_t strn = 7;
universe@301 596
universe@628 597 // push the integer
universe@301 598 ucx_stack_push(&stack, sizeof(int), &i);
universe@301 599
universe@628 600 // push the float and rember the address
universe@301 601 float* remember = ucx_stack_push(&stack, sizeof(float), &f);
universe@301 602
universe@628 603 // push the string with zero terminator
universe@301 604 ucx_stack_push(&stack, strn, str);
universe@301 605
universe@628 606 // if we forget, how big an element was, we can ask the stack
universe@301 607 printf("Length of string: %zu\n", ucx_stack_topsize(&stack)-1);
universe@301 608
universe@628 609 // retrieve the string as sstr_t, without zero terminator!
universe@301 610 sstr_t s;
universe@301 611 s.length = ucx_stack_topsize(&stack)-1;
universe@301 612 s.ptr = malloc(s.length);
universe@301 613 ucx_stack_popn(&stack, s.ptr, s.length);
universe@301 614 printf("%" PRIsstr "\n", SFMT(s));
universe@301 615
universe@628 616 // print the float directly from the stack and free it
universe@301 617 printf("Float: %f\n", *remember);
universe@301 618 ucx_stack_free(&stack, remember);
universe@301 619
universe@628 620 // the last element is the integer
universe@301 621 int j;
universe@301 622 ucx_stack_pop(&stack, &j);
universe@301 623 printf("Integer: %d\n", j);
universe@301 624 ```
universe@301 625
universe@301 626
universe@301 627
universe@259 628 ## String
universe@259 629
universe@390 630 *Header file:* [string.h](api-2.1/string_8h.html)
universe@259 631 *Required modules:* [Allocator](#allocator)
universe@259 632
universe@259 633 This module provides a safe implementation of bounded string.
universe@259 634 Usually C strings do not carry a length. While for zero-terminated strings you
universe@259 635 can easily get the length with `strlen`, this is not generally possible for
universe@259 636 arbitrary strings.
universe@259 637 The `sstr_t` type of this module always carries the string and its length to
universe@259 638 reduce the risk of buffer overflows dramatically.
universe@259 639
universe@267 640 ### Initialization
universe@267 641
universe@267 642 There are several ways to create an `sstr_t`:
universe@267 643
universe@267 644 ```C
universe@628 645 // (1) sstr() uses strlen() internally, hence cstr MUST be zero-terminated
universe@267 646 sstr_t a = sstr(cstr);
universe@267 647
universe@628 648 // (2) cstr does not need to be zero-terminated, if length is specified
universe@267 649 sstr_t b = sstrn(cstr, len);
universe@267 650
universe@628 651 // (3) S() macro creates sstr_t from a string using sizeof() and using sstrn().
universe@628 652 // This version is especially useful for function arguments
universe@267 653 sstr_t c = S("hello");
universe@267 654
universe@628 655 // (4) SC() macro works like S(), but makes the string immutable using scstr_t.
universe@628 656 // (available since UCX 2.0)
universe@325 657 scstr_t d = SC("hello");
universe@325 658
universe@628 659 // (5) ST() macro creates sstr_t struct literal using sizeof()
universe@325 660 sstr_t e = ST("hello");
universe@267 661 ```
universe@267 662
universe@325 663 You should not use the `S()`, `SC()`, or `ST()` macro with string of unknown
universe@325 664 origin, since the `sizeof()` call might not coincide with the string length in
universe@325 665 those cases. If you know what you are doing, it can save you some performance,
universe@267 666 because you do not need the `strlen()` call.
universe@267 667
universe@321 668 ### Handling immutable strings
universe@321 669
universe@321 670 *(Since: UCX 2.0)*
universe@321 671
universe@321 672 For immutable strings (i.e. `const char*` strings), UCX provides the `scstr_t`
universe@321 673 type, which works exactly as the `sstr_t` type but with a pointer
universe@321 674 to `const char`. All UCX string functions come in two flavors: one that enforces
universe@321 675 the `scstr_t` type, and another that usually accepts both types and performs
universe@321 676 a conversion automatically, if necessary.
universe@321 677
universe@321 678 There are some exceptions to this rule, as the return type may depend on the
universe@321 679 argument type.
universe@321 680 E.g. the `sstrchr()` function returns a substring starting at
universe@321 681 the first occurrence of the specified character.
universe@321 682 Since this substring points to the memory of the argument string, it does not
universe@321 683 accept `scstr_t` as input argument, because the return type would break the
universe@717 684 const-ness.
universe@321 685
universe@321 686
universe@267 687 ### Finding the position of a substring
universe@267 688
universe@267 689 The `sstrstr()` function gives you a new `sstr_t` object starting with the
universe@267 690 requested substring. Thus determining the position comes down to a simple
universe@267 691 subtraction.
universe@267 692
universe@267 693 ```C
universe@267 694 sstr_t haystack = ST("Here we go!");
universe@267 695 sstr_t needle = ST("we");
universe@267 696 sstr_t result = sstrstr(haystack, needle);
universe@267 697 if (result.ptr)
universe@267 698 printf("Found at position %zd.\n", haystack.length-result.length);
universe@267 699 else
universe@267 700 printf("Not found.\n");
universe@267 701 ```
universe@267 702
universe@267 703 ### Spliting a string by a delimiter
universe@267 704
universe@267 705 The `sstrsplit()` function (and its allocator based version `sstrsplit_a()`) is
universe@267 706 very powerful and might look a bit nasty at a first glance. But it is indeed
universe@267 707 very simple to use. It is even more convenient in combination with a memory
universe@267 708 pool.
universe@267 709
universe@267 710 ```C
universe@267 711 sstr_t test = ST("here::are::some::strings");
universe@267 712 sstr_t delim = ST("::");
universe@267 713
universe@628 714 ssize_t count = 0; // no limit
universe@267 715 UcxMempool* pool = ucx_mempool_new_default();
universe@267 716
universe@267 717 sstr_t* result = sstrsplit_a(pool->allocator, test, delim, &count);
universe@267 718 for (ssize_t i = 0 ; i < count ; i++) {
universe@628 719 // don't forget to specify the length via the %*s format specifier
universe@267 720 printf("%*s\n", result[i].length, result[i].ptr);
universe@267 721 }
universe@267 722
universe@267 723 ucx_mempool_destroy(pool);
universe@267 724 ```
universe@267 725 The output is:
universe@267 726
universe@267 727 here
universe@267 728 are
universe@267 729 some
universe@267 730 strings
universe@267 731
universe@267 732 The memory pool ensures, that all strings are freed.
universe@267 733
universe@325 734 ### Disabling convenience macros
universe@325 735
universe@325 736 If you are experiencing any troubles with the short convenience macros `S()`,
universe@325 737 `SC()`, or `ST()`, you can disable them by setting the macro
universe@325 738 `UCX_NO_SSTR_SHORTCUTS` before including the header (or via a compiler option).
universe@325 739 For the formatting macros `SFMT()` and `PRIsstr` you can use the macro
universe@325 740 `UCX_NO_SSTR_FORMAT_MACROS` to disable them.
universe@325 741
universe@325 742 Please keep in mind, that after disabling the macros, you cannot use them in
universe@325 743 your code *and* foreign code that you might have included.
universe@325 744 You should only disable the macros, if you are experiencing a nasty name clash
universe@325 745 which cannot be otherwise resolved.
universe@325 746
universe@259 747 ## Testing
universe@259 748
universe@390 749 *Header file:* [test.h](api-2.1/test_8h.html)
universe@259 750 *Required modules:* None.
universe@259 751
universe@259 752 This module provides a testing framework which allows you to execute test cases
universe@259 753 within test suites.
universe@259 754 To avoid code duplication within tests, we also provide the possibility to
universe@259 755 define test subroutines.
universe@259 756
universe@297 757 You should declare test cases and subroutines in a header file per test unit
universe@297 758 and implement them as you would implement normal functions.
universe@297 759 ```C
universe@628 760 // myunit.h
universe@297 761 UCX_TEST(function_name);
universe@628 762 UCX_TEST_SUBROUTINE(subroutine_name, paramlist); // optional
universe@297 763
universe@297 764
universe@628 765 // myunit.c
universe@297 766 UCX_TEST_SUBROUTINE(subroutine_name, paramlist) {
universe@628 767 // ... reusable tests with UCX_TEST_ASSERT() ...
universe@297 768 }
universe@297 769
universe@297 770 UCX_TEST(function_name) {
universe@628 771 // ... resource allocation and other test preparation ...
universe@297 772
universe@628 773 // mandatory marker for the start of the tests
universe@297 774 UCX_TEST_BEGIN
universe@297 775
universe@628 776 // ... verifications with UCX_TEST_ASSERT() ...
universe@628 777 // (and/or calls with UCX_TEST_CALL_SUBROUTINE())
universe@297 778
universe@628 779 // mandatory marker for the end of the tests
universe@297 780 UCX_TEST_END
universe@297 781
universe@628 782 // ... resource cleanup ...
universe@628 783 // (all code after UCX_TEST_END is always executed)
universe@297 784 }
universe@297 785 ```
universe@297 786 If you want to use the `UCX_TEST_ASSERT()` macro in a function, you are
universe@297 787 *required* to use a `UCX_TEST_SUBROUTINE`.
universe@717 788 Otherwise, the testing framework does not know where to jump, when the assertion
universe@297 789 fails.
universe@297 790
universe@297 791 After implementing the tests, you can easily build a test suite and execute it:
universe@297 792 ```C
universe@297 793 UcxTestSuite* suite = ucx_test_suite_new();
universe@297 794 ucx_test_register(suite, testMyTestCase01);
universe@297 795 ucx_test_register(suite, testMyTestCase02);
universe@628 796 // ...
universe@628 797 ucx_test_run(suite, stdout); // stdout, or any other FILE stream
universe@297 798 ```
universe@297 799
universe@259 800 ## Utilities
universe@259 801
universe@390 802 *Header file:* [utils.h](api-2.1/utils_8h.html)
universe@259 803 *Required modules:* [Allocator](#allocator), [String](#string)
universe@259 804
universe@259 805 In this module we provide very general utility function for copy and compare
universe@259 806 operations.
universe@259 807 We also provide several `printf` variants to conveniently print formatted data
universe@259 808 to streams or strings.
universe@259 809
universe@279 810 ### A simple copy program
universe@279 811
universe@279 812 The utilities package provides several stream copy functions.
universe@279 813 One of them has a very simple interface and can, for instance, be used to copy
universe@279 814 whole files in a single call.
universe@279 815 This is a minimal working example:
universe@279 816 ```C
universe@279 817 #include <stdio.h>
universe@279 818 #include <ucx/utils.h>
universe@279 819
universe@279 820 int main(int argc, char** argv) {
universe@279 821
universe@279 822 if (argc != 3) {
universe@279 823 fprintf(stderr, "Use %s <src> <dest>", argv[0]);
universe@279 824 return 1;
universe@279 825 }
universe@279 826
universe@628 827 FILE *srcf = fopen(argv[1], "r"); // insert error handling on your own
universe@279 828 FILE *destf = fopen(argv[2], "w");
universe@279 829
universe@279 830 size_t n = ucx_stream_copy(srcf, destf, fread, fwrite);
universe@279 831 printf("%zu bytes copied.\n", n);
universe@279 832
universe@279 833 fclose(srcf);
universe@279 834 fclose(destf);
universe@279 835
universe@279 836
universe@279 837 return 0;
universe@279 838 }
universe@279 839 ```
universe@279 840
universe@281 841 ### Automatic allocation for formatted strings
universe@279 842
universe@281 843 The UCX utility function `ucx_asprintf()` and it's convenient shortcut
universe@281 844 `ucx_sprintf` allow easy formatting of strings, without ever having to worry
universe@281 845 about the required space.
universe@281 846 ```C
universe@281 847 sstr_t mystring = ucx_sprintf("The answer is: %d!", 42);
universe@281 848 ```
universe@281 849 Still, you have to pass `mystring.ptr` to `free()` (or the free function of
universe@281 850 your allocator, if you use `ucx_asprintf`).
universe@281 851 If you don't have all the information ready to build your string, you can even
universe@281 852 use a [UcxBuffer](#buffer) as a target with the utility function
universe@281 853 `ucx_bprintf()`.
universe@281 854 ```C
universe@281 855 UcxBuffer* strbuffer = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
universe@281 856
universe@281 857 for (unsigned int i = 2 ; i < 100 ; i++) {
universe@281 858 ucx_bprintf(strbuffer, "Integer %d is %s\n",
universe@281 859 i, prime(i) ? "prime" : "not prime");
universe@281 860 }
universe@281 861
universe@628 862 // print the result to stdout
universe@281 863 printf("%s", (char*)strbuffer->space);
universe@281 864
universe@281 865 ucx_buffer_free(strbuffer);
universe@281 866 ```

mercurial