@@ -112,6 +112,36 @@ extern "C"
112
112
return SNMALLOC_NAME_MANGLE (malloc)(size);
113
113
}
114
114
115
+ inline size_t aligned_size (size_t alignment, size_t size)
116
+ {
117
+ // Client responsible for checking alignment is not zero
118
+ // Client responsible for checking for overflow
119
+ assert ((size + alignment) > size);
120
+ // Client responsible for checking alignment is not above SUPERSLAB_SIZE
121
+ assert (alignment <= SUPERSLAB_SIZE);
122
+ // Client responsible for checking alignment is a power of two
123
+ assert (bits::next_pow2 (alignment) == alignment);
124
+
125
+
126
+ size = bits::max (size, alignment);
127
+ snmalloc::sizeclass_t sc = size_to_sizeclass (size);
128
+ if (sc >= NUM_SIZECLASSES)
129
+ {
130
+ // large allocs are 16M aligned, which is maximum we guarantee
131
+ return size;
132
+ }
133
+ for (; sc < NUM_SIZECLASSES; sc++)
134
+ {
135
+ size = sizeclass_to_size (sc);
136
+ if ((size & (~size + 1 )) >= alignment)
137
+ {
138
+ return size;
139
+ }
140
+ }
141
+ // Give max alignment.
142
+ return SUPERSLAB_SIZE;
143
+ }
144
+
115
145
SNMALLOC_EXPORT void *
116
146
SNMALLOC_NAME_MANGLE (memalign)(size_t alignment, size_t size)
117
147
{
@@ -128,22 +158,7 @@ extern "C"
128
158
return nullptr ;
129
159
}
130
160
131
- size = bits::max (size, alignment);
132
- snmalloc::sizeclass_t sc = size_to_sizeclass (size);
133
- if (sc >= NUM_SIZECLASSES)
134
- {
135
- // large allocs are 16M aligned.
136
- return SNMALLOC_NAME_MANGLE (malloc)(size);
137
- }
138
- for (; sc < NUM_SIZECLASSES; sc++)
139
- {
140
- size = sizeclass_to_size (sc);
141
- if ((size & (~size + 1 )) >= alignment)
142
- {
143
- return SNMALLOC_NAME_MANGLE (aligned_alloc )(alignment, size);
144
- }
145
- }
146
- return SNMALLOC_NAME_MANGLE (malloc)(SUPERSLAB_SIZE);
161
+ return SNMALLOC_NAME_MANGLE (malloc)(aligned_size (alignment, size));
147
162
}
148
163
149
164
SNMALLOC_EXPORT int SNMALLOC_NAME_MANGLE (posix_memalign)(
0 commit comments