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
set(exclude_patterns)
if(WIN32)
if (WIN32)
list(APPEND exclude_patterns "linux/" "macos/" "unix/")
elseif(UNIX AND NOT APPLE)
elseif (UNIX AND NOT APPLE)
list(APPEND exclude_patterns "win32/" "windows/" "macos/")
elseif(APPLE)
elseif (APPLE)
list(APPEND exclude_patterns "win32/" "windows/" "linux/")
endif()
endif ()
# Filter out sources from excluded platform directories
set(filtered_sources)
foreach(source ${all_sources})
foreach (source ${all_sources})
set(exclude_file FALSE)
# Check if source is in any excluded platform directory
foreach(pattern ${exclude_patterns})
foreach (pattern ${exclude_patterns})
string(FIND "${source}" "/${pattern}" found_pos)
if(found_pos GREATER -1)
if (found_pos GREATER -1)
set(exclude_file TRUE)
break()
endif()
endforeach()
endif ()
endforeach ()
# Add to filtered list if not excluded
if(NOT exclude_file)
if (NOT exclude_file)
list(APPEND filtered_sources "${source}")
endif()
endforeach()
endif ()
endforeach ()
# Return the filtered list
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")
set(MODULE_FILES ${MODULE_PRIVATE_FILES} ${MODULE_PUBLIC_FILES})
if(${BUILD_SHARED_LIBS})
if (${BUILD_SHARED_LIBS})
add_library(${MODULE_NAME} SHARED ${MODULE_FILES})
target_compile_definitions(${MODULE_NAME} PRIVATE DRANG_PLATFORM_EXPORT=1)
else()
else ()
add_library(${MODULE_NAME} STATIC ${MODULE_FILES})
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)
endif()
endif ()
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.
*/
DRANG_ALLOC_SIZE_COUNT_ATTR(2, 3)
DRANG_API void *drang_recalloc(
void *ptr, size_t new_num, size_t size);
DRANG_API void *drang_recalloc(void *ptr, size_t new_num, size_t size);
/**
* @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_ALIGN_ATTR(4)
DRANG_API void *drang_recalloc_aligned(
void *ptr, size_t new_num, size_t size, size_t alignment);
DRANG_API void *drang_recalloc_aligned(void *ptr, size_t new_num, size_t size, size_t alignment);
/**
* @brief Frees previously allocated memory.
@ -115,9 +113,6 @@ DRANG_API void *drang_recalloc_aligned(
*/
DRANG_API void drang_free(void *ptr);
/**
* @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
* 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.
@ -156,7 +156,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to reallocated memory cast to the appropriate type.
* @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.
@ -196,7 +197,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to allocated zero-initialized aligned memory.
* @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.
@ -217,7 +219,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to reallocated aligned memory.
* @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.
@ -228,7 +231,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to reallocated memory.
* @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.
@ -240,9 +244,8 @@ DRANG_API void drang_free(void *ptr);
* @return Pointer to reallocated aligned memory.
* @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.

View file

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

View file

@ -1,7 +1,6 @@
#include <drang/alloc.h>
#include <mimalloc.h>
void *drang_alloc(const size_t 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);
}
void *drang_recalloc_aligned(
void *ptr, const size_t new_num, const size_t size, const size_t alignment)
void *drang_recalloc_aligned(void *ptr, const size_t new_num, const size_t size, const size_t alignment)
{
return mi_recalloc_aligned(ptr, new_num, size, alignment);
}

View file

@ -3,17 +3,29 @@
const char *drang_error_str(const int err)
{
switch (err) {
case DRANG_EOK: return "No error";
case DRANG_EINVAL: return "Invalid argument";
case DRANG_ENOMEM: return "Out of memory";
case DRANG_EIO: return "I/O error";
case DRANG_NOTSUP: return "Operation not supported";
case DRANG_EAGAIN: 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";
case DRANG_EOK:
return "No error";
case DRANG_EINVAL:
return "Invalid argument";
case DRANG_ENOMEM:
return "Out of memory";
case DRANG_EIO:
return "I/O error";
case DRANG_NOTSUP:
return "Operation not supported";
case DRANG_EAGAIN:
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)
{
DWORD timeout = (timeout_ms == 0) ? INFINITE : (DWORD)timeout_ms;
DWORD timeout = (timeout_ms == 0) ? INFINITE : (DWORD) timeout_ms;
BOOL result;
DRANG_BEGIN_TRY();
@ -113,4 +113,4 @@ int drang_cond_timedwait(struct drang_cond *cond, struct drang_mutex *mutex, uin
}
DRANG_END_TRY_IGNORE();
}
}

View file

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

View file

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

View file

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