Skip to content

Commit ff501b7

Browse files
committed
fix(box_malloc):box_and_child_max_obj_capacity
1 parent 081faba commit ff501b7

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

src/box_malloc.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ static void box_format(box_meta_t *meta, box_head_t *node, uint8_t objlevel, uin
178178
.state = BOX_UNUSED,
179179
};
180180
}
181-
181+
node->child_max_obj_capacity = (obj_usage){
182+
.level =objlevel,
183+
.multiple =1,
184+
};
182185
// childbox
183186
for (int i = 0; i < 16; i++)
184187
{
@@ -216,6 +219,30 @@ static obj_usage box_max_obj_capacity(box_head_t *node)
216219
return node->child_max_obj_capacity;
217220
}
218221
}
222+
223+
static obj_usage box_and_child_max_obj_capacity(box_head_t *node)
224+
{
225+
// 计算“本节点自身槽位”可提供的最大容量
226+
obj_usage own;
227+
228+
if (node->max_obj_capacity == 16)
229+
{
230+
own.level = node->objlevel + 1;
231+
own.multiple = 1;
232+
}
233+
else
234+
{
235+
own.level = node->objlevel;
236+
own.multiple = node->max_obj_capacity;
237+
}
238+
239+
// 子树聚合得到的最大容量
240+
obj_usage child = node->child_max_obj_capacity;
241+
242+
// 返回两者中的更大者
243+
return compare_obj_usage(own, child) >= 0 ? own : child;
244+
}
245+
219246
/*
220247
* 线程安全需求:
221248
* - 需要写锁:修改父节点状态。
@@ -265,7 +292,7 @@ static void update_parent(box_meta_t *meta, box_head_t *node, bool slotstate_cha
265292
LOG("[ERROR] child node should not be NULL");
266293
return;
267294
}
268-
obj_usage childmax = box_max_obj_capacity(child);
295+
obj_usage childmax = box_and_child_max_obj_capacity(child);
269296
if (compare_obj_usage(childmax, newmax) > 0)
270297
newmax = childmax;
271298
}
@@ -409,7 +436,7 @@ static uint64_t box_find_alloc(box_meta_t *meta, box_head_t *node, box_head_t *p
409436
if (node->childs_blockid[i] >= 0)
410437
{
411438
child = boxhead + blockdata_offset(&meta->blocks, node->childs_blockid[i]);
412-
obj_usage child_max = box_max_obj_capacity(child);
439+
obj_usage child_max = box_and_child_max_obj_capacity(child);
413440
if (compare_obj_usage(child_max, objsize) >= 0)
414441
{
415442
uint64_t offset = obj_offset((obj_usage){
@@ -443,11 +470,15 @@ static uint64_t box_find_alloc(box_meta_t *meta, box_head_t *node, box_head_t *p
443470

444471
// 更新node中的child信息
445472
node->used_slots[i].state = BOX_FORMATTED;
473+
node->used_slots[i].continue_max = 0;
446474
// 更新node中的max_obj_capacity
447475
uint8_t new_max = box_continuous_max(node);
448476
if (node->max_obj_capacity != new_max)
449477
{
450478
node->max_obj_capacity = new_max;
479+
if (parent) {
480+
update_parent(meta, parent, true, false);
481+
}
451482
}
452483

453484
// 判断新child box的容量
@@ -497,7 +528,7 @@ uint64_t box_alloc(void *metaptr, const size_t size)
497528
void *boxhead=(void*)meta+sizeof(box_meta_t);
498529
box_head_t *root = boxhead+ blockdata_offset(&meta->blocks, 0);
499530

500-
obj_usage max_capacity = box_max_obj_capacity(root);
531+
obj_usage max_capacity = box_and_child_max_obj_capacity(root);
501532

502533
if (compare_obj_usage(aligned_objsize, max_capacity) > 0)
503534
{

0 commit comments

Comments
 (0)