Introduce nalloc_nofail

This commit is contained in:
Andrea Mazzoleni 2026-07-06 18:38:50 +02:00
parent 8de159289a
commit bbcdb72a9f
12 changed files with 43 additions and 39 deletions

View file

@ -975,8 +975,8 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna
memset(buffer[buffermax - 1], 0, state->block_size);
raid_zero(buffer[buffermax - 1]);
failed = malloc_nofail(diskmax * sizeof(struct failed_struct));
failed_map = malloc_nofail(diskmax * sizeof(unsigned));
failed = nalloc_nofail(diskmax, sizeof(struct failed_struct));
failed_map = nalloc_nofail(diskmax, sizeof(unsigned));
soft_error = 0;
io_error = 0;

View file

@ -171,7 +171,7 @@ static int state_dry_process(struct snapraid_state* state, struct snapraid_parit
/* possibly waiting disks */
waiting_mac = diskmax > RAID_PARITY_MAX ? diskmax : RAID_PARITY_MAX;
waiting_map = malloc_nofail(waiting_mac * sizeof(unsigned));
waiting_map = nalloc_nofail(waiting_mac, sizeof(unsigned));
soft_error = 0;
io_error = 0;

View file

@ -33,7 +33,7 @@ struct snapraid_hash* hash_alloc(struct snapraid_state* state, struct snapraid_d
hash->disk = disk;
hash->file = file;
buf = malloc_nofail(file->blockmax * hash_size);
buf = nalloc_nofail(file->blockmax, hash_size);
/* set the back pointer */
for (i = 0; i < file->blockmax; ++i) {

View file

@ -412,7 +412,7 @@ struct snapraid_file* file_alloc(unsigned block_size, const char* sub, data_off_
file->physical = physical;
file->flag = 0;
file->shared_flag = 0;
file->blockvec = malloc_nofail(file->blockmax * block_sizeof());
file->blockvec = nalloc_nofail(file->blockmax, block_sizeof());
for (i = 0; i < file->blockmax; ++i) {
struct snapraid_block* block = file_block(file, i);
@ -437,7 +437,7 @@ struct snapraid_file* file_dup(struct snapraid_file* copy)
file->inode = copy->inode;
file->physical = copy->physical;
file->flag = copy->flag;
file->blockvec = malloc_nofail(file->blockmax * block_sizeof());
file->blockvec = nalloc_nofail(file->blockmax, block_sizeof());
for (i = 0; i < file->blockmax; ++i) {
struct snapraid_block* block = file_block(file, i);
@ -781,7 +781,7 @@ struct snapraid_dealloc* dealloc_alloc(unsigned block_size, const char* sub, dat
dealloc->mtime_sec = mtime_sec;
dealloc->mtime_nsec = mtime_nsec;
dealloc->blockmax = (size + block_size - 1) / block_size;
dealloc->blockhash = malloc_nofail(dealloc->blockmax * BLOCK_HASH_SIZE);
dealloc->blockhash = nalloc_nofail(dealloc->blockmax, BLOCK_HASH_SIZE);
return dealloc;
}

View file

@ -382,7 +382,7 @@ struct snapraid_handle* handle_mapping(struct snapraid_state* state, unsigned* h
}
++size; /* size is one more than the max */
handle = malloc_nofail(size * sizeof(struct snapraid_handle));
handle = nalloc_nofail(size, sizeof(struct snapraid_handle));
for (j = 0; j < size; ++j) {
/* default for empty position */

View file

@ -54,7 +54,7 @@ static void import_file(struct snapraid_state* state, const char* path, uint64_t
file->path = strdup_nofail(path);
file->size = size;
file->blockmax = (size + block_size - 1) / block_size;
file->blockimp = malloc_nofail(file->blockmax * sizeof(struct snapraid_import_block));
file->blockimp = nalloc_nofail(file->blockmax, sizeof(struct snapraid_import_block));
file->is_runtime = 1;
buffer = malloc_nofail(block_size);
@ -144,7 +144,7 @@ static void import_dealloc(struct snapraid_state* state, const char* dir, struct
file->path = strdup_nofail(path);
file->size = size;
file->blockmax = (size + block_size - 1) / block_size;
file->blockimp = malloc_nofail(file->blockmax * sizeof(struct snapraid_import_block));
file->blockimp = nalloc_nofail(file->blockmax, sizeof(struct snapraid_import_block));
file->is_runtime = 0;
offset = 0;

View file

@ -1021,10 +1021,10 @@ void io_init(struct snapraid_io* io, struct snapraid_state* state,
io->reader_max = readers_count;
io->writer_max = writers_count;
io->reader_map = malloc_nofail(sizeof(struct snapraid_worker) * io->reader_max);
io->reader_list = malloc_nofail(sizeof(unsigned) * (io->reader_max + 1));
io->writer_map = malloc_nofail(sizeof(struct snapraid_worker) * io->writer_max);
io->writer_list = malloc_nofail(sizeof(unsigned) * (io->writer_max + 1));
io->reader_map = nalloc_nofail(io->reader_max, sizeof(struct snapraid_worker));
io->reader_list = nalloc_nofail(io->reader_max + 1, sizeof(unsigned));
io->writer_map = nalloc_nofail(io->writer_max, sizeof(struct snapraid_worker));
io->writer_list = nalloc_nofail(io->writer_max + 1, sizeof(unsigned));
io->data_base = 0;
io->data_count = 0;

View file

@ -275,7 +275,7 @@ static int state_scrub_process(struct snapraid_state* state, struct snapraid_par
/* possibly waiting disks */
waiting_mac = diskmax > RAID_PARITY_MAX ? diskmax : RAID_PARITY_MAX;
waiting_map = malloc_nofail(waiting_mac * sizeof(unsigned));
waiting_map = nalloc_nofail(waiting_mac, sizeof(unsigned));
soft_error = 0;
silent_error = 0;

View file

@ -91,7 +91,7 @@ STREAM* sopen_multi_write(unsigned count, int flags)
STREAM* s = malloc_nofail(sizeof(STREAM));
s->handle_size = count;
s->handle = malloc_nofail(count * sizeof(struct stream_handle));
s->handle = nalloc_nofail(count, sizeof(struct stream_handle));
s->flags = flags;
for (i = 0; i < count; ++i)

View file

@ -2211,14 +2211,17 @@ void* malloc_nofail(size_t size)
return ptr;
}
void* calloc_nofail(size_t count, size_t size)
void* nalloc_nofail(size_t count, size_t size)
{
void* ptr;
if (count > SIZE_MAX / size) {
log_fatal(EINTERNAL, "Allocation size overflow\n");
os_abort();
}
size *= count;
/* see the note in malloc_nofail() of why we don't use calloc() */
ptr = malloc(size ? size : 1);
void* ptr = malloc(size ? size : 1);
if (!ptr) {
/* LCOV_EXCL_START */
@ -2227,33 +2230,28 @@ void* calloc_nofail(size_t count, size_t size)
/* LCOV_EXCL_STOP */
}
memset(ptr, 0, size);
malloc_counter_inc(size);
return ptr;
}
void* calloc_nofail(size_t count, size_t size)
{
void* ptr = nalloc_nofail(count, size);
memset(ptr, 0, count * size);
return ptr;
}
char* strdup_nofail(const char* str)
{
size_t size;
char* ptr;
size_t size = strlen(str) + 1;
size = strlen(str) + 1;
ptr = malloc(size);
if (!ptr) {
/* LCOV_EXCL_START */
malloc_fail(size);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
char* ptr = malloc_nofail(size);
memcpy(ptr, str, size);
malloc_counter_inc(size);
return ptr;
}

View file

@ -534,7 +534,13 @@ size_t malloc_counter_get(void);
void* malloc_nofail(size_t size);
/**
* Safe cmalloc.
* Safe malloc with calloc calling arguments.
* If no memory is available, it aborts.
*/
void* nalloc_nofail(size_t count, size_t size);
/**
* Safe calloc.
* If no memory is available, it aborts.
*/
void* calloc_nofail(size_t count, size_t size);

View file

@ -693,12 +693,12 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari
memset(zero, 0, state->block_size);
raid_zero(zero);
failed = malloc_nofail(diskmax * sizeof(struct failed_struct));
failed_map = malloc_nofail(diskmax * sizeof(unsigned));
failed = nalloc_nofail(diskmax, sizeof(struct failed_struct));
failed_map = nalloc_nofail(diskmax, sizeof(unsigned));
/* possibly waiting disks */
waiting_mac = diskmax > RAID_PARITY_MAX ? diskmax : RAID_PARITY_MAX;
waiting_map = malloc_nofail(waiting_mac * sizeof(unsigned));
waiting_map = nalloc_nofail(waiting_mac, sizeof(unsigned));
soft_error = 0;
silent_error = 0;