| 1 |
|
| #include "symprim/symprim.h" |
| 2 |
|
| |
| 3 |
|
| #include <stdlib.h> |
| 4 |
|
| |
| 5 |
|
| static const char *const messages[SYMPRIM_E_COUNT] = { |
| 6 |
|
| "ok", |
| 7 |
|
| "invalid argument", |
| 8 |
|
| "out of memory", |
| 9 |
|
| "the system gave no random bytes", |
| 10 |
|
| "memory could not be locked", |
| 11 |
|
| "authentication failed" |
| 12 |
|
| }; |
| 13 |
|
| |
| 14 |
32 |
| const char *symprim_error_message(int code) |
| 15 |
|
| { |
| 16 |
32 |
▶ | if (code < 0 || code >= SYMPRIM_E_COUNT) |
Branch: 2 branches
| Branch | True | False | Total |
| if (code < 0 || code >= SYMPRIM_E_COUNT) |
30 |
2 |
32 |
| if (code < 0 || code >= SYMPRIM_E_COUNT) |
3 |
27 |
30 |
|
| 17 |
5 |
| return "unknown error"; |
| 18 |
27 |
| return messages[code]; |
| 19 |
|
| } |
| 20 |
|
| |
| 21 |
|
| static symprim_allocator current = { malloc, realloc, free }; |
| 22 |
|
| |
| 23 |
40 |
| void symprim_set_allocator(const symprim_allocator *allocator) |
| 24 |
|
| { |
| 25 |
40 |
▶ | if (allocator) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allocator) { |
20 |
20 |
40 |
|
| 26 |
20 |
| current = *allocator; |
| 27 |
|
| } else { |
| 28 |
20 |
| current.malloc = malloc; |
| 29 |
20 |
| current.realloc = realloc; |
| 30 |
20 |
| current.free = free; |
| 31 |
|
| } |
| 32 |
40 |
| } |
| 33 |
|
| |
| 34 |
506 |
| void *symprim_malloc(size_t size) |
| 35 |
|
| { |
| 36 |
506 |
| return current.malloc(size); |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
25 |
| void *symprim_realloc(void *ptr, size_t size) |
| 40 |
|
| { |
| 41 |
25 |
| return current.realloc(ptr, size); |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
503 |
| void symprim_free(void *ptr) |
| 45 |
|
| { |
| 46 |
503 |
▶ | if (ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr) |
501 |
2 |
503 |
|
| 47 |
501 |
| current.free(ptr); |
| 48 |
503 |
| } |