mirror of
https://github.com/amadvance/snapraid.git
synced 2026-11-29 11:26:48 -05:00
Avoid memory preinitialization in malloc_nofail and use calloc directly in calloc_nofail
This commit is contained in:
parent
ec71f519b0
commit
94c7d3ef9a
1 changed files with 14 additions and 15 deletions
|
|
@ -97,18 +97,6 @@ void* malloc_nofail(size_t size)
|
||||||
/* LCOV_EXCL_STOP */
|
/* LCOV_EXCL_STOP */
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef CHECKER /* Don't preinitialize when running for valgrind */
|
|
||||||
/*
|
|
||||||
* Here we preinitialize the memory to ensure that the OS is really allocating it
|
|
||||||
* and not only reserving the addressable space.
|
|
||||||
* Otherwise we are risking that the OOM (Out Of Memory) killer in Linux will kill the process.
|
|
||||||
* Filling the memory doesn't ensure to disable OOM, but it increase a lot the chances to
|
|
||||||
* get a real error from malloc() instead than a process killed.
|
|
||||||
* Note that calloc() doesn't have the same effect.
|
|
||||||
*/
|
|
||||||
memset(ptr, 0xA5, size);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
malloc_counter_inc(size);
|
malloc_counter_inc(size);
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
|
|
@ -123,7 +111,6 @@ void* nalloc_nofail(size_t count, size_t size)
|
||||||
|
|
||||||
size *= count;
|
size *= count;
|
||||||
|
|
||||||
/* see the note in malloc_nofail() of why we don't use calloc() */
|
|
||||||
void* ptr = malloc(size ? size : 1);
|
void* ptr = malloc(size ? size : 1);
|
||||||
|
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
|
|
@ -140,9 +127,21 @@ void* nalloc_nofail(size_t count, size_t size)
|
||||||
|
|
||||||
void* calloc_nofail(size_t count, size_t size)
|
void* calloc_nofail(size_t count, size_t size)
|
||||||
{
|
{
|
||||||
void* ptr = nalloc_nofail(count, size);
|
if (size != 0 && count > SIZE_MAX / size) {
|
||||||
|
log_fatal(EINTERNAL, "Allocation size overflow\n");
|
||||||
|
os_abort();
|
||||||
|
}
|
||||||
|
|
||||||
memset(ptr, 0, count * size);
|
void* ptr = calloc(count ? count : 1, size ? size : 1);
|
||||||
|
|
||||||
|
if (!ptr) {
|
||||||
|
/* LCOV_EXCL_START */
|
||||||
|
malloc_fail(count * size);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
/* LCOV_EXCL_STOP */
|
||||||
|
}
|
||||||
|
|
||||||
|
malloc_counter_inc(count * size);
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue