diff --git a/.gitignore b/.gitignore index 0796bd3..932a2e4 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,6 @@ build-*/ CMakeLists.txt.user # CMake -CMakeUserPresets.json \ No newline at end of file +CMakeUserPresets.json + +Playground/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 1760c94..a411444 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/Source/DrangPlatform/Include/drang/alloc.h b/Source/DrangPlatform/Include/drang/alloc.h index 2390210..4ae175b 100644 --- a/Source/DrangPlatform/Include/drang/alloc.h +++ b/Source/DrangPlatform/Include/drang/alloc.h @@ -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. * diff --git a/Source/DrangPlatform/Include/drang/error.h b/Source/DrangPlatform/Include/drang/error.h index 6100d59..8641e9a 100644 --- a/Source/DrangPlatform/Include/drang/error.h +++ b/Source/DrangPlatform/Include/drang/error.h @@ -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 diff --git a/Source/DrangPlatform/Include/drang/platform.h b/Source/DrangPlatform/Include/drang/platform.h index d162d0b..b4bca46 100644 --- a/Source/DrangPlatform/Include/drang/platform.h +++ b/Source/DrangPlatform/Include/drang/platform.h @@ -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)) \ No newline at end of file diff --git a/Source/DrangPlatform/Source/errno_convert.c b/Source/DrangPlatform/Source/errno_convert.c index 0ec599f..c993411 100644 --- a/Source/DrangPlatform/Source/errno_convert.c +++ b/Source/DrangPlatform/Source/errno_convert.c @@ -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; }