docs/src/modules.md

Thu, 03 May 2018 10:44:33 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 03 May 2018 10:44:33 +0200
changeset 287
98da78a1e69a
parent 282
39e69d78b01d
child 290
d5d6ab809ad3
permissions
-rw-r--r--

adds ucx_avl_free_content() function and documentation in modules.md

universe@264 1 ---
universe@264 2 title: Modules
universe@264 3 ---
universe@259 4
universe@259 5 UCX provides several modules for data structures and algorithms.
universe@259 6 You may choose to use specific modules by inclueding the corresponding header
universe@259 7 file.
universe@259 8 Please note, that some modules make use of other UCX 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@259 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@267 16 <div id="modules" align="center">
universe@267 17
universe@280 18 ----------------------- ---------------------- ---------------------------- -------------------------
universe@280 19 [Allocator](#allocator) [AVL&nbsp;Tree](#avl-tree) [Buffer](#buffer) [List](#list)
universe@280 20 [Logging](#logging) [Map](#map) [Memory&nbsp;Pool](#memory-pool) [Properties](#properties)
universe@280 21 [Stack](#stack) [String](#string) [Testing](#testing) [Utilities](#utilities)
universe@280 22 ----------------------- ---------------------- ---------------------------- -------------------------
universe@267 23
universe@267 24 </div>
universe@267 25
universe@259 26 ## Allocator
universe@259 27
universe@259 28 *Header file:* [allocator.h](api/allocator_8h.html)
universe@259 29 *Required modules:* None.
universe@259 30
universe@259 31 A UCX allocator consists of a pointer to the memory area / pool and four
universe@259 32 function pointers to memory management functions operating on this memory
universe@259 33 area / pool. These functions shall behave equivalent to the standard libc
universe@259 34 functions `malloc`, `calloc`, `realloc` and `free`.
universe@259 35
universe@259 36 The signature of the memory management functions is based on the signature
universe@259 37 of the respective libc function but each of them takes the pointer to the
universe@259 38 memory area / pool as first argument.
universe@259 39
universe@259 40 As the pointer to the memory area / pool can be arbitrarily chosen, any data
universe@259 41 can be provided to the memory management functions. One example is the
universe@280 42 [UCX Memory Pool](#memory-pool).
universe@259 43
universe@259 44 ## AVL Tree
universe@259 45
universe@259 46 *Header file:* [avl.h](api/avl_8h.html)
universe@259 47 *Required modules:* [Allocator](#allocator)
universe@259 48
universe@259 49 This binary search tree implementation allows average O(1) insertion and
universe@259 50 removal of elements (excluding binary search time).
universe@259 51 All common binary tree operations are implemented. Furthermore, this module
universe@259 52 provides search functions via lower and upper bounds.
universe@259 53
universe@287 54 ### Filtering items with a time window
universe@287 55
universe@287 56 Suppose you have a list of items which contain a `time_t` value and your task
universe@287 57 is to find all items within a time window `[t_start, t_end]`.
universe@287 58 With AVL Trees this is easy:
universe@287 59 ```C
universe@287 60 /* ---------------------
universe@287 61 * Somewhere in a header
universe@287 62 */
universe@287 63 typedef struct {
universe@287 64 time_t ts;
universe@287 65 // other important data
universe@287 66 } MyObject;
universe@287 67
universe@287 68 /* -----------
universe@287 69 * Source code
universe@287 70 */
universe@287 71
universe@287 72 UcxAVLTree* tree = ucx_avl_new(ucx_longintcmp);
universe@287 73 // ... populate tree with objects, use '& MyObject.ts' as key ...
universe@287 74
universe@287 75
universe@287 76 // Now find every item, with 30 <= ts <= 70
universe@287 77 time_t ts_start = 30;
universe@287 78 time_t ts_end = 70;
universe@287 79
universe@287 80 printf("Values in range:\n");
universe@287 81 for (
universe@287 82 UcxAVLNode* node = ucx_avl_find_node(
universe@287 83 tree, (intptr_t) &ts_start,
universe@287 84 ucx_longintdist, UCX_AVL_FIND_LOWER_BOUNDED);
universe@287 85 node && (*(time_t*)node->key) <= ts_end;
universe@287 86 node = ucx_avl_succ(node)
universe@287 87 ) {
universe@287 88 printf(" ts: %ld\n", ((MyObject*)node->value)->ts);
universe@287 89 }
universe@287 90
universe@287 91 ucx_avl_free_content(tree, free);
universe@287 92 ucx_avl_free(tree);
universe@287 93 ```
universe@287 94
universe@259 95 ## Buffer
universe@259 96
universe@259 97 *Header file:* [buffer.h](api/buffer_8h.html)
universe@259 98 *Required modules:* None.
universe@259 99
universe@259 100 Instances of this buffer implementation can be used to read from or to write to
universe@259 101 memory like you would do with a stream. This allows the use of
universe@282 102 `ucx_stream_copy()` from the [Utilities](#utilities) module to copy contents
universe@282 103 from one buffer to another or from file or network streams to the buffer and
universe@259 104 vice-versa.
universe@259 105
universe@259 106 More features for convenient use of the buffer can be enabled, like automatic
universe@259 107 memory management and automatic resizing of the buffer space.
universe@259 108 See the documentation of the macro constants in the header file for more
universe@259 109 information.
universe@259 110
universe@259 111 ## List
universe@259 112
universe@259 113 *Header file:* [list.h](api/list_8h.html)
universe@259 114 *Required modules:* [Allocator](#allocator)
universe@259 115
universe@259 116 This module provides the data structure and several functions for a doubly
universe@259 117 linked list. Among the common operations like insert, remove, search and sort,
universe@259 118 we allow convenient iteration via a special `UCX_FOREACH` macro.
universe@259 119
universe@259 120 ## Logging
universe@259 121
universe@259 122 *Header file:* [logging.h](api/logging_8h.html)
universe@259 123 *Required modules:* [Map](#map), [String](#string)
universe@259 124
universe@259 125 The logging module comes with some predefined log levels and allows some more
universe@259 126 customization. You may choose if you want to get timestamps or source file and
universe@259 127 line number logged automatically when outputting a message.
universe@259 128
universe@259 129
universe@259 130 ## Map
universe@259 131
universe@259 132 *Header file:* [map.h](api/map_8h.html)
universe@259 133 *Required modules:* [Allocator](#allocator), [String](#string)
universe@259 134
universe@259 135 This module provides a hash map implementation using murmur hash 2 and separate
universe@259 136 chaining with linked lists. Similarly to the list module, we provide a
universe@259 137 `UCX_MAP_FOREACH` macro to conveniently iterate through the key/value pairs.
universe@259 138
universe@259 139 ## Memory Pool
universe@259 140
universe@259 141 *Header file:* [mempool.h](api/mempool_8h.html)
universe@259 142 *Required modules:* [Allocator](#allocator)
universe@259 143
universe@259 144 Here we have a concrete allocator implementation in the sense of a memory pool.
universe@259 145 This pool allows you to register destructor functions for the allocated memory,
universe@259 146 which are automatically called on the destruction of the pool.
universe@259 147 But you may also register *independent* destructor functions within a pool in
universe@259 148 case, some external library allocated memory for you, which you wish to be
universe@259 149 destroyed together with this pool.
universe@259 150
universe@259 151 ## Properties
universe@259 152
universe@259 153 *Header file:* [properties.h](api/properties_8h.html)
universe@259 154 *Required modules:* [Map](#map)
universe@259 155
universe@259 156 This module provides load and store function for `*.properties` files.
universe@259 157 The key/value pairs are stored within an UCX Map.
universe@259 158
universe@277 159 ### Example: Loading properties from a file
universe@277 160
universe@277 161 ```C
universe@277 162 // Open the file as usual
universe@277 163 FILE* file = fopen("myprops.properties", "r");
universe@277 164 if (!file) {
universe@277 165 // error handling
universe@277 166 return 1;
universe@277 167 }
universe@277 168
universe@277 169 // Load the properties from the file
universe@277 170 UcxMap* myprops = ucx_map_new(16);
universe@277 171 if (ucx_properties_load(myprops, file)) {
universe@277 172 // error handling
universe@277 173 fclose(file);
universe@277 174 ucx_map_free(myprops);
universe@277 175 return 1;
universe@277 176 }
universe@277 177
universe@277 178 // Print out the key/value pairs
universe@277 179 char* propval;
universe@277 180 UcxMapIterator propiter = ucx_map_iterator(myprops);
universe@277 181 UCX_MAP_FOREACH(key, propval, propiter) {
universe@277 182 printf("%s = %s\n", (char*)key.data, propval);
universe@277 183 }
universe@277 184
universe@277 185 // Don't forget to free the values before freeing the map
universe@277 186 ucx_map_free_content(myprops, NULL);
universe@277 187 ucx_map_free(myprops);
universe@277 188 fclose(file);
universe@277 189 ```
universe@259 190 ## Stack
universe@259 191
universe@259 192 *Header file:* [stack.h](api/stack_8h.html)
universe@259 193 *Required modules:* [Allocator](#allocator)
universe@259 194
universe@259 195 This concrete implementation of an UCX Allocator allows you to grab some amount
universe@259 196 of memory which is then handled as a stack.
universe@259 197 Please note, that the term *stack* only refers to the behavior of this
universe@259 198 allocator. You may still choose if you want to use stack or heap memory
universe@259 199 for the underlying space.
universe@259 200
universe@259 201 A typical use case is an algorithm where you need to allocate and free large
universe@259 202 amounts of memory very frequently.
universe@259 203
universe@259 204 ## String
universe@259 205
universe@259 206 *Header file:* [string.h](api/string_8h.html)
universe@259 207 *Required modules:* [Allocator](#allocator)
universe@259 208
universe@259 209 This module provides a safe implementation of bounded string.
universe@259 210 Usually C strings do not carry a length. While for zero-terminated strings you
universe@259 211 can easily get the length with `strlen`, this is not generally possible for
universe@259 212 arbitrary strings.
universe@259 213 The `sstr_t` type of this module always carries the string and its length to
universe@259 214 reduce the risk of buffer overflows dramatically.
universe@259 215
universe@267 216 ### Initialization
universe@267 217
universe@267 218 There are several ways to create an `sstr_t`:
universe@267 219
universe@267 220 ```C
universe@267 221 /* (1) sstr() uses strlen() internally, hence cstr MUST be zero-terminated */
universe@267 222 sstr_t a = sstr(cstr);
universe@267 223
universe@267 224 /* (2) cstr does not need to be zero-terminated, if length is specified */
universe@267 225 sstr_t b = sstrn(cstr, len);
universe@267 226
universe@267 227 /* (3) S() macro creates sstr_t from a string using sizeof() and using sstrn().
universe@267 228 This version is especially useful for function arguments */
universe@267 229 sstr_t c = S("hello");
universe@267 230
universe@267 231 /* (4) ST() macro creates sstr_t struct literal using sizeof() */
universe@267 232 sstr_t d = ST("hello");
universe@267 233 ```
universe@267 234
universe@267 235 You should not use the `S()` or `ST()` macro with string of unknown origin,
universe@267 236 since the `sizeof()` call might not coincide with the string length in those
universe@267 237 cases. If you know what you are doing, it can save you some performance,
universe@267 238 because you do not need the `strlen()` call.
universe@267 239
universe@267 240 ### Finding the position of a substring
universe@267 241
universe@267 242 The `sstrstr()` function gives you a new `sstr_t` object starting with the
universe@267 243 requested substring. Thus determining the position comes down to a simple
universe@267 244 subtraction.
universe@267 245
universe@267 246 ```C
universe@267 247 sstr_t haystack = ST("Here we go!");
universe@267 248 sstr_t needle = ST("we");
universe@267 249 sstr_t result = sstrstr(haystack, needle);
universe@267 250 if (result.ptr)
universe@267 251 printf("Found at position %zd.\n", haystack.length-result.length);
universe@267 252 else
universe@267 253 printf("Not found.\n");
universe@267 254 ```
universe@267 255
universe@267 256 ### Spliting a string by a delimiter
universe@267 257
universe@267 258 The `sstrsplit()` function (and its allocator based version `sstrsplit_a()`) is
universe@267 259 very powerful and might look a bit nasty at a first glance. But it is indeed
universe@267 260 very simple to use. It is even more convenient in combination with a memory
universe@267 261 pool.
universe@267 262
universe@267 263 ```C
universe@267 264 sstr_t test = ST("here::are::some::strings");
universe@267 265 sstr_t delim = ST("::");
universe@267 266
universe@267 267 ssize_t count = 0; /* no limit */
universe@267 268 UcxMempool* pool = ucx_mempool_new_default();
universe@267 269
universe@267 270 sstr_t* result = sstrsplit_a(pool->allocator, test, delim, &count);
universe@267 271 for (ssize_t i = 0 ; i < count ; i++) {
universe@267 272 /* don't forget to specify the length via the %*s format specifier */
universe@267 273 printf("%*s\n", result[i].length, result[i].ptr);
universe@267 274 }
universe@267 275
universe@267 276 ucx_mempool_destroy(pool);
universe@267 277 ```
universe@267 278 The output is:
universe@267 279
universe@267 280 here
universe@267 281 are
universe@267 282 some
universe@267 283 strings
universe@267 284
universe@267 285 The memory pool ensures, that all strings are freed.
universe@267 286
universe@259 287 ## Testing
universe@259 288
universe@259 289 *Header file:* [test.h](api/test_8h.html)
universe@259 290 *Required modules:* None.
universe@259 291
universe@259 292 This module provides a testing framework which allows you to execute test cases
universe@259 293 within test suites.
universe@259 294 To avoid code duplication within tests, we also provide the possibility to
universe@259 295 define test subroutines.
universe@259 296
universe@259 297 ## Utilities
universe@259 298
universe@259 299 *Header file:* [utils.h](api/utils_8h.html)
universe@259 300 *Required modules:* [Allocator](#allocator), [String](#string)
universe@259 301
universe@259 302 In this module we provide very general utility function for copy and compare
universe@259 303 operations.
universe@259 304 We also provide several `printf` variants to conveniently print formatted data
universe@259 305 to streams or strings.
universe@259 306
universe@279 307 ### A simple copy program
universe@279 308
universe@279 309 The utilities package provides several stream copy functions.
universe@279 310 One of them has a very simple interface and can, for instance, be used to copy
universe@279 311 whole files in a single call.
universe@279 312 This is a minimal working example:
universe@279 313 ```C
universe@279 314 #include <stdio.h>
universe@279 315 #include <ucx/utils.h>
universe@279 316
universe@279 317 int main(int argc, char** argv) {
universe@279 318
universe@279 319 if (argc != 3) {
universe@279 320 fprintf(stderr, "Use %s <src> <dest>", argv[0]);
universe@279 321 return 1;
universe@279 322 }
universe@279 323
universe@279 324 FILE *srcf = fopen(argv[1], "r"); // insert error handling on your own
universe@279 325 FILE *destf = fopen(argv[2], "w");
universe@279 326
universe@279 327 size_t n = ucx_stream_copy(srcf, destf, fread, fwrite);
universe@279 328 printf("%zu bytes copied.\n", n);
universe@279 329
universe@279 330 fclose(srcf);
universe@279 331 fclose(destf);
universe@279 332
universe@279 333
universe@279 334 return 0;
universe@279 335 }
universe@279 336 ```
universe@279 337
universe@281 338 ### Automatic allocation for formatted strings
universe@279 339
universe@281 340 The UCX utility function `ucx_asprintf()` and it's convenient shortcut
universe@281 341 `ucx_sprintf` allow easy formatting of strings, without ever having to worry
universe@281 342 about the required space.
universe@281 343 ```C
universe@281 344 sstr_t mystring = ucx_sprintf("The answer is: %d!", 42);
universe@281 345 ```
universe@281 346 Still, you have to pass `mystring.ptr` to `free()` (or the free function of
universe@281 347 your allocator, if you use `ucx_asprintf`).
universe@281 348 If you don't have all the information ready to build your string, you can even
universe@281 349 use a [UcxBuffer](#buffer) as a target with the utility function
universe@281 350 `ucx_bprintf()`.
universe@281 351 ```C
universe@281 352 UcxBuffer* strbuffer = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
universe@281 353
universe@281 354 for (unsigned int i = 2 ; i < 100 ; i++) {
universe@281 355 ucx_bprintf(strbuffer, "Integer %d is %s\n",
universe@281 356 i, prime(i) ? "prime" : "not prime");
universe@281 357 }
universe@281 358
universe@281 359 // print the result to stdout
universe@281 360 printf("%s", (char*)strbuffer->space);
universe@281 361
universe@281 362 ucx_buffer_free(strbuffer);
universe@281 363 ```

mercurial