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