@@ -112,6 +112,35 @@ 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
+ size = bits::max (size, alignment);
126
+ snmalloc::sizeclass_t sc = size_to_sizeclass (size);
127
+ if (sc >= NUM_SIZECLASSES)
128
+ {
129
+ // large allocs are 16M aligned, which is maximum we guarantee
130
+ return size;
131
+ }
132
+ for (; sc < NUM_SIZECLASSES; sc++)
133
+ {
134
+ size = sizeclass_to_size (sc);
135
+ if ((size & (~size + 1 )) >= alignment)
136
+ {
137
+ return size;
138
+ }
139
+ }
140
+ // Give max alignment.
141
+ return SUPERSLAB_SIZE;
142
+ }
143
+
115
144
SNMALLOC_EXPORT void *
116
145
SNMALLOC_NAME_MANGLE (memalign)(size_t alignment, size_t size)
117
146
{
@@ -128,22 +157,7 @@ extern "C"
128
157
return nullptr ;
129
158
}
130
159
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);
160
+ return SNMALLOC_NAME_MANGLE (malloc)(aligned_size (alignment, size));
147
161
}
148
162
149
163
SNMALLOC_EXPORT int SNMALLOC_NAME_MANGLE (posix_memalign)(
0 commit comments