fix memory leak
This commit is contained in:
69
deps/oblib/src/lib/alloc/ob_malloc_allocator.cpp
vendored
69
deps/oblib/src/lib/alloc/ob_malloc_allocator.cpp
vendored
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user