summaryrefslogtreecommitdiff
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 @@
6 it under the terms of the GNU Library General Public License as 6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of 7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version. 8 the License, or (at your option) any later version.
9 9
10 This program is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details. 13 GNU Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public 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 16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
@@ -20,7 +20,7 @@
20 20
21/*============================================================================== 21/*==============================================================================
22 22
23 $Id: mmalloc.c,v 1.3 2007/12/03 20:42:58 denis111 Exp $ 23 $Id$
24 24
25 Dynamic memory routines 25 Dynamic memory routines
26 26
@@ -30,150 +30,113 @@
30#include "config.h" 30#include "config.h"
31#endif 31#endif
32 32
33#include "mikmod_internals.h" 33#ifdef HAVE_POSIX_MEMALIGN
34 34#define _XOPEN_SOURCE 600 /* for posix_memalign */
35#define ALIGN_STRIDE 16 35#endif
36/* not used
37static void * align_pointer(char *ptr, size_t stride)
38{
39 char *pptr = ptr + sizeof(void*);
40 char *fptr;
41 size_t err = ((size_t)pptr)&(stride-1);
42 if (err)
43 fptr = pptr + (stride - err);
44 else
45 fptr = pptr;
46 *(size_t*)(fptr - sizeof(void*)) = (size_t)ptr;
47 return fptr;
48}
49 36
50static void *get_pointer(void *data) 37#include "string.h"
51{ 38#include "mikmod_internals.h"
52 unsigned char *_pptr = (unsigned char*)data - sizeof(void*);
53 size_t _ptr = *(size_t*)_pptr;
54 return (void*)_ptr;
55}
56*/
57 39
58void* MikMod_realloc(void *data, size_t size) 40#if defined(HAVE_SSE2) || defined(HAVE_ALTIVEC)
59{ 41#undef WIN32_ALIGNED_MALLOC
60 return realloc(data, size); 42#if defined(_WIN32) && !defined(_WIN32_WCE)
61 43# if defined(_WIN64) /* OK with MSVC and MinGW */
62#if 0 44# define WIN32_ALIGNED_MALLOC
63 if (data) 45# elif defined(_MSC_VER) && (_MSC_VER >= 1300)
64 { 46# define WIN32_ALIGNED_MALLOC
65#if defined __MACH__ 47# elif defined(__MINGW32__)
66 void *d = realloc(data, size); 48 /* no guarantees that msvcrt.dll will have it */
67 if (d) 49# endif
68 {
69 return d;
70 }
71 return 0;
72#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
73 return _aligned_realloc(data, size, ALIGN_STRIDE);
74#else
75 unsigned char *newPtr = (unsigned char *)realloc(get_pointer(data), size + ALIGN_STRIDE + sizeof(void*));
76 return align_pointer(newPtr, ALIGN_STRIDE);
77#endif 50#endif
78 }
79 return MikMod_malloc(size);
80#endif
81}
82 51
52#define PTRSIZE (sizeof(void*))
83 53
84/* Same as malloc, but sets error variable _mm_error when fails. Returns a 16-byte aligned pointer */ 54/* return a 16 byte aligned address */
85void* MikMod_malloc(size_t size) 55void* MikMod_amalloc(size_t size)
86{ 56{
87 void *d; 57 void *d;
88 if(!(d=calloc(1,size))) { 58#if defined(HAVE_POSIX_MEMALIGN)
89 _mm_errno = MMERR_OUT_OF_MEMORY; 59 if (!posix_memalign(&d, 16, size)) {
90 if(_mm_errorhandler) _mm_errorhandler(); 60 memset(d, 0, size);
91 }
92 return d;
93
94#if 0
95#if defined __MACH__
96 void *d = calloc(1, size);
97 if (d)
98 {
99 return d; 61 return d;
100 } 62 }
101 return 0; 63#elif defined(WIN32_ALIGNED_MALLOC)
102#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) 64 d = _aligned_malloc(size, 16);
103 void * d = _aligned_malloc(size, ALIGN_STRIDE); 65 if (d) {
104 if (d)
105 {
106 ZeroMemory(d, size); 66 ZeroMemory(d, size);
107 return d; 67 return d;
108 } 68 }
109 return 0;
110#else 69#else
111 void *d = calloc(1, size + ALIGN_STRIDE + sizeof(void*)); 70 size_t s = (size)? ((size + (PTRSIZE-1)) & ~(PTRSIZE-1)) : PTRSIZE;
112 71 s += PTRSIZE + 16;
113 if(!d) { 72 d = calloc(1, s);
114 _mm_errno = MMERR_OUT_OF_MEMORY; 73 if (d) {
115 if(_mm_errorhandler) _mm_errorhandler(); 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;
78 return fptr;
116 } 79 }
117 return align_pointer(d, ALIGN_STRIDE);
118#endif 80#endif
81
82 _mm_errno = MMERR_OUT_OF_MEMORY;
83 if(_mm_errorhandler) _mm_errorhandler();
84 return NULL;
85}
86
87void MikMod_afree(void *data)
88{
89 if (!data) return;
90#if defined(HAVE_POSIX_MEMALIGN)
91 free(data);
92#elif defined(WIN32_ALIGNED_MALLOC)
93 _aligned_free(data);
94#else
95 free((void *) *(size_t*)((unsigned char *)data - PTRSIZE));
119#endif 96#endif
120} 97}
98#endif /* (HAVE_SSE2) || (HAVE_ALTIVEC) */
99
100void* MikMod_realloc(void *data, size_t size)
101{
102 if (data) return realloc(data, size);
103 return calloc(1, size);
104}
105
106/* Same as malloc, but sets error variable _mm_error when fails */
107void* MikMod_malloc(size_t size)
108{
109 return MikMod_calloc(1, size);
110}
121 111
122/* Same as calloc, but sets error variable _mm_error when fails */ 112/* Same as calloc, but sets error variable _mm_error when fails */
123void* MikMod_calloc(size_t nitems,size_t size) 113void* MikMod_calloc(size_t nitems, size_t size)
124{ 114{
125 void *d;
126
127 if(!(d=calloc(nitems,size))) {
128 _mm_errno = MMERR_OUT_OF_MEMORY;
129 if(_mm_errorhandler) _mm_errorhandler();
130 }
131 return d;
132
133#if 0
134#if defined __MACH__
135 void *d = calloc(nitems, size); 115 void *d = calloc(nitems, size);
136 if (d) 116 if (d) return d;
137 { 117
138 return d; 118 _mm_errno = MMERR_OUT_OF_MEMORY;
139 } 119 if(_mm_errorhandler) _mm_errorhandler();
140 return 0; 120 return NULL;
141#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
142 void * d = _aligned_malloc(size * nitems, ALIGN_STRIDE);
143 if (d)
144 {
145 ZeroMemory(d, size * nitems);
146 return d;
147 }
148 return 0;
149#else
150 void *d = calloc(nitems, size + ALIGN_STRIDE + sizeof(void*));
151
152 if(!d) {
153 _mm_errno = MMERR_OUT_OF_MEMORY;
154 if(_mm_errorhandler) _mm_errorhandler();
155 }
156 return align_pointer(d, ALIGN_STRIDE);
157#endif
158#endif
159} 121}
160 122
161void MikMod_free(void *data) 123void MikMod_free(void *data)
162{ 124{
163 free(data); 125 if (data) free(data);
164 126}
165#if 0 127
166 if (data) 128/* like strdup(), but the result must be freed using MikMod_free() */
167 { 129CHAR *MikMod_strdup(const CHAR *s)
168#if defined __MACH__ 130{
169 free(data); 131 size_t l;
170#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) 132 CHAR *d;
171 _aligned_free(data); 133
172#else 134 if (!s) return NULL;
173 free(get_pointer(data)); 135
174#endif 136 l = strlen(s) + 1;
175 } 137 d = (CHAR *) MikMod_calloc(1, l * sizeof(CHAR));
176#endif 138 if (d) strcpy(d, s);
139 return d;
177} 140}
178 141
179/* ex:set ts=4: */ 142/* ex:set ts=4: */