annotate src/mem.c @ 29:e59c91d050c0

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