Avoid duplicate calculations
[openal-soft.git] / Alc / helpers.c
blob0d074087d67f59b25652cb3b04ba000ce6d6b31f
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 #include <propkeydef.h>
62 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
63 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
64 #endif
65 #endif
66 #endif /* AL_NO_UID_DEFS */
68 #ifdef HAVE_DLFCN_H
69 #include <dlfcn.h>
70 #endif
71 #ifdef HAVE_INTRIN_H
72 #include <intrin.h>
73 #endif
74 #ifdef HAVE_CPUID_H
75 #include <cpuid.h>
76 #endif
77 #ifdef HAVE_SYS_SYSCONF_H
78 #include <sys/sysconf.h>
79 #endif
80 #ifdef HAVE_FLOAT_H
81 #include <float.h>
82 #endif
83 #ifdef HAVE_IEEEFP_H
84 #include <ieeefp.h>
85 #endif
87 #ifdef _WIN32_IE
88 #include <shlobj.h>
89 #endif
91 #include "alMain.h"
92 #include "alu.h"
93 #include "atomic.h"
94 #include "uintmap.h"
95 #include "vector.h"
96 #include "alstring.h"
97 #include "compat.h"
98 #include "threads.h"
101 extern inline ALuint NextPowerOf2(ALuint value);
102 extern inline ALint fastf2i(ALfloat f);
103 extern inline ALuint fastf2u(ALfloat f);
106 ALuint CPUCapFlags = 0;
109 void FillCPUCaps(ALuint capfilter)
111 ALuint caps = 0;
113 /* FIXME: We really should get this for all available CPUs in case different
114 * CPUs have different caps (is that possible on one machine?). */
115 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
116 defined(_M_IX86) || defined(_M_X64))
117 union {
118 unsigned int regs[4];
119 char str[sizeof(unsigned int[4])];
120 } cpuinf[3];
122 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
123 ERR("Failed to get CPUID\n");
124 else
126 unsigned int maxfunc = cpuinf[0].regs[0];
127 unsigned int maxextfunc = 0;
129 if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
130 maxextfunc = cpuinf[0].regs[0];
131 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
133 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
134 if(maxextfunc >= 0x80000004 &&
135 __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
136 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
137 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
138 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
140 if(maxfunc >= 1 &&
141 __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
143 if((cpuinf[0].regs[3]&(1<<25)))
145 caps |= CPU_CAP_SSE;
146 if((cpuinf[0].regs[3]&(1<<26)))
148 caps |= CPU_CAP_SSE2;
149 if((cpuinf[0].regs[2]&(1<<19)))
150 caps |= CPU_CAP_SSE4_1;
155 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
156 defined(_M_IX86) || defined(_M_X64))
157 union {
158 int regs[4];
159 char str[sizeof(int[4])];
160 } cpuinf[3];
162 (__cpuid)(cpuinf[0].regs, 0);
163 if(cpuinf[0].regs[0] == 0)
164 ERR("Failed to get CPUID\n");
165 else
167 unsigned int maxfunc = cpuinf[0].regs[0];
168 unsigned int maxextfunc;
170 (__cpuid)(cpuinf[0].regs, 0x80000000);
171 maxextfunc = cpuinf[0].regs[0];
173 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
175 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
176 if(maxextfunc >= 0x80000004)
178 (__cpuid)(cpuinf[0].regs, 0x80000002);
179 (__cpuid)(cpuinf[1].regs, 0x80000003);
180 (__cpuid)(cpuinf[2].regs, 0x80000004);
181 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
184 if(maxfunc >= 1)
186 (__cpuid)(cpuinf[0].regs, 1);
187 if((cpuinf[0].regs[3]&(1<<25)))
189 caps |= CPU_CAP_SSE;
190 if((cpuinf[0].regs[3]&(1<<26)))
192 caps |= CPU_CAP_SSE2;
193 if((cpuinf[0].regs[2]&(1<<19)))
194 caps |= CPU_CAP_SSE4_1;
199 #else
200 /* Assume support for whatever's supported if we can't check for it */
201 #if defined(HAVE_SSE4_1)
202 #warning "Assuming SSE 4.1 run-time support!"
203 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE4_1;
204 #elif defined(HAVE_SSE2)
205 #warning "Assuming SSE 2 run-time support!"
206 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2;
207 #elif defined(HAVE_SSE)
208 #warning "Assuming SSE run-time support!"
209 capfilter |= CPU_CAP_SSE;
210 #endif
211 #endif
212 #ifdef HAVE_NEON
213 /* Assume Neon support if compiled with it */
214 caps |= CPU_CAP_NEON;
215 #endif
217 TRACE("Extensions:%s%s%s%s%s\n",
218 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
219 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
220 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
221 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
222 ((!capfilter) ? " -none-" : "")
224 CPUCapFlags = caps & capfilter;
228 void *al_malloc(size_t alignment, size_t size)
230 #if defined(HAVE_ALIGNED_ALLOC)
231 size = (size+(alignment-1))&~(alignment-1);
232 return aligned_alloc(alignment, size);
233 #elif defined(HAVE_POSIX_MEMALIGN)
234 void *ret;
235 if(posix_memalign(&ret, alignment, size) == 0)
236 return ret;
237 return NULL;
238 #elif defined(HAVE__ALIGNED_MALLOC)
239 return _aligned_malloc(size, alignment);
240 #else
241 char *ret = malloc(size+alignment);
242 if(ret != NULL)
244 *(ret++) = 0x00;
245 while(((ptrdiff_t)ret&(alignment-1)) != 0)
246 *(ret++) = 0x55;
248 return ret;
249 #endif
252 void *al_calloc(size_t alignment, size_t size)
254 void *ret = al_malloc(alignment, size);
255 if(ret) memset(ret, 0, size);
256 return ret;
259 void al_free(void *ptr)
261 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
262 free(ptr);
263 #elif defined(HAVE__ALIGNED_MALLOC)
264 _aligned_free(ptr);
265 #else
266 if(ptr != NULL)
268 char *finder = ptr;
269 do {
270 --finder;
271 } while(*finder == 0x55);
272 free(finder);
274 #endif
278 void SetMixerFPUMode(FPUCtl *ctl)
280 #ifdef HAVE_FENV_H
281 fegetenv(STATIC_CAST(fenv_t, ctl));
282 #if defined(__GNUC__) && defined(HAVE_SSE)
283 if((CPUCapFlags&CPU_CAP_SSE))
284 __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
285 #endif
287 #ifdef FE_TOWARDZERO
288 fesetround(FE_TOWARDZERO);
289 #endif
290 #if defined(__GNUC__) && defined(HAVE_SSE)
291 if((CPUCapFlags&CPU_CAP_SSE))
293 int sseState = ctl->sse_state;
294 sseState |= 0x6000; /* set round-to-zero */
295 sseState |= 0x8000; /* set flush-to-zero */
296 if((CPUCapFlags&CPU_CAP_SSE2))
297 sseState |= 0x0040; /* set denormals-are-zero */
298 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
300 #endif
302 #elif defined(HAVE___CONTROL87_2)
304 int mode;
305 __control87_2(0, 0, &ctl->state, NULL);
306 __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
307 #ifdef HAVE_SSE
308 if((CPUCapFlags&CPU_CAP_SSE))
310 __control87_2(0, 0, NULL, &ctl->sse_state);
311 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
313 #endif
315 #elif defined(HAVE__CONTROLFP)
317 ctl->state = _controlfp(0, 0);
318 (void)_controlfp(_RC_CHOP, _MCW_RC);
319 #endif
322 void RestoreFPUMode(const FPUCtl *ctl)
324 #ifdef HAVE_FENV_H
325 fesetenv(STATIC_CAST(fenv_t, ctl));
326 #if defined(__GNUC__) && defined(HAVE_SSE)
327 if((CPUCapFlags&CPU_CAP_SSE))
328 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
329 #endif
331 #elif defined(HAVE___CONTROL87_2)
333 int mode;
334 __control87_2(ctl->state, _MCW_RC, &mode, NULL);
335 #ifdef HAVE_SSE
336 if((CPUCapFlags&CPU_CAP_SSE))
337 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
338 #endif
340 #elif defined(HAVE__CONTROLFP)
342 _controlfp(ctl->state, _MCW_RC);
343 #endif
347 #ifdef _WIN32
349 static WCHAR *FromUTF8(const char *str)
351 WCHAR *out = NULL;
352 int len;
354 if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
356 out = calloc(sizeof(WCHAR), len);
357 MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
359 return out;
363 void *LoadLib(const char *name)
365 HANDLE hdl = NULL;
366 WCHAR *wname;
368 wname = FromUTF8(name);
369 if(!wname)
370 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
371 else
373 hdl = LoadLibraryW(wname);
374 free(wname);
376 return hdl;
378 void CloseLib(void *handle)
379 { FreeLibrary((HANDLE)handle); }
380 void *GetSymbol(void *handle, const char *name)
382 void *ret;
384 ret = (void*)GetProcAddress((HANDLE)handle, name);
385 if(ret == NULL)
386 ERR("Failed to load %s\n", name);
387 return ret;
390 WCHAR *strdupW(const WCHAR *str)
392 const WCHAR *n;
393 WCHAR *ret;
394 size_t len;
396 n = str;
397 while(*n) n++;
398 len = n - str;
400 ret = calloc(sizeof(WCHAR), len+1);
401 if(ret != NULL)
402 memcpy(ret, str, sizeof(WCHAR)*len);
403 return ret;
406 FILE *al_fopen(const char *fname, const char *mode)
408 WCHAR *wname=NULL, *wmode=NULL;
409 FILE *file = NULL;
411 wname = FromUTF8(fname);
412 wmode = FromUTF8(mode);
413 if(!wname)
414 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
415 else if(!wmode)
416 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
417 else
418 file = _wfopen(wname, wmode);
420 free(wname);
421 free(wmode);
423 return file;
426 #else
428 #ifdef HAVE_DLFCN_H
430 void *LoadLib(const char *name)
432 const char *err;
433 void *handle;
435 dlerror();
436 handle = dlopen(name, RTLD_NOW);
437 if((err=dlerror()) != NULL)
438 handle = NULL;
439 return handle;
441 void CloseLib(void *handle)
442 { dlclose(handle); }
443 void *GetSymbol(void *handle, const char *name)
445 const char *err;
446 void *sym;
448 dlerror();
449 sym = dlsym(handle, name);
450 if((err=dlerror()) != NULL)
452 WARN("Failed to load %s: %s\n", name, err);
453 sym = NULL;
455 return sym;
458 #endif
459 #endif
462 void al_print(const char *type, const char *func, const char *fmt, ...)
464 va_list ap;
466 va_start(ap, fmt);
467 fprintf(LogFile, "AL lib: %s %s: ", type, func);
468 vfprintf(LogFile, fmt, ap);
469 va_end(ap);
471 fflush(LogFile);
474 #ifdef _WIN32
475 static inline int is_slash(int c)
476 { return (c == '\\' || c == '/'); }
478 FILE *OpenDataFile(const char *fname, const char *subdir)
480 static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
481 WCHAR *wname=NULL, *wsubdir=NULL;
482 FILE *f;
483 int i;
485 /* If the path is absolute, open it directly. */
486 if(fname[0] != '\0' && fname[1] == ':' && is_slash(fname[2]))
488 if((f=al_fopen(fname, "rb")) != NULL)
490 TRACE("Opened %s\n", fname);
491 return f;
493 WARN("Could not open %s\n", fname);
494 return NULL;
497 /* If it's relative, try the current directory first before the data directories. */
498 if((f=al_fopen(fname, "rb")) != NULL)
500 TRACE("Opened %s\n", fname);
501 return f;
504 wname = FromUTF8(fname);
505 wsubdir = FromUTF8(subdir);
506 if(!wname)
507 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
508 else if(!wsubdir)
509 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
510 else for(i = 0;i < 2;i++)
512 WCHAR buffer[PATH_MAX];
513 size_t len;
515 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
516 continue;
518 len = lstrlenW(buffer);
519 if(len > 0 && is_slash(buffer[len-1]))
520 buffer[--len] = '\0';
521 _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
522 len = lstrlenW(buffer);
523 while(len > 0)
525 --len;
526 if(buffer[len] == '/')
527 buffer[len] = '\\';
530 if((f=_wfopen(buffer, L"rb")) != NULL)
532 TRACE("Opened %ls\n", buffer);
533 free(wname);
534 free(wsubdir);
535 return f;
538 WARN("Could not open %ls\\%ls\n", wsubdir, wname);
539 free(wname);
540 free(wsubdir);
542 return NULL;
544 #else
545 FILE *OpenDataFile(const char *fname, const char *subdir)
547 char buffer[PATH_MAX] = "";
548 const char *str, *next;
549 FILE *f;
551 if(fname[0] == '/')
553 if((f=al_fopen(fname, "rb")) != NULL)
555 TRACE("Opened %s\n", fname);
556 return f;
558 WARN("Could not open %s\n", fname);
559 return NULL;
562 if((f=al_fopen(fname, "rb")) != NULL)
564 TRACE("Opened %s\n", fname);
565 return f;
568 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
569 snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
570 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
571 snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
572 if(buffer[0])
574 if((f=al_fopen(buffer, "rb")) != NULL)
576 TRACE("Opened %s\n", buffer);
577 return f;
581 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
582 str = "/usr/local/share/:/usr/share/";
584 next = str;
585 while((str=next) != NULL && str[0] != '\0')
587 size_t len;
588 next = strchr(str, ':');
590 if(!next)
591 len = strlen(str);
592 else
594 len = next - str;
595 next++;
598 if(len > sizeof(buffer)-1)
599 len = sizeof(buffer)-1;
600 strncpy(buffer, str, len);
601 buffer[len] = '\0';
602 snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
604 if((f=al_fopen(buffer, "rb")) != NULL)
606 TRACE("Opened %s\n", buffer);
607 return f;
610 WARN("Could not open %s/%s\n", subdir, fname);
612 return NULL;
614 #endif
617 void SetRTPriority(void)
619 ALboolean failed = AL_FALSE;
621 #ifdef _WIN32
622 if(RTPrioLevel > 0)
623 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
624 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
625 if(RTPrioLevel > 0)
627 struct sched_param param;
628 /* Use the minimum real-time priority possible for now (on Linux this
629 * should be 1 for SCHED_RR) */
630 param.sched_priority = sched_get_priority_min(SCHED_RR);
631 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
633 #else
634 /* Real-time priority not available */
635 failed = (RTPrioLevel>0);
636 #endif
637 if(failed)
638 ERR("Failed to set priority level for thread\n");
642 ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact)
644 vector_ *vecptr = (vector_*)ptr;
645 if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
647 size_t old_size = (*vecptr ? (*vecptr)->Size : 0);
648 void *temp;
650 /* Use the next power-of-2 size if we don't need to allocate the exact
651 * amount. This is preferred when regularly increasing the vector since
652 * it means fewer reallocations. Though it means it also wastes some
653 * memory. */
654 if(exact == AL_FALSE && obj_count < INT_MAX)
655 obj_count = NextPowerOf2((ALuint)obj_count);
657 /* Need to be explicit with the caller type's base size, because it
658 * could have extra padding before the start of the array (that is,
659 * sizeof(*vector_) may not equal base_size). */
660 temp = realloc(*vecptr, base_size + obj_size*obj_count);
661 if(temp == NULL) return AL_FALSE;
663 *vecptr = temp;
664 (*vecptr)->Capacity = obj_count;
665 (*vecptr)->Size = old_size;
667 return AL_TRUE;
670 ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count)
672 vector_ *vecptr = (vector_*)ptr;
673 if(*vecptr || obj_count > 0)
675 if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
676 return AL_FALSE;
677 (*vecptr)->Size = obj_count;
679 return AL_TRUE;
682 ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
684 vector_ *vecptr = (vector_*)ptr;
685 if(datstart != datend)
687 ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
688 ((char*)ins_pos - (char*)NULL)) /
689 obj_size;
690 ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
692 assert(numins > 0);
693 if((size_t)numins + VECTOR_SIZE(*vecptr) < (size_t)numins ||
694 !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
695 return AL_FALSE;
697 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
698 if((size_t)ins_elem < (*vecptr)->Size)
700 memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
701 (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
702 ((*vecptr)->Size-ins_elem)*obj_size);
704 memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
705 datstart, numins*obj_size);
706 (*vecptr)->Size += numins;
708 return AL_TRUE;
712 extern inline void al_string_deinit(al_string *str);
713 extern inline size_t al_string_length(const_al_string str);
714 extern inline ALboolean al_string_empty(const_al_string str);
715 extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
717 void al_string_clear(al_string *str)
719 /* Reserve one more character than the total size of the string. This is to
720 * ensure we have space to add a null terminator in the string data so it
721 * can be used as a C-style string. */
722 VECTOR_RESERVE(*str, 1);
723 VECTOR_RESIZE(*str, 0);
724 *VECTOR_ITER_END(*str) = 0;
727 static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
728 const al_string_char_type *str2, size_t str2len)
730 size_t complen = (str1len < str2len) ? str1len : str2len;
731 int ret = memcmp(str1, str2, complen);
732 if(ret == 0)
734 if(str1len > str2len) return 1;
735 if(str1len < str2len) return -1;
737 return ret;
739 int al_string_cmp(const_al_string str1, const_al_string str2)
741 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
742 &VECTOR_FRONT(str2), al_string_length(str2));
744 int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
746 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
747 str2, strlen(str2));
750 void al_string_copy(al_string *str, const_al_string from)
752 size_t len = al_string_length(from);
753 VECTOR_RESERVE(*str, len+1);
754 VECTOR_RESIZE(*str, 0);
755 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
756 *VECTOR_ITER_END(*str) = 0;
759 void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
761 size_t len = strlen(from);
762 VECTOR_RESERVE(*str, len+1);
763 VECTOR_RESIZE(*str, 0);
764 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
765 *VECTOR_ITER_END(*str) = 0;
768 void al_string_append_char(al_string *str, const al_string_char_type c)
770 VECTOR_RESERVE(*str, al_string_length(*str)+2);
771 VECTOR_PUSH_BACK(*str, c);
772 *VECTOR_ITER_END(*str) = 0;
775 void al_string_append_cstr(al_string *str, const al_string_char_type *from)
777 size_t len = strlen(from);
778 if(len != 0)
780 VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
781 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
782 *VECTOR_ITER_END(*str) = 0;
786 void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
788 if(to != from)
790 VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
791 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
792 *VECTOR_ITER_END(*str) = 0;
796 #ifdef _WIN32
797 void al_string_copy_wcstr(al_string *str, const wchar_t *from)
799 int len;
800 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
802 VECTOR_RESERVE(*str, len);
803 VECTOR_RESIZE(*str, len-1);
804 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
805 *VECTOR_ITER_END(*str) = 0;
808 #endif