Update OpenAL Soft to commit 414b56edec5441211dc924fef365c54267c04f1c

This commit is contained in:
Alex Szpakowski
2018-04-03 20:46:26 -03:00
parent ac70ec9f6f
commit 5be7c968b8
138 changed files with 18084 additions and 18482 deletions
+48
View File
@@ -10,8 +10,20 @@
#endif
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#else
#include <unistd.h>
#endif
#ifdef __GNUC__
#define LIKELY(x) __builtin_expect(!!(x), !0)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define LIKELY(x) (!!(x))
#define UNLIKELY(x) (!!(x))
#endif
void *al_malloc(size_t alignment, size_t size)
{
#if defined(HAVE_ALIGNED_ALLOC)
@@ -60,3 +72,39 @@ void al_free(void *ptr)
}
#endif
}
size_t al_get_page_size(void)
{
static size_t psize = 0;
if(UNLIKELY(!psize))
{
#ifdef HAVE_SYSCONF
#if defined(_SC_PAGESIZE)
if(!psize) psize = sysconf(_SC_PAGESIZE);
#elif defined(_SC_PAGE_SIZE)
if(!psize) psize = sysconf(_SC_PAGE_SIZE);
#endif
#endif /* HAVE_SYSCONF */
#ifdef _WIN32
if(!psize)
{
SYSTEM_INFO sysinfo;
memset(&sysinfo, 0, sizeof(sysinfo));
GetSystemInfo(&sysinfo);
psize = sysinfo.dwPageSize;
}
#endif
if(!psize) psize = DEF_ALIGN;
}
return psize;
}
int al_is_sane_alignment_allocator(void)
{
#if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN) || defined(HAVE__ALIGNED_MALLOC)
return 1;
#else
return 0;
#endif
}
+9
View File
@@ -14,6 +14,15 @@ void *al_malloc(size_t alignment, size_t size);
void *al_calloc(size_t alignment, size_t size);
void al_free(void *ptr);
size_t al_get_page_size(void);
/**
* Returns non-0 if the allocation function has direct alignment handling.
* Otherwise, the standard malloc is used with an over-allocation and pointer
* offset strategy.
*/
int al_is_sane_alignment_allocator(void);
#ifdef __cplusplus
}
#endif
+18 -4
View File
@@ -4,6 +4,19 @@
#include "static_assert.h"
#include "bool.h"
#ifdef __GNUC__
/* This helps cast away the const-ness of a pointer without accidentally
* changing the pointer type. This is necessary due to Clang's inability to use
* atomic_load on a const _Atomic variable.
*/
#define CONST_CAST(T, V) __extension__({ \
const T _tmp = (V); \
(T)_tmp; \
})
#else
#define CONST_CAST(T, V) ((T)(V))
#endif
#ifdef __cplusplus
extern "C" {
#endif
@@ -293,6 +306,7 @@ enum almemory_order {
} while(0)
int _al_invalid_atomic_size(); /* not defined */
void *_al_invalid_atomic_ptr_size(); /* not defined */
#define ATOMIC_ADD(_val, _incr, _MO) \
((sizeof((_val)->value)==4) ? WRAP_ADDSUB(LONG, AtomicAdd32, &(_val)->value, (_incr)) : \
@@ -314,7 +328,7 @@ int _al_invalid_atomic_size(); /* not defined */
#define ATOMIC_EXCHANGE_PTR(_val, _newval, _MO) \
((sizeof((_val)->value)==sizeof(void*)) ? AtomicSwapPtr((void*volatile*)&(_val)->value, (_newval)) : \
(void*)_al_invalid_atomic_size())
_al_invalid_atomic_ptr_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())
@@ -399,11 +413,11 @@ 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); }
{ return ATOMIC_LOAD(ptr, almemory_order_acquire); }
inline uint IncrementRef(RefCount *ptr)
{ return ATOMIC_ADD_SEQ(ptr, 1)+1; }
{ return ATOMIC_ADD(ptr, 1, almemory_order_acq_rel)+1; }
inline uint DecrementRef(RefCount *ptr)
{ return ATOMIC_SUB_SEQ(ptr, 1)-1; }
{ return ATOMIC_SUB(ptr, 1, almemory_order_acq_rel)-1; }
/* WARNING: A livelock is theoretically possible if another thread keeps
+11
View File
@@ -6,6 +6,10 @@
#include <float.h>
#endif
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
#define F_PI (3.14159265358979323846f)
#define F_PI_2 (1.57079632679489661923f)
#define F_TAU (6.28318530717958647692f)
@@ -29,6 +33,13 @@ static inline float log2f(float f)
}
#endif
#ifndef HAVE_CBRTF
static inline float cbrtf(float f)
{
return powf(f, 1.0f/3.0f);
}
#endif
#define DEG2RAD(x) ((float)(x) * (F_PI/180.0f))
#define RAD2DEG(x) ((float)(x) * (180.0f/F_PI))
+103 -140
View File
@@ -64,6 +64,22 @@ extern inline int altss_set(altss_t tss_id, void *val);
#include <mmsystem.h>
/* An associative map of uint:void* pairs. The key is the unique Thread ID and
* the value is the thread HANDLE. The thread ID is passed around as the
* althrd_t since there is only one ID per thread, whereas a thread may be
* referenced by multiple different HANDLEs. This map allows retrieving the
* original handle which is needed to join the thread and get its return value.
*/
static UIntMap ThrdIdHandle = UINTMAP_STATIC_INITIALIZE;
/* An associative map of uint:void* pairs. The key is the TLS index (given by
* TlsAlloc), and the value is the altss_dtor_t callback. When a thread exits,
* we iterate over the TLS indices for their thread-local value and call the
* destructor function with it if they're both not NULL.
*/
static UIntMap TlsDestructors = UINTMAP_STATIC_INITIALIZE;
void althrd_setname(althrd_t thr, const char *name)
{
#if defined(_MSC_VER)
@@ -94,23 +110,6 @@ void althrd_setname(althrd_t thr, const char *name)
}
static UIntMap ThrdIdHandle = UINTMAP_STATIC_INITIALIZE;
static void NTAPI althrd_callback(void* UNUSED(handle), DWORD reason, void* UNUSED(reserved))
{
if(reason == DLL_PROCESS_DETACH)
ResetUIntMap(&ThrdIdHandle);
}
#ifdef _MSC_VER
#pragma section(".CRT$XLC",read)
__declspec(allocate(".CRT$XLC")) PIMAGE_TLS_CALLBACK althrd_callback_ = althrd_callback;
#elif defined(__GNUC__)
PIMAGE_TLS_CALLBACK althrd_callback_ __attribute__((section(".CRT$XLC"))) = althrd_callback;
#else
PIMAGE_TLS_CALLBACK althrd_callback_ = althrd_callback;
#endif
typedef struct thread_cntr {
althrd_start_t func;
void *arg;
@@ -208,12 +207,6 @@ void almtx_destroy(almtx_t *mtx)
DeleteCriticalSection(mtx);
}
int almtx_timedlock(almtx_t* UNUSED(mtx), const struct timespec* UNUSED(ts))
{
/* Windows CRITICAL_SECTIONs don't seem to have a timedlock method. */
return althrd_error;
}
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
int alcnd_init(alcnd_t *cond)
{
@@ -240,30 +233,6 @@ int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
return althrd_error;
}
int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
{
struct timespec curtime;
DWORD sleeptime;
if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
return althrd_error;
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;
}
void alcnd_destroy(alcnd_t* UNUSED(cond))
{
/* Nothing to delete? */
@@ -348,37 +317,6 @@ int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
return althrd_success;
}
int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
{
_int_alcnd_t *icond = cond->Ptr;
struct timespec curtime;
DWORD sleeptime;
int res;
if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
return althrd_error;
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);
res = WaitForMultipleObjects(2, icond->events, FALSE, sleeptime);
if(DecrementRef(&icond->wait_count) == 0 && res == WAIT_OBJECT_0+BROADCAST)
ResetEvent(icond->events[BROADCAST]);
EnterCriticalSection(mtx);
return (res == WAIT_TIMEOUT) ? althrd_timedout : althrd_success;
}
void alcnd_destroy(alcnd_t *cond)
{
_int_alcnd_t *icond = cond->Ptr;
@@ -389,46 +327,40 @@ void alcnd_destroy(alcnd_t *cond)
#endif /* defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600 */
/* An associative map of uint:void* pairs. The key is the TLS index (given by
* TlsAlloc), and the value is the altss_dtor_t callback. When a thread exits,
* we iterate over the TLS indices for their thread-local value and call the
* destructor function with it if they're both not NULL. To avoid using
* DllMain, a PIMAGE_TLS_CALLBACK function pointer is placed in a ".CRT$XLx"
* section (where x is a character A to Z) which will be called by the CRT.
*/
static UIntMap TlsDestructors = UINTMAP_STATIC_INITIALIZE;
static void NTAPI altss_callback(void* UNUSED(handle), DWORD reason, void* UNUSED(reserved))
int alsem_init(alsem_t *sem, unsigned int initial)
{
ALsizei i;
if(reason == DLL_PROCESS_DETACH)
{
ResetUIntMap(&TlsDestructors);
return;
}
if(reason != DLL_THREAD_DETACH)
return;
LockUIntMapRead(&TlsDestructors);
for(i = 0;i < TlsDestructors.size;i++)
{
void *ptr = altss_get(TlsDestructors.keys[i]);
altss_dtor_t callback = (altss_dtor_t)TlsDestructors.values[i];
if(ptr && callback)
callback(ptr);
}
UnlockUIntMapRead(&TlsDestructors);
*sem = CreateSemaphore(NULL, initial, INT_MAX, NULL);
if(*sem != NULL) return althrd_success;
return althrd_error;
}
#ifdef _MSC_VER
#pragma section(".CRT$XLB",read)
__declspec(allocate(".CRT$XLB")) PIMAGE_TLS_CALLBACK altss_callback_ = altss_callback;
#elif defined(__GNUC__)
PIMAGE_TLS_CALLBACK altss_callback_ __attribute__((section(".CRT$XLB"))) = altss_callback;
#else
#warning "No TLS callback support, thread-local contexts may leak references on poorly written applications."
PIMAGE_TLS_CALLBACK altss_callback_ = altss_callback;
#endif
void alsem_destroy(alsem_t *sem)
{
CloseHandle(*sem);
}
int alsem_post(alsem_t *sem)
{
DWORD ret = ReleaseSemaphore(*sem, 1, NULL);
if(ret) return althrd_success;
return althrd_error;
}
int alsem_wait(alsem_t *sem)
{
DWORD ret = WaitForSingleObject(*sem, INFINITE);
if(ret == WAIT_OBJECT_0) return althrd_success;
return althrd_error;
}
int alsem_trywait(alsem_t *sem)
{
DWORD ret = WaitForSingleObject(*sem, 0);
if(ret == WAIT_OBJECT_0) return althrd_success;
if(ret == WAIT_TIMEOUT) return althrd_busy;
return althrd_error;
}
int altss_create(altss_t *tss_id, altss_dtor_t callback)
{
@@ -480,6 +412,27 @@ void alcall_once(alonce_flag *once, void (*callback)(void))
InterlockedExchange(once, 2);
}
void althrd_deinit(void)
{
ResetUIntMap(&ThrdIdHandle);
ResetUIntMap(&TlsDestructors);
}
void althrd_thread_detach(void)
{
ALsizei i;
LockUIntMapRead(&TlsDestructors);
for(i = 0;i < TlsDestructors.size;i++)
{
void *ptr = altss_get(TlsDestructors.keys[i]);
altss_dtor_t callback = (altss_dtor_t)TlsDestructors.values[i];
if(ptr && callback) callback(ptr);
}
UnlockUIntMapRead(&TlsDestructors);
}
#else
#include <sys/time.h>
@@ -493,6 +446,8 @@ void alcall_once(alonce_flag *once, void (*callback)(void))
extern inline int althrd_sleep(const struct timespec *ts, struct timespec *rem);
extern inline void alcall_once(alonce_flag *once, void (*callback)(void));
extern inline void althrd_deinit(void);
extern inline void althrd_thread_detach(void);
void althrd_setname(althrd_t thr, const char *name)
{
@@ -604,15 +559,9 @@ 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)
ret = pthread_mutex_init(mtx, NULL);
else
@@ -644,20 +593,6 @@ void almtx_destroy(almtx_t *mtx)
pthread_mutex_destroy(mtx);
}
int almtx_timedlock(almtx_t *mtx, const struct timespec *ts)
{
#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
int ret = pthread_mutex_timedlock(mtx, ts);
switch(ret)
{
case 0: return althrd_success;
case ETIMEDOUT: return althrd_timedout;
case EBUSY: return althrd_busy;
}
#endif
return althrd_error;
}
int alcnd_init(alcnd_t *cond)
{
if(pthread_cond_init(cond, NULL) == 0)
@@ -686,16 +621,44 @@ int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
return althrd_error;
}
int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
void alcnd_destroy(alcnd_t *cond)
{
if(pthread_cond_timedwait(cond, mtx, time_point) == 0)
pthread_cond_destroy(cond);
}
int alsem_init(alsem_t *sem, unsigned int initial)
{
if(sem_init(sem, 0, initial) == 0)
return althrd_success;
return althrd_error;
}
void alcnd_destroy(alcnd_t *cond)
void alsem_destroy(alsem_t *sem)
{
pthread_cond_destroy(cond);
sem_destroy(sem);
}
int alsem_post(alsem_t *sem)
{
if(sem_post(sem) == 0)
return althrd_success;
return althrd_error;
}
int alsem_wait(alsem_t *sem)
{
if(sem_wait(sem) == 0) return althrd_success;
if(errno == EINTR) return -2;
return althrd_error;
}
int alsem_trywait(alsem_t *sem)
{
if(sem_trywait(sem) == 0) return althrd_success;
if(errno == EWOULDBLOCK) return althrd_busy;
if(errno == EINTR) return -2;
return althrd_error;
}
+26 -3
View File
@@ -3,6 +3,16 @@
#include <time.h>
#if defined(__GNUC__) && defined(__i386__)
/* force_align_arg_pointer is required for proper function arguments aligning
* when SSE code is used. Some systems (Windows, QNX) do not guarantee our
* thread functions will be properly aligned on the stack, even though GCC may
* generate code with the assumption that it is. */
#define FORCE_ALIGN __attribute__((force_align_arg_pointer))
#else
#define FORCE_ALIGN
#endif
#ifdef __cplusplus
extern "C" {
#endif
@@ -18,7 +28,6 @@ enum {
enum {
almtx_plain = 0,
almtx_recursive = 1,
almtx_timed = 2
};
typedef int (*althrd_start_t)(void*);
@@ -47,6 +56,7 @@ typedef CONDITION_VARIABLE alcnd_t;
#else
typedef struct { void *Ptr; } alcnd_t;
#endif
typedef HANDLE alsem_t;
typedef DWORD altss_t;
typedef LONG alonce_flag;
@@ -55,6 +65,9 @@ typedef LONG alonce_flag;
int althrd_sleep(const struct timespec *ts, struct timespec *rem);
void alcall_once(alonce_flag *once, void (*callback)(void));
void althrd_deinit(void);
void althrd_thread_detach(void);
inline althrd_t althrd_current(void)
{
@@ -117,11 +130,13 @@ inline int altss_set(altss_t tss_id, void *val)
#include <stdint.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
typedef pthread_t althrd_t;
typedef pthread_mutex_t almtx_t;
typedef pthread_cond_t alcnd_t;
typedef sem_t alsem_t;
typedef pthread_key_t altss_t;
typedef pthread_once_t alonce_flag;
@@ -204,6 +219,10 @@ inline void alcall_once(alonce_flag *once, void (*callback)(void))
pthread_once(once, callback);
}
inline void althrd_deinit(void) { }
inline void althrd_thread_detach(void) { }
#endif
@@ -214,15 +233,19 @@ 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 alsem_init(alsem_t *sem, unsigned int initial);
void alsem_destroy(alsem_t *sem);
int alsem_post(alsem_t *sem);
int alsem_wait(alsem_t *sem);
int alsem_trywait(alsem_t *sem);
int altss_create(altss_t *tss_id, altss_dtor_t callback);
void altss_delete(altss_t tss_id);
-137
View File
@@ -36,11 +36,6 @@ void ResetUIntMap(UIntMap *map)
WriteUnlock(&map->lock);
}
void RelimitUIntMapNoLock(UIntMap *map, ALsizei limit)
{
map->limit = limit;
}
ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
{
ALsizei pos = 0;
@@ -124,81 +119,6 @@ ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
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;
@@ -235,40 +155,6 @@ 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;
@@ -294,26 +180,3 @@ ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
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;
}
+6 -12
View File
@@ -1,6 +1,8 @@
#ifndef AL_UINTMAP_H
#define AL_UINTMAP_H
#include <limits.h>
#include "AL/al.h"
#include "rwlock.h"
@@ -23,22 +25,14 @@ typedef struct UIntMap {
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); }
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
}
+97
View File
@@ -0,0 +1,97 @@
#ifndef WIN_MAIN_UTF8_H
#define WIN_MAIN_UTF8_H
/* For Windows systems this provides a way to get UTF-8 encoded argv strings,
* and also overrides fopen to accept UTF-8 filenames. Working with wmain
* directly complicates cross-platform compatibility, while normal main() in
* Windows uses the current codepage (which has limited availability of
* characters).
*
* For MinGW, you must link with -municode
*/
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
static FILE *my_fopen(const char *fname, const char *mode)
{
WCHAR *wname=NULL, *wmode=NULL;
int namelen, modelen;
FILE *file = NULL;
errno_t err;
namelen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
modelen = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
if(namelen <= 0 || modelen <= 0)
{
fprintf(stderr, "Failed to convert UTF-8 fname \"%s\", mode \"%s\"\n", fname, mode);
return NULL;
}
wname = calloc(sizeof(WCHAR), namelen+modelen);
wmode = wname + namelen;
MultiByteToWideChar(CP_UTF8, 0, fname, -1, wname, namelen);
MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, modelen);
err = _wfopen_s(&file, wname, wmode);
if(err)
{
errno = err;
file = NULL;
}
free(wname);
return file;
}
#define fopen my_fopen
static char **arglist;
static void cleanup_arglist(void)
{
free(arglist);
}
static void GetUnicodeArgs(int *argc, char ***argv)
{
size_t total;
wchar_t **args;
int nargs, i;
args = CommandLineToArgvW(GetCommandLineW(), &nargs);
if(!args)
{
fprintf(stderr, "Failed to get command line args: %ld\n", GetLastError());
exit(EXIT_FAILURE);
}
total = sizeof(**argv) * nargs;
for(i = 0;i < nargs;i++)
total += WideCharToMultiByte(CP_UTF8, 0, args[i], -1, NULL, 0, NULL, NULL);
atexit(cleanup_arglist);
arglist = *argv = calloc(1, total);
(*argv)[0] = (char*)(*argv + nargs);
for(i = 0;i < nargs-1;i++)
{
int len = WideCharToMultiByte(CP_UTF8, 0, args[i], -1, (*argv)[i], 65535, NULL, NULL);
(*argv)[i+1] = (*argv)[i] + len;
}
WideCharToMultiByte(CP_UTF8, 0, args[i], -1, (*argv)[i], 65535, NULL, NULL);
*argc = nargs;
LocalFree(args);
}
#define GET_UNICODE_ARGS(argc, argv) GetUnicodeArgs(argc, argv)
#else
/* Do nothing. */
#define GET_UNICODE_ARGS(argc, argv)
#endif
#endif /* WIN_MAIN_UTF8_H */