1 /* MikMod sound library
2 (c) 1998, 1999 Miodrag Vallat and others - see file AUTHORS for
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 /*==============================================================================
25 Dynamic memory routines
27 ==============================================================================*/
33 #ifdef HAVE_POSIX_MEMALIGN
34 #define _XOPEN_SOURCE 600 /* for posix_memalign */
38 #include "mikmod_internals.h"
40 #if defined(HAVE_SSE2) || defined(HAVE_ALTIVEC)
41 #undef WIN32_ALIGNED_MALLOC
42 #if defined(_WIN32) && !defined(_WIN32_WCE)
43 # if defined(_WIN64) /* OK with MSVC and MinGW */
44 # define WIN32_ALIGNED_MALLOC
45 # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
46 # define WIN32_ALIGNED_MALLOC
47 # elif defined(__MINGW32__)
48 /* no guarantees that msvcrt.dll will have it */
52 #define PTRSIZE (sizeof(void*))
54 /* return a 16 byte aligned address */
55 void* MikMod_amalloc(size_t size)
58 #if defined(HAVE_POSIX_MEMALIGN)
59 if (!posix_memalign(&d, 16, size)) {
63 #elif defined(WIN32_ALIGNED_MALLOC)
64 d = _aligned_malloc(size, 16);
70 size_t s = (size)? ((size + (PTRSIZE-1)) & ~(PTRSIZE-1)) : PTRSIZE;
74 char *pptr = (char *)d + PTRSIZE;
75 size_t err = ((size_t)pptr) & 15;
76 char *fptr = pptr + (16 - err);
77 *(size_t*)(fptr - PTRSIZE) = (size_t)d;
82 _mm_errno = MMERR_OUT_OF_MEMORY;
83 if(_mm_errorhandler) _mm_errorhandler();
87 void MikMod_afree(void *data)
90 #if defined(HAVE_POSIX_MEMALIGN)
92 #elif defined(WIN32_ALIGNED_MALLOC)
95 free((void *) *(size_t*)((unsigned char *)data - PTRSIZE));
98 #endif /* (HAVE_SSE2) || (HAVE_ALTIVEC) */
100 void* MikMod_realloc(void *data, size_t size)
102 if (data) return realloc(data, size);
103 return calloc(1, size);
106 /* Same as malloc, but sets error variable _mm_error when fails */
107 void* MikMod_malloc(size_t size)
109 return MikMod_calloc(1, size);
112 /* Same as calloc, but sets error variable _mm_error when fails */
113 void* MikMod_calloc(size_t nitems, size_t size)
115 void *d = calloc(nitems, size);
118 _mm_errno = MMERR_OUT_OF_MEMORY;
119 if(_mm_errorhandler) _mm_errorhandler();
123 void MikMod_free(void *data)
125 if (data) free(data);
128 /* like strdup(), but the result must be freed using MikMod_free() */
129 CHAR *MikMod_strdup(const CHAR *s)
137 d = (CHAR *) MikMod_calloc(1, l * sizeof(CHAR));