/work/_deps/deflate-src/lib/utils.c
Line | Count | Source (jump to first uncovered line) |
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 | | static void *(*libdeflate_malloc_func)(size_t) = malloc; |
38 | | static void (*libdeflate_free_func)(void *) = free; |
39 | | |
40 | | void * |
41 | | libdeflate_malloc(size_t size) |
42 | 1.67k | { |
43 | 1.67k | return (*libdeflate_malloc_func)(size); |
44 | 1.67k | } |
45 | | |
46 | | void |
47 | | libdeflate_free(void *ptr) |
48 | 1.67k | { |
49 | 1.67k | (*libdeflate_free_func)(ptr); |
50 | 1.67k | } |
51 | | |
52 | | void * |
53 | | libdeflate_aligned_malloc(size_t alignment, size_t size) |
54 | 0 | { |
55 | 0 | void *ptr = libdeflate_malloc(sizeof(void *) + alignment - 1 + size); |
56 | 0 | if (ptr) { |
57 | 0 | void *orig_ptr = ptr; |
58 | 0 | ptr = (void *)ALIGN((uintptr_t)ptr + sizeof(void *), alignment); |
59 | 0 | ((void **)ptr)[-1] = orig_ptr; |
60 | 0 | } |
61 | 0 | return ptr; |
62 | 0 | } |
63 | | |
64 | | void |
65 | | libdeflate_aligned_free(void *ptr) |
66 | 0 | { |
67 | 0 | if (ptr) |
68 | 0 | libdeflate_free(((void **)ptr)[-1]); |
69 | 0 | } |
70 | | |
71 | | LIBDEFLATEAPI void |
72 | | libdeflate_set_memory_allocator(void *(*malloc_func)(size_t), |
73 | | void (*free_func)(void *)) |
74 | 1.67k | { |
75 | 1.67k | libdeflate_malloc_func = malloc_func; |
76 | 1.67k | libdeflate_free_func = free_func; |
77 | 1.67k | } |
78 | | |
79 | | /* |
80 | | * Implementations of libc functions for freestanding library builds. |
81 | | * Normal library builds don't use these. Not optimized yet; usually the |
82 | | * compiler expands these functions and doesn't actually call them anyway. |
83 | | */ |
84 | | #ifdef FREESTANDING |
85 | | #undef memset |
86 | | void * __attribute__((weak)) |
87 | | memset(void *s, int c, size_t n) |
88 | | { |
89 | | u8 *p = s; |
90 | | size_t i; |
91 | | |
92 | | for (i = 0; i < n; i++) |
93 | | p[i] = c; |
94 | | return s; |
95 | | } |
96 | | |
97 | | #undef memcpy |
98 | | void * __attribute__((weak)) |
99 | | memcpy(void *dest, const void *src, size_t n) |
100 | | { |
101 | | u8 *d = dest; |
102 | | const u8 *s = src; |
103 | | size_t i; |
104 | | |
105 | | for (i = 0; i < n; i++) |
106 | | d[i] = s[i]; |
107 | | return dest; |
108 | | } |
109 | | |
110 | | #undef memmove |
111 | | void * __attribute__((weak)) |
112 | | memmove(void *dest, const void *src, size_t n) |
113 | | { |
114 | | u8 *d = dest; |
115 | | const u8 *s = src; |
116 | | size_t i; |
117 | | |
118 | | if (d <= s) |
119 | | return memcpy(d, s, n); |
120 | | |
121 | | for (i = n; i > 0; i--) |
122 | | d[i - 1] = s[i - 1]; |
123 | | return dest; |
124 | | } |
125 | | |
126 | | #undef memcmp |
127 | | int __attribute__((weak)) |
128 | | memcmp(const void *s1, const void *s2, size_t n) |
129 | | { |
130 | | const u8 *p1 = s1; |
131 | | const u8 *p2 = s2; |
132 | | size_t i; |
133 | | |
134 | | for (i = 0; i < n; i++) { |
135 | | if (p1[i] != p2[i]) |
136 | | return (int)p1[i] - (int)p2[i]; |
137 | | } |
138 | | return 0; |
139 | | } |
140 | | #endif /* FREESTANDING */ |
141 | | |
142 | | #ifdef LIBDEFLATE_ENABLE_ASSERTIONS |
143 | | #include <stdio.h> |
144 | | #include <stdlib.h> |
145 | | void |
146 | | libdeflate_assertion_failed(const char *expr, const char *file, int line) |
147 | | { |
148 | | fprintf(stderr, "Assertion failed: %s at %s:%d\n", expr, file, line); |
149 | | abort(); |
150 | | } |
151 | | #endif /* LIBDEFLATE_ENABLE_ASSERTIONS */ |