add mi_new_realloc(n) to support C++ style reallocation that raises std::bad_alloc on out-of-memory

This commit is contained in:
daan 2020-01-20 15:41:56 -08:00
parent 146899af8a
commit 3957b2fd28
2 changed files with 24 additions and 1 deletions

View file

@ -721,3 +721,22 @@ void* mi_new_n(size_t count, size_t size) {
return mi_new(total);
}
}
void* mi_new_realloc(void* p, size_t newsize) {
void* q;
do {
q = mi_realloc(p, newsize);
} while (q == NULL && mi_try_new_handler(false));
return q;
}
void* mi_new_reallocn(void* p, size_t newcount, size_t size) {
size_t total;
if (mi_unlikely(mi_count_size_overflow(newcount, size, &total))) {
mi_try_new_handler(false); // on overflow we invoke the try_new_handler once to potentially throw std::bad_alloc
return NULL;
}
else {
return mi_new_realloc(p, total);
}
}