29
|
1 /**
|
|
2 * vec - a tiny SIMD vector library in C99
|
|
3 *
|
|
4 * Copyright (c) 2024 Paper
|
|
5 *
|
|
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7 * of this software and associated documentation files (the "Software"), to deal
|
|
8 * in the Software without restriction, including without limitation the rights
|
|
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10 * copies of the Software, and to permit persons to whom the Software is
|
|
11 * furnished to do so, subject to the following conditions:
|
|
12 *
|
|
13 * The above copyright notice and this permission notice shall be included in all
|
|
14 * copies or substantial portions of the Software.
|
|
15 *
|
|
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22 * SOFTWARE.
|
|
23 **/
|
|
24
|
|
25 #include "vec/mem.h"
|
|
26
|
|
27 #include <stdlib.h>
|
|
28 #include <string.h>
|
|
29
|
|
30 #define VEC_MALLOC_ALIGNMENT (64)
|
|
31
|
|
32 VEC_STATIC_ASSERT(!(VEC_MALLOC_ALIGNMENT & (VEC_MALLOC_ALIGNMENT - 1))
|
|
33 && (VEC_MALLOC_ALIGNMENT > 0),
|
|
34 "VEC_MALLOC_ALIGNMENT must be a power of two");
|
|
35
|
|
36 typedef unsigned char vec_alignment_type;
|
|
37
|
|
38 #define VEC_MALLOC_ADDITIONAL_SIZE (sizeof(vec_alignment_type) + (VEC_MALLOC_ALIGNMENT - 1))
|
|
39 #define VEC_MALLOC_MAX_SIZE (SIZE_MAX - VEC_MALLOC_ADDITIONAL_SIZE)
|
|
40
|
|
41 VEC_FUNC_IMPL void *vec_align_ptr(void *q)
|
|
42 {
|
|
43 vec_alignment_type diff;
|
|
44
|
|
45 diff = (((uintptr_t)q + (VEC_MALLOC_ALIGNMENT - 1)) & ~(VEC_MALLOC_ALIGNMENT - 1)) - (uintptr_t)q;
|
|
46 q = (char *)q + diff;
|
|
47
|
|
48 memcpy((char *)q - sizeof(diff), &diff, sizeof(diff));
|
|
49
|
|
50 return q;
|
|
51 }
|
|
52
|
|
53 /* reverses vec_align_ptr */
|
|
54 VEC_FUNC_IMPL void *vec_unalign_ptr(void *q)
|
|
55 {
|
|
56 vec_alignment_type diff;
|
|
57
|
|
58 memcpy(&diff, (char *)q - sizeof(diff), sizeof(diff));
|
|
59 q = (char *)q - diff;
|
|
60
|
|
61 return q;
|
|
62 }
|
|
63
|
|
64 void *vec_malloc(size_t size)
|
|
65 {
|
|
66 void *q;
|
|
67
|
|
68 if (size > VEC_MALLOC_MAX_SIZE)
|
|
69 return NULL;
|
|
70
|
|
71 /* allocate space for the diff (we have to do this,
|
|
72 * for realloc has no way of knowing the original ptr) */
|
|
73 q = malloc(size + VEC_MALLOC_ADDITIONAL_SIZE);
|
|
74 if (!q)
|
|
75 return NULL;
|
|
76
|
|
77 return vec_align_ptr(q);
|
|
78 }
|
|
79
|
|
80 void *vec_calloc(size_t count, size_t nmemb)
|
|
81 {
|
|
82 size_t size;
|
|
83 void *q;
|
|
84
|
|
85 size = count * nmemb;
|
|
86 if (size && size / count != nmemb)
|
|
87 return NULL; /* nope */
|
|
88
|
|
89 q = vec_malloc(size);
|
|
90
|
|
91 if (q)
|
|
92 memset(q, 0, size);
|
|
93
|
|
94 return q;
|
|
95 }
|
|
96
|
|
97 void *vec_realloc(void *ptr, size_t newsize)
|
|
98 {
|
|
99 vec_alignment_type diff;
|
|
100 void *q;
|
|
101
|
|
102 if (!ptr)
|
|
103 return vec_malloc(newsize);
|
|
104
|
|
105 if (newsize > VEC_MALLOC_MAX_SIZE)
|
|
106 return NULL;
|
|
107
|
|
108 q = realloc(vec_unalign_ptr(ptr), VEC_MALLOC_ADDITIONAL_SIZE);
|
|
109 if (!q)
|
|
110 return NULL;
|
|
111
|
|
112 return vec_align_ptr(q);
|
|
113 }
|
|
114
|
|
115 void vec_free(void *ptr)
|
|
116 {
|
|
117 if (ptr)
|
|
118 free(vec_unalign_ptr(ptr));
|
|
119 }
|