2019-05-07 02:18:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
2019-11-11 23:45:21 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-05-07 02:18:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ems_gc_internal.h"
|
|
|
|
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
static inline bool
|
|
|
|
hmu_is_in_heap(void *hmu,
|
|
|
|
gc_uint8 *heap_base_addr,
|
|
|
|
gc_uint8 *heap_end_addr)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-10-22 10:52:33 +00:00
|
|
|
gc_uint8 *addr = (gc_uint8 *)hmu;
|
|
|
|
return (addr >= heap_base_addr && addr < heap_end_addr)
|
|
|
|
? true : false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
/**
|
|
|
|
* Remove a node from the tree it belongs to
|
|
|
|
*
|
|
|
|
* @param p the node to remove, can not be NULL, can not be the ROOT node
|
|
|
|
* the node will be removed from the tree, and the left, right and
|
|
|
|
* parent pointers of the node @p will be set to be NULL. Other fields
|
|
|
|
* won't be touched. The tree will be re-organized so that the order
|
|
|
|
* conditions are still satisified.
|
|
|
|
*/
|
2020-10-22 10:52:33 +00:00
|
|
|
static bool
|
|
|
|
remove_tree_node(gc_heap_t *heap, hmu_tree_node_t *p)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-10-22 10:52:33 +00:00
|
|
|
hmu_tree_node_t *q = NULL, **slot = NULL, *parent;
|
|
|
|
hmu_tree_node_t *root = &heap->kfc_tree_root;
|
|
|
|
gc_uint8 *base_addr = heap->base_addr;
|
|
|
|
gc_uint8 *end_addr = base_addr + heap->current_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
bh_assert(p);
|
2020-10-22 10:52:33 +00:00
|
|
|
|
|
|
|
parent = p->parent;
|
|
|
|
if (!parent || p == root /* p can not be the ROOT node */
|
|
|
|
|| !hmu_is_in_heap(p, base_addr, end_addr)
|
|
|
|
|| (parent != root
|
|
|
|
&& !hmu_is_in_heap(parent, base_addr, end_addr))) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
/* get the slot which holds pointer to node p*/
|
|
|
|
if (p == p->parent->right) {
|
|
|
|
slot = &p->parent->right;
|
2020-10-22 10:52:33 +00:00
|
|
|
}
|
|
|
|
else if (p == p->parent->left) {
|
|
|
|
/* p should be a child of its parent*/
|
2019-05-07 02:18:18 +00:00
|
|
|
slot = &p->parent->left;
|
|
|
|
}
|
2020-10-22 10:52:33 +00:00
|
|
|
else {
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
/**
|
|
|
|
* algorithms used to remove node p
|
|
|
|
* case 1: if p has no left child, replace p with its right child
|
|
|
|
* case 2: if p has no right child, replace p with its left child
|
|
|
|
* case 3: otherwise, find p's predecessor, remove it from the tree
|
|
|
|
* and replace p with it.
|
|
|
|
* use predecessor can keep the left <= root < right condition.
|
|
|
|
*/
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (!p->left) {
|
|
|
|
/* move right child up*/
|
|
|
|
*slot = p->right;
|
2020-10-22 10:52:33 +00:00
|
|
|
if (p->right) {
|
|
|
|
if (!hmu_is_in_heap(p->right, base_addr, end_addr)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
p->right->parent = p->parent;
|
2020-10-22 10:52:33 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
p->left = p->right = p->parent = NULL;
|
2020-10-22 10:52:33 +00:00
|
|
|
return true;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!p->right) {
|
|
|
|
/* move left child up*/
|
|
|
|
*slot = p->left;
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!hmu_is_in_heap(p->left, base_addr, end_addr)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* p->left can never be NULL unless it is corrupted. */
|
|
|
|
p->left->parent = p->parent;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
p->left = p->right = p->parent = NULL;
|
2020-10-22 10:52:33 +00:00
|
|
|
return true;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* both left & right exist, find p's predecessor at first*/
|
|
|
|
q = p->left;
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!hmu_is_in_heap(q, base_addr, end_addr)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
while (q->right) {
|
2019-05-07 02:18:18 +00:00
|
|
|
q = q->right;
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!hmu_is_in_heap(q, base_addr, end_addr)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
/* remove from the tree*/
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!remove_tree_node(heap, q))
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
*slot = q;
|
|
|
|
q->parent = p->parent;
|
|
|
|
q->left = p->left;
|
|
|
|
q->right = p->right;
|
2020-10-22 10:52:33 +00:00
|
|
|
if (q->left) {
|
|
|
|
if (!hmu_is_in_heap(q->left, base_addr, end_addr)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
q->left->parent = q;
|
2020-10-22 10:52:33 +00:00
|
|
|
}
|
|
|
|
if (q->right) {
|
|
|
|
if (!hmu_is_in_heap(q->right, base_addr, end_addr)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
q->right->parent = q;
|
2020-10-22 10:52:33 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
p->left = p->right = p->parent = NULL;
|
2020-10-22 10:52:33 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
heap->is_heap_corrupted = true;
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
static bool
|
2020-03-30 03:06:39 +00:00
|
|
|
unlink_hmu(gc_heap_t *heap, hmu_t *hmu)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-10-22 10:52:33 +00:00
|
|
|
gc_uint8 *base_addr, *end_addr;
|
2019-05-07 02:18:18 +00:00
|
|
|
gc_size_t size;
|
|
|
|
|
|
|
|
bh_assert(gci_is_heap_valid(heap));
|
2020-03-30 03:06:39 +00:00
|
|
|
bh_assert(hmu && (gc_uint8*) hmu >= heap->base_addr
|
|
|
|
&& (gc_uint8*) hmu < heap->base_addr + heap->current_size);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
if (hmu_get_ut(hmu) != HMU_FC) {
|
|
|
|
heap->is_heap_corrupted = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_addr = heap->base_addr;
|
|
|
|
end_addr = base_addr + heap->current_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
size = hmu_get_size(hmu);
|
|
|
|
|
|
|
|
if (HMU_IS_FC_NORMAL(size)) {
|
2019-11-20 13:16:36 +00:00
|
|
|
uint32 node_idx = size >> 3;
|
2020-10-22 10:52:33 +00:00
|
|
|
hmu_normal_node_t *node_prev = NULL, *node_next;
|
|
|
|
hmu_normal_node_t *node = heap->kfc_normal_list[node_idx].next;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
while (node) {
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!hmu_is_in_heap(node, base_addr, end_addr)) {
|
|
|
|
heap->is_heap_corrupted = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
node_next = get_hmu_normal_node_next(node);
|
|
|
|
if ((hmu_t*)node == hmu) {
|
|
|
|
if (!node_prev) /* list head */
|
|
|
|
heap->kfc_normal_list[node_idx].next = node_next;
|
|
|
|
else
|
|
|
|
set_hmu_normal_node_next(node_prev, node_next);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-03-16 08:43:57 +00:00
|
|
|
node_prev = node;
|
2020-10-22 10:52:33 +00:00
|
|
|
node = node_next;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!node) {
|
2020-03-30 03:06:39 +00:00
|
|
|
os_printf("[GC_ERROR]couldn't find the node in the normal list\n");
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-03-30 03:06:39 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!remove_tree_node(heap, (hmu_tree_node_t *)hmu))
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-10-22 10:52:33 +00:00
|
|
|
return true;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
static void
|
|
|
|
hmu_set_free_size(hmu_t *hmu)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
gc_size_t size;
|
|
|
|
bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC);
|
|
|
|
|
|
|
|
size = hmu_get_size(hmu);
|
2020-03-30 03:06:39 +00:00
|
|
|
*((uint32*)((char*) hmu + size) - 1) = size;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
/**
|
|
|
|
* Add free chunk back to KFC
|
|
|
|
*
|
|
|
|
* @param heap should not be NULL and it should be a valid heap
|
|
|
|
* @param hmu should not be NULL and it should be a HMU of length @size inside @heap
|
|
|
|
* hmu should be 8-bytes aligned
|
|
|
|
* @param size should be positive and multiple of 8
|
|
|
|
* hmu with size @size will be added into KFC as a new FC.
|
|
|
|
*/
|
2020-10-22 10:52:33 +00:00
|
|
|
bool
|
2020-03-30 03:06:39 +00:00
|
|
|
gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-10-22 10:52:33 +00:00
|
|
|
gc_uint8 *base_addr, *end_addr;
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu_normal_node_t *np = NULL;
|
|
|
|
hmu_tree_node_t *root = NULL, *tp = NULL, *node = NULL;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint32 node_idx;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
bh_assert(gci_is_heap_valid(heap));
|
2020-10-22 10:52:33 +00:00
|
|
|
bh_assert(hmu && (gc_uint8*)hmu >= heap->base_addr
|
|
|
|
&& (gc_uint8*)hmu < heap->base_addr + heap->current_size);
|
2019-09-10 02:23:46 +00:00
|
|
|
bh_assert(((gc_uint32)(uintptr_t)hmu_to_obj(hmu) & 7) == 0);
|
2020-03-30 03:06:39 +00:00
|
|
|
bh_assert(size > 0
|
2020-10-22 10:52:33 +00:00
|
|
|
&& ((gc_uint8*)hmu) + size <= heap->base_addr + heap->current_size);
|
2019-05-07 02:18:18 +00:00
|
|
|
bh_assert(!(size & 7));
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
base_addr = heap->base_addr;
|
|
|
|
end_addr = base_addr + heap->current_size;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu_set_ut(hmu, HMU_FC);
|
|
|
|
hmu_set_size(hmu, size);
|
|
|
|
hmu_set_free_size(hmu);
|
|
|
|
|
|
|
|
if (HMU_IS_FC_NORMAL(size)) {
|
2020-10-22 10:52:33 +00:00
|
|
|
np = (hmu_normal_node_t*)hmu;
|
|
|
|
if (!hmu_is_in_heap(np, base_addr, end_addr)) {
|
|
|
|
heap->is_heap_corrupted = true;
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
node_idx = size >> 3;
|
2020-10-22 10:52:33 +00:00
|
|
|
set_hmu_normal_node_next(np, heap->kfc_normal_list[node_idx].next);
|
|
|
|
heap->kfc_normal_list[node_idx].next = np;
|
|
|
|
return true;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* big block*/
|
2020-10-22 10:52:33 +00:00
|
|
|
node = (hmu_tree_node_t*)hmu;
|
2019-05-07 02:18:18 +00:00
|
|
|
node->size = size;
|
|
|
|
node->left = node->right = node->parent = NULL;
|
|
|
|
|
|
|
|
/* find proper node to link this new node to*/
|
|
|
|
root = &heap->kfc_tree_root;
|
|
|
|
tp = root;
|
|
|
|
bh_assert(tp->size < size);
|
|
|
|
while (1) {
|
|
|
|
if (tp->size < size) {
|
|
|
|
if (!tp->right) {
|
|
|
|
tp->right = node;
|
|
|
|
node->parent = tp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tp = tp->right;
|
2020-03-30 03:06:39 +00:00
|
|
|
}
|
|
|
|
else { /* tp->size >= size*/
|
2019-05-07 02:18:18 +00:00
|
|
|
if (!tp->left) {
|
|
|
|
tp->left = node;
|
|
|
|
node->parent = tp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tp = tp->left;
|
|
|
|
}
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!hmu_is_in_heap(tp, base_addr, end_addr)) {
|
|
|
|
heap->is_heap_corrupted = true;
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-10-22 10:52:33 +00:00
|
|
|
return true;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
/**
|
|
|
|
* Find a proper hmu for required memory size
|
|
|
|
*
|
|
|
|
* @param heap should not be NULL and should be a valid heap
|
|
|
|
* @param size should cover the header and should be 8 bytes aligned
|
|
|
|
* GC will not be performed here.
|
|
|
|
* Heap extension will not be performed here.
|
|
|
|
*
|
|
|
|
* @return hmu allocated if success, which will be aligned to 8 bytes,
|
|
|
|
* NULL otherwise
|
|
|
|
*/
|
|
|
|
static hmu_t *
|
|
|
|
alloc_hmu(gc_heap_t *heap, gc_size_t size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-10-22 10:52:33 +00:00
|
|
|
gc_uint8 *base_addr, *end_addr;
|
|
|
|
hmu_normal_list_t *normal_head = NULL;
|
|
|
|
hmu_normal_node_t *p = NULL;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint32 node_idx = 0, init_node_idx = 0;
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu_tree_node_t *root = NULL, *tp = NULL, *last_tp = NULL;
|
|
|
|
hmu_t *next, *rest;
|
|
|
|
|
|
|
|
bh_assert(gci_is_heap_valid(heap));
|
|
|
|
bh_assert(size > 0 && !(size & 7));
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
base_addr = heap->base_addr;
|
|
|
|
end_addr = base_addr + heap->current_size;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
if (size < GC_SMALLEST_SIZE)
|
|
|
|
size = GC_SMALLEST_SIZE;
|
|
|
|
|
|
|
|
/* check normal list at first*/
|
|
|
|
if (HMU_IS_FC_NORMAL(size)) {
|
|
|
|
/* find a non-empty slot in normal_node_list with good size*/
|
2019-11-20 13:16:36 +00:00
|
|
|
init_node_idx = (size >> 3);
|
2019-05-07 02:18:18 +00:00
|
|
|
for (node_idx = init_node_idx; node_idx < HMU_NORMAL_NODE_CNT;
|
|
|
|
node_idx++) {
|
2020-10-22 10:52:33 +00:00
|
|
|
normal_head = heap->kfc_normal_list + node_idx;
|
|
|
|
if (normal_head->next)
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
2020-10-22 10:52:33 +00:00
|
|
|
normal_head = NULL;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
/* found in normal list*/
|
|
|
|
if (normal_head) {
|
2019-05-07 02:18:18 +00:00
|
|
|
bh_assert(node_idx >= init_node_idx);
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
p = normal_head->next;
|
|
|
|
if (!hmu_is_in_heap(p, base_addr, end_addr)) {
|
|
|
|
heap->is_heap_corrupted = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
normal_head->next = get_hmu_normal_node_next(p);
|
|
|
|
if (((gc_int32)(uintptr_t)hmu_to_obj(p) & 7) != 0) {
|
|
|
|
heap->is_heap_corrupted = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2019-11-20 13:16:36 +00:00
|
|
|
if ((gc_size_t)node_idx != (uint32)init_node_idx
|
2020-03-30 03:06:39 +00:00
|
|
|
/* with bigger size*/
|
|
|
|
&& ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) {
|
2019-05-07 02:18:18 +00:00
|
|
|
rest = (hmu_t*) (((char *) p) + size);
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!gci_add_fc(heap, rest, (node_idx << 3) - size)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu_mark_pinuse(rest);
|
2020-03-30 03:06:39 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-05-07 02:18:18 +00:00
|
|
|
size = node_idx << 3;
|
|
|
|
next = (hmu_t*) ((char*) p + size);
|
2020-10-22 10:52:33 +00:00
|
|
|
if (hmu_is_in_heap(next, base_addr, end_addr))
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu_mark_pinuse(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
heap->total_free_size -= size;
|
|
|
|
if ((heap->current_size - heap->total_free_size)
|
2020-03-30 03:06:39 +00:00
|
|
|
> heap->highmark_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
heap->highmark_size = heap->current_size
|
2020-03-30 03:06:39 +00:00
|
|
|
- heap->total_free_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
hmu_set_size((hmu_t* )p, size);
|
|
|
|
return (hmu_t*)p;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need to find a node in tree*/
|
|
|
|
root = &heap->kfc_tree_root;
|
|
|
|
|
|
|
|
/* find the best node*/
|
|
|
|
bh_assert(root);
|
|
|
|
tp = root->right;
|
|
|
|
while (tp) {
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!hmu_is_in_heap(tp, base_addr, end_addr)) {
|
|
|
|
heap->is_heap_corrupted = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
if (tp->size < size) {
|
|
|
|
tp = tp->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* record the last node with size equal to or bigger than given size*/
|
|
|
|
last_tp = tp;
|
|
|
|
tp = tp->left;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_tp) {
|
|
|
|
bh_assert(last_tp->size >= size);
|
|
|
|
|
|
|
|
/* alloc in last_p*/
|
|
|
|
|
|
|
|
/* remove node last_p from tree*/
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!remove_tree_node(heap, last_tp))
|
|
|
|
return NULL;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (last_tp->size >= size + GC_SMALLEST_SIZE) {
|
2020-10-22 10:52:33 +00:00
|
|
|
rest = (hmu_t*)((char*)last_tp + size);
|
|
|
|
if (!gci_add_fc(heap, rest, last_tp->size - size))
|
|
|
|
return NULL;
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu_mark_pinuse(rest);
|
2020-03-30 03:06:39 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-05-07 02:18:18 +00:00
|
|
|
size = last_tp->size;
|
2020-10-22 10:52:33 +00:00
|
|
|
next = (hmu_t*)((char*)last_tp + size);
|
|
|
|
if (hmu_is_in_heap(next, base_addr, end_addr))
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu_mark_pinuse(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
heap->total_free_size -= size;
|
|
|
|
if ((heap->current_size - heap->total_free_size) > heap->highmark_size)
|
|
|
|
heap->highmark_size = heap->current_size - heap->total_free_size;
|
2020-03-30 03:06:39 +00:00
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
hmu_set_size((hmu_t*)last_tp, size);
|
|
|
|
return (hmu_t*)last_tp;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
/**
|
|
|
|
* Find a proper HMU with given size
|
|
|
|
*
|
|
|
|
* @param heap should not be NULL and should be a valid heap
|
|
|
|
* @param size should cover the header and should be 8 bytes aligned
|
|
|
|
*
|
|
|
|
* Note: This function will try several ways to satisfy the allocation request:
|
|
|
|
* 1. Find a proper on available HMUs.
|
|
|
|
* 2. GC will be triggered if 1 failed.
|
|
|
|
* 3. Find a proper on available HMUS.
|
|
|
|
* 4. Return NULL if 3 failed
|
|
|
|
*
|
|
|
|
* @return hmu allocated if success, which will be aligned to 8 bytes,
|
|
|
|
* NULL otherwise
|
|
|
|
*/
|
|
|
|
static hmu_t *
|
|
|
|
alloc_hmu_ex(gc_heap_t *heap, gc_size_t size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
bh_assert(gci_is_heap_valid(heap));
|
|
|
|
bh_assert(size > 0 && !(size & 7));
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
return alloc_hmu(heap, size);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
static unsigned long g_total_malloc = 0;
|
|
|
|
static unsigned long g_total_free = 0;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
#if BH_ENABLE_GC_VERIFY == 0
|
|
|
|
gc_object_t
|
|
|
|
gc_alloc_vo(void *vheap, gc_size_t size)
|
|
|
|
#else
|
|
|
|
gc_object_t
|
|
|
|
gc_alloc_vo_internal(void *vheap, gc_size_t size,
|
|
|
|
const char *file, int line)
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
gc_heap_t* heap = (gc_heap_t*) vheap;
|
|
|
|
hmu_t *hmu = NULL;
|
|
|
|
gc_object_t ret = (gc_object_t) NULL;
|
2020-03-16 08:43:57 +00:00
|
|
|
gc_size_t tot_size = 0, tot_size_unaligned;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-16 08:43:57 +00:00
|
|
|
/* hmu header + prefix + obj + suffix */
|
|
|
|
tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
|
|
|
|
/* aligned size*/
|
|
|
|
tot_size = GC_ALIGN_8(tot_size_unaligned);
|
2019-05-07 02:18:18 +00:00
|
|
|
if (tot_size < size)
|
2020-03-16 08:43:57 +00:00
|
|
|
/* integer overflow */
|
2019-05-07 02:18:18 +00:00
|
|
|
return NULL;
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
if (heap->is_heap_corrupted) {
|
|
|
|
os_printf("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
os_mutex_lock(&heap->lock);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
hmu = alloc_hmu_ex(heap, tot_size);
|
|
|
|
if (!hmu)
|
2020-03-30 03:06:39 +00:00
|
|
|
goto finish;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
g_total_malloc += tot_size;
|
|
|
|
|
|
|
|
hmu_set_ut(hmu, HMU_VO);
|
|
|
|
hmu_unfree_vo(hmu);
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
#if BH_ENABLE_GC_VERIFY != 0
|
|
|
|
hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
|
2019-05-07 02:18:18 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = hmu_to_obj(hmu);
|
2020-03-16 08:43:57 +00:00
|
|
|
if (tot_size > tot_size_unaligned)
|
|
|
|
/* clear buffer appended by GC_ALIGN_8() */
|
|
|
|
memset((uint8*)ret + size, 0, tot_size - tot_size_unaligned);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
finish:
|
|
|
|
os_mutex_unlock(&heap->lock);
|
2019-05-07 02:18:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
#if BH_ENABLE_GC_VERIFY == 0
|
|
|
|
gc_object_t
|
|
|
|
gc_realloc_vo(void *vheap, void *ptr, gc_size_t size)
|
|
|
|
#else
|
|
|
|
gc_object_t
|
|
|
|
gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size,
|
|
|
|
const char *file, int line)
|
|
|
|
#endif
|
2020-03-08 13:18:18 +00:00
|
|
|
{
|
|
|
|
gc_heap_t* heap = (gc_heap_t*) vheap;
|
2020-04-13 02:49:40 +00:00
|
|
|
hmu_t *hmu = NULL, *hmu_old = NULL, *hmu_next;
|
2020-03-08 13:18:18 +00:00
|
|
|
gc_object_t ret = (gc_object_t) NULL, obj_old = (gc_object_t)ptr;
|
2020-04-13 02:49:40 +00:00
|
|
|
gc_size_t tot_size, tot_size_unaligned, tot_size_old = 0, tot_size_next;
|
2020-03-16 08:43:57 +00:00
|
|
|
gc_size_t obj_size, obj_size_old;
|
2020-10-22 10:52:33 +00:00
|
|
|
gc_uint8 *base_addr, *end_addr;
|
2020-04-13 02:49:40 +00:00
|
|
|
hmu_type_t ut;
|
2020-03-16 08:43:57 +00:00
|
|
|
|
|
|
|
/* hmu header + prefix + obj + suffix */
|
|
|
|
tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
|
|
|
|
/* aligned size*/
|
|
|
|
tot_size = GC_ALIGN_8(tot_size_unaligned);
|
|
|
|
if (tot_size < size)
|
|
|
|
/* integer overflow */
|
|
|
|
return NULL;
|
2020-03-08 13:18:18 +00:00
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
if (heap->is_heap_corrupted) {
|
|
|
|
os_printf("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-08 13:18:18 +00:00
|
|
|
if (obj_old) {
|
|
|
|
hmu_old = obj_to_hmu(obj_old);
|
2020-03-16 08:43:57 +00:00
|
|
|
tot_size_old = hmu_get_size(hmu_old);
|
|
|
|
if (tot_size <= tot_size_old)
|
|
|
|
/* current node alreay meets requirement */
|
2020-03-08 13:18:18 +00:00
|
|
|
return obj_old;
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
base_addr = heap->base_addr;
|
|
|
|
end_addr = base_addr + heap->current_size;
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
os_mutex_lock(&heap->lock);
|
2020-03-08 13:18:18 +00:00
|
|
|
|
2020-04-13 02:49:40 +00:00
|
|
|
if (hmu_old) {
|
|
|
|
hmu_next = (hmu_t*)((char *)hmu_old + tot_size_old);
|
2020-10-22 10:52:33 +00:00
|
|
|
if (hmu_is_in_heap(hmu_next, base_addr, end_addr)) {
|
2020-04-13 02:49:40 +00:00
|
|
|
ut = hmu_get_ut(hmu_next);
|
|
|
|
tot_size_next = hmu_get_size(hmu_next);
|
|
|
|
if (ut == HMU_FC
|
|
|
|
&& tot_size <= tot_size_old + tot_size_next) {
|
|
|
|
/* current node and next node meets requirement */
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!unlink_hmu(heap, hmu_next)) {
|
|
|
|
os_mutex_unlock(&heap->lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-04-13 02:49:40 +00:00
|
|
|
hmu_set_size(hmu_old, tot_size);
|
|
|
|
memset((char*)hmu_old + tot_size_old, 0, tot_size - tot_size_old);
|
|
|
|
#if BH_ENABLE_GC_VERIFY != 0
|
|
|
|
hmu_init_prefix_and_suffix(hmu_old, tot_size, file, line);
|
|
|
|
#endif
|
|
|
|
if (tot_size < tot_size_old + tot_size_next) {
|
|
|
|
hmu_next = (hmu_t*)((char*)hmu_old + tot_size);
|
|
|
|
tot_size_next = tot_size_old + tot_size_next - tot_size;
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!gci_add_fc(heap, hmu_next, tot_size_next)) {
|
|
|
|
os_mutex_unlock(&heap->lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-04-13 02:49:40 +00:00
|
|
|
}
|
|
|
|
os_mutex_unlock(&heap->lock);
|
|
|
|
return obj_old;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-08 13:18:18 +00:00
|
|
|
hmu = alloc_hmu_ex(heap, tot_size);
|
|
|
|
if (!hmu)
|
2020-03-30 03:06:39 +00:00
|
|
|
goto finish;
|
2020-03-08 13:18:18 +00:00
|
|
|
|
|
|
|
g_total_malloc += tot_size;
|
|
|
|
|
|
|
|
hmu_set_ut(hmu, HMU_VO);
|
|
|
|
hmu_unfree_vo(hmu);
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
#if BH_ENABLE_GC_VERIFY != 0
|
|
|
|
hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
|
2020-03-08 13:18:18 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = hmu_to_obj(hmu);
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
finish:
|
|
|
|
os_mutex_unlock(&heap->lock);
|
2020-03-08 13:18:18 +00:00
|
|
|
|
|
|
|
if (ret) {
|
2020-03-16 08:43:57 +00:00
|
|
|
obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
|
|
|
|
memset(ret, 0, obj_size);
|
2020-03-08 13:18:18 +00:00
|
|
|
if (obj_old) {
|
2020-03-16 08:43:57 +00:00
|
|
|
obj_size_old = tot_size_old - HMU_SIZE
|
|
|
|
- OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
|
|
|
|
bh_memcpy_s(ret, obj_size, obj_old, obj_size_old);
|
2020-03-30 03:06:39 +00:00
|
|
|
gc_free_vo(vheap, obj_old);
|
2020-03-08 13:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
/**
|
|
|
|
* Do some checking to see if given pointer is a possible valid heap
|
|
|
|
* @return GC_TRUE if all checking passed, GC_FALSE otherwise
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
gci_is_heap_valid(gc_heap_t *heap)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
if (!heap)
|
|
|
|
return GC_FALSE;
|
|
|
|
if (heap->heap_id != (gc_handle_t) heap)
|
|
|
|
return GC_FALSE;
|
|
|
|
|
|
|
|
return GC_TRUE;
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
#if BH_ENABLE_GC_VERIFY == 0
|
|
|
|
int
|
|
|
|
gc_free_vo(void *vheap, gc_object_t obj)
|
|
|
|
#else
|
|
|
|
int
|
|
|
|
gc_free_vo_internal(void *vheap, gc_object_t obj,
|
|
|
|
const char *file, int line)
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
gc_heap_t* heap = (gc_heap_t*) vheap;
|
2020-10-22 10:52:33 +00:00
|
|
|
gc_uint8 *base_addr, *end_addr;
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu_t *hmu = NULL;
|
|
|
|
hmu_t *prev = NULL;
|
|
|
|
hmu_t *next = NULL;
|
|
|
|
gc_size_t size = 0;
|
|
|
|
hmu_type_t ut;
|
|
|
|
int ret = GC_SUCCESS;
|
|
|
|
|
|
|
|
if (!obj) {
|
|
|
|
return GC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
if (heap->is_heap_corrupted) {
|
|
|
|
os_printf("[GC_ERROR]Heap is corrupted, free memory failed.\n");
|
|
|
|
return GC_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu = obj_to_hmu(obj);
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
base_addr = heap->base_addr;
|
|
|
|
end_addr = base_addr + heap->current_size;
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
os_mutex_lock(&heap->lock);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
if (hmu_is_in_heap(hmu, base_addr, end_addr)) {
|
2020-03-30 03:06:39 +00:00
|
|
|
#if BH_ENABLE_GC_VERIFY != 0
|
2020-10-22 10:52:33 +00:00
|
|
|
hmu_verify(heap, hmu);
|
2019-05-07 02:18:18 +00:00
|
|
|
#endif
|
|
|
|
ut = hmu_get_ut(hmu);
|
|
|
|
if (ut == HMU_VO) {
|
|
|
|
if (hmu_is_vo_freed(hmu)) {
|
|
|
|
bh_assert(0);
|
|
|
|
ret = GC_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = hmu_get_size(hmu);
|
|
|
|
|
|
|
|
g_total_free += size;
|
|
|
|
|
|
|
|
heap->total_free_size += size;
|
|
|
|
|
|
|
|
if (!hmu_get_pinuse(hmu)) {
|
|
|
|
prev = (hmu_t*) ((char*) hmu - *((int*) hmu - 1));
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
if (hmu_is_in_heap(prev, base_addr, end_addr)
|
|
|
|
&& hmu_get_ut(prev) == HMU_FC) {
|
2019-05-07 02:18:18 +00:00
|
|
|
size += hmu_get_size(prev);
|
|
|
|
hmu = prev;
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!unlink_hmu(heap, prev)) {
|
|
|
|
ret = GC_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next = (hmu_t*) ((char*) hmu + size);
|
2020-10-22 10:52:33 +00:00
|
|
|
if (hmu_is_in_heap(next, base_addr, end_addr)) {
|
2019-05-07 02:18:18 +00:00
|
|
|
if (hmu_get_ut(next) == HMU_FC) {
|
|
|
|
size += hmu_get_size(next);
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!unlink_hmu(heap, next)) {
|
|
|
|
ret = GC_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
next = (hmu_t*)((char*) hmu + size);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
if (!gci_add_fc(heap, hmu, size)) {
|
|
|
|
ret = GC_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
if (hmu_is_in_heap(next, base_addr, end_addr)) {
|
2019-05-07 02:18:18 +00:00
|
|
|
hmu_unmark_pinuse(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ret = GC_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = GC_SUCCESS;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
out:
|
|
|
|
os_mutex_unlock(&heap->lock);
|
2019-05-07 02:18:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
void
|
|
|
|
gc_dump_heap_stats(gc_heap_t *heap)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-03-16 08:43:57 +00:00
|
|
|
os_printf("heap: %p, heap start: %p\n", heap, heap->base_addr);
|
2020-03-30 03:06:39 +00:00
|
|
|
os_printf("total free: %u, current: %u, highmark: %u\n",
|
|
|
|
heap->total_free_size, heap->current_size, heap->highmark_size);
|
2020-03-16 08:43:57 +00:00
|
|
|
os_printf("g_total_malloc=%lu, g_total_free=%lu, occupied=%lu\n",
|
2020-03-30 03:06:39 +00:00
|
|
|
g_total_malloc, g_total_free, g_total_malloc - g_total_free);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 10:04:56 +00:00
|
|
|
uint32
|
|
|
|
gc_get_heap_highmark_size(gc_heap_t *heap)
|
|
|
|
{
|
|
|
|
return heap->highmark_size;
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
void
|
|
|
|
gci_dump(gc_heap_t *heap)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
hmu_t *cur = NULL, *end = NULL;
|
|
|
|
hmu_type_t ut;
|
|
|
|
gc_size_t size;
|
2020-03-30 03:06:39 +00:00
|
|
|
int i = 0, p, mark;
|
|
|
|
char inuse = 'U';
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
cur = (hmu_t*)heap->base_addr;
|
|
|
|
end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
while(cur < end) {
|
2019-05-07 02:18:18 +00:00
|
|
|
ut = hmu_get_ut(cur);
|
|
|
|
size = hmu_get_size(cur);
|
|
|
|
p = hmu_get_pinuse(cur);
|
|
|
|
mark = hmu_is_jo_marked (cur);
|
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
if (ut == HMU_VO)
|
|
|
|
inuse = 'V';
|
|
|
|
else if (ut == HMU_JO)
|
|
|
|
inuse = hmu_is_jo_marked(cur) ? 'J' : 'j';
|
|
|
|
else if (ut == HMU_FC)
|
|
|
|
inuse = 'F';
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
if (size == 0) {
|
|
|
|
os_printf("[GC_ERROR]Heap is corrupted, heap dump failed.\n");
|
|
|
|
heap->is_heap_corrupted = true;
|
|
|
|
return;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-30 03:06:39 +00:00
|
|
|
os_printf("#%d %08x %x %x %d %c %d\n",
|
|
|
|
i, (int32)((char*) cur - (char*) heap->base_addr),
|
|
|
|
ut, p, mark, inuse, (int32)hmu_obj_size(size));
|
2020-08-10 07:12:26 +00:00
|
|
|
#if BH_ENABLE_GC_VERIFY != 0
|
|
|
|
if (inuse == 'V') {
|
|
|
|
gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1);
|
|
|
|
os_printf("#%s:%d\n", prefix->file_name, prefix->line_no);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
cur = (hmu_t*)((char *)cur + size);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bh_assert(cur == end);
|
|
|
|
}
|
|
|
|
|