make the behavior of ob_realloc consistent with standard realloc when memory allocation fails

This commit is contained in:
gaopy3 2024-11-01 06:47:33 +00:00 committed by ob-robot
parent 24574c16f7
commit 82f4a136bb
3 changed files with 25 additions and 19 deletions

View File

@ -478,7 +478,8 @@ void* ObTenantCtxAllocator::common_realloc(const void *ptr, const int64_t size,
LIB_LOG_RET(ERROR, OB_INVALID_ARGUMENT, "OB_MOD_DO_NOT_USE_ME REALLOC", K(size));
}
AObject *obj = NULL;
AObject *obj = NULL; // original object
AObject *nobj = NULL; // newly allocated object
bool sample_allowed = false;
bool is_errsim = false;
if (NULL != ptr) {
@ -489,7 +490,7 @@ void* ObTenantCtxAllocator::common_realloc(const void *ptr, const int64_t size,
const ObErrsimModuleType type = THIS_WORKER.get_module_type();
if (is_errsim_module(ta.get_tenant_id(), type.type_)) {
//errsim alloc memory failed.
obj = nullptr;
nobj = nullptr;
is_errsim = true;
}
#endif
@ -501,17 +502,13 @@ void* ObTenantCtxAllocator::common_realloc(const void *ptr, const int64_t size,
if (OB_UNLIKELY(OB_FAIL(ret) || is_errsim)) {
AllocFailedCtx &afc = g_alloc_failed_ctx();
afc.reason_ = AllocFailedReason::ERRSIM_INJECTION;
if (OB_NOT_NULL(obj)) {
allocator.free_object(obj);
obj = NULL;
}
} else {
BASIC_TIME_GUARD(time_guard, "ObMalloc");
DEFER(ObMallocTimeMonitor::get_instance().record_malloc_time(time_guard, size, inner_attr));
sample_allowed = ObMallocSampleLimiter::malloc_sample_allowed(size, inner_attr);
inner_attr.alloc_extra_info_ = sample_allowed;
obj = allocator.realloc_object(obj, size, inner_attr);
if(OB_ISNULL(obj)) {
nobj = allocator.realloc_object(obj, size, inner_attr);
if(OB_ISNULL(nobj)) {
int64_t total_size = 0;
if (g_alloc_failed_ctx().need_wash_block()) {
total_size += ta.sync_wash();
@ -521,14 +518,18 @@ void* ObTenantCtxAllocator::common_realloc(const void *ptr, const int64_t size,
BASIC_TIME_GUARD_CLICK("WASH_CHUNK_END");
}
if (total_size > 0) {
obj = allocator.realloc_object(obj, size, inner_attr);
nobj = allocator.realloc_object(obj, size, inner_attr);
}
}
}
if (obj != NULL) {
on_alloc(*obj, inner_attr);
nptr = obj->data_;
if (OB_UNLIKELY(NULL == nobj && NULL != obj)) {
SANITY_UNPOISON(obj->data_, obj->alloc_bytes_);
}
if (OB_NOT_NULL(nobj)) {
on_alloc(*nobj, inner_attr);
nptr = nobj->data_;
} else if (TC_REACH_TIME_INTERVAL(1 * 1000 * 1000)) {
#ifdef FATAL_ERROR_HANG
if (REACH_TIME_INTERVAL(60 * 1000 * 1000)) {

View File

@ -106,11 +106,12 @@ AObject *ObjectSet::realloc_object(
abort_unless(obj->is_valid());
uint64_t copy_size = MIN(obj->alloc_bytes_, size);
new_obj = alloc_object(size, attr);
if (NULL != new_obj && copy_size != 0) {
memmove(new_obj->data_, obj->data_, copy_size);
if (NULL != new_obj) {
if (copy_size != 0) {
memmove(new_obj->data_, obj->data_, copy_size);
}
do_free_object(obj);
}
do_free_object(obj);
}
return new_obj;

View File

@ -1456,10 +1456,14 @@ static int ob_coll_rule_expand(ob_wc_t *wc, size_t limit, ob_wc_t code) {
}
static void ob_coll_rule_reset(ObCollRule *r) { memset(r, 0, sizeof(*r)); }
static int ob_coll_rules_realloc(ObCollRules *rules, size_t n) {
if (rules->nrules < rules->mrules ||
(rules->rule = static_cast<ObCollRule *>(rules->loader->mem_realloc(
rules->rule, sizeof(ObCollRule) * (rules->mrules = n + 128)))))
ObCollRule *new_rule = nullptr;
if (rules->nrules < rules->mrules)
return 0;
if ((new_rule = static_cast<ObCollRule *>(rules->loader->mem_realloc(
rules->rule, sizeof(ObCollRule) * (rules->mrules = n + 128))))) {
rules->rule = new_rule;
return 0;
}
return -1;
}
static int ob_coll_rules_add(ObCollRules *rules, ObCollRule *rule) {