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.c179
1 files changed, 179 insertions, 0 deletions
diff --git a/apps/plugins/mikmod/mmalloc.c b/apps/plugins/mikmod/mmalloc.c
new file mode 100644
index 0000000000..7d505dff93
--- /dev/null
+++ b/apps/plugins/mikmod/mmalloc.c
@@ -0,0 +1,179 @@
1/* MikMod sound library
2 (c) 1998, 1999 Miodrag Vallat and others - see file AUTHORS for
3 complete list.
4
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.
9
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.
14
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
18 02111-1307, USA.
19*/
20
21/*==============================================================================
22
23 $Id: mmalloc.c,v 1.3 2007/12/03 20:42:58 denis111 Exp $
24
25 Dynamic memory routines
26
27==============================================================================*/
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include "mikmod_internals.h"
34
35#define ALIGN_STRIDE 16
36
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
50static void *get_pointer(void *data)
51{
52 unsigned char *_pptr = (unsigned char*)data - sizeof(void*);
53 size_t _ptr = *(size_t*)_pptr;
54 return (void*)_ptr;
55}
56
57
58void* MikMod_realloc(void *data, size_t size)
59{
60 return realloc(data, size);
61
62#if 0
63 if (data)
64 {
65#if defined __MACH__
66 void *d = realloc(data, size);
67 if (d)
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
78 }
79 return MikMod_malloc(size);
80#endif
81}
82
83
84/* Same as malloc, but sets error variable _mm_error when fails. Returns a 16-byte aligned pointer */
85void* MikMod_malloc(size_t size)
86{
87 void *d;
88 if(!(d=calloc(1,size))) {
89 _mm_errno = MMERR_OUT_OF_MEMORY;
90 if(_mm_errorhandler) _mm_errorhandler();
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;
100 }
101 return 0;
102#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
103 void * d = _aligned_malloc(size, ALIGN_STRIDE);
104 if (d)
105 {
106 ZeroMemory(d, size);
107 return d;
108 }
109 return 0;
110#else
111 void *d = calloc(1, size + ALIGN_STRIDE + sizeof(void*));
112
113 if(!d) {
114 _mm_errno = MMERR_OUT_OF_MEMORY;
115 if(_mm_errorhandler) _mm_errorhandler();
116 }
117 return align_pointer(d, ALIGN_STRIDE);
118#endif
119#endif
120}
121
122/* Same as calloc, but sets error variable _mm_error when fails */
123void* MikMod_calloc(size_t nitems,size_t size)
124{
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);
136 if (d)
137 {
138 return d;
139 }
140 return 0;
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}
160
161void MikMod_free(void *data)
162{
163 free(data);
164
165#if 0
166 if (data)
167 {
168#if defined __MACH__
169 free(data);
170#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
171 _aligned_free(data);
172#else
173 free(get_pointer(data));
174#endif
175 }
176#endif
177}
178
179/* ex:set ts=4: */