Skip to content

Commit e79001f

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

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

src/box_malloc.c

Lines changed: 36 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,31 @@ 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 = { .level = 0, .multiple = 0 };
227+
if (node->max_obj_capacity > 0)
228+
{
229+
if (node->max_obj_capacity == 16)
230+
{
231+
own.level = node->objlevel + 1;
232+
own.multiple = 1;
233+
}
234+
else
235+
{
236+
own.level = node->objlevel;
237+
own.multiple = node->max_obj_capacity;
238+
}
239+
}
240+
// 子树聚合得到的最大容量
241+
obj_usage child = node->child_max_obj_capacity;
242+
243+
// 返回两者中的更大者
244+
return compare_obj_usage(own, child) >= 0 ? own : child;
245+
}
246+
219247
/*
220248
* 线程安全需求:
221249
* - 需要写锁:修改父节点状态。
@@ -265,7 +293,7 @@ static void update_parent(box_meta_t *meta, box_head_t *node, bool slotstate_cha
265293
LOG("[ERROR] child node should not be NULL");
266294
return;
267295
}
268-
obj_usage childmax = box_max_obj_capacity(child);
296+
obj_usage childmax = box_and_child_max_obj_capacity(child);
269297
if (compare_obj_usage(childmax, newmax) > 0)
270298
newmax = childmax;
271299
}
@@ -409,7 +437,7 @@ static uint64_t box_find_alloc(box_meta_t *meta, box_head_t *node, box_head_t *p
409437
if (node->childs_blockid[i] >= 0)
410438
{
411439
child = boxhead + blockdata_offset(&meta->blocks, node->childs_blockid[i]);
412-
obj_usage child_max = box_max_obj_capacity(child);
440+
obj_usage child_max = box_and_child_max_obj_capacity(child);
413441
if (compare_obj_usage(child_max, objsize) >= 0)
414442
{
415443
uint64_t offset = obj_offset((obj_usage){
@@ -443,11 +471,15 @@ static uint64_t box_find_alloc(box_meta_t *meta, box_head_t *node, box_head_t *p
443471

444472
// 更新node中的child信息
445473
node->used_slots[i].state = BOX_FORMATTED;
474+
node->used_slots[i].continue_max = 0;
446475
// 更新node中的max_obj_capacity
447476
uint8_t new_max = box_continuous_max(node);
448477
if (node->max_obj_capacity != new_max)
449478
{
450479
node->max_obj_capacity = new_max;
480+
if (parent) {
481+
update_parent(meta, parent, true, false);
482+
}
451483
}
452484

453485
// 判断新child box的容量
@@ -497,7 +529,7 @@ uint64_t box_alloc(void *metaptr, const size_t size)
497529
void *boxhead=(void*)meta+sizeof(box_meta_t);
498530
box_head_t *root = boxhead+ blockdata_offset(&meta->blocks, 0);
499531

500-
obj_usage max_capacity = box_max_obj_capacity(root);
532+
obj_usage max_capacity = box_and_child_max_obj_capacity(root);
501533

502534
if (compare_obj_usage(aligned_objsize, max_capacity) > 0)
503535
{

0 commit comments

Comments
 (0)