Support capture with mmdevapi
[openal-soft.git] / Alc / helpers.c
blob8811f3c3ea28ea15e9f4a9ddda4a17a46f537c7a
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);
58 DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
60 #ifdef HAVE_MMDEVAPI
61 #include <devpropdef.h>
62 #include <propkeydef.h>
63 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
64 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
65 #endif
66 #endif
67 #endif /* AL_NO_UID_DEFS */
69 #ifdef HAVE_DLFCN_H
70 #include <dlfcn.h>
71 #endif
72 #ifdef HAVE_INTRIN_H
73 #include <intrin.h>
74 #endif
75 #ifdef HAVE_CPUID_H
76 #include <cpuid.h>
77 #endif
78 #ifdef HAVE_SYS_SYSCONF_H
79 #include <sys/sysconf.h>
80 #endif
81 #ifdef HAVE_FLOAT_H
82 #include <float.h>
83 #endif
84 #ifdef HAVE_IEEEFP_H
85 #include <ieeefp.h>
86 #endif
88 #ifdef _WIN32_IE
89 #include <shlobj.h>
90 #endif
92 #include "alMain.h"
93 #include "alu.h"
94 #include "atomic.h"
95 #include "uintmap.h"
96 #include "vector.h"
97 #include "alstring.h"
98 #include "compat.h"
99 #include "threads.h"
102 extern inline ALuint NextPowerOf2(ALuint value);
103 extern inline ALint fastf2i(ALfloat f);
104 extern inline ALuint fastf2u(ALfloat f);
107 ALuint CPUCapFlags = 0;
110 void FillCPUCaps(ALuint capfilter)
112 ALuint caps = 0;
114 /* FIXME: We really should get this for all available CPUs in case different
115 * CPUs have different caps (is that possible on one machine?). */
116 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
117 defined(_M_IX86) || defined(_M_X64))
118 union {
119 unsigned int regs[4];
120 char str[sizeof(unsigned int[4])];
121 } cpuinf[3];
123 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
124 ERR("Failed to get CPUID\n");
125 else
127 unsigned int maxfunc = cpuinf[0].regs[0];
128 unsigned int maxextfunc = 0;
130 if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
131 maxextfunc = cpuinf[0].regs[0];
132 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
134 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
135 if(maxextfunc >= 0x80000004 &&
136 __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
137 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
138 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
139 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
141 if(maxfunc >= 1 &&
142 __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
144 if((cpuinf[0].regs[3]&(1<<25)))
146 caps |= CPU_CAP_SSE;
147 if((cpuinf[0].regs[3]&(1<<26)))
149 caps |= CPU_CAP_SSE2;
150 if((cpuinf[0].regs[2]&(1<<19)))
151 caps |= CPU_CAP_SSE4_1;
156 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
157 defined(_M_IX86) || defined(_M_X64))
158 union {
159 int regs[4];
160 char str[sizeof(int[4])];
161 } cpuinf[3];
163 (__cpuid)(cpuinf[0].regs, 0);
164 if(cpuinf[0].regs[0] == 0)
165 ERR("Failed to get CPUID\n");
166 else
168 unsigned int maxfunc = cpuinf[0].regs[0];
169 unsigned int maxextfunc;
171 (__cpuid)(cpuinf[0].regs, 0x80000000);
172 maxextfunc = cpuinf[0].regs[0];
174 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
176 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
177 if(maxextfunc >= 0x80000004)
179 (__cpuid)(cpuinf[0].regs, 0x80000002);
180 (__cpuid)(cpuinf[1].regs, 0x80000003);
181 (__cpuid)(cpuinf[2].regs, 0x80000004);
182 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
185 if(maxfunc >= 1)
187 (__cpuid)(cpuinf[0].regs, 1);
188 if((cpuinf[0].regs[3]&(1<<25)))
190 caps |= CPU_CAP_SSE;
191 if((cpuinf[0].regs[3]&(1<<26)))
193 caps |= CPU_CAP_SSE2;
194 if((cpuinf[0].regs[2]&(1<<19)))
195 caps |= CPU_CAP_SSE4_1;
200 #else
201 /* Assume support for whatever's supported if we can't check for it */
202 #if defined(HAVE_SSE4_1)
203 #warning "Assuming SSE 4.1 run-time support!"
204 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE4_1;
205 #elif defined(HAVE_SSE2)
206 #warning "Assuming SSE 2 run-time support!"
207 caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
208 #elif defined(HAVE_SSE)
209 #warning "Assuming SSE run-time support!"
210 caps |= CPU_CAP_SSE;
211 #endif
212 #endif
213 #ifdef HAVE_NEON
214 /* Assume Neon support if compiled with it */
215 caps |= CPU_CAP_NEON;
216 #endif
218 TRACE("Extensions:%s%s%s%s%s\n",
219 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
220 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
221 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
222 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
223 ((!capfilter) ? " -none-" : "")
225 CPUCapFlags = caps & capfilter;
229 void *al_malloc(size_t alignment, size_t size)
231 #if defined(HAVE_ALIGNED_ALLOC)
232 size = (size+(alignment-1))&~(alignment-1);
233 return aligned_alloc(alignment, size);
234 #elif defined(HAVE_POSIX_MEMALIGN)
235 void *ret;
236 if(posix_memalign(&ret, alignment, size) == 0)
237 return ret;
238 return NULL;
239 #elif defined(HAVE__ALIGNED_MALLOC)
240 return _aligned_malloc(size, alignment);
241 #else
242 char *ret = malloc(size+alignment);
243 if(ret != NULL)
245 *(ret++) = 0x00;
246 while(((ptrdiff_t)ret&(alignment-1)) != 0)
247 *(ret++) = 0x55;
249 return ret;
250 #endif
253 void *al_calloc(size_t alignment, size_t size)
255 void *ret = al_malloc(alignment, size);
256 if(ret) memset(ret, 0, size);
257 return ret;
260 void al_free(void *ptr)
262 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
263 free(ptr);
264 #elif defined(HAVE__ALIGNED_MALLOC)
265 _aligned_free(ptr);
266 #else
267 if(ptr != NULL)
269 char *finder = ptr;
270 do {
271 --finder;
272 } while(*finder == 0x55);
273 free(finder);
275 #endif
279 void SetMixerFPUMode(FPUCtl *ctl)
281 #ifdef HAVE_FENV_H
282 fegetenv(STATIC_CAST(fenv_t, ctl));
283 #if defined(__GNUC__) && defined(HAVE_SSE)
284 if((CPUCapFlags&CPU_CAP_SSE))
285 __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
286 #endif
288 #ifdef FE_TOWARDZERO
289 fesetround(FE_TOWARDZERO);
290 #endif
291 #if defined(__GNUC__) && defined(HAVE_SSE)
292 if((CPUCapFlags&CPU_CAP_SSE))
294 int sseState = ctl->sse_state;
295 sseState |= 0x6000; /* set round-to-zero */
296 sseState |= 0x8000; /* set flush-to-zero */
297 if((CPUCapFlags&CPU_CAP_SSE2))
298 sseState |= 0x0040; /* set denormals-are-zero */
299 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
301 #endif
303 #elif defined(HAVE___CONTROL87_2)
305 int mode;
306 __control87_2(0, 0, &ctl->state, NULL);
307 __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
308 #ifdef HAVE_SSE
309 if((CPUCapFlags&CPU_CAP_SSE))
311 __control87_2(0, 0, NULL, &ctl->sse_state);
312 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
314 #endif
316 #elif defined(HAVE__CONTROLFP)
318 ctl->state = _controlfp(0, 0);
319 (void)_controlfp(_RC_CHOP, _MCW_RC);
320 #endif
323 void RestoreFPUMode(const FPUCtl *ctl)
325 #ifdef HAVE_FENV_H
326 fesetenv(STATIC_CAST(fenv_t, ctl));
327 #if defined(__GNUC__) && defined(HAVE_SSE)
328 if((CPUCapFlags&CPU_CAP_SSE))
329 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
330 #endif
332 #elif defined(HAVE___CONTROL87_2)
334 int mode;
335 __control87_2(ctl->state, _MCW_RC, &mode, NULL);
336 #ifdef HAVE_SSE
337 if((CPUCapFlags&CPU_CAP_SSE))
338 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
339 #endif
341 #elif defined(HAVE__CONTROLFP)
343 _controlfp(ctl->state, _MCW_RC);
344 #endif
348 #ifdef _WIN32
350 static WCHAR *FromUTF8(const char *str)
352 WCHAR *out = NULL;
353 int len;
355 if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
357 out = calloc(sizeof(WCHAR), len);
358 MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
360 return out;
364 void *LoadLib(const char *name)
366 HANDLE hdl = NULL;
367 WCHAR *wname;
369 wname = FromUTF8(name);
370 if(!wname)
371 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
372 else
374 hdl = LoadLibraryW(wname);
375 free(wname);
377 return hdl;
379 void CloseLib(void *handle)
380 { FreeLibrary((HANDLE)handle); }
381 void *GetSymbol(void *handle, const char *name)
383 void *ret;
385 ret = (void*)GetProcAddress((HANDLE)handle, name);
386 if(ret == NULL)
387 ERR("Failed to load %s\n", name);
388 return ret;
391 WCHAR *strdupW(const WCHAR *str)
393 const WCHAR *n;
394 WCHAR *ret;
395 size_t len;
397 n = str;
398 while(*n) n++;
399 len = n - str;
401 ret = calloc(sizeof(WCHAR), len+1);
402 if(ret != NULL)
403 memcpy(ret, str, sizeof(WCHAR)*len);
404 return ret;
407 FILE *al_fopen(const char *fname, const char *mode)
409 WCHAR *wname=NULL, *wmode=NULL;
410 FILE *file = NULL;
412 wname = FromUTF8(fname);
413 wmode = FromUTF8(mode);
414 if(!wname)
415 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
416 else if(!wmode)
417 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
418 else
419 file = _wfopen(wname, wmode);
421 free(wname);
422 free(wmode);
424 return file;
428 void al_print(const char *type, const char *func, const char *fmt, ...)
430 char str[1024];
431 WCHAR *wstr;
432 va_list ap;
434 va_start(ap, fmt);
435 vsnprintf(str, sizeof(str), fmt, ap);
436 va_end(ap);
438 str[sizeof(str)-1] = 0;
439 wstr = FromUTF8(str);
440 if(!wstr)
441 fputs(str, LogFile);
442 else
444 fwprintf(LogFile, L"AL lib: %s %s: %ls", type, func, wstr);
445 free(wstr);
446 wstr = NULL;
448 fflush(LogFile);
452 static inline int is_slash(int c)
453 { return (c == '\\' || c == '/'); }
455 FILE *OpenDataFile(const char *fname, const char *subdir)
457 static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
458 WCHAR *wname=NULL, *wsubdir=NULL;
459 FILE *f;
460 int i;
462 /* If the path is absolute, open it directly. */
463 if(fname[0] != '\0' && fname[1] == ':' && is_slash(fname[2]))
465 if((f=al_fopen(fname, "rb")) != NULL)
467 TRACE("Opened %s\n", fname);
468 return f;
470 WARN("Could not open %s\n", fname);
471 return NULL;
474 /* If it's relative, try the current directory first before the data directories. */
475 if((f=al_fopen(fname, "rb")) != NULL)
477 TRACE("Opened %s\n", fname);
478 return f;
481 wname = FromUTF8(fname);
482 wsubdir = FromUTF8(subdir);
483 if(!wname)
484 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
485 else if(!wsubdir)
486 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
487 else for(i = 0;i < 2;i++)
489 WCHAR buffer[PATH_MAX];
490 size_t len;
492 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
493 continue;
495 len = lstrlenW(buffer);
496 if(len > 0 && is_slash(buffer[len-1]))
497 buffer[--len] = '\0';
498 _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
499 len = lstrlenW(buffer);
500 while(len > 0)
502 --len;
503 if(buffer[len] == '/')
504 buffer[len] = '\\';
507 if((f=_wfopen(buffer, L"rb")) != NULL)
509 TRACE("Opened %ls\n", buffer);
510 free(wname);
511 free(wsubdir);
512 return f;
515 WARN("Could not open %ls\\%ls\n", wsubdir, wname);
516 free(wname);
517 free(wsubdir);
519 return NULL;
522 #else
524 #ifdef HAVE_DLFCN_H
526 void *LoadLib(const char *name)
528 const char *err;
529 void *handle;
531 dlerror();
532 handle = dlopen(name, RTLD_NOW);
533 if((err=dlerror()) != NULL)
534 handle = NULL;
535 return handle;
537 void CloseLib(void *handle)
538 { dlclose(handle); }
539 void *GetSymbol(void *handle, const char *name)
541 const char *err;
542 void *sym;
544 dlerror();
545 sym = dlsym(handle, name);
546 if((err=dlerror()) != NULL)
548 WARN("Failed to load %s: %s\n", name, err);
549 sym = NULL;
551 return sym;
554 #endif /* HAVE_DLFCN_H */
556 void al_print(const char *type, const char *func, const char *fmt, ...)
558 va_list ap;
560 va_start(ap, fmt);
561 fprintf(LogFile, "AL lib: %s %s: ", type, func);
562 vfprintf(LogFile, fmt, ap);
563 va_end(ap);
565 fflush(LogFile);
569 FILE *OpenDataFile(const char *fname, const char *subdir)
571 char buffer[PATH_MAX] = "";
572 const char *str, *next;
573 FILE *f;
575 if(fname[0] == '/')
577 if((f=al_fopen(fname, "rb")) != NULL)
579 TRACE("Opened %s\n", fname);
580 return f;
582 WARN("Could not open %s\n", fname);
583 return NULL;
586 if((f=al_fopen(fname, "rb")) != NULL)
588 TRACE("Opened %s\n", fname);
589 return f;
592 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
593 snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
594 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
595 snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
596 if(buffer[0])
598 if((f=al_fopen(buffer, "rb")) != NULL)
600 TRACE("Opened %s\n", buffer);
601 return f;
605 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
606 str = "/usr/local/share/:/usr/share/";
608 next = str;
609 while((str=next) != NULL && str[0] != '\0')
611 size_t len;
612 next = strchr(str, ':');
614 if(!next)
615 len = strlen(str);
616 else
618 len = next - str;
619 next++;
622 if(len > sizeof(buffer)-1)
623 len = sizeof(buffer)-1;
624 strncpy(buffer, str, len);
625 buffer[len] = '\0';
626 snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
628 if((f=al_fopen(buffer, "rb")) != NULL)
630 TRACE("Opened %s\n", buffer);
631 return f;
634 WARN("Could not open %s/%s\n", subdir, fname);
636 return NULL;
639 #endif
642 void SetRTPriority(void)
644 ALboolean failed = AL_FALSE;
646 #ifdef _WIN32
647 if(RTPrioLevel > 0)
648 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
649 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
650 if(RTPrioLevel > 0)
652 struct sched_param param;
653 /* Use the minimum real-time priority possible for now (on Linux this
654 * should be 1 for SCHED_RR) */
655 param.sched_priority = sched_get_priority_min(SCHED_RR);
656 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
658 #else
659 /* Real-time priority not available */
660 failed = (RTPrioLevel>0);
661 #endif
662 if(failed)
663 ERR("Failed to set priority level for thread\n");
667 ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact)
669 vector_ *vecptr = (vector_*)ptr;
670 if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
672 size_t old_size = (*vecptr ? (*vecptr)->Size : 0);
673 void *temp;
675 /* Use the next power-of-2 size if we don't need to allocate the exact
676 * amount. This is preferred when regularly increasing the vector since
677 * it means fewer reallocations. Though it means it also wastes some
678 * memory. */
679 if(exact == AL_FALSE && obj_count < INT_MAX)
680 obj_count = NextPowerOf2((ALuint)obj_count);
682 /* Need to be explicit with the caller type's base size, because it
683 * could have extra padding before the start of the array (that is,
684 * sizeof(*vector_) may not equal base_size). */
685 temp = realloc(*vecptr, base_size + obj_size*obj_count);
686 if(temp == NULL) return AL_FALSE;
688 *vecptr = temp;
689 (*vecptr)->Capacity = obj_count;
690 (*vecptr)->Size = old_size;
692 return AL_TRUE;
695 ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count)
697 vector_ *vecptr = (vector_*)ptr;
698 if(*vecptr || obj_count > 0)
700 if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
701 return AL_FALSE;
702 (*vecptr)->Size = obj_count;
704 return AL_TRUE;
707 ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
709 vector_ *vecptr = (vector_*)ptr;
710 if(datstart != datend)
712 ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
713 ((char*)ins_pos - (char*)NULL)) /
714 obj_size;
715 ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
717 assert(numins > 0);
718 if((size_t)numins + VECTOR_SIZE(*vecptr) < (size_t)numins ||
719 !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
720 return AL_FALSE;
722 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
723 if((size_t)ins_elem < (*vecptr)->Size)
725 memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
726 (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
727 ((*vecptr)->Size-ins_elem)*obj_size);
729 memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
730 datstart, numins*obj_size);
731 (*vecptr)->Size += numins;
733 return AL_TRUE;
737 extern inline void al_string_deinit(al_string *str);
738 extern inline size_t al_string_length(const_al_string str);
739 extern inline ALboolean al_string_empty(const_al_string str);
740 extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
742 void al_string_clear(al_string *str)
744 /* Reserve one more character than the total size of the string. This is to
745 * ensure we have space to add a null terminator in the string data so it
746 * can be used as a C-style string. */
747 VECTOR_RESERVE(*str, 1);
748 VECTOR_RESIZE(*str, 0);
749 *VECTOR_ITER_END(*str) = 0;
752 static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
753 const al_string_char_type *str2, size_t str2len)
755 size_t complen = (str1len < str2len) ? str1len : str2len;
756 int ret = memcmp(str1, str2, complen);
757 if(ret == 0)
759 if(str1len > str2len) return 1;
760 if(str1len < str2len) return -1;
762 return ret;
764 int al_string_cmp(const_al_string str1, const_al_string str2)
766 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
767 &VECTOR_FRONT(str2), al_string_length(str2));
769 int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
771 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
772 str2, strlen(str2));
775 void al_string_copy(al_string *str, const_al_string from)
777 size_t len = al_string_length(from);
778 VECTOR_RESERVE(*str, len+1);
779 VECTOR_RESIZE(*str, 0);
780 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
781 *VECTOR_ITER_END(*str) = 0;
784 void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
786 size_t len = strlen(from);
787 VECTOR_RESERVE(*str, len+1);
788 VECTOR_RESIZE(*str, 0);
789 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
790 *VECTOR_ITER_END(*str) = 0;
793 void al_string_append_char(al_string *str, const al_string_char_type c)
795 VECTOR_RESERVE(*str, al_string_length(*str)+2);
796 VECTOR_PUSH_BACK(*str, c);
797 *VECTOR_ITER_END(*str) = 0;
800 void al_string_append_cstr(al_string *str, const al_string_char_type *from)
802 size_t len = strlen(from);
803 if(len != 0)
805 VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
806 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
807 *VECTOR_ITER_END(*str) = 0;
811 void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
813 if(to != from)
815 VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
816 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
817 *VECTOR_ITER_END(*str) = 0;
821 #ifdef _WIN32
822 void al_string_copy_wcstr(al_string *str, const wchar_t *from)
824 int len;
825 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
827 VECTOR_RESERVE(*str, len);
828 VECTOR_RESIZE(*str, len-1);
829 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
830 *VECTOR_ITER_END(*str) = 0;
833 #endif