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 /*==============================================================================
23 $Id: mmalloc.c,v 1.3 2007/12/03 20:42:58 denis111 Exp $
25 Dynamic memory routines
27 ==============================================================================*/
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*);
41 size_t err
= ((size_t)pptr
)&(stride
-1);
43 fptr
= pptr
+ (stride
- err
);
46 *(size_t*)(fptr
- sizeof(void*)) = (size_t)ptr
;
50 static void *get_pointer(void *data
)
52 unsigned char *_pptr
= (unsigned char*)data
- sizeof(void*);
53 size_t _ptr
= *(size_t*)_pptr
;
58 void* MikMod_realloc(void *data
, size_t size
)
60 return realloc(data
, size
);
66 void *d
= realloc(data
, size
);
72 #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
73 return _aligned_realloc(data
, size
, ALIGN_STRIDE
);
75 unsigned char *newPtr
= (unsigned char *)realloc(get_pointer(data
), size
+ ALIGN_STRIDE
+ sizeof(void*));
76 return align_pointer(newPtr
, ALIGN_STRIDE
);
79 return MikMod_malloc(size
);
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
)
88 if(!(d
=calloc(1,size
))) {
89 _mm_errno
= MMERR_OUT_OF_MEMORY
;
90 if(_mm_errorhandler
) _mm_errorhandler();
96 void *d
= calloc(1, size
);
102 #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
103 void * d
= _aligned_malloc(size
, ALIGN_STRIDE
);
111 void *d
= calloc(1, size
+ ALIGN_STRIDE
+ sizeof(void*));
114 _mm_errno
= MMERR_OUT_OF_MEMORY
;
115 if(_mm_errorhandler
) _mm_errorhandler();
117 return align_pointer(d
, ALIGN_STRIDE
);
122 /* Same as calloc, but sets error variable _mm_error when fails */
123 void* MikMod_calloc(size_t nitems
,size_t size
)
127 if(!(d
=calloc(nitems
,size
))) {
128 _mm_errno
= MMERR_OUT_OF_MEMORY
;
129 if(_mm_errorhandler
) _mm_errorhandler();
135 void *d
= calloc(nitems
, size
);
141 #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
142 void * d
= _aligned_malloc(size
* nitems
, ALIGN_STRIDE
);
145 ZeroMemory(d
, size
* nitems
);
150 void *d
= calloc(nitems
, size
+ ALIGN_STRIDE
+ sizeof(void*));
153 _mm_errno
= MMERR_OUT_OF_MEMORY
;
154 if(_mm_errorhandler
) _mm_errorhandler();
156 return align_pointer(d
, ALIGN_STRIDE
);
161 void MikMod_free(void *data
)
170 #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
173 free(get_pointer(data
));