Update OpenAL Soft to 1.18.2

This commit is contained in:
Alex Szpakowski
2017-12-10 22:34:10 -04:00
parent 75e0077566
commit b160006eb1
152 changed files with 33572 additions and 15363 deletions
+21
View File
@@ -0,0 +1,21 @@
#ifndef AL_ALIGN_H
#define AL_ALIGN_H
#if defined(HAVE_STDALIGN_H) && defined(HAVE_C11_ALIGNAS)
#include <stdalign.h>
#endif
#ifndef alignas
#if defined(IN_IDE_PARSER)
/* KDevelop has problems with our align macro, so just use nothing for parsing. */
#define alignas(x)
#elif defined(HAVE_C11_ALIGNAS)
#define alignas _Alignas
#else
/* NOTE: Our custom ALIGN macro can't take a type name like alignas can. For
* maximum compatibility, only provide constant integer values to alignas. */
#define alignas(_x) ALIGN(_x)
#endif
#endif
#endif /* AL_ALIGN_H */
+62
View File
@@ -0,0 +1,62 @@
#include "config.h"
#include "almalloc.h"
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
void *al_malloc(size_t alignment, size_t size)
{
#if defined(HAVE_ALIGNED_ALLOC)
size = (size+(alignment-1))&~(alignment-1);
return aligned_alloc(alignment, size);
#elif defined(HAVE_POSIX_MEMALIGN)
void *ret;
if(posix_memalign(&ret, alignment, size) == 0)
return ret;
return NULL;
#elif defined(HAVE__ALIGNED_MALLOC)
return _aligned_malloc(size, alignment);
#else
char *ret = malloc(size+alignment);
if(ret != NULL)
{
*(ret++) = 0x00;
while(((ptrdiff_t)ret&(alignment-1)) != 0)
*(ret++) = 0x55;
}
return ret;
#endif
}
void *al_calloc(size_t alignment, size_t size)
{
void *ret = al_malloc(alignment, size);
if(ret) memset(ret, 0, size);
return ret;
}
void al_free(void *ptr)
{
#if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
free(ptr);
#elif defined(HAVE__ALIGNED_MALLOC)
_aligned_free(ptr);
#else
if(ptr != NULL)
{
char *finder = ptr;
do {
--finder;
} while(*finder == 0x55);
free(finder);
}
#endif
}
+21
View File
@@ -0,0 +1,21 @@
#ifndef AL_MALLOC_H
#define AL_MALLOC_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Minimum alignment required by posix_memalign. */
#define DEF_ALIGN sizeof(void*)
void *al_malloc(size_t alignment, size_t size);
void *al_calloc(size_t alignment, size_t size);
void al_free(void *ptr);
#ifdef __cplusplus
}
#endif
#endif /* AL_MALLOC_H */
-3
View File
@@ -8,6 +8,3 @@ extern inline void InitRef(RefCount *ptr, uint value);
extern inline uint ReadRef(RefCount *ptr);
extern inline uint IncrementRef(RefCount *ptr);
extern inline uint DecrementRef(RefCount *ptr);
extern inline int ExchangeInt(volatile int *ptr, int newval);
extern inline void *ExchangePtr(XchgPtr *ptr, void *newval);
+425
View File
@@ -0,0 +1,425 @@
#ifndef AL_ATOMIC_H
#define AL_ATOMIC_H
#include "static_assert.h"
#include "bool.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Atomics using C11 */
#ifdef HAVE_C11_ATOMIC
#include <stdatomic.h>
#define almemory_order memory_order
#define almemory_order_relaxed memory_order_relaxed
#define almemory_order_consume memory_order_consume
#define almemory_order_acquire memory_order_acquire
#define almemory_order_release memory_order_release
#define almemory_order_acq_rel memory_order_acq_rel
#define almemory_order_seq_cst memory_order_seq_cst
#define ATOMIC(T) T _Atomic
#define ATOMIC_FLAG atomic_flag
#define ATOMIC_INIT atomic_init
#define ATOMIC_INIT_STATIC ATOMIC_VAR_INIT
/*#define ATOMIC_FLAG_INIT ATOMIC_FLAG_INIT*/
#define ATOMIC_LOAD atomic_load_explicit
#define ATOMIC_STORE atomic_store_explicit
#define ATOMIC_ADD atomic_fetch_add_explicit
#define ATOMIC_SUB atomic_fetch_sub_explicit
#define ATOMIC_EXCHANGE atomic_exchange_explicit
#define ATOMIC_COMPARE_EXCHANGE_STRONG atomic_compare_exchange_strong_explicit
#define ATOMIC_COMPARE_EXCHANGE_WEAK atomic_compare_exchange_weak_explicit
#define ATOMIC_FLAG_TEST_AND_SET atomic_flag_test_and_set_explicit
#define ATOMIC_FLAG_CLEAR atomic_flag_clear_explicit
#define ATOMIC_THREAD_FENCE atomic_thread_fence
/* Atomics using GCC intrinsics */
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && !defined(__QNXNTO__)
enum almemory_order {
almemory_order_relaxed,
almemory_order_consume,
almemory_order_acquire,
almemory_order_release,
almemory_order_acq_rel,
almemory_order_seq_cst
};
#define ATOMIC(T) struct { T volatile value; }
#define ATOMIC_FLAG ATOMIC(int)
#define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0)
#define ATOMIC_INIT_STATIC(_newval) {(_newval)}
#define ATOMIC_FLAG_INIT ATOMIC_INIT_STATIC(0)
#define ATOMIC_LOAD(_val, _MO) __extension__({ \
__typeof((_val)->value) _r = (_val)->value; \
__asm__ __volatile__("" ::: "memory"); \
_r; \
})
#define ATOMIC_STORE(_val, _newval, _MO) do { \
__asm__ __volatile__("" ::: "memory"); \
(_val)->value = (_newval); \
} while(0)
#define ATOMIC_ADD(_val, _incr, _MO) __sync_fetch_and_add(&(_val)->value, (_incr))
#define ATOMIC_SUB(_val, _decr, _MO) __sync_fetch_and_sub(&(_val)->value, (_decr))
#define ATOMIC_EXCHANGE(_val, _newval, _MO) __extension__({ \
__asm__ __volatile__("" ::: "memory"); \
__sync_lock_test_and_set(&(_val)->value, (_newval)); \
})
#define ATOMIC_COMPARE_EXCHANGE_STRONG(_val, _oldval, _newval, _MO1, _MO2) __extension__({ \
__typeof(*(_oldval)) _o = *(_oldval); \
*(_oldval) = __sync_val_compare_and_swap(&(_val)->value, _o, (_newval)); \
*(_oldval) == _o; \
})
#define ATOMIC_FLAG_TEST_AND_SET(_val, _MO) __extension__({ \
__asm__ __volatile__("" ::: "memory"); \
__sync_lock_test_and_set(&(_val)->value, 1); \
})
#define ATOMIC_FLAG_CLEAR(_val, _MO) __extension__({ \
__sync_lock_release(&(_val)->value); \
__asm__ __volatile__("" ::: "memory"); \
})
#define ATOMIC_THREAD_FENCE(order) do { \
enum { must_be_constant = (order) }; \
const int _o = must_be_constant; \
if(_o > almemory_order_relaxed) \
__asm__ __volatile__("" ::: "memory"); \
} while(0)
/* Atomics using x86/x86-64 GCC inline assembly */
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
#define WRAP_ADD(S, ret, dest, incr) __asm__ __volatile__( \
"lock; xadd"S" %0,(%1)" \
: "=r" (ret) \
: "r" (dest), "0" (incr) \
: "memory" \
)
#define WRAP_SUB(S, ret, dest, decr) __asm__ __volatile__( \
"lock; xadd"S" %0,(%1)" \
: "=r" (ret) \
: "r" (dest), "0" (-(decr)) \
: "memory" \
)
#define WRAP_XCHG(S, ret, dest, newval) __asm__ __volatile__( \
"lock; xchg"S" %0,(%1)" \
: "=r" (ret) \
: "r" (dest), "0" (newval) \
: "memory" \
)
#define WRAP_CMPXCHG(S, ret, dest, oldval, newval) __asm__ __volatile__( \
"lock; cmpxchg"S" %2,(%1)" \
: "=a" (ret) \
: "r" (dest), "r" (newval), "0" (oldval) \
: "memory" \
)
enum almemory_order {
almemory_order_relaxed,
almemory_order_consume,
almemory_order_acquire,
almemory_order_release,
almemory_order_acq_rel,
almemory_order_seq_cst
};
#define ATOMIC(T) struct { T volatile value; }
#define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0)
#define ATOMIC_INIT_STATIC(_newval) {(_newval)}
#define ATOMIC_LOAD(_val, _MO) __extension__({ \
__typeof((_val)->value) _r = (_val)->value; \
__asm__ __volatile__("" ::: "memory"); \
_r; \
})
#define ATOMIC_STORE(_val, _newval, _MO) do { \
__asm__ __volatile__("" ::: "memory"); \
(_val)->value = (_newval); \
} while(0)
#define ATOMIC_ADD(_val, _incr, _MO) __extension__({ \
static_assert(sizeof((_val)->value)==4 || sizeof((_val)->value)==8, "Unsupported size!"); \
__typeof((_val)->value) _r; \
if(sizeof((_val)->value) == 4) WRAP_ADD("l", _r, &(_val)->value, _incr); \
else if(sizeof((_val)->value) == 8) WRAP_ADD("q", _r, &(_val)->value, _incr); \
_r; \
})
#define ATOMIC_SUB(_val, _decr, _MO) __extension__({ \
static_assert(sizeof((_val)->value)==4 || sizeof((_val)->value)==8, "Unsupported size!"); \
__typeof((_val)->value) _r; \
if(sizeof((_val)->value) == 4) WRAP_SUB("l", _r, &(_val)->value, _decr); \
else if(sizeof((_val)->value) == 8) WRAP_SUB("q", _r, &(_val)->value, _decr); \
_r; \
})
#define ATOMIC_EXCHANGE(_val, _newval, _MO) __extension__({ \
__typeof((_val)->value) _r; \
if(sizeof((_val)->value) == 4) WRAP_XCHG("l", _r, &(_val)->value, (_newval)); \
else if(sizeof((_val)->value) == 8) WRAP_XCHG("q", _r, &(_val)->value, (_newval)); \
_r; \
})
#define ATOMIC_COMPARE_EXCHANGE_STRONG(_val, _oldval, _newval, _MO1, _MO2) __extension__({ \
__typeof(*(_oldval)) _old = *(_oldval); \
if(sizeof((_val)->value) == 4) WRAP_CMPXCHG("l", *(_oldval), &(_val)->value, _old, (_newval)); \
else if(sizeof((_val)->value) == 8) WRAP_CMPXCHG("q", *(_oldval), &(_val)->value, _old, (_newval)); \
*(_oldval) == _old; \
})
#define ATOMIC_EXCHANGE_PTR(_val, _newval, _MO) __extension__({ \
void *_r; \
if(sizeof(void*) == 4) WRAP_XCHG("l", _r, &(_val)->value, (_newval)); \
else if(sizeof(void*) == 8) WRAP_XCHG("q", _r, &(_val)->value, (_newval));\
_r; \
})
#define ATOMIC_COMPARE_EXCHANGE_PTR_STRONG(_val, _oldval, _newval, _MO1, _MO2) __extension__({ \
void *_old = *(_oldval); \
if(sizeof(void*) == 4) WRAP_CMPXCHG("l", *(_oldval), &(_val)->value, _old, (_newval)); \
else if(sizeof(void*) == 8) WRAP_CMPXCHG("q", *(_oldval), &(_val)->value, _old, (_newval)); \
*(_oldval) == _old; \
})
#define ATOMIC_THREAD_FENCE(order) do { \
enum { must_be_constant = (order) }; \
const int _o = must_be_constant; \
if(_o > almemory_order_relaxed) \
__asm__ __volatile__("" ::: "memory"); \
} while(0)
/* Atomics using Windows methods */
#elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
/* NOTE: This mess is *extremely* touchy. It lacks quite a bit of safety
* checking due to the lack of multi-statement expressions, typeof(), and C99
* compound literals. It is incapable of properly exchanging floats, which get
* casted to LONG/int, and could cast away potential warnings.
*
* Unfortunately, it's the only semi-safe way that doesn't rely on C99 (because
* MSVC).
*/
inline LONG AtomicAdd32(volatile LONG *dest, LONG incr)
{
return InterlockedExchangeAdd(dest, incr);
}
inline LONGLONG AtomicAdd64(volatile LONGLONG *dest, LONGLONG incr)
{
return InterlockedExchangeAdd64(dest, incr);
}
inline LONG AtomicSub32(volatile LONG *dest, LONG decr)
{
return InterlockedExchangeAdd(dest, -decr);
}
inline LONGLONG AtomicSub64(volatile LONGLONG *dest, LONGLONG decr)
{
return InterlockedExchangeAdd64(dest, -decr);
}
inline LONG AtomicSwap32(volatile LONG *dest, LONG newval)
{
return InterlockedExchange(dest, newval);
}
inline LONGLONG AtomicSwap64(volatile LONGLONG *dest, LONGLONG newval)
{
return InterlockedExchange64(dest, newval);
}
inline void *AtomicSwapPtr(void *volatile *dest, void *newval)
{
return InterlockedExchangePointer(dest, newval);
}
inline bool CompareAndSwap32(volatile LONG *dest, LONG newval, LONG *oldval)
{
LONG old = *oldval;
*oldval = InterlockedCompareExchange(dest, newval, *oldval);
return old == *oldval;
}
inline bool CompareAndSwap64(volatile LONGLONG *dest, LONGLONG newval, LONGLONG *oldval)
{
LONGLONG old = *oldval;
*oldval = InterlockedCompareExchange64(dest, newval, *oldval);
return old == *oldval;
}
inline bool CompareAndSwapPtr(void *volatile *dest, void *newval, void **oldval)
{
void *old = *oldval;
*oldval = InterlockedCompareExchangePointer(dest, newval, *oldval);
return old == *oldval;
}
#define WRAP_ADDSUB(T, _func, _ptr, _amnt) _func((T volatile*)(_ptr), (_amnt))
#define WRAP_XCHG(T, _func, _ptr, _newval) _func((T volatile*)(_ptr), (_newval))
#define WRAP_CMPXCHG(T, _func, _ptr, _newval, _oldval) _func((T volatile*)(_ptr), (_newval), (T*)(_oldval))
enum almemory_order {
almemory_order_relaxed,
almemory_order_consume,
almemory_order_acquire,
almemory_order_release,
almemory_order_acq_rel,
almemory_order_seq_cst
};
#define ATOMIC(T) struct { T volatile value; }
#define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0)
#define ATOMIC_INIT_STATIC(_newval) {(_newval)}
#define ATOMIC_LOAD(_val, _MO) ((_val)->value)
#define ATOMIC_STORE(_val, _newval, _MO) do { \
(_val)->value = (_newval); \
} while(0)
int _al_invalid_atomic_size(); /* not defined */
#define ATOMIC_ADD(_val, _incr, _MO) \
((sizeof((_val)->value)==4) ? WRAP_ADDSUB(LONG, AtomicAdd32, &(_val)->value, (_incr)) : \
(sizeof((_val)->value)==8) ? WRAP_ADDSUB(LONGLONG, AtomicAdd64, &(_val)->value, (_incr)) : \
_al_invalid_atomic_size())
#define ATOMIC_SUB(_val, _decr, _MO) \
((sizeof((_val)->value)==4) ? WRAP_ADDSUB(LONG, AtomicSub32, &(_val)->value, (_decr)) : \
(sizeof((_val)->value)==8) ? WRAP_ADDSUB(LONGLONG, AtomicSub64, &(_val)->value, (_decr)) : \
_al_invalid_atomic_size())
#define ATOMIC_EXCHANGE(_val, _newval, _MO) \
((sizeof((_val)->value)==4) ? WRAP_XCHG(LONG, AtomicSwap32, &(_val)->value, (_newval)) : \
(sizeof((_val)->value)==8) ? WRAP_XCHG(LONGLONG, AtomicSwap64, &(_val)->value, (_newval)) : \
(LONG)_al_invalid_atomic_size())
#define ATOMIC_COMPARE_EXCHANGE_STRONG(_val, _oldval, _newval, _MO1, _MO2) \
((sizeof((_val)->value)==4) ? WRAP_CMPXCHG(LONG, CompareAndSwap32, &(_val)->value, (_newval), (_oldval)) : \
(sizeof((_val)->value)==8) ? WRAP_CMPXCHG(LONGLONG, CompareAndSwap64, &(_val)->value, (_newval), (_oldval)) : \
(bool)_al_invalid_atomic_size())
#define ATOMIC_EXCHANGE_PTR(_val, _newval, _MO) \
((sizeof((_val)->value)==sizeof(void*)) ? AtomicSwapPtr((void*volatile*)&(_val)->value, (_newval)) : \
(void*)_al_invalid_atomic_size())
#define ATOMIC_COMPARE_EXCHANGE_PTR_STRONG(_val, _oldval, _newval, _MO1, _MO2)\
((sizeof((_val)->value)==sizeof(void*)) ? CompareAndSwapPtr((void*volatile*)&(_val)->value, (_newval), (void**)(_oldval)) : \
(bool)_al_invalid_atomic_size())
#define ATOMIC_THREAD_FENCE(order) do { \
enum { must_be_constant = (order) }; \
const int _o = must_be_constant; \
if(_o > almemory_order_relaxed) \
_ReadWriteBarrier(); \
} while(0)
#else
#error "No atomic functions available on this platform!"
#define ATOMIC(T) T
#define ATOMIC_INIT(_val, _newval) ((void)0)
#define ATOMIC_INIT_STATIC(_newval) (0)
#define ATOMIC_LOAD(...) (0)
#define ATOMIC_STORE(...) ((void)0)
#define ATOMIC_ADD(...) (0)
#define ATOMIC_SUB(...) (0)
#define ATOMIC_EXCHANGE(...) (0)
#define ATOMIC_COMPARE_EXCHANGE_STRONG(...) (0)
#define ATOMIC_THREAD_FENCE(...) ((void)0)
#endif
/* If no PTR xchg variants are provided, the normal ones can handle it. */
#ifndef ATOMIC_EXCHANGE_PTR
#define ATOMIC_EXCHANGE_PTR ATOMIC_EXCHANGE
#define ATOMIC_COMPARE_EXCHANGE_PTR_STRONG ATOMIC_COMPARE_EXCHANGE_STRONG
#define ATOMIC_COMPARE_EXCHANGE_PTR_WEAK ATOMIC_COMPARE_EXCHANGE_WEAK
#endif
/* If no weak cmpxchg is provided (not all systems will have one), substitute a
* strong cmpxchg. */
#ifndef ATOMIC_COMPARE_EXCHANGE_WEAK
#define ATOMIC_COMPARE_EXCHANGE_WEAK ATOMIC_COMPARE_EXCHANGE_STRONG
#endif
#ifndef ATOMIC_COMPARE_EXCHANGE_PTR_WEAK
#define ATOMIC_COMPARE_EXCHANGE_PTR_WEAK ATOMIC_COMPARE_EXCHANGE_PTR_STRONG
#endif
/* If no ATOMIC_FLAG is defined, simulate one with an atomic int using exchange
* and store ops.
*/
#ifndef ATOMIC_FLAG
#define ATOMIC_FLAG ATOMIC(int)
#define ATOMIC_FLAG_INIT ATOMIC_INIT_STATIC(0)
#define ATOMIC_FLAG_TEST_AND_SET(_val, _MO) ATOMIC_EXCHANGE(_val, 1, _MO)
#define ATOMIC_FLAG_CLEAR(_val, _MO) ATOMIC_STORE(_val, 0, _MO)
#endif
#define ATOMIC_LOAD_SEQ(_val) ATOMIC_LOAD(_val, almemory_order_seq_cst)
#define ATOMIC_STORE_SEQ(_val, _newval) ATOMIC_STORE(_val, _newval, almemory_order_seq_cst)
#define ATOMIC_ADD_SEQ(_val, _incr) ATOMIC_ADD(_val, _incr, almemory_order_seq_cst)
#define ATOMIC_SUB_SEQ(_val, _decr) ATOMIC_SUB(_val, _decr, almemory_order_seq_cst)
#define ATOMIC_EXCHANGE_SEQ(_val, _newval) ATOMIC_EXCHANGE(_val, _newval, almemory_order_seq_cst)
#define ATOMIC_COMPARE_EXCHANGE_STRONG_SEQ(_val, _oldval, _newval) \
ATOMIC_COMPARE_EXCHANGE_STRONG(_val, _oldval, _newval, almemory_order_seq_cst, almemory_order_seq_cst)
#define ATOMIC_COMPARE_EXCHANGE_WEAK_SEQ(_val, _oldval, _newval) \
ATOMIC_COMPARE_EXCHANGE_WEAK(_val, _oldval, _newval, almemory_order_seq_cst, almemory_order_seq_cst)
#define ATOMIC_EXCHANGE_PTR_SEQ(_val, _newval) ATOMIC_EXCHANGE_PTR(_val, _newval, almemory_order_seq_cst)
#define ATOMIC_COMPARE_EXCHANGE_PTR_STRONG_SEQ(_val, _oldval, _newval) \
ATOMIC_COMPARE_EXCHANGE_PTR_STRONG(_val, _oldval, _newval, almemory_order_seq_cst, almemory_order_seq_cst)
#define ATOMIC_COMPARE_EXCHANGE_PTR_WEAK_SEQ(_val, _oldval, _newval) \
ATOMIC_COMPARE_EXCHANGE_PTR_WEAK(_val, _oldval, _newval, almemory_order_seq_cst, almemory_order_seq_cst)
typedef unsigned int uint;
typedef ATOMIC(uint) RefCount;
inline void InitRef(RefCount *ptr, uint value)
{ ATOMIC_INIT(ptr, value); }
inline uint ReadRef(RefCount *ptr)
{ return ATOMIC_LOAD_SEQ(ptr); }
inline uint IncrementRef(RefCount *ptr)
{ return ATOMIC_ADD_SEQ(ptr, 1)+1; }
inline uint DecrementRef(RefCount *ptr)
{ return ATOMIC_SUB_SEQ(ptr, 1)-1; }
/* WARNING: A livelock is theoretically possible if another thread keeps
* changing the head without giving this a chance to actually swap in the new
* one (practically impossible with this little code, but...).
*/
#define ATOMIC_REPLACE_HEAD(T, _head, _entry) do { \
T _first = ATOMIC_LOAD(_head, almemory_order_acquire); \
do { \
ATOMIC_STORE(&(_entry)->next, _first, almemory_order_relaxed); \
} while(ATOMIC_COMPARE_EXCHANGE_PTR_WEAK(_head, &_first, _entry, \
almemory_order_acq_rel, almemory_order_acquire) == 0); \
} while(0)
#ifdef __cplusplus
}
#endif
#endif /* AL_ATOMIC_H */
+18
View File
@@ -0,0 +1,18 @@
#ifndef AL_BOOL_H
#define AL_BOOL_H
#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#endif
#ifndef bool
#ifdef HAVE_C99_BOOL
#define bool _Bool
#else
#define bool int
#endif
#define false 0
#define true 1
#endif
#endif /* AL_BOOL_H */
+35
View File
@@ -0,0 +1,35 @@
#ifndef AL_MATH_DEFS_H
#define AL_MATH_DEFS_H
#include <math.h>
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
#define F_PI (3.14159265358979323846f)
#define F_PI_2 (1.57079632679489661923f)
#define F_TAU (6.28318530717958647692f)
#ifndef FLT_EPSILON
#define FLT_EPSILON (1.19209290e-07f)
#endif
#ifndef HUGE_VALF
static const union msvc_inf_hack {
unsigned char b[4];
float f;
} msvc_inf_union = {{ 0x00, 0x00, 0x80, 0x7F }};
#define HUGE_VALF (msvc_inf_union.f)
#endif
#ifndef HAVE_LOG2F
static inline float log2f(float f)
{
return logf(f) / logf(2.0f);
}
#endif
#define DEG2RAD(x) ((float)(x) * (F_PI/180.0f))
#define RAD2DEG(x) ((float)(x) * (180.0f/F_PI))
#endif /* AL_MATH_DEFS_H */
+11 -9
View File
@@ -11,26 +11,27 @@
/* A simple spinlock. Yield the thread while the given integer is set by
* another. Could probably be improved... */
#define LOCK(l) do { \
while(ATOMIC_EXCHANGE(int, &(l), true) == true) \
while(ATOMIC_FLAG_TEST_AND_SET(&(l), almemory_order_acq_rel) == true) \
althrd_yield(); \
} while(0)
#define UNLOCK(l) ATOMIC_STORE(&(l), false)
#define UNLOCK(l) ATOMIC_FLAG_CLEAR(&(l), almemory_order_release)
void RWLockInit(RWLock *lock)
{
InitRef(&lock->read_count, 0);
InitRef(&lock->write_count, 0);
ATOMIC_INIT(&lock->read_lock, false);
ATOMIC_INIT(&lock->read_entry_lock, false);
ATOMIC_INIT(&lock->write_lock, false);
ATOMIC_FLAG_CLEAR(&lock->read_lock, almemory_order_relaxed);
ATOMIC_FLAG_CLEAR(&lock->read_entry_lock, almemory_order_relaxed);
ATOMIC_FLAG_CLEAR(&lock->write_lock, almemory_order_relaxed);
}
void ReadLock(RWLock *lock)
{
LOCK(lock->read_entry_lock);
LOCK(lock->read_lock);
if(IncrementRef(&lock->read_count) == 1)
/* NOTE: ATOMIC_ADD returns the *old* value! */
if(ATOMIC_ADD(&lock->read_count, 1, almemory_order_acq_rel) == 0)
LOCK(lock->write_lock);
UNLOCK(lock->read_lock);
UNLOCK(lock->read_entry_lock);
@@ -38,13 +39,14 @@ void ReadLock(RWLock *lock)
void ReadUnlock(RWLock *lock)
{
if(DecrementRef(&lock->read_count) == 0)
/* NOTE: ATOMIC_SUB returns the *old* value! */
if(ATOMIC_SUB(&lock->read_count, 1, almemory_order_acq_rel) == 1)
UNLOCK(lock->write_lock);
}
void WriteLock(RWLock *lock)
{
if(IncrementRef(&lock->write_count) == 1)
if(ATOMIC_ADD(&lock->write_count, 1, almemory_order_acq_rel) == 0)
LOCK(lock->read_lock);
LOCK(lock->write_lock);
}
@@ -52,6 +54,6 @@ void WriteLock(RWLock *lock)
void WriteUnlock(RWLock *lock)
{
UNLOCK(lock->write_lock);
if(DecrementRef(&lock->write_count) == 0)
if(ATOMIC_SUB(&lock->write_count, 1, almemory_order_acq_rel) == 1)
UNLOCK(lock->read_lock);
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef AL_RWLOCK_H
#define AL_RWLOCK_H
#include "bool.h"
#include "atomic.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
RefCount read_count;
RefCount write_count;
ATOMIC_FLAG read_lock;
ATOMIC_FLAG read_entry_lock;
ATOMIC_FLAG write_lock;
} RWLock;
#define RWLOCK_STATIC_INITIALIZE { ATOMIC_INIT_STATIC(0), ATOMIC_INIT_STATIC(0), \
ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT }
void RWLockInit(RWLock *lock);
void ReadLock(RWLock *lock);
void ReadUnlock(RWLock *lock);
void WriteLock(RWLock *lock);
void WriteUnlock(RWLock *lock);
#ifdef __cplusplus
}
#endif
#endif /* AL_RWLOCK_H */
+21
View File
@@ -0,0 +1,21 @@
#ifndef AL_STATIC_ASSERT_H
#define AL_STATIC_ASSERT_H
#include <assert.h>
#ifndef static_assert
#ifdef HAVE_C11_STATIC_ASSERT
#define static_assert _Static_assert
#else
#define CTASTR2(_pre,_post) _pre##_post
#define CTASTR(_pre,_post) CTASTR2(_pre,_post)
#if defined(__COUNTER__)
#define static_assert(_cond, _msg) typedef struct { int CTASTR(static_assert_failed_at_line_,__LINE__) : !!(_cond); } CTASTR(static_assertion_,__COUNTER__)
#else
#define static_assert(_cond, _msg) struct { int CTASTR(static_assert_failed_at_line_,__LINE__) : !!(_cond); }
#endif
#endif
#endif
#endif /* AL_STATIC_ASSERT_H */
+65 -60
View File
@@ -55,7 +55,7 @@ extern inline int altss_set(altss_t tss_id, void *val);
#endif
#define THREAD_STACK_SIZE (1*1024*1024) /* 1MB */
#define THREAD_STACK_SIZE (2*1024*1024) /* 2MB */
#ifdef _WIN32
@@ -194,7 +194,8 @@ int althrd_sleep(const struct timespec *ts, struct timespec* UNUSED(rem))
int almtx_init(almtx_t *mtx, int type)
{
if(!mtx) return althrd_error;
type &= ~(almtx_recursive|almtx_timed);
type &= ~almtx_recursive;
if(type != almtx_plain)
return althrd_error;
@@ -207,27 +208,10 @@ void almtx_destroy(almtx_t *mtx)
DeleteCriticalSection(mtx);
}
int almtx_timedlock(almtx_t *mtx, const struct timespec *ts)
int almtx_timedlock(almtx_t* UNUSED(mtx), const struct timespec* UNUSED(ts))
{
int ret;
if(!mtx || !ts)
return althrd_error;
while((ret=almtx_trylock(mtx)) == althrd_busy)
{
struct timespec now;
if(ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000 ||
altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC)
return althrd_error;
if(now.tv_sec > ts->tv_sec || (now.tv_sec == ts->tv_sec && now.tv_nsec >= ts->tv_nsec))
return althrd_timedout;
althrd_yield();
}
return ret;
/* Windows CRITICAL_SECTIONs don't seem to have a timedlock method. */
return althrd_error;
}
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
@@ -264,10 +248,19 @@ int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_poi
if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
return althrd_error;
sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
sleeptime += (time_point->tv_sec - curtime.tv_sec)*1000;
if(SleepConditionVariableCS(cond, mtx, sleeptime) != 0)
return althrd_success;
if(curtime.tv_sec > time_point->tv_sec || (curtime.tv_sec == time_point->tv_sec &&
curtime.tv_nsec >= time_point->tv_nsec))
{
if(SleepConditionVariableCS(cond, mtx, 0) != 0)
return althrd_success;
}
else
{
sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
sleeptime += (DWORD)(time_point->tv_sec - curtime.tv_sec)*1000;
if(SleepConditionVariableCS(cond, mtx, sleeptime) != 0)
return althrd_success;
}
return (GetLastError()==ERROR_TIMEOUT) ? althrd_timedout : althrd_error;
}
@@ -306,8 +299,8 @@ int alcnd_init(alcnd_t *cond)
InitRef(&icond->wait_count, 0);
icond->events[SIGNAL] = CreateEvent(NULL, FALSE, FALSE, NULL);
icond->events[BROADCAST] = CreateEvent(NULL, TRUE, FALSE, NULL);
icond->events[SIGNAL] = CreateEventW(NULL, FALSE, FALSE, NULL);
icond->events[BROADCAST] = CreateEventW(NULL, TRUE, FALSE, NULL);
if(!icond->events[SIGNAL] || !icond->events[BROADCAST])
{
if(icond->events[SIGNAL])
@@ -364,8 +357,15 @@ int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_poi
if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
return althrd_error;
sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
sleeptime += (time_point->tv_sec - curtime.tv_sec)*1000;
if(curtime.tv_sec > time_point->tv_sec || (curtime.tv_sec == time_point->tv_sec &&
curtime.tv_nsec >= time_point->tv_nsec))
sleeptime = 0;
else
{
sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
sleeptime += (DWORD)(time_point->tv_sec - curtime.tv_sec)*1000;
}
IncrementRef(&icond->wait_count);
LeaveCriticalSection(mtx);
@@ -413,8 +413,8 @@ static void NTAPI altss_callback(void* UNUSED(handle), DWORD reason, void* UNUSE
LockUIntMapRead(&TlsDestructors);
for(i = 0;i < TlsDestructors.size;i++)
{
void *ptr = altss_get(TlsDestructors.array[i].key);
altss_dtor_t callback = (altss_dtor_t)TlsDestructors.array[i].value;
void *ptr = altss_get(TlsDestructors.keys[i]);
altss_dtor_t callback = (altss_dtor_t)TlsDestructors.values[i];
if(ptr && callback)
callback(ptr);
}
@@ -500,6 +500,8 @@ void althrd_setname(althrd_t thr, const char *name)
#if defined(PTHREAD_SETNAME_NP_ONE_PARAM)
if(althrd_equal(thr, althrd_current()))
pthread_setname_np(name);
#elif defined(PTHREAD_SETNAME_NP_THREE_PARAMS)
pthread_setname_np(thr, "%s", (void*)name);
#else
pthread_setname_np(thr, name);
#endif
@@ -531,6 +533,8 @@ int althrd_create(althrd_t *thr, althrd_start_t func, void *arg)
{
thread_cntr *cntr;
pthread_attr_t attr;
size_t stackmult = 1;
int err;
cntr = malloc(sizeof(*cntr));
if(!cntr) return althrd_nomem;
@@ -540,7 +544,8 @@ int althrd_create(althrd_t *thr, althrd_start_t func, void *arg)
free(cntr);
return althrd_error;
}
if(pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE) != 0)
retry_stacksize:
if(pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE*stackmult) != 0)
{
pthread_attr_destroy(&attr);
free(cntr);
@@ -549,15 +554,30 @@ int althrd_create(althrd_t *thr, althrd_start_t func, void *arg)
cntr->func = func;
cntr->arg = arg;
if(pthread_create(thr, &attr, althrd_starter, cntr) != 0)
if((err=pthread_create(thr, &attr, althrd_starter, cntr)) == 0)
{
pthread_attr_destroy(&attr);
free(cntr);
return althrd_error;
return althrd_success;
}
if(err == EINVAL)
{
/* If an invalid stack size, try increasing it (limit x4, 8MB). */
if(stackmult < 4)
{
stackmult *= 2;
goto retry_stacksize;
}
/* If still nothing, try defaults and hope they're good enough. */
if(pthread_create(thr, NULL, althrd_starter, cntr) == 0)
{
pthread_attr_destroy(&attr);
return althrd_success;
}
}
pthread_attr_destroy(&attr);
return althrd_success;
free(cntr);
return althrd_error;
}
int althrd_detach(althrd_t thr)
@@ -584,8 +604,13 @@ int almtx_init(almtx_t *mtx, int type)
int ret;
if(!mtx) return althrd_error;
#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
if((type&~(almtx_recursive|almtx_timed)) != 0)
return althrd_error;
#else
if((type&~almtx_recursive) != 0)
return althrd_error;
#endif
type &= ~almtx_timed;
if(type == almtx_plain)
@@ -621,36 +646,16 @@ void almtx_destroy(almtx_t *mtx)
int almtx_timedlock(almtx_t *mtx, const struct timespec *ts)
{
int ret;
#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
ret = pthread_mutex_timedlock(mtx, ts);
int ret = pthread_mutex_timedlock(mtx, ts);
switch(ret)
{
case 0: return althrd_success;
case ETIMEDOUT: return althrd_timedout;
case EBUSY: return althrd_busy;
}
return althrd_error;
#else
if(!mtx || !ts)
return althrd_error;
while((ret=almtx_trylock(mtx)) == althrd_busy)
{
struct timespec now;
if(ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000 ||
altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC)
return althrd_error;
if(now.tv_sec > ts->tv_sec || (now.tv_sec == ts->tv_sec && now.tv_nsec >= ts->tv_nsec))
return althrd_timedout;
althrd_yield();
}
return ret;
#endif
return althrd_error;
}
int alcnd_init(alcnd_t *cond)
+237
View File
@@ -0,0 +1,237 @@
#ifndef AL_THREADS_H
#define AL_THREADS_H
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
enum {
althrd_success = 0,
althrd_error,
althrd_nomem,
althrd_timedout,
althrd_busy
};
enum {
almtx_plain = 0,
almtx_recursive = 1,
almtx_timed = 2
};
typedef int (*althrd_start_t)(void*);
typedef void (*altss_dtor_t)(void*);
#define AL_TIME_UTC 1
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifndef HAVE_STRUCT_TIMESPEC
struct timespec {
time_t tv_sec;
long tv_nsec;
};
#endif
typedef DWORD althrd_t;
typedef CRITICAL_SECTION almtx_t;
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
typedef CONDITION_VARIABLE alcnd_t;
#else
typedef struct { void *Ptr; } alcnd_t;
#endif
typedef DWORD altss_t;
typedef LONG alonce_flag;
#define AL_ONCE_FLAG_INIT 0
int althrd_sleep(const struct timespec *ts, struct timespec *rem);
void alcall_once(alonce_flag *once, void (*callback)(void));
inline althrd_t althrd_current(void)
{
return GetCurrentThreadId();
}
inline int althrd_equal(althrd_t thr0, althrd_t thr1)
{
return thr0 == thr1;
}
inline void althrd_exit(int res)
{
ExitThread(res);
}
inline void althrd_yield(void)
{
SwitchToThread();
}
inline int almtx_lock(almtx_t *mtx)
{
if(!mtx) return althrd_error;
EnterCriticalSection(mtx);
return althrd_success;
}
inline int almtx_unlock(almtx_t *mtx)
{
if(!mtx) return althrd_error;
LeaveCriticalSection(mtx);
return althrd_success;
}
inline int almtx_trylock(almtx_t *mtx)
{
if(!mtx) return althrd_error;
if(!TryEnterCriticalSection(mtx))
return althrd_busy;
return althrd_success;
}
inline void *altss_get(altss_t tss_id)
{
return TlsGetValue(tss_id);
}
inline int altss_set(altss_t tss_id, void *val)
{
if(TlsSetValue(tss_id, val) == 0)
return althrd_error;
return althrd_success;
}
#else
#include <stdint.h>
#include <errno.h>
#include <pthread.h>
typedef pthread_t althrd_t;
typedef pthread_mutex_t almtx_t;
typedef pthread_cond_t alcnd_t;
typedef pthread_key_t altss_t;
typedef pthread_once_t alonce_flag;
#define AL_ONCE_FLAG_INIT PTHREAD_ONCE_INIT
inline althrd_t althrd_current(void)
{
return pthread_self();
}
inline int althrd_equal(althrd_t thr0, althrd_t thr1)
{
return pthread_equal(thr0, thr1);
}
inline void althrd_exit(int res)
{
pthread_exit((void*)(intptr_t)res);
}
inline void althrd_yield(void)
{
sched_yield();
}
inline int althrd_sleep(const struct timespec *ts, struct timespec *rem)
{
int ret = nanosleep(ts, rem);
if(ret != 0)
{
ret = ((errno==EINTR) ? -1 : -2);
errno = 0;
}
return ret;
}
inline int almtx_lock(almtx_t *mtx)
{
if(pthread_mutex_lock(mtx) != 0)
return althrd_error;
return althrd_success;
}
inline int almtx_unlock(almtx_t *mtx)
{
if(pthread_mutex_unlock(mtx) != 0)
return althrd_error;
return althrd_success;
}
inline int almtx_trylock(almtx_t *mtx)
{
int ret = pthread_mutex_trylock(mtx);
switch(ret)
{
case 0: return althrd_success;
case EBUSY: return althrd_busy;
}
return althrd_error;
}
inline void *altss_get(altss_t tss_id)
{
return pthread_getspecific(tss_id);
}
inline int altss_set(altss_t tss_id, void *val)
{
if(pthread_setspecific(tss_id, val) != 0)
return althrd_error;
return althrd_success;
}
inline void alcall_once(alonce_flag *once, void (*callback)(void))
{
pthread_once(once, callback);
}
#endif
int althrd_create(althrd_t *thr, althrd_start_t func, void *arg);
int althrd_detach(althrd_t thr);
int althrd_join(althrd_t thr, int *res);
void althrd_setname(althrd_t thr, const char *name);
int almtx_init(almtx_t *mtx, int type);
void almtx_destroy(almtx_t *mtx);
int almtx_timedlock(almtx_t *mtx, const struct timespec *ts);
int alcnd_init(alcnd_t *cond);
int alcnd_signal(alcnd_t *cond);
int alcnd_broadcast(alcnd_t *cond);
int alcnd_wait(alcnd_t *cond, almtx_t *mtx);
int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point);
void alcnd_destroy(alcnd_t *cond);
int altss_create(altss_t *tss_id, altss_dtor_t callback);
void altss_delete(altss_t tss_id);
int altimespec_get(struct timespec *ts, int base);
void al_nssleep(unsigned long nsec);
#ifdef __cplusplus
}
#endif
#endif /* AL_THREADS_H */
+232 -57
View File
@@ -6,6 +6,8 @@
#include <stdlib.h>
#include <string.h>
#include "almalloc.h"
extern inline void LockUIntMapRead(UIntMap *map);
extern inline void UnlockUIntMapRead(UIntMap *map);
@@ -15,9 +17,10 @@ extern inline void UnlockUIntMapWrite(UIntMap *map);
void InitUIntMap(UIntMap *map, ALsizei limit)
{
map->array = NULL;
map->keys = NULL;
map->values = NULL;
map->size = 0;
map->maxsize = 0;
map->capacity = 0;
map->limit = limit;
RWLockInit(&map->lock);
}
@@ -25,13 +28,19 @@ void InitUIntMap(UIntMap *map, ALsizei limit)
void ResetUIntMap(UIntMap *map)
{
WriteLock(&map->lock);
free(map->array);
map->array = NULL;
al_free(map->keys);
map->keys = NULL;
map->values = NULL;
map->size = 0;
map->maxsize = 0;
map->capacity = 0;
WriteUnlock(&map->lock);
}
void RelimitUIntMapNoLock(UIntMap *map, ALsizei limit)
{
map->limit = limit;
}
ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
{
ALsizei pos = 0;
@@ -39,80 +48,186 @@ ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
WriteLock(&map->lock);
if(map->size > 0)
{
ALsizei low = 0;
ALsizei high = map->size - 1;
while(low < high)
{
ALsizei mid = low + (high-low)/2;
if(map->array[mid].key < key)
low = mid + 1;
ALsizei count = map->size;
do {
ALsizei step = count>>1;
ALsizei i = pos+step;
if(!(map->keys[i] < key))
count = step;
else
high = mid;
}
if(map->array[low].key < key)
low++;
pos = low;
{
pos = i+1;
count -= step+1;
}
} while(count > 0);
}
if(pos == map->size || map->array[pos].key != key)
if(pos == map->size || map->keys[pos] != key)
{
if(map->size == map->limit)
if(map->size >= map->limit)
{
WriteUnlock(&map->lock);
return AL_OUT_OF_MEMORY;
}
if(map->size == map->maxsize)
if(map->size == map->capacity)
{
ALvoid *temp = NULL;
ALsizei newsize;
ALuint *keys = NULL;
ALvoid **values;
ALsizei newcap, keylen;
newsize = (map->maxsize ? (map->maxsize<<1) : 4);
if(newsize >= map->maxsize)
temp = realloc(map->array, newsize*sizeof(map->array[0]));
if(!temp)
newcap = (map->capacity ? (map->capacity<<1) : 4);
if(map->limit > 0 && newcap > map->limit)
newcap = map->limit;
if(newcap > map->capacity)
{
/* Round the memory size for keys up to a multiple of the
* pointer size.
*/
keylen = newcap * sizeof(map->keys[0]);
keylen += sizeof(map->values[0]) - 1;
keylen -= keylen%sizeof(map->values[0]);
keys = al_malloc(16, keylen + newcap*sizeof(map->values[0]));
}
if(!keys)
{
WriteUnlock(&map->lock);
return AL_OUT_OF_MEMORY;
}
map->array = temp;
map->maxsize = newsize;
values = (ALvoid**)((ALbyte*)keys + keylen);
if(map->keys)
{
memcpy(keys, map->keys, map->size*sizeof(map->keys[0]));
memcpy(values, map->values, map->size*sizeof(map->values[0]));
}
al_free(map->keys);
map->keys = keys;
map->values = values;
map->capacity = newcap;
}
if(pos < map->size)
memmove(&map->array[pos+1], &map->array[pos],
(map->size-pos)*sizeof(map->array[0]));
{
memmove(&map->keys[pos+1], &map->keys[pos],
(map->size-pos)*sizeof(map->keys[0]));
memmove(&map->values[pos+1], &map->values[pos],
(map->size-pos)*sizeof(map->values[0]));
}
map->size++;
}
map->array[pos].key = key;
map->array[pos].value = value;
map->keys[pos] = key;
map->values[pos] = value;
WriteUnlock(&map->lock);
return AL_NO_ERROR;
}
ALenum InsertUIntMapEntryNoLock(UIntMap *map, ALuint key, ALvoid *value)
{
ALsizei pos = 0;
if(map->size > 0)
{
ALsizei count = map->size;
do {
ALsizei step = count>>1;
ALsizei i = pos+step;
if(!(map->keys[i] < key))
count = step;
else
{
pos = i+1;
count -= step+1;
}
} while(count > 0);
}
if(pos == map->size || map->keys[pos] != key)
{
if(map->size >= map->limit)
return AL_OUT_OF_MEMORY;
if(map->size == map->capacity)
{
ALuint *keys = NULL;
ALvoid **values;
ALsizei newcap, keylen;
newcap = (map->capacity ? (map->capacity<<1) : 4);
if(map->limit > 0 && newcap > map->limit)
newcap = map->limit;
if(newcap > map->capacity)
{
/* Round the memory size for keys up to a multiple of the
* pointer size.
*/
keylen = newcap * sizeof(map->keys[0]);
keylen += sizeof(map->values[0]) - 1;
keylen -= keylen%sizeof(map->values[0]);
keys = al_malloc(16, keylen + newcap*sizeof(map->values[0]));
}
if(!keys)
return AL_OUT_OF_MEMORY;
values = (ALvoid**)((ALbyte*)keys + keylen);
if(map->keys)
{
memcpy(keys, map->keys, map->size*sizeof(map->keys[0]));
memcpy(values, map->values, map->size*sizeof(map->values[0]));
}
al_free(map->keys);
map->keys = keys;
map->values = values;
map->capacity = newcap;
}
if(pos < map->size)
{
memmove(&map->keys[pos+1], &map->keys[pos],
(map->size-pos)*sizeof(map->keys[0]));
memmove(&map->values[pos+1], &map->values[pos],
(map->size-pos)*sizeof(map->values[0]));
}
map->size++;
}
map->keys[pos] = key;
map->values[pos] = value;
return AL_NO_ERROR;
}
ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
{
ALvoid *ptr = NULL;
WriteLock(&map->lock);
if(map->size > 0)
{
ALsizei low = 0;
ALsizei high = map->size - 1;
while(low < high)
{
ALsizei mid = low + (high-low)/2;
if(map->array[mid].key < key)
low = mid + 1;
ALsizei pos = 0;
ALsizei count = map->size;
do {
ALsizei step = count>>1;
ALsizei i = pos+step;
if(!(map->keys[i] < key))
count = step;
else
high = mid;
}
if(map->array[low].key == key)
{
pos = i+1;
count -= step+1;
}
} while(count > 0);
if(pos < map->size && map->keys[pos] == key)
{
ptr = map->array[low].value;
if(low < map->size-1)
memmove(&map->array[low], &map->array[low+1],
(map->size-1-low)*sizeof(map->array[0]));
ptr = map->values[pos];
if(pos < map->size-1)
{
memmove(&map->keys[pos], &map->keys[pos+1],
(map->size-1-pos)*sizeof(map->keys[0]));
memmove(&map->values[pos], &map->values[pos+1],
(map->size-1-pos)*sizeof(map->values[0]));
}
map->size--;
}
}
@@ -120,25 +235,85 @@ ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
return ptr;
}
ALvoid *RemoveUIntMapKeyNoLock(UIntMap *map, ALuint key)
{
ALvoid *ptr = NULL;
if(map->size > 0)
{
ALsizei pos = 0;
ALsizei count = map->size;
do {
ALsizei step = count>>1;
ALsizei i = pos+step;
if(!(map->keys[i] < key))
count = step;
else
{
pos = i+1;
count -= step+1;
}
} while(count > 0);
if(pos < map->size && map->keys[pos] == key)
{
ptr = map->values[pos];
if(pos < map->size-1)
{
memmove(&map->keys[pos], &map->keys[pos+1],
(map->size-1-pos)*sizeof(map->keys[0]));
memmove(&map->values[pos], &map->values[pos+1],
(map->size-1-pos)*sizeof(map->values[0]));
}
map->size--;
}
}
return ptr;
}
ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
{
ALvoid *ptr = NULL;
ReadLock(&map->lock);
if(map->size > 0)
{
ALsizei low = 0;
ALsizei high = map->size - 1;
while(low < high)
{
ALsizei mid = low + (high-low)/2;
if(map->array[mid].key < key)
low = mid + 1;
ALsizei pos = 0;
ALsizei count = map->size;
do {
ALsizei step = count>>1;
ALsizei i = pos+step;
if(!(map->keys[i] < key))
count = step;
else
high = mid;
}
if(map->array[low].key == key)
ptr = map->array[low].value;
{
pos = i+1;
count -= step+1;
}
} while(count > 0);
if(pos < map->size && map->keys[pos] == key)
ptr = map->values[pos];
}
ReadUnlock(&map->lock);
return ptr;
}
ALvoid *LookupUIntMapKeyNoLock(UIntMap *map, ALuint key)
{
if(map->size > 0)
{
ALsizei pos = 0;
ALsizei count = map->size;
do {
ALsizei step = count>>1;
ALsizei i = pos+step;
if(!(map->keys[i] < key))
count = step;
else
{
pos = i+1;
count -= step+1;
}
} while(count > 0);
if(pos < map->size && map->keys[pos] == key)
return map->values[pos];
}
return NULL;
}
+47
View File
@@ -0,0 +1,47 @@
#ifndef AL_UINTMAP_H
#define AL_UINTMAP_H
#include "AL/al.h"
#include "rwlock.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct UIntMap {
ALuint *keys;
/* Shares memory with keys. */
ALvoid **values;
ALsizei size;
ALsizei capacity;
ALsizei limit;
RWLock lock;
} UIntMap;
#define UINTMAP_STATIC_INITIALIZE_N(_n) { NULL, NULL, 0, 0, (_n), RWLOCK_STATIC_INITIALIZE }
#define UINTMAP_STATIC_INITIALIZE UINTMAP_STATIC_INITIALIZE_N(INT_MAX)
void InitUIntMap(UIntMap *map, ALsizei limit);
void ResetUIntMap(UIntMap *map);
void RelimitUIntMapNoLock(UIntMap *map, ALsizei limit);
ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value);
ALenum InsertUIntMapEntryNoLock(UIntMap *map, ALuint key, ALvoid *value);
ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key);
ALvoid *RemoveUIntMapKeyNoLock(UIntMap *map, ALuint key);
ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key);
ALvoid *LookupUIntMapKeyNoLock(UIntMap *map, ALuint key);
inline void LockUIntMapRead(UIntMap *map)
{ ReadLock(&map->lock); }
inline void UnlockUIntMapRead(UIntMap *map)
{ ReadUnlock(&map->lock); }
inline void LockUIntMapWrite(UIntMap *map)
{ WriteLock(&map->lock); }
inline void UnlockUIntMapWrite(UIntMap *map)
{ WriteUnlock(&map->lock); }
#ifdef __cplusplus
}
#endif
#endif /* AL_UINTMAP_H */