fix memory leak

This commit is contained in:
nroskill
2022-01-27 10:58:30 +08:00
committed by LINxiansheng
parent b0e85791f2
commit 57528cd7e4
14 changed files with 80 additions and 151 deletions

View File

@ -24,17 +24,12 @@
using namespace oceanbase::lib;
using namespace oceanbase::common;
ObMallocAllocator* ObMallocAllocator::instance_ = NULL;
uint64_t ObMallocAllocator::max_used_tenant_id_ = 0;
bool ObMallocAllocator::is_inited_ = false;
ObMallocAllocator::ObMallocAllocator() : locks_(), allocators_(), reserved_(0), urgent_(0)
{}
ObMallocAllocator::~ObMallocAllocator()
{}
int ObMallocAllocator::init()
{
set_root_allocator();
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < ObCtxIds::MAX_CTX_ID; i++) {
if (OB_FAIL(create_tenant_ctx_allocator(OB_SYS_TENANT_ID, i))) {
@ -43,7 +38,12 @@ int ObMallocAllocator::init()
LOG_ERROR("create tenant allocator fail", K(ret), K(i));
}
}
return ret;
is_inited_ = true;
}
ObMallocAllocator::~ObMallocAllocator()
{
is_inited_ = false;
}
void* ObMallocAllocator::alloc(const int64_t size)
@ -321,60 +321,21 @@ int ObMallocAllocator::delete_tenant_ctx_allocator(uint64_t tenant_id)
return ret;
}
int ObMallocAllocator::set_root_allocator(ObTenantCtxAllocator* allocator)
void ObMallocAllocator::set_root_allocator()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(allocator)) {
ret = OB_INVALID_ARGUMENT;
} else if (!OB_ISNULL(allocators_[OB_SERVER_TENANT_ID][0])) {
ret = OB_ENTRY_EXIST;
static ObTenantCtxAllocator allocator(OB_SERVER_TENANT_ID);
if (OB_FAIL(allocator.set_tenant_memory_mgr())) {
LOG_ERROR("set_tenant_memory_mgr failed", K(ret));
} else {
allocators_[OB_SERVER_TENANT_ID][0] = allocator;
allocators_[OB_SERVER_TENANT_ID][0] = &allocator;
}
return ret;
}
ObMallocAllocator* ObMallocAllocator::get_instance()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(ObMallocAllocator::instance_)) {
ObMallocAllocator* malloc_allocator = NULL;
ObTenantCtxAllocator* allocator = new (std::nothrow) ObTenantCtxAllocator(OB_SERVER_TENANT_ID);
if (!OB_ISNULL(allocator)) {
if (OB_FAIL(allocator->set_tenant_memory_mgr())) {
LOG_ERROR("set_tenant_memory_mgr failed", K(ret));
} else {
ObMemAttr attr;
attr.tenant_id_ = OB_SERVER_TENANT_ID;
attr.label_ = ObModIds::OB_TENANT_CTX_ALLOCATOR;
void* buf = allocator->alloc(sizeof(ObMallocAllocator), attr);
if (!OB_ISNULL(buf)) {
malloc_allocator = new (buf) ObMallocAllocator();
ret = malloc_allocator->set_root_allocator(allocator);
} else {
ret = OB_ALLOCATE_MEMORY_FAILED;
}
}
} else {
ret = OB_ALLOCATE_MEMORY_FAILED;
}
if (OB_FAIL(ret)) {
if (!OB_ISNULL(malloc_allocator)) {
malloc_allocator->~ObMallocAllocator();
allocator->free(malloc_allocator);
malloc_allocator = NULL;
}
if (!OB_ISNULL(allocator)) {
delete allocator;
allocator = NULL;
}
} else {
ObMallocAllocator::instance_ = malloc_allocator;
}
}
return ObMallocAllocator::instance_;
static ObMallocAllocator instance;
return &instance;
}
int ObMallocAllocator::with_resource_handle_invoke(uint64_t tenant_id, InvokeFunc func)