summaryrefslogtreecommitdiff
path: root/apps/plugins/mikmod/mmalloc.c
blob: 2797def57d2caec12f0e6b7631c7903f9dd3c948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*	MikMod sound library
	(c) 1998, 1999 Miodrag Vallat and others - see file AUTHORS for
	complete list.

	This library is free software; you can redistribute it and/or modify
	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
	02111-1307, USA.
*/

/*==============================================================================

  $Id: mmalloc.c,v 1.3 2007/12/03 20:42:58 denis111 Exp $

  Dynamic memory routines

==============================================================================*/

#ifdef HAVE_CONFIG_H
#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;
}

static void *get_pointer(void *data)
{
	unsigned char *_pptr = (unsigned char*)data - sizeof(void*);
	size_t _ptr = *(size_t*)_pptr;
	return (void*)_ptr;
}
*/

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);
#endif
	}
	return MikMod_malloc(size);
#endif
}


/* Same as malloc, but sets error variable _mm_error when fails. Returns a 16-byte aligned pointer */
void* MikMod_malloc(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)
	{
		return d;
	}
	return 0;
#elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE)
	void * d = _aligned_malloc(size, ALIGN_STRIDE);
	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();
	}
	return align_pointer(d, ALIGN_STRIDE);
#endif
#endif
}

/* Same as calloc, but sets error variable _mm_error when fails */
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
}

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
}

/* ex:set ts=4: */