feature: add zero-initialized memory allocation macros and error handling for overflow
This commit is contained in:
parent
4f3ae943db
commit
f3d307c03e
6 changed files with 59 additions and 1 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -11,4 +11,6 @@ build-*/
|
|||
CMakeLists.txt.user
|
||||
|
||||
# CMake
|
||||
CMakeUserPresets.json
|
||||
CMakeUserPresets.json
|
||||
|
||||
Playground/
|
||||
|
|
@ -33,3 +33,7 @@ set(MI_BUILD_OBJECT OFF CACHE BOOL "" FORCE)
|
|||
FetchContent_MakeAvailable(mimalloc)
|
||||
|
||||
add_subdirectory(Source/DrangPlatform)
|
||||
|
||||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Playground)
|
||||
add_subdirectory(Playground)
|
||||
endif()
|
||||
|
|
|
|||
|
|
@ -147,6 +147,25 @@ DRANG_PLATFORM_API void drang_free(void *ptr);
|
|||
*/
|
||||
#define DRANG_ALLOC_T_N(_Type_, _Num_) ((_Type_ *) drang_alloc_aligned(sizeof(_Type_) * (_Num_), alignof(_Type_)))
|
||||
|
||||
/**
|
||||
* @brief Allocates zero-initialized memory for a single object of the specified type with proper alignment.
|
||||
*
|
||||
* @param _Type_ The type to allocate memory for.
|
||||
* @return Pointer to allocated zero-initialized memory cast to the appropriate type.
|
||||
* @remarks Automatically calculates total size and uses proper alignment for the type.
|
||||
*/
|
||||
#define DRANG_CALLOC_T(_Type_) ((_Type_ *) drang_calloc_aligned(1, sizeof(_Type_), alignof(_Type_)))
|
||||
|
||||
/**
|
||||
* @brief Allocates zero-initialized memory for an array of objects of the specified type with proper alignment.
|
||||
*
|
||||
* @param _Type_ The type to allocate memory for.
|
||||
* @param _Num_ Number of objects to allocate.
|
||||
* @return Pointer to allocated zero-initialized memory cast to the appropriate type.
|
||||
* @remarks Automatically calculates total size and uses proper alignment for the type.
|
||||
*/
|
||||
#define DRANG_CALLOC_T_N(_Type_, _Num_) ((_Type_ *) drang_calloc_aligned((_Num_), sizeof(_Type_), alignof(_Type_)))
|
||||
|
||||
/**
|
||||
* @brief Reallocates memory for an array of objects of the specified type with proper alignment.
|
||||
*
|
||||
|
|
@ -266,6 +285,24 @@ DRANG_PLATFORM_API void drang_free(void *ptr);
|
|||
*/
|
||||
#define DRANG_TRY_ALLOC_T_N(_Type_, _Num_) DRANG_ALLOC_TRY_IMPL(DRANG_ALLOC_T_N(_Type_, (_Num_)))
|
||||
|
||||
/**
|
||||
* @brief Allocates zero-initialized memory for a typed object with automatic error handling.
|
||||
*
|
||||
* @param _Type_ The type to allocate memory for.
|
||||
* @return Pointer to allocated zero-initialized memory cast to the appropriate type.
|
||||
* @remarks Combines type-safe allocation with automatic error handling. Preferred for single object allocation.
|
||||
*/
|
||||
#define DRANG_TRY_CALLOC_T(_Type_) DRANG_ALLOC_TRY_IMPL(DRANG_CALLOC_T(_Type_))
|
||||
|
||||
/**
|
||||
* @brief Allocates zero-initialized memory for a typed array with automatic error handling.
|
||||
*
|
||||
* @param _Type_ The type to allocate memory for.
|
||||
* @param _Num_ Number of objects to allocate.
|
||||
* @return Pointer to allocated zero-initialized memory cast to the appropriate type.
|
||||
*/
|
||||
#define DRANG_TRY_CALLOC_T_N(_Type_, _Num_) DRANG_ALLOC_TRY_IMPL(DRANG_CALLOC_T_N(_Type_, (_Num_)))
|
||||
|
||||
/**
|
||||
* @brief Reallocates memory for a typed array with automatic error handling.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -21,6 +21,9 @@ DRANG_PLATFORM_API const char *drang_error_str(int err);
|
|||
#define DRANG_EPERM (-8) // Operation not permitted
|
||||
#define DRANG_ETIMEDOUT (-9) // Connection timed out
|
||||
#define DRANG_EBUSY (-10) // Device or resource busy
|
||||
#define DRANG_EOVERFLOW (-11) // Value too large to be stored in data type
|
||||
#define DRANG_ENOSPC (-12) // No space left on device
|
||||
#define DRANG_EROFS (-13) // Read-only file system
|
||||
#define DRANG_EPLATFORM (-1000) // An unknown platform-specific error occurred
|
||||
|
||||
/**
|
||||
|
|
@ -170,4 +173,6 @@ DRANG_PLATFORM_API const char *drang_error_str(int err);
|
|||
*/
|
||||
#define DRANG_FAIL_IF_NULL_OOM(ptr) DRANG_FAIL_IF_NULL(ptr, DRANG_ENOMEM)
|
||||
|
||||
|
||||
|
||||
DRANG_END_DECLS
|
||||
|
|
|
|||
|
|
@ -39,6 +39,12 @@
|
|||
|
||||
#define DRANG_BIT_CAST(_Expr_, _From_, _To_) __builtin_bit_cast(_To_, _Expr_)
|
||||
|
||||
#define DRANG_MUL_OVERFLOW(_A_, _B_, _Result_) (__builtin_mul_overflow(_A_, _B_, _Result_) != 0)
|
||||
#define DRANG_ADD_OVERFLOW(_A_, _B_, _Result_) (__builtin_add_overflow(_A_, _B_, _Result_) != 0)
|
||||
#define DRANG_SUB_OVERFLOW(_A_, _B_, _Result_) (__builtin_sub_overflow(_A_, _B_, _Result_) != 0)
|
||||
|
||||
#else
|
||||
#error "Unsupported compiler, please implement attribute macros"
|
||||
#endif
|
||||
|
||||
#define DRANG_UNUSED(x) ((void)(x))
|
||||
|
|
@ -24,6 +24,8 @@ int drang_error_to_errno(int error)
|
|||
return ETIMEDOUT;
|
||||
case DRANG_EBUSY:
|
||||
return EBUSY;
|
||||
case DRANG_EOVERFLOW:
|
||||
return EOVERFLOW;
|
||||
case DRANG_EPLATFORM:
|
||||
default:
|
||||
return EIO; // Generic I/O error for unknown platform errors
|
||||
|
|
@ -50,6 +52,8 @@ int drang_errno_to_error(const int errnum)
|
|||
return DRANG_ETIMEDOUT;
|
||||
case EBUSY:
|
||||
return DRANG_EBUSY;
|
||||
case EOVERFLOW:
|
||||
return DRANG_EOVERFLOW;
|
||||
default:
|
||||
return DRANG_EPLATFORM;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue