Debian Codegraph @

src/common/aligned.c

raw symbols SCIP
1 // Copyright (c) 2017-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include "aligned.h"
35
36 static inline int is_power_of_2(size_t n) { return n && (n & (n-1)) == 0; }
37
38 #define MAGIC 0xaa0817161500ff81
39 #define MAGIC_FREED 0xaa0817161500ff82
40
41 static void local_abort(const char *msg)
42 {
43 fprintf(stderr, "%s\n", msg);
44 #ifdef NDEBUG
45 _exit(1);
46 #else
47 abort();
48 #endif
49 }
50
51 typedef struct
52 {
53 uint64_t magic;
54 void *raw;
55 size_t bytes;
56 size_t align;
57 } control;
58
59 void *aligned_malloc(size_t bytes, size_t align)
60 {
61 void *raw, *ptr;
62 control *ctrl;
63
64 if (!is_power_of_2(align))
65 return NULL;
66 if (bytes > (size_t)-1 - align)
67 return NULL;
68 if (bytes + align > (size_t)-1 - sizeof(control))
69 return NULL;
70
71 raw = malloc(bytes + sizeof(control) + align);
72 if (!raw)
73 return NULL;
74 ptr = (void*)(((uintptr_t)raw + align + sizeof(control) - 1) & ~(align-1));
75 ctrl = ((control*)ptr) - 1;
76 ctrl->magic = MAGIC;
77 ctrl->raw = raw;
78 ctrl->bytes = bytes;
79 ctrl->align = align;
80 return ptr;
81 }
82
83 void *aligned_realloc(void *ptr, size_t bytes, size_t align)
84 {
85 void *raw, *ptr2;
86 control *ctrl, *ctrl2;
87
88 if (!ptr)
89 return aligned_malloc(bytes, align);
90 if (!bytes)
91 {
92 aligned_free(ptr);
93 return NULL;
94 }
95 if (!is_power_of_2(align))
96 return NULL;
97
98 ctrl = ((control*)ptr) - 1;
99 if (ctrl->magic == MAGIC_FREED)
100 local_abort("Double free detected");
101 if (ctrl->magic != MAGIC)
102 local_abort("Freeing unallocated memory");
103 if (ctrl->align != align)
104 return NULL;
105 if (ctrl->bytes >= bytes)
106 return ptr;
107
108 if (ctrl->bytes > (size_t)-1 - ctrl->align)
109 return NULL;
110 if (ctrl->bytes + ctrl->align > (size_t)-1 - sizeof(control))
111 return NULL;
112
113 raw = malloc(bytes + sizeof(control) + ctrl->align);
114 if (!raw)
115 return NULL;
116 ptr2 = (void*)(((uintptr_t)raw + ctrl->align + sizeof(control) - 1) & ~(ctrl->align-1));
117 memcpy(ptr2, ptr, ctrl->bytes);
118 ctrl2 = ((control*)ptr2) - 1;
119 ctrl2->magic = MAGIC;
120 ctrl2->raw = raw;
121 ctrl2->bytes = bytes;
122 ctrl2->align = ctrl->align;
123 ctrl->magic = MAGIC_FREED;
124 free(ctrl->raw);
125 return ptr2;
126 }
127
128 void aligned_free(void *ptr)
129 {
130 if (!ptr)
131 return;
132 control *ctrl = ((control*)ptr) - 1;
133 if (ctrl->magic == MAGIC_FREED)
134 local_abort("Double free detected");
135 if (ctrl->magic != MAGIC)
136 local_abort("Freeing unallocated memory");
137 ctrl->magic = MAGIC_FREED;
138 free(ctrl->raw);
139 }