Try the __cpuid intrinsic if GCC's __get_cpuid isn't available
[openal-soft.git] / Alc / helpers.c
blob90419c174156d7c856c3d614041bed512847cd74
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 #endif
198 #ifdef HAVE_NEON
199 /* Assume Neon support if compiled with it */
200 caps |= CPU_CAP_NEON;
201 #endif
203 TRACE("Extensions:%s%s%s%s%s\n",
204 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
205 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
206 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
207 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
208 ((!capfilter) ? " -none-" : "")
210 CPUCapFlags = caps & capfilter;
214 void *al_malloc(size_t alignment, size_t size)
216 #if defined(HAVE_ALIGNED_ALLOC)
217 size = (size+(alignment-1))&~(alignment-1);
218 return aligned_alloc(alignment, size);
219 #elif defined(HAVE_POSIX_MEMALIGN)
220 void *ret;
221 if(posix_memalign(&ret, alignment, size) == 0)
222 return ret;
223 return NULL;
224 #elif defined(HAVE__ALIGNED_MALLOC)
225 return _aligned_malloc(size, alignment);
226 #else
227 char *ret = malloc(size+alignment);
228 if(ret != NULL)
230 *(ret++) = 0x00;
231 while(((ALintptrEXT)ret&(alignment-1)) != 0)
232 *(ret++) = 0x55;
234 return ret;
235 #endif
238 void *al_calloc(size_t alignment, size_t size)
240 void *ret = al_malloc(alignment, size);
241 if(ret) memset(ret, 0, size);
242 return ret;
245 void al_free(void *ptr)
247 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
248 free(ptr);
249 #elif defined(HAVE__ALIGNED_MALLOC)
250 _aligned_free(ptr);
251 #else
252 if(ptr != NULL)
254 char *finder = ptr;
255 do {
256 --finder;
257 } while(*finder == 0x55);
258 free(finder);
260 #endif
264 void SetMixerFPUMode(FPUCtl *ctl)
266 #ifdef HAVE_FENV_H
267 fegetenv(STATIC_CAST(fenv_t, ctl));
268 #if defined(__GNUC__) && defined(HAVE_SSE)
269 if((CPUCapFlags&CPU_CAP_SSE))
270 __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
271 #endif
273 #ifdef FE_TOWARDZERO
274 fesetround(FE_TOWARDZERO);
275 #endif
276 #if defined(__GNUC__) && defined(HAVE_SSE)
277 if((CPUCapFlags&CPU_CAP_SSE))
279 int sseState = ctl->sse_state;
280 sseState |= 0x6000; /* set round-to-zero */
281 sseState |= 0x8000; /* set flush-to-zero */
282 if((CPUCapFlags&CPU_CAP_SSE2))
283 sseState |= 0x0040; /* set denormals-are-zero */
284 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
286 #endif
288 #elif defined(HAVE___CONTROL87_2)
290 int mode;
291 __control87_2(0, 0, &ctl->state, NULL);
292 __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
293 #ifdef HAVE_SSE
294 if((CPUCapFlags&CPU_CAP_SSE))
296 __control87_2(0, 0, NULL, &ctl->sse_state);
297 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
299 #endif
301 #elif defined(HAVE__CONTROLFP)
303 ctl->state = _controlfp(0, 0);
304 (void)_controlfp(_RC_CHOP, _MCW_RC);
305 #endif
308 void RestoreFPUMode(const FPUCtl *ctl)
310 #ifdef HAVE_FENV_H
311 fesetenv(STATIC_CAST(fenv_t, ctl));
312 #if defined(__GNUC__) && defined(HAVE_SSE)
313 if((CPUCapFlags&CPU_CAP_SSE))
314 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
315 #endif
317 #elif defined(HAVE___CONTROL87_2)
319 int mode;
320 __control87_2(ctl->state, _MCW_RC, &mode, NULL);
321 #ifdef HAVE_SSE
322 if((CPUCapFlags&CPU_CAP_SSE))
323 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
324 #endif
326 #elif defined(HAVE__CONTROLFP)
328 _controlfp(ctl->state, _MCW_RC);
329 #endif
333 #ifdef _WIN32
335 static WCHAR *FromUTF8(const char *str)
337 WCHAR *out = NULL;
338 int len;
340 if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
342 out = calloc(sizeof(WCHAR), len);
343 MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
345 return out;
349 void *LoadLib(const char *name)
351 HANDLE hdl = NULL;
352 WCHAR *wname;
354 wname = FromUTF8(name);
355 if(!wname)
356 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
357 else
359 hdl = LoadLibraryW(wname);
360 free(wname);
362 return hdl;
364 void CloseLib(void *handle)
365 { FreeLibrary((HANDLE)handle); }
366 void *GetSymbol(void *handle, const char *name)
368 void *ret;
370 ret = (void*)GetProcAddress((HANDLE)handle, name);
371 if(ret == NULL)
372 ERR("Failed to load %s\n", name);
373 return ret;
376 WCHAR *strdupW(const WCHAR *str)
378 const WCHAR *n;
379 WCHAR *ret;
380 size_t len;
382 n = str;
383 while(*n) n++;
384 len = n - str;
386 ret = calloc(sizeof(WCHAR), len+1);
387 if(ret != NULL)
388 memcpy(ret, str, sizeof(WCHAR)*len);
389 return ret;
392 FILE *al_fopen(const char *fname, const char *mode)
394 WCHAR *wname=NULL, *wmode=NULL;
395 FILE *file = NULL;
397 wname = FromUTF8(fname);
398 wmode = FromUTF8(mode);
399 if(!wname)
400 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
401 else if(!wmode)
402 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
403 else
404 file = _wfopen(wname, wmode);
406 free(wname);
407 free(wmode);
409 return file;
412 #else
414 #ifdef HAVE_DLFCN_H
416 void *LoadLib(const char *name)
418 const char *err;
419 void *handle;
421 dlerror();
422 handle = dlopen(name, RTLD_NOW);
423 if((err=dlerror()) != NULL)
424 handle = NULL;
425 return handle;
427 void CloseLib(void *handle)
428 { dlclose(handle); }
429 void *GetSymbol(void *handle, const char *name)
431 const char *err;
432 void *sym;
434 dlerror();
435 sym = dlsym(handle, name);
436 if((err=dlerror()) != NULL)
438 WARN("Failed to load %s: %s\n", name, err);
439 sym = NULL;
441 return sym;
444 #endif
445 #endif
448 void al_print(const char *type, const char *func, const char *fmt, ...)
450 va_list ap;
452 va_start(ap, fmt);
453 fprintf(LogFile, "AL lib: %s %s: ", type, func);
454 vfprintf(LogFile, fmt, ap);
455 va_end(ap);
457 fflush(LogFile);
460 #ifdef _WIN32
461 static inline int is_slash(int c)
462 { return (c == '\\' || c == '/'); }
464 FILE *OpenDataFile(const char *fname, const char *subdir)
466 static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
467 WCHAR *wname=NULL, *wsubdir=NULL;
468 FILE *f;
469 int i;
471 /* If the path is absolute, open it directly. */
472 if(fname[0] != '\0' && fname[1] == ':' && is_slash(fname[2]))
474 if((f=al_fopen(fname, "rb")) != NULL)
476 TRACE("Opened %s\n", fname);
477 return f;
479 WARN("Could not open %s\n", fname);
480 return NULL;
483 /* If it's relative, try the current directory first before the data directories. */
484 if((f=al_fopen(fname, "rb")) != NULL)
486 TRACE("Opened %s\n", fname);
487 return f;
489 WARN("Could not open %s\n", fname);
491 wname = FromUTF8(fname);
492 wsubdir = FromUTF8(subdir);
493 if(!wname)
494 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
495 else if(!wsubdir)
496 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
497 else for(i = 0;i < 2;i++)
499 WCHAR buffer[PATH_MAX];
500 size_t len;
502 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
503 continue;
505 len = lstrlenW(buffer);
506 if(len > 0 && is_slash(buffer[len-1]))
507 buffer[--len] = '\0';
508 _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
509 len = lstrlenW(buffer);
510 while(len > 0)
512 --len;
513 if(buffer[len] == '/')
514 buffer[len] = '\\';
517 if((f=_wfopen(buffer, L"rb")) != NULL)
519 TRACE("Opened %ls\n", buffer);
520 return f;
522 WARN("Could not open %ls\n", buffer);
524 free(wname);
525 free(wsubdir);
527 return NULL;
529 #else
530 FILE *OpenDataFile(const char *fname, const char *subdir)
532 char buffer[PATH_MAX] = "";
533 const char *str, *next;
534 FILE *f;
536 if(fname[0] == '/')
538 if((f=al_fopen(fname, "rb")) != NULL)
540 TRACE("Opened %s\n", fname);
541 return f;
543 WARN("Could not open %s\n", fname);
544 return NULL;
547 if((f=al_fopen(fname, "rb")) != NULL)
549 TRACE("Opened %s\n", fname);
550 return f;
552 WARN("Could not open %s\n", fname);
554 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
555 snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
556 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
557 snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
558 if(buffer[0])
560 if((f=al_fopen(buffer, "rb")) != NULL)
562 TRACE("Opened %s\n", buffer);
563 return f;
565 WARN("Could not open %s\n", buffer);
568 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
569 str = "/usr/local/share/:/usr/share/";
571 next = str;
572 while((str=next) != NULL && str[0] != '\0')
574 size_t len;
575 next = strchr(str, ':');
577 if(!next)
578 len = strlen(str);
579 else
581 len = next - str;
582 next++;
585 if(len > sizeof(buffer)-1)
586 len = sizeof(buffer)-1;
587 strncpy(buffer, str, len);
588 buffer[len] = '\0';
589 snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
591 if((f=al_fopen(buffer, "rb")) != NULL)
593 TRACE("Opened %s\n", buffer);
594 return f;
596 WARN("Could not open %s\n", buffer);
599 return NULL;
601 #endif
604 void SetRTPriority(void)
606 ALboolean failed = AL_FALSE;
608 #ifdef _WIN32
609 if(RTPrioLevel > 0)
610 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
611 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
612 if(RTPrioLevel > 0)
614 struct sched_param param;
615 /* Use the minimum real-time priority possible for now (on Linux this
616 * should be 1 for SCHED_RR) */
617 param.sched_priority = sched_get_priority_min(SCHED_RR);
618 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
620 #else
621 /* Real-time priority not available */
622 failed = (RTPrioLevel>0);
623 #endif
624 if(failed)
625 ERR("Failed to set priority level for thread\n");
629 ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, ALsizei obj_count, ALboolean exact)
631 vector_ *vecptr = (vector_*)ptr;
632 if(obj_count < 0)
633 return AL_FALSE;
634 if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
636 ALsizei old_size = (*vecptr ? (*vecptr)->Size : 0);
637 void *temp;
639 /* Use the next power-of-2 size if we don't need to allocate the exact
640 * amount. This is preferred when regularly increasing the vector since
641 * it means fewer reallocations. Though it means it also wastes some
642 * memory. */
643 if(exact == AL_FALSE)
645 obj_count = NextPowerOf2((ALuint)obj_count);
646 if(obj_count < 0) return AL_FALSE;
649 /* Need to be explicit with the caller type's base size, because it
650 * could have extra padding before the start of the array (that is,
651 * sizeof(*vector_) may not equal base_size). */
652 temp = realloc(*vecptr, base_size + obj_size*obj_count);
653 if(temp == NULL) return AL_FALSE;
655 *vecptr = temp;
656 (*vecptr)->Capacity = obj_count;
657 (*vecptr)->Size = old_size;
659 return AL_TRUE;
662 ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, ALsizei obj_count)
664 vector_ *vecptr = (vector_*)ptr;
665 if(obj_count < 0)
666 return AL_FALSE;
667 if(*vecptr || obj_count > 0)
669 if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
670 return AL_FALSE;
671 (*vecptr)->Size = obj_count;
673 return AL_TRUE;
676 ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
678 vector_ *vecptr = (vector_*)ptr;
679 if(datstart != datend)
681 ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
682 ((char*)ins_pos - (char*)NULL)) /
683 obj_size;
684 ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
686 assert(numins > 0);
687 if(INT_MAX-VECTOR_SIZE(*vecptr) <= numins ||
688 !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
689 return AL_FALSE;
691 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
692 if(ins_elem < (*vecptr)->Size)
694 memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
695 (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
696 ((*vecptr)->Size-ins_elem)*obj_size);
698 memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
699 datstart, numins*obj_size);
700 (*vecptr)->Size += (ALsizei)numins;
702 return AL_TRUE;
706 extern inline void al_string_deinit(al_string *str);
707 extern inline ALsizei al_string_length(const_al_string str);
708 extern inline ALboolean al_string_empty(const_al_string str);
709 extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
711 void al_string_clear(al_string *str)
713 /* Reserve one more character than the total size of the string. This is to
714 * ensure we have space to add a null terminator in the string data so it
715 * can be used as a C-style string. */
716 VECTOR_RESERVE(*str, 1);
717 VECTOR_RESIZE(*str, 0);
718 *VECTOR_ITER_END(*str) = 0;
721 static inline int al_string_compare(const al_string_char_type *str1, ALsizei str1len,
722 const al_string_char_type *str2, ALsizei str2len)
724 ALsizei complen = mini(str1len, str2len);
725 int ret = memcmp(str1, str2, complen);
726 if(ret == 0)
728 if(str1len > str2len) return 1;
729 if(str1len < str2len) return -1;
731 return ret;
733 int al_string_cmp(const_al_string str1, const_al_string str2)
735 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
736 &VECTOR_FRONT(str2), al_string_length(str2));
738 int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
740 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
741 str2, (ALsizei)strlen(str2));
744 void al_string_copy(al_string *str, const_al_string from)
746 ALsizei len = VECTOR_SIZE(from);
747 VECTOR_RESERVE(*str, len+1);
748 VECTOR_RESIZE(*str, 0);
749 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
750 *VECTOR_ITER_END(*str) = 0;
753 void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
755 size_t len = strlen(from);
756 VECTOR_RESERVE(*str, len+1);
757 VECTOR_RESIZE(*str, 0);
758 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
759 *VECTOR_ITER_END(*str) = 0;
762 void al_string_append_char(al_string *str, const al_string_char_type c)
764 VECTOR_RESERVE(*str, al_string_length(*str)+2);
765 VECTOR_PUSH_BACK(*str, c);
766 *VECTOR_ITER_END(*str) = 0;
769 void al_string_append_cstr(al_string *str, const al_string_char_type *from)
771 size_t len = strlen(from);
772 if(len != 0)
774 VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
775 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
776 *VECTOR_ITER_END(*str) = 0;
780 void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
782 if(to != from)
784 VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
785 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
786 *VECTOR_ITER_END(*str) = 0;
790 #ifdef _WIN32
791 void al_string_copy_wcstr(al_string *str, const wchar_t *from)
793 int len;
794 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
796 VECTOR_RESERVE(*str, len);
797 VECTOR_RESIZE(*str, len-1);
798 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
799 *VECTOR_ITER_END(*str) = 0;
802 #endif