Wed, 02 May 2018 18:47:22 +0200
removes artificial anchors from 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@259 | 12 | systems include directory. In this case you can use an 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 Tree](#avl-tree) [Buffer](#buffer) [List](#list) |
universe@280 | 20 | [Logging](#logging) [Map](#map) [Memory 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@259 | 54 | ## Buffer |
universe@259 | 55 | |
universe@259 | 56 | *Header file:* [buffer.h](api/buffer_8h.html) |
universe@259 | 57 | *Required modules:* None. |
universe@259 | 58 | |
universe@259 | 59 | Instances of this buffer implementation can be used to read from or to write to |
universe@259 | 60 | memory like you would do with a stream. This allows the use of |
universe@280 | 61 | `ucx_stream_copy` from the [Utilities](#utilities) module to copy contents from one |
universe@259 | 62 | buffer to another or from file or network streams to the buffer and |
universe@259 | 63 | vice-versa. |
universe@259 | 64 | |
universe@259 | 65 | More features for convenient use of the buffer can be enabled, like automatic |
universe@259 | 66 | memory management and automatic resizing of the buffer space. |
universe@259 | 67 | See the documentation of the macro constants in the header file for more |
universe@259 | 68 | information. |
universe@259 | 69 | |
universe@259 | 70 | ## List |
universe@259 | 71 | |
universe@259 | 72 | *Header file:* [list.h](api/list_8h.html) |
universe@259 | 73 | *Required modules:* [Allocator](#allocator) |
universe@259 | 74 | |
universe@259 | 75 | This module provides the data structure and several functions for a doubly |
universe@259 | 76 | linked list. Among the common operations like insert, remove, search and sort, |
universe@259 | 77 | we allow convenient iteration via a special `UCX_FOREACH` macro. |
universe@259 | 78 | |
universe@259 | 79 | ## Logging |
universe@259 | 80 | |
universe@259 | 81 | *Header file:* [logging.h](api/logging_8h.html) |
universe@259 | 82 | *Required modules:* [Map](#map), [String](#string) |
universe@259 | 83 | |
universe@259 | 84 | The logging module comes with some predefined log levels and allows some more |
universe@259 | 85 | customization. You may choose if you want to get timestamps or source file and |
universe@259 | 86 | line number logged automatically when outputting a message. |
universe@259 | 87 | |
universe@259 | 88 | |
universe@259 | 89 | ## Map |
universe@259 | 90 | |
universe@259 | 91 | *Header file:* [map.h](api/map_8h.html) |
universe@259 | 92 | *Required modules:* [Allocator](#allocator), [String](#string) |
universe@259 | 93 | |
universe@259 | 94 | This module provides a hash map implementation using murmur hash 2 and separate |
universe@259 | 95 | chaining with linked lists. Similarly to the list module, we provide a |
universe@259 | 96 | `UCX_MAP_FOREACH` macro to conveniently iterate through the key/value pairs. |
universe@259 | 97 | |
universe@259 | 98 | ## Memory Pool |
universe@259 | 99 | |
universe@259 | 100 | *Header file:* [mempool.h](api/mempool_8h.html) |
universe@259 | 101 | *Required modules:* [Allocator](#allocator) |
universe@259 | 102 | |
universe@259 | 103 | Here we have a concrete allocator implementation in the sense of a memory pool. |
universe@259 | 104 | This pool allows you to register destructor functions for the allocated memory, |
universe@259 | 105 | which are automatically called on the destruction of the pool. |
universe@259 | 106 | But you may also register *independent* destructor functions within a pool in |
universe@259 | 107 | case, some external library allocated memory for you, which you wish to be |
universe@259 | 108 | destroyed together with this pool. |
universe@259 | 109 | |
universe@259 | 110 | ## Properties |
universe@259 | 111 | |
universe@259 | 112 | *Header file:* [properties.h](api/properties_8h.html) |
universe@259 | 113 | *Required modules:* [Map](#map) |
universe@259 | 114 | |
universe@259 | 115 | This module provides load and store function for `*.properties` files. |
universe@259 | 116 | The key/value pairs are stored within an UCX Map. |
universe@259 | 117 | |
universe@277 | 118 | ### Example: Loading properties from a file |
universe@277 | 119 | |
universe@277 | 120 | ```C |
universe@277 | 121 | // Open the file as usual |
universe@277 | 122 | FILE* file = fopen("myprops.properties", "r"); |
universe@277 | 123 | if (!file) { |
universe@277 | 124 | // error handling |
universe@277 | 125 | return 1; |
universe@277 | 126 | } |
universe@277 | 127 | |
universe@277 | 128 | // Load the properties from the file |
universe@277 | 129 | UcxMap* myprops = ucx_map_new(16); |
universe@277 | 130 | if (ucx_properties_load(myprops, file)) { |
universe@277 | 131 | // error handling |
universe@277 | 132 | fclose(file); |
universe@277 | 133 | ucx_map_free(myprops); |
universe@277 | 134 | return 1; |
universe@277 | 135 | } |
universe@277 | 136 | |
universe@277 | 137 | // Print out the key/value pairs |
universe@277 | 138 | char* propval; |
universe@277 | 139 | UcxMapIterator propiter = ucx_map_iterator(myprops); |
universe@277 | 140 | UCX_MAP_FOREACH(key, propval, propiter) { |
universe@277 | 141 | printf("%s = %s\n", (char*)key.data, propval); |
universe@277 | 142 | } |
universe@277 | 143 | |
universe@277 | 144 | // Don't forget to free the values before freeing the map |
universe@277 | 145 | ucx_map_free_content(myprops, NULL); |
universe@277 | 146 | ucx_map_free(myprops); |
universe@277 | 147 | fclose(file); |
universe@277 | 148 | ``` |
universe@259 | 149 | ## Stack |
universe@259 | 150 | |
universe@259 | 151 | *Header file:* [stack.h](api/stack_8h.html) |
universe@259 | 152 | *Required modules:* [Allocator](#allocator) |
universe@259 | 153 | |
universe@259 | 154 | This concrete implementation of an UCX Allocator allows you to grab some amount |
universe@259 | 155 | of memory which is then handled as a stack. |
universe@259 | 156 | Please note, that the term *stack* only refers to the behavior of this |
universe@259 | 157 | allocator. You may still choose if you want to use stack or heap memory |
universe@259 | 158 | for the underlying space. |
universe@259 | 159 | |
universe@259 | 160 | A typical use case is an algorithm where you need to allocate and free large |
universe@259 | 161 | amounts of memory very frequently. |
universe@259 | 162 | |
universe@259 | 163 | ## String |
universe@259 | 164 | |
universe@259 | 165 | *Header file:* [string.h](api/string_8h.html) |
universe@259 | 166 | *Required modules:* [Allocator](#allocator) |
universe@259 | 167 | |
universe@259 | 168 | This module provides a safe implementation of bounded string. |
universe@259 | 169 | Usually C strings do not carry a length. While for zero-terminated strings you |
universe@259 | 170 | can easily get the length with `strlen`, this is not generally possible for |
universe@259 | 171 | arbitrary strings. |
universe@259 | 172 | The `sstr_t` type of this module always carries the string and its length to |
universe@259 | 173 | reduce the risk of buffer overflows dramatically. |
universe@259 | 174 | |
universe@267 | 175 | ### Initialization |
universe@267 | 176 | |
universe@267 | 177 | There are several ways to create an `sstr_t`: |
universe@267 | 178 | |
universe@267 | 179 | ```C |
universe@267 | 180 | /* (1) sstr() uses strlen() internally, hence cstr MUST be zero-terminated */ |
universe@267 | 181 | sstr_t a = sstr(cstr); |
universe@267 | 182 | |
universe@267 | 183 | /* (2) cstr does not need to be zero-terminated, if length is specified */ |
universe@267 | 184 | sstr_t b = sstrn(cstr, len); |
universe@267 | 185 | |
universe@267 | 186 | /* (3) S() macro creates sstr_t from a string using sizeof() and using sstrn(). |
universe@267 | 187 | This version is especially useful for function arguments */ |
universe@267 | 188 | sstr_t c = S("hello"); |
universe@267 | 189 | |
universe@267 | 190 | /* (4) ST() macro creates sstr_t struct literal using sizeof() */ |
universe@267 | 191 | sstr_t d = ST("hello"); |
universe@267 | 192 | ``` |
universe@267 | 193 | |
universe@267 | 194 | You should not use the `S()` or `ST()` macro with string of unknown origin, |
universe@267 | 195 | since the `sizeof()` call might not coincide with the string length in those |
universe@267 | 196 | cases. If you know what you are doing, it can save you some performance, |
universe@267 | 197 | because you do not need the `strlen()` call. |
universe@267 | 198 | |
universe@267 | 199 | ### Finding the position of a substring |
universe@267 | 200 | |
universe@267 | 201 | The `sstrstr()` function gives you a new `sstr_t` object starting with the |
universe@267 | 202 | requested substring. Thus determining the position comes down to a simple |
universe@267 | 203 | subtraction. |
universe@267 | 204 | |
universe@267 | 205 | ```C |
universe@267 | 206 | sstr_t haystack = ST("Here we go!"); |
universe@267 | 207 | sstr_t needle = ST("we"); |
universe@267 | 208 | sstr_t result = sstrstr(haystack, needle); |
universe@267 | 209 | if (result.ptr) |
universe@267 | 210 | printf("Found at position %zd.\n", haystack.length-result.length); |
universe@267 | 211 | else |
universe@267 | 212 | printf("Not found.\n"); |
universe@267 | 213 | ``` |
universe@267 | 214 | |
universe@267 | 215 | ### Spliting a string by a delimiter |
universe@267 | 216 | |
universe@267 | 217 | The `sstrsplit()` function (and its allocator based version `sstrsplit_a()`) is |
universe@267 | 218 | very powerful and might look a bit nasty at a first glance. But it is indeed |
universe@267 | 219 | very simple to use. It is even more convenient in combination with a memory |
universe@267 | 220 | pool. |
universe@267 | 221 | |
universe@267 | 222 | ```C |
universe@267 | 223 | sstr_t test = ST("here::are::some::strings"); |
universe@267 | 224 | sstr_t delim = ST("::"); |
universe@267 | 225 | |
universe@267 | 226 | ssize_t count = 0; /* no limit */ |
universe@267 | 227 | UcxMempool* pool = ucx_mempool_new_default(); |
universe@267 | 228 | |
universe@267 | 229 | sstr_t* result = sstrsplit_a(pool->allocator, test, delim, &count); |
universe@267 | 230 | for (ssize_t i = 0 ; i < count ; i++) { |
universe@267 | 231 | /* don't forget to specify the length via the %*s format specifier */ |
universe@267 | 232 | printf("%*s\n", result[i].length, result[i].ptr); |
universe@267 | 233 | } |
universe@267 | 234 | |
universe@267 | 235 | ucx_mempool_destroy(pool); |
universe@267 | 236 | ``` |
universe@267 | 237 | The output is: |
universe@267 | 238 | |
universe@267 | 239 | here |
universe@267 | 240 | are |
universe@267 | 241 | some |
universe@267 | 242 | strings |
universe@267 | 243 | |
universe@267 | 244 | The memory pool ensures, that all strings are freed. |
universe@267 | 245 | |
universe@259 | 246 | ## Testing |
universe@259 | 247 | |
universe@259 | 248 | *Header file:* [test.h](api/test_8h.html) |
universe@259 | 249 | *Required modules:* None. |
universe@259 | 250 | |
universe@259 | 251 | This module provides a testing framework which allows you to execute test cases |
universe@259 | 252 | within test suites. |
universe@259 | 253 | To avoid code duplication within tests, we also provide the possibility to |
universe@259 | 254 | define test subroutines. |
universe@259 | 255 | |
universe@259 | 256 | ## Utilities |
universe@259 | 257 | |
universe@259 | 258 | *Header file:* [utils.h](api/utils_8h.html) |
universe@259 | 259 | *Required modules:* [Allocator](#allocator), [String](#string) |
universe@259 | 260 | |
universe@259 | 261 | In this module we provide very general utility function for copy and compare |
universe@259 | 262 | operations. |
universe@259 | 263 | We also provide several `printf` variants to conveniently print formatted data |
universe@259 | 264 | to streams or strings. |
universe@259 | 265 | |
universe@279 | 266 | ### A simple copy program |
universe@279 | 267 | |
universe@279 | 268 | The utilities package provides several stream copy functions. |
universe@279 | 269 | One of them has a very simple interface and can, for instance, be used to copy |
universe@279 | 270 | whole files in a single call. |
universe@279 | 271 | This is a minimal working example: |
universe@279 | 272 | ```C |
universe@279 | 273 | #include <stdio.h> |
universe@279 | 274 | #include <ucx/utils.h> |
universe@279 | 275 | |
universe@279 | 276 | int main(int argc, char** argv) { |
universe@279 | 277 | |
universe@279 | 278 | if (argc != 3) { |
universe@279 | 279 | fprintf(stderr, "Use %s <src> <dest>", argv[0]); |
universe@279 | 280 | return 1; |
universe@279 | 281 | } |
universe@279 | 282 | |
universe@279 | 283 | FILE *srcf = fopen(argv[1], "r"); // insert error handling on your own |
universe@279 | 284 | FILE *destf = fopen(argv[2], "w"); |
universe@279 | 285 | |
universe@279 | 286 | size_t n = ucx_stream_copy(srcf, destf, fread, fwrite); |
universe@279 | 287 | printf("%zu bytes copied.\n", n); |
universe@279 | 288 | |
universe@279 | 289 | fclose(srcf); |
universe@279 | 290 | fclose(destf); |
universe@279 | 291 | |
universe@279 | 292 | |
universe@279 | 293 | return 0; |
universe@279 | 294 | } |
universe@279 | 295 | ``` |
universe@279 | 296 | |
universe@279 | 297 |