feature: separate dll export/import macros from api macro, and rename DRANG_API to DRANG_PLATFORM_API

This commit is contained in:
MechSlayer 2025-09-05 16:36:30 +02:00
parent 9538637d58
commit f7c5fb8ed8
4 changed files with 50 additions and 51 deletions

View file

@ -14,7 +14,7 @@ DRANG_BEGIN_DECLS
*/
DRANG_ALLOC_SIZE_ATTR(1)
DRANG_MALLOC_ATTR
DRANG_API void *drang_alloc(size_t size);
DRANG_PLATFORM_API void *drang_alloc(size_t size);
/**
* @brief Allocates aligned memory of the specified size.
@ -27,7 +27,7 @@ DRANG_API void *drang_alloc(size_t size);
DRANG_ALLOC_SIZE_ATTR(1)
DRANG_ALLOC_ALIGN_ATTR(2)
DRANG_MALLOC_ATTR
DRANG_API void *drang_alloc_aligned(size_t size, size_t alignment);
DRANG_PLATFORM_API void *drang_alloc_aligned(size_t size, size_t alignment);
/**
* @brief Allocates zero-initialized memory for an array of elements.
@ -39,7 +39,7 @@ DRANG_API void *drang_alloc_aligned(size_t size, size_t alignment);
*/
DRANG_ALLOC_SIZE_COUNT_ATTR(1, 2)
DRANG_MALLOC_ATTR
DRANG_API void *drang_calloc(size_t num, size_t size);
DRANG_PLATFORM_API void *drang_calloc(size_t num, size_t size);
/**
* @brief Allocates zero-initialized aligned memory for an array of elements.
@ -53,7 +53,7 @@ DRANG_API void *drang_calloc(size_t num, size_t size);
DRANG_ALLOC_SIZE_COUNT_ATTR(1, 2)
DRANG_ALLOC_ALIGN_ATTR(3)
DRANG_MALLOC_ATTR
DRANG_API void *drang_calloc_aligned(size_t num, size_t size, size_t alignment);
DRANG_PLATFORM_API void *drang_calloc_aligned(size_t num, size_t size, size_t alignment);
/**
* @brief Reallocates memory to a new size.
@ -64,7 +64,7 @@ DRANG_API void *drang_calloc_aligned(size_t num, size_t size, size_t alignment);
* @remarks If ptr is NULL, behaves like drang_alloc().
*/
DRANG_ALLOC_SIZE_ATTR(2)
DRANG_API void *drang_realloc(void *ptr, size_t size);
DRANG_PLATFORM_API void *drang_realloc(void *ptr, size_t size);
/**
* @brief Reallocates aligned memory to a new size.
@ -77,7 +77,7 @@ DRANG_API void *drang_realloc(void *ptr, size_t size);
*/
DRANG_ALLOC_SIZE_ATTR(2)
DRANG_ALLOC_ALIGN_ATTR(3)
DRANG_API void *drang_realloc_aligned(void *ptr, size_t size, size_t alignment);
DRANG_PLATFORM_API void *drang_realloc_aligned(void *ptr, size_t size, size_t alignment);
/**
* @brief Reallocates memory for an array and initializes new elements to zero.
@ -89,7 +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_PLATFORM_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.
@ -103,7 +103,7 @@ DRANG_API void *drang_recalloc(void *ptr, size_t new_num, size_t size);
*/
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_PLATFORM_API void *drang_recalloc_aligned(void *ptr, size_t new_num, size_t size, size_t alignment);
/**
* @brief Frees previously allocated memory.
@ -111,7 +111,7 @@ DRANG_API void *drang_recalloc_aligned(void *ptr, size_t new_num, size_t size, s
* @param[in] ptr Pointer to memory to free, or NULL.
* @remarks Safe to call with NULL pointer. Works with memory allocated by any drang allocation function.
*/
DRANG_API void drang_free(void *ptr);
DRANG_PLATFORM_API void drang_free(void *ptr);
/**
* @brief Internal implementation macro for allocation with error handling.

View file

@ -8,7 +8,7 @@ DRANG_BEGIN_DECLS
* @param err Error code.
* @return String description of the error code.
*/
DRANG_API const char *drang_error_str(int err);
DRANG_PLATFORM_API const char *drang_error_str(int err);
#define DRANG_EOK (0) // Success
#define DRANG_EINVAL (-1) // Invalid argument

View file

@ -1,25 +1,24 @@
#pragma once
#ifdef _WIN32
#define DRANG_DLL_EXPORT __declspec(dllexport)
#define DRANG_DLL_IMPORT __declspec(dllimport)
#elif defined(__linux__)
#define DRANG_DLL_EXPORT __attribute__((visibility("default")))
#define DRANG_DLL_IMPORT __attribute__((visibility("default")))
#else
#error "Unknown platform, please implement shared library export/import macros"
#endif
#if !DRANG_PLATFORM_STATIC
# if DRANG_PLATFORM_EXPORT
# ifdef _WIN32
# define DRANG_API __declspec(dllexport)
# elif defined(__linux__)
# define DRANG_API __attribute__((visibility("default")))
# else
# error "Unknown platform, please implement shared library export macros"
# endif
# else
# ifdef _WIN32
# define DRANG_API __declspec(dllimport)
# elif defined(__linux__)
# define DRANG_API __attribute__((visibility("default")))
# else
# error "Unknown platform, please implement shared library import macros"
# endif
#define DRANG_PLATFORM_API DRANG_DLL_EXPORT
#else
#define DRANG_PLATFORM_API DRANG_DLL_IMPORT
# endif
#else
# define DRANG_API
# define DRANG_PLATFORM_API
#endif
#ifdef __cplusplus

View file

@ -26,7 +26,7 @@ struct drang_mutex;
* @return Size in bytes required for a drang_mutex structure.
* @remarks The returned size is platform-specific and may vary between builds.
*/
DRANG_API size_t drang_mutex_size(void);
DRANG_PLATFORM_API size_t drang_mutex_size(void);
/**
* @brief Creates and initializes a new mutex on the heap.
@ -42,7 +42,7 @@ DRANG_API size_t drang_mutex_size(void);
* @remarks The created mutex must be freed with drang_mutex_free().
* The mutex is created in an unlocked state and ready for use.
*/
DRANG_API int drang_mutex_new(struct drang_mutex **mutex);
DRANG_PLATFORM_API int drang_mutex_new(struct drang_mutex **mutex);
/**
* @brief Destroys and frees a mutex created with drang_mutex_new().
@ -55,7 +55,7 @@ DRANG_API int drang_mutex_new(struct drang_mutex **mutex);
* before calling this function. Undefined behavior if the mutex
* is currently locked or if other threads are waiting on it.
*/
DRANG_API void drang_mutex_free(struct drang_mutex *mutex);
DRANG_PLATFORM_API void drang_mutex_free(struct drang_mutex *mutex);
/**
* @brief Initializes a pre-allocated mutex structure.
@ -72,7 +72,7 @@ DRANG_API void drang_mutex_free(struct drang_mutex *mutex);
* @remarks The mutex must be finalized with drang_mutex_fini() when no longer needed.
* Use this for stack-allocated mutexes or when embedding in other structures.
*/
DRANG_API int drang_mutex_init(struct drang_mutex *mutex);
DRANG_PLATFORM_API int drang_mutex_init(struct drang_mutex *mutex);
/**
* @brief Finalizes a mutex initialized with drang_mutex_init().
@ -85,7 +85,7 @@ DRANG_API int drang_mutex_init(struct drang_mutex *mutex);
* Undefined behavior if the mutex is currently locked or if other
* threads are waiting on it. Safe to call with NULL pointer.
*/
DRANG_API void drang_mutex_fini(struct drang_mutex *mutex);
DRANG_PLATFORM_API void drang_mutex_fini(struct drang_mutex *mutex);
/**
* @brief Locks the mutex, blocking until acquisition is possible.
@ -102,7 +102,7 @@ DRANG_API void drang_mutex_fini(struct drang_mutex *mutex);
* @remarks The mutex must be unlocked with drang_mutex_unlock() by the same thread.
* Recursive locking (same thread locking twice) results in undefined behavior.
*/
DRANG_API int drang_mutex_lock(struct drang_mutex *mutex);
DRANG_PLATFORM_API int drang_mutex_lock(struct drang_mutex *mutex);
/**
* @brief Unlocks a previously locked mutex.
@ -118,7 +118,7 @@ DRANG_API int drang_mutex_lock(struct drang_mutex *mutex);
* @remarks Only the thread that locked the mutex should unlock it.
* Unlocking a mutex that is not locked results in undefined behavior.
*/
DRANG_API int drang_mutex_unlock(struct drang_mutex *mutex);
DRANG_PLATFORM_API int drang_mutex_unlock(struct drang_mutex *mutex);
/**
* @brief Opaque structure representing a condition variable.
@ -142,7 +142,7 @@ struct drang_cond;
* @return Size in bytes required for a drang_cond structure.
* @remarks The returned size is platform-specific and may vary between builds.
*/
DRANG_API size_t drang_cond_size(void);
DRANG_PLATFORM_API size_t drang_cond_size(void);
/**
* @brief Creates and initializes a new condition variable on the heap.
@ -158,7 +158,7 @@ DRANG_API size_t drang_cond_size(void);
* @remarks The created condition variable must be freed with drang_cond_free().
* Ready for use immediately after successful creation.
*/
DRANG_API int drang_cond_new(struct drang_cond **cond);
DRANG_PLATFORM_API int drang_cond_new(struct drang_cond **cond);
/**
* @brief Destroys and frees a condition variable created with drang_cond_new().
@ -170,7 +170,7 @@ DRANG_API int drang_cond_new(struct drang_cond **cond);
* @remarks Safe to call with NULL pointer. Undefined behavior if threads are
* currently waiting on this condition variable.
*/
DRANG_API void drang_cond_free(struct drang_cond *cond);
DRANG_PLATFORM_API void drang_cond_free(struct drang_cond *cond);
/**
* @brief Initializes a pre-allocated condition variable structure.
@ -187,7 +187,7 @@ DRANG_API void drang_cond_free(struct drang_cond *cond);
* @remarks The condition variable must be finalized with drang_cond_fini() when no longer needed.
* Use this for stack-allocated condition variables or when embedding in other structures.
*/
DRANG_API int drang_cond_init(struct drang_cond *cond);
DRANG_PLATFORM_API int drang_cond_init(struct drang_cond *cond);
/**
* @brief Finalizes a condition variable initialized with drang_cond_init().
@ -200,7 +200,7 @@ DRANG_API int drang_cond_init(struct drang_cond *cond);
* @remarks Undefined behavior if threads are currently waiting on this condition variable.
* Safe to call with NULL pointer.
*/
DRANG_API void drang_cond_fini(struct drang_cond *cond);
DRANG_PLATFORM_API void drang_cond_fini(struct drang_cond *cond);
/**
* @brief Signals one thread waiting on the condition variable.
@ -216,7 +216,7 @@ DRANG_API void drang_cond_fini(struct drang_cond *cond);
* @remarks Does not require holding the associated mutex, but it's recommended
* for predictable behavior. If no threads are waiting, the signal is lost.
*/
DRANG_API int drang_cond_signal(struct drang_cond *cond);
DRANG_PLATFORM_API int drang_cond_signal(struct drang_cond *cond);
/**
* @brief Signals all threads waiting on the condition variable.
@ -232,7 +232,7 @@ DRANG_API int drang_cond_signal(struct drang_cond *cond);
* @remarks Does not require holding the associated mutex, but it's recommended
* for predictable behavior. If no threads are waiting, the broadcast is lost.
*/
DRANG_API int drang_cond_broadcast(struct drang_cond *cond);
DRANG_PLATFORM_API int drang_cond_broadcast(struct drang_cond *cond);
/**
* @brief Waits indefinitely on a condition variable.
@ -251,7 +251,7 @@ DRANG_API int drang_cond_broadcast(struct drang_cond *cond);
* Spurious wakeups are possible - always check your condition in a loop.
* The mutex is guaranteed to be locked when this function returns successfully.
*/
DRANG_API int drang_cond_wait(struct drang_cond *cond, struct drang_mutex *mutex);
DRANG_PLATFORM_API int drang_cond_wait(struct drang_cond *cond, struct drang_mutex *mutex);
/**
* @brief Waits on a condition variable with a timeout.
@ -273,7 +273,7 @@ DRANG_API int drang_cond_wait(struct drang_cond *cond, struct drang_mutex *mutex
* The mutex is guaranteed to be locked when this function returns (success or timeout).
* A timeout of 0 means wait indefinitely (equivalent to drang_cond_wait).
*/
DRANG_API int drang_cond_timedwait(struct drang_cond *cond, struct drang_mutex *mutex, uint64_t timeout_ms);
DRANG_PLATFORM_API int drang_cond_timedwait(struct drang_cond *cond, struct drang_mutex *mutex, uint64_t timeout_ms);
/**
* @brief Opaque structure representing a read-write lock.
@ -298,7 +298,7 @@ struct drang_rwlock;
* @return Size in bytes required for a drang_rwlock structure.
* @remarks The returned size is platform-specific and may vary between builds.
*/
DRANG_API size_t drang_rwlock_size(void);
DRANG_PLATFORM_API size_t drang_rwlock_size(void);
/**
* @brief Creates and initializes a new read-write lock on the heap.
@ -314,7 +314,7 @@ DRANG_API size_t drang_rwlock_size(void);
* @remarks The created read-write lock must be freed with drang_rwlock_free().
* The lock is created in an unlocked state and ready for use.
*/
DRANG_API int drang_rwlock_new(struct drang_rwlock **rwlock);
DRANG_PLATFORM_API int drang_rwlock_new(struct drang_rwlock **rwlock);
/**
* @brief Destroys and frees a read-write lock created with drang_rwlock_new().
@ -327,7 +327,7 @@ DRANG_API int drang_rwlock_new(struct drang_rwlock **rwlock);
* before calling this function. Undefined behavior if the lock
* currently has readers or a writer.
*/
DRANG_API void drang_rwlock_free(struct drang_rwlock *rwlock);
DRANG_PLATFORM_API void drang_rwlock_free(struct drang_rwlock *rwlock);
/**
* @brief Initializes a pre-allocated read-write lock structure.
@ -344,7 +344,7 @@ DRANG_API void drang_rwlock_free(struct drang_rwlock *rwlock);
* @remarks The lock must be finalized with drang_rwlock_fini() when no longer needed.
* Use this for stack-allocated locks or when embedding in other structures.
*/
DRANG_API int drang_rwlock_init(struct drang_rwlock *rwlock);
DRANG_PLATFORM_API int drang_rwlock_init(struct drang_rwlock *rwlock);
/**
* @brief Finalizes a read-write lock initialized with drang_rwlock_init().
@ -358,7 +358,7 @@ DRANG_API int drang_rwlock_init(struct drang_rwlock *rwlock);
* Undefined behavior if the lock currently has readers or a writer.
* Safe to call with NULL pointer.
*/
DRANG_API void drang_rwlock_fini(struct drang_rwlock *rwlock);
DRANG_PLATFORM_API void drang_rwlock_fini(struct drang_rwlock *rwlock);
/**
* @brief Acquires a read lock on the read-write lock.
@ -377,7 +377,7 @@ DRANG_API void drang_rwlock_fini(struct drang_rwlock *rwlock);
* Multiple threads can hold read locks concurrently.
* Read locks block writers but not other readers.
*/
DRANG_API int drang_rwlock_rdlock(struct drang_rwlock *rwlock);
DRANG_PLATFORM_API int drang_rwlock_rdlock(struct drang_rwlock *rwlock);
/**
* @brief Acquires a write lock on the read-write lock.
@ -396,7 +396,7 @@ DRANG_API int drang_rwlock_rdlock(struct drang_rwlock *rwlock);
* Write locks are exclusive - only one writer at a time.
* Write locks block both readers and other writers.
*/
DRANG_API int drang_rwlock_wrlock(struct drang_rwlock *rwlock);
DRANG_PLATFORM_API int drang_rwlock_wrlock(struct drang_rwlock *rwlock);
/**
* @brief Releases a read lock on the read-write lock.
@ -414,7 +414,7 @@ DRANG_API int drang_rwlock_wrlock(struct drang_rwlock *rwlock);
* Releasing a read lock that is not held results in undefined behavior.
* When the last reader releases their lock, waiting writers may be awakened.
*/
DRANG_API int drang_rwlock_rdunlock(struct drang_rwlock *rwlock);
DRANG_PLATFORM_API int drang_rwlock_rdunlock(struct drang_rwlock *rwlock);
/**
* @brief Releases a write lock on the read-write lock.
@ -432,6 +432,6 @@ DRANG_API int drang_rwlock_rdunlock(struct drang_rwlock *rwlock);
* Releasing a write lock that is not held results in undefined behavior.
* When the write lock is released, waiting readers and writers may be awakened.
*/
DRANG_API int drang_rwlock_wrunlock(struct drang_rwlock *rwlock);
DRANG_PLATFORM_API int drang_rwlock_wrunlock(struct drang_rwlock *rwlock);
DRANG_END_DECLS