Manual: Fix part 1 of FS#11874 - touchscreen region in the manual isn't 100% correct.
[kugel-rb.git] / apps / plugins / mikmod / mmalloc.c
blob7d505dff9336d299c55eec009e5316ca94c94322
1 /* MikMod sound library
2 (c) 1998, 1999 Miodrag Vallat and others - see file AUTHORS for
3 complete list.
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
18 02111-1307, USA.
21 /*==============================================================================
23 $Id: mmalloc.c,v 1.3 2007/12/03 20:42:58 denis111 Exp $
25 Dynamic memory routines
27 ==============================================================================*/
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include "mikmod_internals.h"
35 #define ALIGN_STRIDE 16
37 static void * align_pointer(char *ptr, size_t stride)
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;
50 static void *get_pointer(void *data)
52 unsigned char *_pptr = (unsigned char*)data - sizeof(void*);
53 size_t _ptr = *(size_t*)_pptr;
54 return (void*)_ptr;
58 void* MikMod_realloc(void *data, size_t size)
60 return realloc(data, size);
62 #if 0
63 if (data)
65 #if defined __MACH__
66 void *d = realloc(data, size);
67 if (d)
69 return d;
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
79 return MikMod_malloc(size);
80 #endif
84 /* Same as malloc, but sets error variable _mm_error when fails. Returns a 16-byte aligned pointer */
85 void* MikMod_malloc(size_t size)
87 void *d;
88 if(!(d=calloc(1,size))) {
89 _mm_errno = MMERR_OUT_OF_MEMORY;
90 if(_mm_errorhandler) _mm_errorhandler();
92 return d;
94 #if 0
95 #if defined __MACH__
96 void *d = calloc(1, size);
97 if (d)
99 return d;
101 return 0;
102 #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
103 void * d = _aligned_malloc(size, ALIGN_STRIDE);
104 if (d)
106 ZeroMemory(d, size);
107 return d;
109 return 0;
110 #else
111 void *d = calloc(1, size + ALIGN_STRIDE + sizeof(void*));
113 if(!d) {
114 _mm_errno = MMERR_OUT_OF_MEMORY;
115 if(_mm_errorhandler) _mm_errorhandler();
117 return align_pointer(d, ALIGN_STRIDE);
118 #endif
119 #endif
122 /* Same as calloc, but sets error variable _mm_error when fails */
123 void* MikMod_calloc(size_t nitems,size_t size)
125 void *d;
127 if(!(d=calloc(nitems,size))) {
128 _mm_errno = MMERR_OUT_OF_MEMORY;
129 if(_mm_errorhandler) _mm_errorhandler();
131 return d;
133 #if 0
134 #if defined __MACH__
135 void *d = calloc(nitems, size);
136 if (d)
138 return d;
140 return 0;
141 #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
142 void * d = _aligned_malloc(size * nitems, ALIGN_STRIDE);
143 if (d)
145 ZeroMemory(d, size * nitems);
146 return d;
148 return 0;
149 #else
150 void *d = calloc(nitems, size + ALIGN_STRIDE + sizeof(void*));
152 if(!d) {
153 _mm_errno = MMERR_OUT_OF_MEMORY;
154 if(_mm_errorhandler) _mm_errorhandler();
156 return align_pointer(d, ALIGN_STRIDE);
157 #endif
158 #endif
161 void MikMod_free(void *data)
163 free(data);
165 #if 0
166 if (data)
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
176 #endif
179 /* ex:set ts=4: */