Rework HRTF decision logic
[openal-soft.git] / Alc / helpers.c
blob5dded69914511cdb02424e3735278d0bc3b75f14
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;
502 wname = FromUTF8(fname);
503 wsubdir = FromUTF8(subdir);
504 if(!wname)
505 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
506 else if(!wsubdir)
507 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
508 else for(i = 0;i < 2;i++)
510 WCHAR buffer[PATH_MAX];
511 size_t len;
513 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
514 continue;
516 len = lstrlenW(buffer);
517 if(len > 0 && is_slash(buffer[len-1]))
518 buffer[--len] = '\0';
519 _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
520 len = lstrlenW(buffer);
521 while(len > 0)
523 --len;
524 if(buffer[len] == '/')
525 buffer[len] = '\\';
528 if((f=_wfopen(buffer, L"rb")) != NULL)
530 TRACE("Opened %ls\n", buffer);
531 return f;
534 WARN("Could not open %ls\\%ls\n", wsubdir, wname);
535 free(wname);
536 free(wsubdir);
538 return NULL;
540 #else
541 FILE *OpenDataFile(const char *fname, const char *subdir)
543 char buffer[PATH_MAX] = "";
544 const char *str, *next;
545 FILE *f;
547 if(fname[0] == '/')
549 if((f=al_fopen(fname, "rb")) != NULL)
551 TRACE("Opened %s\n", fname);
552 return f;
554 WARN("Could not open %s\n", fname);
555 return NULL;
558 if((f=al_fopen(fname, "rb")) != NULL)
560 TRACE("Opened %s\n", fname);
561 return f;
564 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
565 snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
566 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
567 snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
568 if(buffer[0])
570 if((f=al_fopen(buffer, "rb")) != NULL)
572 TRACE("Opened %s\n", buffer);
573 return f;
577 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
578 str = "/usr/local/share/:/usr/share/";
580 next = str;
581 while((str=next) != NULL && str[0] != '\0')
583 size_t len;
584 next = strchr(str, ':');
586 if(!next)
587 len = strlen(str);
588 else
590 len = next - str;
591 next++;
594 if(len > sizeof(buffer)-1)
595 len = sizeof(buffer)-1;
596 strncpy(buffer, str, len);
597 buffer[len] = '\0';
598 snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
600 if((f=al_fopen(buffer, "rb")) != NULL)
602 TRACE("Opened %s\n", buffer);
603 return f;
606 WARN("Could not open %s/%s\n", subdir, fname);
608 return NULL;
610 #endif
613 void SetRTPriority(void)
615 ALboolean failed = AL_FALSE;
617 #ifdef _WIN32
618 if(RTPrioLevel > 0)
619 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
620 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
621 if(RTPrioLevel > 0)
623 struct sched_param param;
624 /* Use the minimum real-time priority possible for now (on Linux this
625 * should be 1 for SCHED_RR) */
626 param.sched_priority = sched_get_priority_min(SCHED_RR);
627 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
629 #else
630 /* Real-time priority not available */
631 failed = (RTPrioLevel>0);
632 #endif
633 if(failed)
634 ERR("Failed to set priority level for thread\n");
638 ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact)
640 vector_ *vecptr = (vector_*)ptr;
641 if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
643 size_t old_size = (*vecptr ? (*vecptr)->Size : 0);
644 void *temp;
646 /* Use the next power-of-2 size if we don't need to allocate the exact
647 * amount. This is preferred when regularly increasing the vector since
648 * it means fewer reallocations. Though it means it also wastes some
649 * memory. */
650 if(exact == AL_FALSE && obj_count < INT_MAX)
651 obj_count = NextPowerOf2((ALuint)obj_count);
653 /* Need to be explicit with the caller type's base size, because it
654 * could have extra padding before the start of the array (that is,
655 * sizeof(*vector_) may not equal base_size). */
656 temp = realloc(*vecptr, base_size + obj_size*obj_count);
657 if(temp == NULL) return AL_FALSE;
659 *vecptr = temp;
660 (*vecptr)->Capacity = obj_count;
661 (*vecptr)->Size = old_size;
663 return AL_TRUE;
666 ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count)
668 vector_ *vecptr = (vector_*)ptr;
669 if(*vecptr || obj_count > 0)
671 if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
672 return AL_FALSE;
673 (*vecptr)->Size = obj_count;
675 return AL_TRUE;
678 ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
680 vector_ *vecptr = (vector_*)ptr;
681 if(datstart != datend)
683 ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
684 ((char*)ins_pos - (char*)NULL)) /
685 obj_size;
686 ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
688 assert(numins > 0);
689 if((size_t)numins + VECTOR_SIZE(*vecptr) < (size_t)numins ||
690 !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
691 return AL_FALSE;
693 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
694 if((size_t)ins_elem < (*vecptr)->Size)
696 memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
697 (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
698 ((*vecptr)->Size-ins_elem)*obj_size);
700 memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
701 datstart, numins*obj_size);
702 (*vecptr)->Size += numins;
704 return AL_TRUE;
708 extern inline void al_string_deinit(al_string *str);
709 extern inline size_t al_string_length(const_al_string str);
710 extern inline ALboolean al_string_empty(const_al_string str);
711 extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
713 void al_string_clear(al_string *str)
715 /* Reserve one more character than the total size of the string. This is to
716 * ensure we have space to add a null terminator in the string data so it
717 * can be used as a C-style string. */
718 VECTOR_RESERVE(*str, 1);
719 VECTOR_RESIZE(*str, 0);
720 *VECTOR_ITER_END(*str) = 0;
723 static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
724 const al_string_char_type *str2, size_t str2len)
726 size_t complen = (str1len < str2len) ? str1len : str2len;
727 int ret = memcmp(str1, str2, complen);
728 if(ret == 0)
730 if(str1len > str2len) return 1;
731 if(str1len < str2len) return -1;
733 return ret;
735 int al_string_cmp(const_al_string str1, const_al_string str2)
737 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
738 &VECTOR_FRONT(str2), al_string_length(str2));
740 int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
742 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
743 str2, strlen(str2));
746 void al_string_copy(al_string *str, const_al_string from)
748 size_t len = al_string_length(from);
749 VECTOR_RESERVE(*str, len+1);
750 VECTOR_RESIZE(*str, 0);
751 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
752 *VECTOR_ITER_END(*str) = 0;
755 void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
757 size_t len = strlen(from);
758 VECTOR_RESERVE(*str, len+1);
759 VECTOR_RESIZE(*str, 0);
760 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
761 *VECTOR_ITER_END(*str) = 0;
764 void al_string_append_char(al_string *str, const al_string_char_type c)
766 VECTOR_RESERVE(*str, al_string_length(*str)+2);
767 VECTOR_PUSH_BACK(*str, c);
768 *VECTOR_ITER_END(*str) = 0;
771 void al_string_append_cstr(al_string *str, const al_string_char_type *from)
773 size_t len = strlen(from);
774 if(len != 0)
776 VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
777 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
778 *VECTOR_ITER_END(*str) = 0;
782 void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
784 if(to != from)
786 VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
787 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
788 *VECTOR_ITER_END(*str) = 0;
792 #ifdef _WIN32
793 void al_string_copy_wcstr(al_string *str, const wchar_t *from)
795 int len;
796 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
798 VECTOR_RESERVE(*str, len);
799 VECTOR_RESIZE(*str, len-1);
800 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
801 *VECTOR_ITER_END(*str) = 0;
804 #endif