| 1 |
/* |
| 2 |
* utils.c - utility functions for libdeflate |
| 3 |
* |
| 4 |
* Copyright 2016 Eric Biggers |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person |
| 7 |
* obtaining a copy of this software and associated documentation |
| 8 |
* files (the "Software"), to deal in the Software without |
| 9 |
* restriction, including without limitation the rights to use, |
| 10 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following |
| 13 |
* conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be |
| 16 |
* included in all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 20 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 22 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 23 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 25 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "lib_common.h" |
| 29 |
|
| 30 |
#ifdef FREESTANDING |
| 31 |
# define malloc NULL |
| 32 |
# define free NULL |
| 33 |
#else |
| 34 |
# include <stdlib.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
malloc_func_t libdeflate_default_malloc_func = malloc; |
| 38 |
free_func_t libdeflate_default_free_func = free; |
| 39 |
|
| 40 |
void * |
| 41 |
libdeflate_aligned_malloc(malloc_func_t malloc_func, |
| 42 |
size_t alignment, size_t size) |
| 43 |
{ |
| 44 |
void *ptr = (*malloc_func)(sizeof(void *) + alignment - 1 + size); |
| 45 |
|
| 46 |
if (ptr) { |
| 47 |
void *orig_ptr = ptr; |
| 48 |
|
| 49 |
ptr = (void *)ALIGN((uintptr_t)ptr + sizeof(void *), alignment); |
| 50 |
((void **)ptr)[-1] = orig_ptr; |
| 51 |
} |
| 52 |
return ptr; |
| 53 |
} |
| 54 |
|
| 55 |
void |
| 56 |
libdeflate_aligned_free(free_func_t free_func, void *ptr) |
| 57 |
{ |
| 58 |
(*free_func)(((void **)ptr)[-1]); |
| 59 |
} |
| 60 |
|
| 61 |
LIBDEFLATEAPI void |
| 62 |
libdeflate_set_memory_allocator(malloc_func_t malloc_func, |
| 63 |
free_func_t free_func) |
| 64 |
{ |
| 65 |
libdeflate_default_malloc_func = malloc_func; |
| 66 |
libdeflate_default_free_func = free_func; |
| 67 |
} |
| 68 |
|
| 69 |
/* |
| 70 |
* Implementations of libc functions for freestanding library builds. |
| 71 |
* Normal library builds don't use these. Not optimized yet; usually the |
| 72 |
* compiler expands these functions and doesn't actually call them anyway. |
| 73 |
*/ |
| 74 |
#ifdef FREESTANDING |
| 75 |
#undef memset |
| 76 |
void * __attribute__((weak)) |
| 77 |
memset(void *s, int c, size_t n) |
| 78 |
{ |
| 79 |
u8 *p = s; |
| 80 |
size_t i; |
| 81 |
|
| 82 |
for (i = 0; i < n; i++) |
| 83 |
p[i] = c; |
| 84 |
return s; |
| 85 |
} |
| 86 |
|
| 87 |
#undef memcpy |
| 88 |
void * __attribute__((weak)) |
| 89 |
memcpy(void *dest, const void *src, size_t n) |
| 90 |
{ |
| 91 |
u8 *d = dest; |
| 92 |
const u8 *s = src; |
| 93 |
size_t i; |
| 94 |
|
| 95 |
for (i = 0; i < n; i++) |
| 96 |
d[i] = s[i]; |
| 97 |
return dest; |
| 98 |
} |
| 99 |
|
| 100 |
#undef memmove |
| 101 |
void * __attribute__((weak)) |
| 102 |
memmove(void *dest, const void *src, size_t n) |
| 103 |
{ |
| 104 |
u8 *d = dest; |
| 105 |
const u8 *s = src; |
| 106 |
size_t i; |
| 107 |
|
| 108 |
if (d <= s) |
| 109 |
return memcpy(d, s, n); |
| 110 |
|
| 111 |
for (i = n; i > 0; i--) |
| 112 |
d[i - 1] = s[i - 1]; |
| 113 |
return dest; |
| 114 |
} |
| 115 |
|
| 116 |
#undef memcmp |
| 117 |
int __attribute__((weak)) |
| 118 |
memcmp(const void *s1, const void *s2, size_t n) |
| 119 |
{ |
| 120 |
const u8 *p1 = s1; |
| 121 |
const u8 *p2 = s2; |
| 122 |
size_t i; |
| 123 |
|
| 124 |
for (i = 0; i < n; i++) { |
| 125 |
if (p1[i] != p2[i]) |
| 126 |
return (int)p1[i] - (int)p2[i]; |
| 127 |
} |
| 128 |
return 0; |
| 129 |
} |
| 130 |
#endif /* FREESTANDING */ |
| 131 |
|
| 132 |
#ifdef LIBDEFLATE_ENABLE_ASSERTIONS |
| 133 |
#include <stdio.h> |
| 134 |
#include <stdlib.h> |
| 135 |
NORETURN void |
| 136 |
libdeflate_assertion_failed(const char *expr, const char *file, int line) |
| 137 |
{ |
| 138 |
fprintf(stderr, "Assertion failed: %s at %s:%d\n", expr, file, line); |
| 139 |
abort(); |
| 140 |
} |
| 141 |
#endif /* LIBDEFLATE_ENABLE_ASSERTIONS */ |