%% options copyright owner = Dirk Krause copyright year = 2015-xxxx SPDX-License-Identifier: BSD-3-Clause %% header /** @file Memory allocation with included diagnostics. */ #ifndef DK4CONF_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4conf.h" #else #include #endif #endif #ifndef DK4TYPES_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4types.h" #else #include #endif #endif #ifndef DK4APP_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4app.h" #else #include #endif #endif #ifdef __cplusplus extern "C" { #endif /** Allocate and initialize memory. @param elsize Element size. @param nelem Number of elements. @param app Application structure for diagnostics, may be NULL. @return Pointer to new memory on success, NULL on error. */ void * dk4mem_calloc_app(size_t elsize, size_t nelem, dk4_app_t *app); #ifdef __cplusplus } #endif /** Allocate and initialize memory. @param tp Type to allocate. @param ne Number of elements to allocate. @param app Application structure for diagnostics, may be NULL. @return Pointer to a new tp on success, NULL on error. */ #define dk4mem_new_app(tp,ne,app) (tp *)dk4mem_calloc_app(sizeof(tp),ne,app) %% module #include "dk4conf.h" #include "dk4mema.h" #include "dk4mem.h" #include "dk4maodd.h" #include "dk4const.h" #if DK4_HAVE_ASSERT_H #ifndef ASSERT_H_INCLUDED #include #define ASSERT_H_INCLUDED 1 #endif #endif void * dk4mem_calloc_app(size_t elsize, size_t nelem, dk4_app_t *app) { dkChar elsb[64]; dkChar nelb[64]; void *back = NULL; dk4_er_t er; int res; #if DK4_USE_ASSERT assert(0 < elsize); assert(0 < nelem); #endif dk4error_init(&er); back = dk4mem_calloc(elsize, nelem, &er); if (NULL == back) { res = dk4ma_write_decimal_unsigned( elsb, DK4_SIZEOF(elsb,dkChar), (dk4_um_t)elsize, 0, NULL ); if (0 != res) { res = dk4ma_write_decimal_unsigned( nelb, DK4_SIZEOF(nelb,dkChar), (dk4_um_t)nelem, 0, NULL ); } switch (er.ec) { case DK4_E_MATH_OVERFLOW: { /* ERROR: Numeric overflow in size calculation */ if (0 != res) { dk4app_log_base5(app,DK4_LL_ERROR,83,84,85,nelb,elsb); } else { dk4app_log_base1(app, DK4_LL_ERROR, 82); } } break; case DK4_E_INVALID_ARGUMENTS: { /* ERROR: Invalid sizes */ if (0 != res) { dk4app_log_base5(app,DK4_LL_ERROR,87,88,89,nelb,elsb); } else { dk4app_log_base1(app, DK4_LL_ERROR, 86); } } break; default: { /* ERROR: Not enough memory */ if (0 != res) { dk4app_log_base5(app,DK4_LL_ERROR,91,92,93,nelb,elsb); } else { dk4app_log_base1(app, DK4_LL_ERROR, 90); } } break; } } return back; }