Skip to content

Commit 09bc48d

Browse files
committed
select bucket that fits multiple of alignment
1 parent 52616da commit 09bc48d

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/gc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,8 @@ int jl_gc_classify_pools(size_t sz, size_t alignment, int *osize)
10051005
if (sz > GC_MAX_SZCLASS)
10061006
return -1;
10071007
size_t allocsz = sz + sizeof(jl_taggedvalue_t);
1008-
int klass = jl_gc_szclass(allocsz, alignment);
1008+
size_t alignsz = jl_gc_alignsz(allocsz, alignment);
1009+
int klass = jl_gc_szclass(alignsz);
10091010
*osize = jl_gc_sizeclasses[klass];
10101011
return (int)(intptr_t)(&((jl_ptls_t)0)->heap.norm_pools[klass]);
10111012
}

src/julia_internal.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,21 @@ static const int jl_gc_sizeclasses[JL_GC_N_POOLS] = {
192192
// 64, 32, 160, 64, 16, 64, 112, 128, bytes lost
193193
};
194194

195-
STATIC_INLINE int JL_CONST_FUNC jl_gc_szclass(size_t sz, size_t alignment)
195+
STATIC_INLINE size_t JL_CONST_FUNC jl_gc_alignsz(size_t sz, size_t alignment)
196196
{
197+
// The pools are aligned with JL_HEAP_ALIGNMENT and no bigger alignment is possible.
198+
assert(alignment <= JL_HEAP_ALIGNMENT);
199+
// Alignment need to be powers of two
200+
assert((alignment & (alignment - 1)) == 0);
201+
size_t alsz = LLT_ALIGN(sz, alignment);
202+
return alignment ? alsz : sz;
203+
}
204+
205+
// Use jl_gc_alignsz to obtain the right sz.
206+
STATIC_INLINE int JL_CONST_FUNC jl_gc_szclass(size_t sz)
207+
{
208+
// Check that the object fits in the largest pool.
209+
assert(sz <= GC_MAX_SZCLASS + sizeof(jl_taggedvalue_t));
197210
#ifdef _P64
198211
if (sz <= 8)
199212
return 0;
@@ -227,17 +240,20 @@ STATIC_INLINE jl_value_t *jl_gc_alloc_(jl_ptls_t ptls, size_t sz, size_t alignme
227240
const size_t allocsz = sz + sizeof(jl_taggedvalue_t);
228241
if (allocsz < sz) // overflow in adding offs, size was "negative"
229242
jl_throw(jl_memory_exception);
243+
const size_t alignsz = jl_gc_alignsz(allocsz, alignment);
230244
jl_value_t *v;
231-
if (allocsz <= GC_MAX_SZCLASS + sizeof(jl_taggedvalue_t)) {
232-
int pool_id = jl_gc_szclass(allocsz, alignment);
245+
if (alignsz <= GC_MAX_SZCLASS + sizeof(jl_taggedvalue_t)) {
246+
int pool_id = jl_gc_szclass(alignsz);
233247
jl_gc_pool_t *p = &ptls->heap.norm_pools[pool_id];
234248
int osize;
235-
if (jl_is_constexpr(allocsz)) {
249+
if (jl_is_constexpr(alignsz)) {
236250
osize = jl_gc_sizeclasses[pool_id];
237251
}
238252
else {
239253
osize = p->osize;
240254
}
255+
assert((size_t)osize >= alignment &&
256+
(alignment == 0 || (osize & (alignment - 1)) == 0));
241257
v = jl_gc_pool_alloc(ptls, (char*)p - (char*)ptls, osize);
242258
}
243259
else {

0 commit comments

Comments
 (0)