Use an ambisonics-based panning method
[openal-soft.git] / Alc / helpers.c
blob6cb28e3d8dde77fad2604c794d3a2124eebc364b
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2011 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #ifdef _WIN32
22 #ifdef __MINGW32__
23 #define _WIN32_IE 0x501
24 #else
25 #define _WIN32_IE 0x400
26 #endif
27 #endif
29 #include "config.h"
31 #include <stdlib.h>
32 #include <time.h>
33 #include <errno.h>
34 #include <stdarg.h>
35 #ifdef HAVE_MALLOC_H
36 #include <malloc.h>
37 #endif
39 #ifndef AL_NO_UID_DEFS
40 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
41 #define INITGUID
42 #include <windows.h>
43 #ifdef HAVE_GUIDDEF_H
44 #include <guiddef.h>
45 #else
46 #include <initguid.h>
47 #endif
49 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
50 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
52 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
54 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
55 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
56 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
57 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
59 #ifdef HAVE_MMDEVAPI
60 #include <devpropdef.h>
61 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
62 #endif
63 #endif
64 #endif /* AL_NO_UID_DEFS */
66 #ifdef HAVE_DLFCN_H
67 #include <dlfcn.h>
68 #endif
69 #ifdef HAVE_INTRIN_H
70 #include <intrin.h>
71 #endif
72 #ifdef HAVE_CPUID_H
73 #include <cpuid.h>
74 #endif
75 #ifdef HAVE_SYS_SYSCONF_H
76 #include <sys/sysconf.h>
77 #endif
78 #ifdef HAVE_FLOAT_H
79 #include <float.h>
80 #endif
81 #ifdef HAVE_IEEEFP_H
82 #include <ieeefp.h>
83 #endif
85 #ifdef _WIN32_IE
86 #include <shlobj.h>
87 #endif
89 #include "alMain.h"
90 #include "alu.h"
91 #include "atomic.h"
92 #include "uintmap.h"
93 #include "vector.h"
94 #include "alstring.h"
95 #include "compat.h"
96 #include "threads.h"
99 extern inline ALuint NextPowerOf2(ALuint value);
100 extern inline ALint fastf2i(ALfloat f);
101 extern inline ALuint fastf2u(ALfloat f);
104 ALuint CPUCapFlags = 0;
107 void FillCPUCaps(ALuint capfilter)
109 ALuint caps = 0;
111 /* FIXME: We really should get this for all available CPUs in case different
112 * CPUs have different caps (is that possible on one machine?). */
113 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
114 defined(_M_IX86) || defined(_M_X64))
115 union {
116 unsigned int regs[4];
117 char str[sizeof(unsigned int[4])];
118 } cpuinf[3];
120 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
121 ERR("Failed to get CPUID\n");
122 else
124 unsigned int maxfunc = cpuinf[0].regs[0];
125 unsigned int maxextfunc = 0;
127 if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
128 maxextfunc = cpuinf[0].regs[0];
129 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
131 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
132 if(maxextfunc >= 0x80000004 &&
133 __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
134 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
135 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
136 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
138 if(maxfunc >= 1 &&
139 __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
141 if((cpuinf[0].regs[3]&(1<<25)))
143 caps |= CPU_CAP_SSE;
144 if((cpuinf[0].regs[3]&(1<<26)))
146 caps |= CPU_CAP_SSE2;
147 if((cpuinf[0].regs[2]&(1<<19)))
148 caps |= CPU_CAP_SSE4_1;
153 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
154 defined(_M_IX86) || defined(_M_X64))
155 union {
156 int regs[4];
157 char str[sizeof(int[4])];
158 } cpuinf[3];
160 (__cpuid)(cpuinf[0].regs, 0);
161 if(cpuinf[0].regs[0] == 0)
162 ERR("Failed to get CPUID\n");
163 else
165 unsigned int maxfunc = cpuinf[0].regs[0];
166 unsigned int maxextfunc;
168 (__cpuid)(cpuinf[0].regs, 0x80000000);
169 maxextfunc = cpuinf[0].regs[0];
171 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
173 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
174 if(maxextfunc >= 0x80000004)
176 (__cpuid)(cpuinf[0].regs, 0x80000002);
177 (__cpuid)(cpuinf[1].regs, 0x80000003);
178 (__cpuid)(cpuinf[2].regs, 0x80000004);
179 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
182 if(maxfunc >= 1)
184 (__cpuid)(cpuinf[0].regs, 1);
185 if((cpuinf[0].regs[3]&(1<<25)))
187 caps |= CPU_CAP_SSE;
188 if((cpuinf[0].regs[3]&(1<<26)))
190 caps |= CPU_CAP_SSE2;
191 if((cpuinf[0].regs[2]&(1<<19)))
192 caps |= CPU_CAP_SSE4_1;
197 #else
198 /* Assume support for whatever's supported if we can't check for it */
199 #if defined(HAVE_SSE4_1)
200 #warning "Assuming SSE 4.1 run-time support!"
201 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE4_1;
202 #elif defined(HAVE_SSE2)
203 #warning "Assuming SSE 2 run-time support!"
204 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2;
205 #elif defined(HAVE_SSE)
206 #warning "Assuming SSE run-time support!"
207 capfilter |= CPU_CAP_SSE;
208 #endif
209 #endif
210 #ifdef HAVE_NEON
211 /* Assume Neon support if compiled with it */
212 caps |= CPU_CAP_NEON;
213 #endif
215 TRACE("Extensions:%s%s%s%s%s\n",
216 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
217 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
218 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
219 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
220 ((!capfilter) ? " -none-" : "")
222 CPUCapFlags = caps & capfilter;
226 void *al_malloc(size_t alignment, size_t size)
228 #if defined(HAVE_ALIGNED_ALLOC)
229 size = (size+(alignment-1))&~(alignment-1);
230 return aligned_alloc(alignment, size);
231 #elif defined(HAVE_POSIX_MEMALIGN)
232 void *ret;
233 if(posix_memalign(&ret, alignment, size) == 0)
234 return ret;
235 return NULL;
236 #elif defined(HAVE__ALIGNED_MALLOC)
237 return _aligned_malloc(size, alignment);
238 #else
239 char *ret = malloc(size+alignment);
240 if(ret != NULL)
242 *(ret++) = 0x00;
243 while(((ptrdiff_t)ret&(alignment-1)) != 0)
244 *(ret++) = 0x55;
246 return ret;
247 #endif
250 void *al_calloc(size_t alignment, size_t size)
252 void *ret = al_malloc(alignment, size);
253 if(ret) memset(ret, 0, size);
254 return ret;
257 void al_free(void *ptr)
259 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
260 free(ptr);
261 #elif defined(HAVE__ALIGNED_MALLOC)
262 _aligned_free(ptr);
263 #else
264 if(ptr != NULL)
266 char *finder = ptr;
267 do {
268 --finder;
269 } while(*finder == 0x55);
270 free(finder);
272 #endif
276 void SetMixerFPUMode(FPUCtl *ctl)
278 #ifdef HAVE_FENV_H
279 fegetenv(STATIC_CAST(fenv_t, ctl));
280 #if defined(__GNUC__) && defined(HAVE_SSE)
281 if((CPUCapFlags&CPU_CAP_SSE))
282 __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
283 #endif
285 #ifdef FE_TOWARDZERO
286 fesetround(FE_TOWARDZERO);
287 #endif
288 #if defined(__GNUC__) && defined(HAVE_SSE)
289 if((CPUCapFlags&CPU_CAP_SSE))
291 int sseState = ctl->sse_state;
292 sseState |= 0x6000; /* set round-to-zero */
293 sseState |= 0x8000; /* set flush-to-zero */
294 if((CPUCapFlags&CPU_CAP_SSE2))
295 sseState |= 0x0040; /* set denormals-are-zero */
296 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
298 #endif
300 #elif defined(HAVE___CONTROL87_2)
302 int mode;
303 __control87_2(0, 0, &ctl->state, NULL);
304 __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
305 #ifdef HAVE_SSE
306 if((CPUCapFlags&CPU_CAP_SSE))
308 __control87_2(0, 0, NULL, &ctl->sse_state);
309 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
311 #endif
313 #elif defined(HAVE__CONTROLFP)
315 ctl->state = _controlfp(0, 0);
316 (void)_controlfp(_RC_CHOP, _MCW_RC);
317 #endif
320 void RestoreFPUMode(const FPUCtl *ctl)
322 #ifdef HAVE_FENV_H
323 fesetenv(STATIC_CAST(fenv_t, ctl));
324 #if defined(__GNUC__) && defined(HAVE_SSE)
325 if((CPUCapFlags&CPU_CAP_SSE))
326 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
327 #endif
329 #elif defined(HAVE___CONTROL87_2)
331 int mode;
332 __control87_2(ctl->state, _MCW_RC, &mode, NULL);
333 #ifdef HAVE_SSE
334 if((CPUCapFlags&CPU_CAP_SSE))
335 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
336 #endif
338 #elif defined(HAVE__CONTROLFP)
340 _controlfp(ctl->state, _MCW_RC);
341 #endif
345 #ifdef _WIN32
347 static WCHAR *FromUTF8(const char *str)
349 WCHAR *out = NULL;
350 int len;
352 if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
354 out = calloc(sizeof(WCHAR), len);
355 MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
357 return out;
361 void *LoadLib(const char *name)
363 HANDLE hdl = NULL;
364 WCHAR *wname;
366 wname = FromUTF8(name);
367 if(!wname)
368 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
369 else
371 hdl = LoadLibraryW(wname);
372 free(wname);
374 return hdl;
376 void CloseLib(void *handle)
377 { FreeLibrary((HANDLE)handle); }
378 void *GetSymbol(void *handle, const char *name)
380 void *ret;
382 ret = (void*)GetProcAddress((HANDLE)handle, name);
383 if(ret == NULL)
384 ERR("Failed to load %s\n", name);
385 return ret;
388 WCHAR *strdupW(const WCHAR *str)
390 const WCHAR *n;
391 WCHAR *ret;
392 size_t len;
394 n = str;
395 while(*n) n++;
396 len = n - str;
398 ret = calloc(sizeof(WCHAR), len+1);
399 if(ret != NULL)
400 memcpy(ret, str, sizeof(WCHAR)*len);
401 return ret;
404 FILE *al_fopen(const char *fname, const char *mode)
406 WCHAR *wname=NULL, *wmode=NULL;
407 FILE *file = NULL;
409 wname = FromUTF8(fname);
410 wmode = FromUTF8(mode);
411 if(!wname)
412 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
413 else if(!wmode)
414 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
415 else
416 file = _wfopen(wname, wmode);
418 free(wname);
419 free(wmode);
421 return file;
424 #else
426 #ifdef HAVE_DLFCN_H
428 void *LoadLib(const char *name)
430 const char *err;
431 void *handle;
433 dlerror();
434 handle = dlopen(name, RTLD_NOW);
435 if((err=dlerror()) != NULL)
436 handle = NULL;
437 return handle;
439 void CloseLib(void *handle)
440 { dlclose(handle); }
441 void *GetSymbol(void *handle, const char *name)
443 const char *err;
444 void *sym;
446 dlerror();
447 sym = dlsym(handle, name);
448 if((err=dlerror()) != NULL)
450 WARN("Failed to load %s: %s\n", name, err);
451 sym = NULL;
453 return sym;
456 #endif
457 #endif
460 void al_print(const char *type, const char *func, const char *fmt, ...)
462 va_list ap;
464 va_start(ap, fmt);
465 fprintf(LogFile, "AL lib: %s %s: ", type, func);
466 vfprintf(LogFile, fmt, ap);
467 va_end(ap);
469 fflush(LogFile);
472 #ifdef _WIN32
473 static inline int is_slash(int c)
474 { return (c == '\\' || c == '/'); }
476 FILE *OpenDataFile(const char *fname, const char *subdir)
478 static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
479 WCHAR *wname=NULL, *wsubdir=NULL;
480 FILE *f;
481 int i;
483 /* If the path is absolute, open it directly. */
484 if(fname[0] != '\0' && fname[1] == ':' && is_slash(fname[2]))
486 if((f=al_fopen(fname, "rb")) != NULL)
488 TRACE("Opened %s\n", fname);
489 return f;
491 WARN("Could not open %s\n", fname);
492 return NULL;
495 /* If it's relative, try the current directory first before the data directories. */
496 if((f=al_fopen(fname, "rb")) != NULL)
498 TRACE("Opened %s\n", fname);
499 return f;
501 WARN("Could not open %s\n", fname);
503 wname = FromUTF8(fname);
504 wsubdir = FromUTF8(subdir);
505 if(!wname)
506 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
507 else if(!wsubdir)
508 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
509 else for(i = 0;i < 2;i++)
511 WCHAR buffer[PATH_MAX];
512 size_t len;
514 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
515 continue;
517 len = lstrlenW(buffer);
518 if(len > 0 && is_slash(buffer[len-1]))
519 buffer[--len] = '\0';
520 _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
521 len = lstrlenW(buffer);
522 while(len > 0)
524 --len;
525 if(buffer[len] == '/')
526 buffer[len] = '\\';
529 if((f=_wfopen(buffer, L"rb")) != NULL)
531 TRACE("Opened %ls\n", buffer);
532 return f;
534 WARN("Could not open %ls\n", buffer);
536 free(wname);
537 free(wsubdir);
539 return NULL;
541 #else
542 FILE *OpenDataFile(const char *fname, const char *subdir)
544 char buffer[PATH_MAX] = "";
545 const char *str, *next;
546 FILE *f;
548 if(fname[0] == '/')
550 if((f=al_fopen(fname, "rb")) != NULL)
552 TRACE("Opened %s\n", fname);
553 return f;
555 WARN("Could not open %s\n", fname);
556 return NULL;
559 if((f=al_fopen(fname, "rb")) != NULL)
561 TRACE("Opened %s\n", fname);
562 return f;
564 WARN("Could not open %s\n", fname);
566 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
567 snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
568 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
569 snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
570 if(buffer[0])
572 if((f=al_fopen(buffer, "rb")) != NULL)
574 TRACE("Opened %s\n", buffer);
575 return f;
577 WARN("Could not open %s\n", buffer);
580 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
581 str = "/usr/local/share/:/usr/share/";
583 next = str;
584 while((str=next) != NULL && str[0] != '\0')
586 size_t len;
587 next = strchr(str, ':');
589 if(!next)
590 len = strlen(str);
591 else
593 len = next - str;
594 next++;
597 if(len > sizeof(buffer)-1)
598 len = sizeof(buffer)-1;
599 strncpy(buffer, str, len);
600 buffer[len] = '\0';
601 snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
603 if((f=al_fopen(buffer, "rb")) != NULL)
605 TRACE("Opened %s\n", buffer);
606 return f;
608 WARN("Could not open %s\n", buffer);
611 return NULL;
613 #endif
616 void SetRTPriority(void)
618 ALboolean failed = AL_FALSE;
620 #ifdef _WIN32
621 if(RTPrioLevel > 0)
622 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
623 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
624 if(RTPrioLevel > 0)
626 struct sched_param param;
627 /* Use the minimum real-time priority possible for now (on Linux this
628 * should be 1 for SCHED_RR) */
629 param.sched_priority = sched_get_priority_min(SCHED_RR);
630 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
632 #else
633 /* Real-time priority not available */
634 failed = (RTPrioLevel>0);
635 #endif
636 if(failed)
637 ERR("Failed to set priority level for thread\n");
641 ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, ALsizei obj_count, ALboolean exact)
643 vector_ *vecptr = (vector_*)ptr;
644 if(obj_count < 0)
645 return AL_FALSE;
646 if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
648 ALsizei old_size = (*vecptr ? (*vecptr)->Size : 0);
649 void *temp;
651 /* Use the next power-of-2 size if we don't need to allocate the exact
652 * amount. This is preferred when regularly increasing the vector since
653 * it means fewer reallocations. Though it means it also wastes some
654 * memory. */
655 if(exact == AL_FALSE)
657 obj_count = NextPowerOf2((ALuint)obj_count);
658 if(obj_count < 0) return AL_FALSE;
661 /* Need to be explicit with the caller type's base size, because it
662 * could have extra padding before the start of the array (that is,
663 * sizeof(*vector_) may not equal base_size). */
664 temp = realloc(*vecptr, base_size + obj_size*obj_count);
665 if(temp == NULL) return AL_FALSE;
667 *vecptr = temp;
668 (*vecptr)->Capacity = obj_count;
669 (*vecptr)->Size = old_size;
671 return AL_TRUE;
674 ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, ALsizei obj_count)
676 vector_ *vecptr = (vector_*)ptr;
677 if(obj_count < 0)
678 return AL_FALSE;
679 if(*vecptr || obj_count > 0)
681 if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
682 return AL_FALSE;
683 (*vecptr)->Size = obj_count;
685 return AL_TRUE;
688 ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
690 vector_ *vecptr = (vector_*)ptr;
691 if(datstart != datend)
693 ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
694 ((char*)ins_pos - (char*)NULL)) /
695 obj_size;
696 ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
698 assert(numins > 0);
699 if(INT_MAX-VECTOR_SIZE(*vecptr) <= numins ||
700 !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
701 return AL_FALSE;
703 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
704 if(ins_elem < (*vecptr)->Size)
706 memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
707 (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
708 ((*vecptr)->Size-ins_elem)*obj_size);
710 memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
711 datstart, numins*obj_size);
712 (*vecptr)->Size += (ALsizei)numins;
714 return AL_TRUE;
718 extern inline void al_string_deinit(al_string *str);
719 extern inline ALsizei al_string_length(const_al_string str);
720 extern inline ALboolean al_string_empty(const_al_string str);
721 extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
723 void al_string_clear(al_string *str)
725 /* Reserve one more character than the total size of the string. This is to
726 * ensure we have space to add a null terminator in the string data so it
727 * can be used as a C-style string. */
728 VECTOR_RESERVE(*str, 1);
729 VECTOR_RESIZE(*str, 0);
730 *VECTOR_ITER_END(*str) = 0;
733 static inline int al_string_compare(const al_string_char_type *str1, ALsizei str1len,
734 const al_string_char_type *str2, ALsizei str2len)
736 ALsizei complen = mini(str1len, str2len);
737 int ret = memcmp(str1, str2, complen);
738 if(ret == 0)
740 if(str1len > str2len) return 1;
741 if(str1len < str2len) return -1;
743 return ret;
745 int al_string_cmp(const_al_string str1, const_al_string str2)
747 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
748 &VECTOR_FRONT(str2), al_string_length(str2));
750 int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
752 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
753 str2, (ALsizei)strlen(str2));
756 void al_string_copy(al_string *str, const_al_string from)
758 ALsizei len = VECTOR_SIZE(from);
759 VECTOR_RESERVE(*str, len+1);
760 VECTOR_RESIZE(*str, 0);
761 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
762 *VECTOR_ITER_END(*str) = 0;
765 void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
767 size_t len = strlen(from);
768 VECTOR_RESERVE(*str, len+1);
769 VECTOR_RESIZE(*str, 0);
770 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
771 *VECTOR_ITER_END(*str) = 0;
774 void al_string_append_char(al_string *str, const al_string_char_type c)
776 VECTOR_RESERVE(*str, al_string_length(*str)+2);
777 VECTOR_PUSH_BACK(*str, c);
778 *VECTOR_ITER_END(*str) = 0;
781 void al_string_append_cstr(al_string *str, const al_string_char_type *from)
783 size_t len = strlen(from);
784 if(len != 0)
786 VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
787 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
788 *VECTOR_ITER_END(*str) = 0;
792 void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
794 if(to != from)
796 VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
797 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
798 *VECTOR_ITER_END(*str) = 0;
802 #ifdef _WIN32
803 void al_string_copy_wcstr(al_string *str, const wchar_t *from)
805 int len;
806 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
808 VECTOR_RESERVE(*str, len);
809 VECTOR_RESIZE(*str, len-1);
810 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
811 *VECTOR_ITER_END(*str) = 0;
814 #endif