summaryrefslogtreecommitdiffstats
path: root/apps/plugins/mikmod/mmalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/mikmod/mmalloc.c')
-rw-r--r--apps/plugins/mikmod/mmalloc.c207
1 files changed, 85 insertions, 122 deletions
diff --git a/apps/plugins/mikmod/mmalloc.c b/apps/plugins/mikmod/mmalloc.c
index 2797def57d..ed5fd4684c 100644
--- a/apps/plugins/mikmod/mmalloc.c
+++ b/apps/plugins/mikmod/mmalloc.c
@@ -6,12 +6,12 @@
it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
-
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
-
+
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
@@ -20,7 +20,7 @@
/*==============================================================================
- $Id: mmalloc.c,v 1.3 2007/12/03 20:42:58 denis111 Exp $
+ $Id$
Dynamic memory routines
@@ -30,150 +30,113 @@
#include "config.h"
#endif
-#include "mikmod_internals.h"
-
-#define ALIGN_STRIDE 16
-/* not used
-static void * align_pointer(char *ptr, size_t stride)
-{
- char *pptr = ptr + sizeof(void*);
- char *fptr;
- size_t err = ((size_t)pptr)&(stride-1);
- if (err)
- fptr = pptr + (stride - err);
- else
- fptr = pptr;
- *(size_t*)(fptr - sizeof(void*)) = (size_t)ptr;
- return fptr;
-}
+#ifdef HAVE_POSIX_MEMALIGN
+#define _XOPEN_SOURCE 600 /* for posix_memalign */
+#endif
-static void *get_pointer(void *data)
-{
- unsigned char *_pptr = (unsigned char*)data - sizeof(void*);
- size_t _ptr = *(size_t*)_pptr;
- return (void*)_ptr;
-}
-*/
+#include "string.h"
+#include "mikmod_internals.h"
-void* MikMod_realloc(void *data, size_t size)
-{
- return realloc(data, size);
-
-#if 0
- if (data)
- {
-#if defined __MACH__
- void *d = realloc(data, size);
- if (d)
- {
- return d;
- }
- return 0;
-#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
- return _aligned_realloc(data, size, ALIGN_STRIDE);
-#else
- unsigned char *newPtr = (unsigned char *)realloc(get_pointer(data), size + ALIGN_STRIDE + sizeof(void*));
- return align_pointer(newPtr, ALIGN_STRIDE);
+#if defined(HAVE_SSE2) || defined(HAVE_ALTIVEC)
+#undef WIN32_ALIGNED_MALLOC
+#if defined(_WIN32) && !defined(_WIN32_WCE)
+# if defined(_WIN64) /* OK with MSVC and MinGW */
+# define WIN32_ALIGNED_MALLOC
+# elif defined(_MSC_VER) && (_MSC_VER >= 1300)
+# define WIN32_ALIGNED_MALLOC
+# elif defined(__MINGW32__)
+ /* no guarantees that msvcrt.dll will have it */
+# endif
#endif
- }
- return MikMod_malloc(size);
-#endif
-}
+#define PTRSIZE (sizeof(void*))
-/* Same as malloc, but sets error variable _mm_error when fails. Returns a 16-byte aligned pointer */
-void* MikMod_malloc(size_t size)
+/* return a 16 byte aligned address */
+void* MikMod_amalloc(size_t size)
{
void *d;
- if(!(d=calloc(1,size))) {
- _mm_errno = MMERR_OUT_OF_MEMORY;
- if(_mm_errorhandler) _mm_errorhandler();
- }
- return d;
-
-#if 0
-#if defined __MACH__
- void *d = calloc(1, size);
- if (d)
- {
+#if defined(HAVE_POSIX_MEMALIGN)
+ if (!posix_memalign(&d, 16, size)) {
+ memset(d, 0, size);
return d;
}
- return 0;
-#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
- void * d = _aligned_malloc(size, ALIGN_STRIDE);
- if (d)
- {
+#elif defined(WIN32_ALIGNED_MALLOC)
+ d = _aligned_malloc(size, 16);
+ if (d) {
ZeroMemory(d, size);
return d;
}
- return 0;
#else
- void *d = calloc(1, size + ALIGN_STRIDE + sizeof(void*));
-
- if(!d) {
- _mm_errno = MMERR_OUT_OF_MEMORY;
- if(_mm_errorhandler) _mm_errorhandler();
+ size_t s = (size)? ((size + (PTRSIZE-1)) & ~(PTRSIZE-1)) : PTRSIZE;
+ s += PTRSIZE + 16;
+ d = calloc(1, s);
+ if (d) {
+ char *pptr = (char *)d + PTRSIZE;
+ size_t err = ((size_t)pptr) & 15;
+ char *fptr = pptr + (16 - err);
+ *(size_t*)(fptr - PTRSIZE) = (size_t)d;
+ return fptr;
}
- return align_pointer(d, ALIGN_STRIDE);
#endif
+
+ _mm_errno = MMERR_OUT_OF_MEMORY;
+ if(_mm_errorhandler) _mm_errorhandler();
+ return NULL;
+}
+
+void MikMod_afree(void *data)
+{
+ if (!data) return;
+#if defined(HAVE_POSIX_MEMALIGN)
+ free(data);
+#elif defined(WIN32_ALIGNED_MALLOC)
+ _aligned_free(data);
+#else
+ free((void *) *(size_t*)((unsigned char *)data - PTRSIZE));
#endif
}
+#endif /* (HAVE_SSE2) || (HAVE_ALTIVEC) */
+
+void* MikMod_realloc(void *data, size_t size)
+{
+ if (data) return realloc(data, size);
+ return calloc(1, size);
+}
+
+/* Same as malloc, but sets error variable _mm_error when fails */
+void* MikMod_malloc(size_t size)
+{
+ return MikMod_calloc(1, size);
+}
/* Same as calloc, but sets error variable _mm_error when fails */
-void* MikMod_calloc(size_t nitems,size_t size)
+void* MikMod_calloc(size_t nitems, size_t size)
{
- void *d;
-
- if(!(d=calloc(nitems,size))) {
- _mm_errno = MMERR_OUT_OF_MEMORY;
- if(_mm_errorhandler) _mm_errorhandler();
- }
- return d;
-
-#if 0
-#if defined __MACH__
void *d = calloc(nitems, size);
- if (d)
- {
- return d;
- }
- return 0;
-#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
- void * d = _aligned_malloc(size * nitems, ALIGN_STRIDE);
- if (d)
- {
- ZeroMemory(d, size * nitems);
- return d;
- }
- return 0;
-#else
- void *d = calloc(nitems, size + ALIGN_STRIDE + sizeof(void*));
-
- if(!d) {
- _mm_errno = MMERR_OUT_OF_MEMORY;
- if(_mm_errorhandler) _mm_errorhandler();
- }
- return align_pointer(d, ALIGN_STRIDE);
-#endif
-#endif
+ if (d) return d;
+
+ _mm_errno = MMERR_OUT_OF_MEMORY;
+ if(_mm_errorhandler) _mm_errorhandler();
+ return NULL;
}
void MikMod_free(void *data)
{
- free(data);
-
-#if 0
- if (data)
- {
-#if defined __MACH__
- free(data);
-#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
- _aligned_free(data);
-#else
- free(get_pointer(data));
-#endif
- }
-#endif
+ if (data) free(data);
+}
+
+/* like strdup(), but the result must be freed using MikMod_free() */
+CHAR *MikMod_strdup(const CHAR *s)
+{
+ size_t l;
+ CHAR *d;
+
+ if (!s) return NULL;
+
+ l = strlen(s) + 1;
+ d = (CHAR *) MikMod_calloc(1, l * sizeof(CHAR));
+ if (d) strcpy(d, s);
+ return d;
}
/* ex:set ts=4: */