style: apply consistent formatting and remove unnecessary blank lines

This commit is contained in:
MechSlayer 2025-09-05 16:05:19 +02:00
parent 20e51fd6af
commit 121c54227a
10 changed files with 79 additions and 73 deletions

View file

@ -16,33 +16,33 @@ function(collect_platform_sources output_var source_dir)
# Define platform directories to exclude based on current platform # Define platform directories to exclude based on current platform
set(exclude_patterns) set(exclude_patterns)
if(WIN32) if (WIN32)
list(APPEND exclude_patterns "linux/" "macos/" "unix/") list(APPEND exclude_patterns "linux/" "macos/" "unix/")
elseif(UNIX AND NOT APPLE) elseif (UNIX AND NOT APPLE)
list(APPEND exclude_patterns "win32/" "windows/" "macos/") list(APPEND exclude_patterns "win32/" "windows/" "macos/")
elseif(APPLE) elseif (APPLE)
list(APPEND exclude_patterns "win32/" "windows/" "linux/") list(APPEND exclude_patterns "win32/" "windows/" "linux/")
endif() endif ()
# Filter out sources from excluded platform directories # Filter out sources from excluded platform directories
set(filtered_sources) set(filtered_sources)
foreach(source ${all_sources}) foreach (source ${all_sources})
set(exclude_file FALSE) set(exclude_file FALSE)
# Check if source is in any excluded platform directory # Check if source is in any excluded platform directory
foreach(pattern ${exclude_patterns}) foreach (pattern ${exclude_patterns})
string(FIND "${source}" "/${pattern}" found_pos) string(FIND "${source}" "/${pattern}" found_pos)
if(found_pos GREATER -1) if (found_pos GREATER -1)
set(exclude_file TRUE) set(exclude_file TRUE)
break() break()
endif() endif ()
endforeach() endforeach ()
# Add to filtered list if not excluded # Add to filtered list if not excluded
if(NOT exclude_file) if (NOT exclude_file)
list(APPEND filtered_sources "${source}") list(APPEND filtered_sources "${source}")
endif() endif ()
endforeach() endforeach ()
# Return the filtered list # Return the filtered list
set(${output_var} ${filtered_sources} PARENT_SCOPE) set(${output_var} ${filtered_sources} PARENT_SCOPE)
@ -52,17 +52,17 @@ collect_platform_sources(MODULE_PRIVATE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Sourc
collect_platform_sources(MODULE_PUBLIC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Include") collect_platform_sources(MODULE_PUBLIC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Include")
set(MODULE_FILES ${MODULE_PRIVATE_FILES} ${MODULE_PUBLIC_FILES}) set(MODULE_FILES ${MODULE_PRIVATE_FILES} ${MODULE_PUBLIC_FILES})
if(${BUILD_SHARED_LIBS}) if (${BUILD_SHARED_LIBS})
add_library(${MODULE_NAME} SHARED ${MODULE_FILES}) add_library(${MODULE_NAME} SHARED ${MODULE_FILES})
target_compile_definitions(${MODULE_NAME} PRIVATE DRANG_PLATFORM_EXPORT=1) target_compile_definitions(${MODULE_NAME} PRIVATE DRANG_PLATFORM_EXPORT=1)
else() else ()
add_library(${MODULE_NAME} STATIC ${MODULE_FILES}) add_library(${MODULE_NAME} STATIC ${MODULE_FILES})
target_compile_definitions(${MODULE_NAME} PRIVATE DRANG_PLATFORM_STATIC=1) target_compile_definitions(${MODULE_NAME} PRIVATE DRANG_PLATFORM_STATIC=1)
endif() endif ()
if(UNIX) if (UNIX)
target_compile_definitions(${MODULE_NAME} PRIVATE _GNU_SOURCE=1) target_compile_definitions(${MODULE_NAME} PRIVATE _GNU_SOURCE=1)
endif() endif ()
set_target_properties(${MODULE_NAME} PROPERTIES LINKER_LANGUAGE C) set_target_properties(${MODULE_NAME} PROPERTIES LINKER_LANGUAGE C)

View file

@ -89,8 +89,7 @@ DRANG_API void *drang_realloc_aligned(void *ptr, size_t size, size_t alignment);
* @remarks If the array grows, new elements are zero-initialized. Similar to _recalloc() on Windows. * @remarks If the array grows, new elements are zero-initialized. Similar to _recalloc() on Windows.
*/ */
DRANG_ALLOC_SIZE_COUNT_ATTR(2, 3) DRANG_ALLOC_SIZE_COUNT_ATTR(2, 3)
DRANG_API void *drang_recalloc( DRANG_API void *drang_recalloc(void *ptr, size_t new_num, size_t size);
void *ptr, size_t new_num, size_t size);
/** /**
* @brief Reallocates aligned memory for an array and initializes new elements to zero. * @brief Reallocates aligned memory for an array and initializes new elements to zero.
@ -104,8 +103,7 @@ DRANG_API void *drang_recalloc(
*/ */
DRANG_ALLOC_SIZE_COUNT_ATTR(2, 3) DRANG_ALLOC_SIZE_COUNT_ATTR(2, 3)
DRANG_ALLOC_ALIGN_ATTR(4) DRANG_ALLOC_ALIGN_ATTR(4)
DRANG_API void *drang_recalloc_aligned( DRANG_API void *drang_recalloc_aligned(void *ptr, size_t new_num, size_t size, size_t alignment);
void *ptr, size_t new_num, size_t size, size_t alignment);
/** /**
* @brief Frees previously allocated memory. * @brief Frees previously allocated memory.
@ -115,9 +113,6 @@ DRANG_API void *drang_recalloc_aligned(
*/ */
DRANG_API void drang_free(void *ptr); DRANG_API void drang_free(void *ptr);
/** /**
* @brief Internal implementation macro for allocation with error handling. * @brief Internal implementation macro for allocation with error handling.
* *
@ -126,7 +121,12 @@ DRANG_API void drang_free(void *ptr);
* @remarks This macro calls the allocation function and automatically handles NULL return * @remarks This macro calls the allocation function and automatically handles NULL return
* by triggering DRANG error handling with DRANG_ENOMEM. Uses GCC statement expressions. * by triggering DRANG error handling with DRANG_ENOMEM. Uses GCC statement expressions.
*/ */
#define DRANG_ALLOC_TRY_IMPL(_Func_) ({void* _tmp_ptr = (_Func_); DRANG_FAIL_IF_NULL_OOM(_tmp_ptr); _tmp_ptr; }) #define DRANG_ALLOC_TRY_IMPL(_Func_) \
({ \
void *_tmp_ptr = (_Func_); \
DRANG_FAIL_IF_NULL_OOM(_tmp_ptr); \
_tmp_ptr; \
})
/** /**
* @brief Allocates memory for a single object of the specified type with proper alignment. * @brief Allocates memory for a single object of the specified type with proper alignment.
@ -156,7 +156,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to reallocated memory cast to the appropriate type. * @return Pointer to reallocated memory cast to the appropriate type.
* @remarks Maintains proper alignment during reallocation. * @remarks Maintains proper alignment during reallocation.
*/ */
#define DRANG_REALLOC_T(_Ptr_, _Type_, _Num_) ((_Type_ *) drang_realloc_aligned((_Ptr_), sizeof(_Type_) * (_Num_), alignof(_Type_))) #define DRANG_REALLOC_T(_Ptr_, _Type_, _Num_) \
((_Type_ *) drang_realloc_aligned((_Ptr_), sizeof(_Type_) * (_Num_), alignof(_Type_)))
/** /**
* @brief Allocates memory with automatic error handling. * @brief Allocates memory with automatic error handling.
@ -196,7 +197,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to allocated zero-initialized aligned memory. * @return Pointer to allocated zero-initialized aligned memory.
* @remarks Automatically triggers DRANG error handling if allocation fails. * @remarks Automatically triggers DRANG error handling if allocation fails.
*/ */
#define DRANG_TRY_CALLOC_ALIGNED(_Num_, _Size_, _Alignment_) DRANG_ALLOC_TRY_IMPL(drang_calloc_aligned((_Num_), (_Size_), (_Alignment_))) #define DRANG_TRY_CALLOC_ALIGNED(_Num_, _Size_, _Alignment_) \
DRANG_ALLOC_TRY_IMPL(drang_calloc_aligned((_Num_), (_Size_), (_Alignment_)))
/** /**
* @brief Reallocates memory with automatic error handling. * @brief Reallocates memory with automatic error handling.
@ -217,7 +219,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to reallocated aligned memory. * @return Pointer to reallocated aligned memory.
* @remarks Automatically triggers DRANG error handling if allocation fails. * @remarks Automatically triggers DRANG error handling if allocation fails.
*/ */
#define DRANG_TRY_REALLOC_ALIGNED(_Ptr_, _Size_, _Alignment_) DRANG_ALLOC_TRY_IMPL(drang_realloc_aligned((_Ptr_), (_Size_), (_Alignment_))) #define DRANG_TRY_REALLOC_ALIGNED(_Ptr_, _Size_, _Alignment_) \
DRANG_ALLOC_TRY_IMPL(drang_realloc_aligned((_Ptr_), (_Size_), (_Alignment_)))
/** /**
* @brief Reallocates memory for an array with zero-initialization and automatic error handling. * @brief Reallocates memory for an array with zero-initialization and automatic error handling.
@ -228,7 +231,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to reallocated memory. * @return Pointer to reallocated memory.
* @remarks Automatically triggers DRANG error handling if allocation fails. New elements are zero-initialized. * @remarks Automatically triggers DRANG error handling if allocation fails. New elements are zero-initialized.
*/ */
#define DRANG_TRY_RECALLOC(_Ptr_, _New_Num_, _Size_) DRANG_ALLOC_TRY_IMPL(drang_recalloc((_Ptr_), (_New_Num_), (_Size_))) #define DRANG_TRY_RECALLOC(_Ptr_, _New_Num_, _Size_) \
DRANG_ALLOC_TRY_IMPL(drang_recalloc((_Ptr_), (_New_Num_), (_Size_)))
/** /**
* @brief Reallocates aligned memory for an array with zero-initialization and automatic error handling. * @brief Reallocates aligned memory for an array with zero-initialization and automatic error handling.
@ -240,9 +244,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to reallocated aligned memory. * @return Pointer to reallocated aligned memory.
* @remarks Automatically triggers DRANG error handling if allocation fails. New elements are zero-initialized. * @remarks Automatically triggers DRANG error handling if allocation fails. New elements are zero-initialized.
*/ */
#define DRANG_TRY_RECALLOC_ALIGNED(_Ptr_, _New_Num_, _Size_, _Alignment_) DRANG_ALLOC_TRY_IMPL(drang_recalloc_aligned((_Ptr_), (_New_Num_), (_Size_), (_Alignment_))) #define DRANG_TRY_RECALLOC_ALIGNED(_Ptr_, _New_Num_, _Size_, _Alignment_) \
DRANG_ALLOC_TRY_IMPL(drang_recalloc_aligned((_Ptr_), (_New_Num_), (_Size_), (_Alignment_)))
/** /**
* @brief Allocates memory for a single typed object with automatic error handling. * @brief Allocates memory for a single typed object with automatic error handling.

View file

@ -7,8 +7,7 @@
# elif defined(__linux__) # elif defined(__linux__)
# define DRANG_API __attribute__((visibility("default"))) # define DRANG_API __attribute__((visibility("default")))
# else # else
# error \ # error "Unknown platform, please implement shared library export macros"
"Unknown platform, please implement shared library export macros"
# endif # endif
# else # else
# ifdef _WIN32 # ifdef _WIN32
@ -16,8 +15,7 @@
# elif defined(__linux__) # elif defined(__linux__)
# define DRANG_API __attribute__((visibility("default"))) # define DRANG_API __attribute__((visibility("default")))
# else # else
# error \ # error "Unknown platform, please implement shared library import macros"
"Unknown platform, please implement shared library import macros"
# endif # endif
# endif # endif
#else #else
@ -32,20 +30,19 @@
# define DRANG_END_DECLS # define DRANG_END_DECLS
#endif #endif
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
#define DRANG_PRINTF_ATTR(fmt_idx, arg_idx) __attribute__((format(printf, fmt_idx, arg_idx))) # define DRANG_PRINTF_ATTR(fmt_idx, arg_idx) __attribute__((format(printf, fmt_idx, arg_idx)))
#define DRANG_VPRINTF_ATTR(fmt_idx) __attribute__((format(printf, fmt_idx, 0))) # define DRANG_VPRINTF_ATTR(fmt_idx) __attribute__((format(printf, fmt_idx, 0)))
#define DRANG_ALLOC_SIZE_COUNT_ATTR(count, idx) __attribute__((alloc_size(idx, count))) # define DRANG_ALLOC_SIZE_COUNT_ATTR(count, idx) __attribute__((alloc_size(idx, count)))
#define DRANG_ALLOC_SIZE_ATTR(idx) __attribute__((alloc_size(idx))) # define DRANG_ALLOC_SIZE_ATTR(idx) __attribute__((alloc_size(idx)))
#define DRANG_ALLOC_ALIGN_ATTR(idx) __attribute__((alloc_align(idx))) # define DRANG_ALLOC_ALIGN_ATTR(idx) __attribute__((alloc_align(idx)))
#define DRANG_MALLOC_ATTR __attribute__((malloc)) # define DRANG_MALLOC_ATTR __attribute__((malloc))
#else #else
#define DRANG_PRINTF_ATTR(fmt_idx, arg_idx) # define DRANG_PRINTF_ATTR(fmt_idx, arg_idx)
#define DRANG_VPRINTF_ATTR(fmt_idx) # define DRANG_VPRINTF_ATTR(fmt_idx)
#define DRANG_ALLOC_SIZE_COUNT_ATTR(idx, count) # define DRANG_ALLOC_SIZE_COUNT_ATTR(idx, count)
#define DRANG_ALLOC_SIZE_ATTR(idx) # define DRANG_ALLOC_SIZE_ATTR(idx)
#define DRANG_ALLOC_ALIGN_ATTR(idx) # define DRANG_ALLOC_ALIGN_ATTR(idx)
#define DRANG_MALLOC_ATTR # define DRANG_MALLOC_ATTR
#endif #endif

View file

@ -434,5 +434,4 @@ DRANG_API int drang_rwlock_rdunlock(struct drang_rwlock *rwlock);
*/ */
DRANG_API int drang_rwlock_wrunlock(struct drang_rwlock *rwlock); DRANG_API int drang_rwlock_wrunlock(struct drang_rwlock *rwlock);
DRANG_END_DECLS DRANG_END_DECLS

View file

@ -1,7 +1,6 @@
#include <drang/alloc.h> #include <drang/alloc.h>
#include <mimalloc.h> #include <mimalloc.h>
void *drang_alloc(const size_t size) void *drang_alloc(const size_t size)
{ {
return mi_malloc(size); return mi_malloc(size);
@ -30,8 +29,7 @@ void *drang_recalloc(void *ptr, const size_t new_num, const size_t size)
{ {
return mi_recalloc(ptr, new_num, size); return mi_recalloc(ptr, new_num, size);
} }
void *drang_recalloc_aligned( void *drang_recalloc_aligned(void *ptr, const size_t new_num, const size_t size, const size_t alignment)
void *ptr, const size_t new_num, const size_t size, const size_t alignment)
{ {
return mi_recalloc_aligned(ptr, new_num, size, alignment); return mi_recalloc_aligned(ptr, new_num, size, alignment);
} }

View file

@ -3,17 +3,29 @@
const char *drang_error_str(const int err) const char *drang_error_str(const int err)
{ {
switch (err) { switch (err) {
case DRANG_EOK: return "No error"; case DRANG_EOK:
case DRANG_EINVAL: return "Invalid argument"; return "No error";
case DRANG_ENOMEM: return "Out of memory"; case DRANG_EINVAL:
case DRANG_EIO: return "I/O error"; return "Invalid argument";
case DRANG_NOTSUP: return "Operation not supported"; case DRANG_ENOMEM:
case DRANG_EAGAIN: return "Resource temporarily unavailable"; return "Out of memory";
case DRANG_ENOENT: return "No such file or directory"; case DRANG_EIO:
case DRANG_EDEADLK: return "Resource deadlock would occur"; return "I/O error";
case DRANG_EPERM: return "Operation not permitted"; case DRANG_NOTSUP:
case DRANG_ETIMEDOUT: return "Connection timed out"; return "Operation not supported";
case DRANG_EBUSY: return "Device or resource busy"; case DRANG_EAGAIN:
default: return "Unknown error"; return "Resource temporarily unavailable";
case DRANG_ENOENT:
return "No such file or directory";
case DRANG_EDEADLK:
return "Resource deadlock would occur";
case DRANG_EPERM:
return "Operation not permitted";
case DRANG_ETIMEDOUT:
return "Connection timed out";
case DRANG_EBUSY:
return "Device or resource busy";
default:
return "Unknown error";
} }
} }

View file

@ -94,7 +94,7 @@ int drang_cond_wait(struct drang_cond *cond, struct drang_mutex *mutex)
int drang_cond_timedwait(struct drang_cond *cond, struct drang_mutex *mutex, uint64_t timeout_ms) int drang_cond_timedwait(struct drang_cond *cond, struct drang_mutex *mutex, uint64_t timeout_ms)
{ {
DWORD timeout = (timeout_ms == 0) ? INFINITE : (DWORD)timeout_ms; DWORD timeout = (timeout_ms == 0) ? INFINITE : (DWORD) timeout_ms;
BOOL result; BOOL result;
DRANG_BEGIN_TRY(); DRANG_BEGIN_TRY();
@ -113,4 +113,4 @@ int drang_cond_timedwait(struct drang_cond *cond, struct drang_mutex *mutex, uin
} }
DRANG_END_TRY_IGNORE(); DRANG_END_TRY_IGNORE();
} }

View file

@ -1,8 +1,8 @@
#pragma once #pragma once
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdbool.h> #include <stdbool.h>
#include <windows.h>
struct drang_mutex struct drang_mutex
{ {

View file

@ -1,9 +1,6 @@
#include "internal.h"
#include <drang/alloc.h> #include <drang/alloc.h>
#include <drang/sync.h> #include <drang/sync.h>
#include "internal.h"
size_t drang_mutex_size(void) size_t drang_mutex_size(void)
{ {

View file

@ -1,6 +1,6 @@
#include "internal.h"
#include <drang/alloc.h> #include <drang/alloc.h>
#include <drang/sync.h> #include <drang/sync.h>
#include "internal.h"
size_t drang_rwlock_size(void) size_t drang_rwlock_size(void)
{ {