Make basic cache functions into calls, and get rid of CACHE_FUNCTION_WRAPPERS and...
[kugel-rb.git] / apps / plugins / mpegplayer / alloc.c
blobda92f8754aa56db6c3825357e00e10d6093e066d
1 /*
2 * alloc.c
3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7 * See http://libmpeg2.sourceforge.net/ for updates.
9 * mpeg2dec is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * mpeg2dec is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * $Id$
24 * libmpeg2 sync history:
25 * 2008-07-01 - CVS revision 1.13
28 #include "plugin.h"
29 #include "mpegplayer.h"
30 #include <system.h>
32 /* Main allocator */
33 static off_t mem_ptr;
34 static size_t bufsize;
35 static unsigned char* mallocbuf;
37 /* libmpeg2 allocator */
38 static off_t mpeg2_mem_ptr SHAREDBSS_ATTR;
39 static size_t mpeg2_bufsize SHAREDBSS_ATTR;
40 static unsigned char *mpeg2_mallocbuf SHAREDBSS_ATTR;
41 static unsigned char *mpeg2_bufallocbuf SHAREDBSS_ATTR;
43 #if defined(DEBUG) || defined(SIMULATOR)
44 const char * mpeg_get_reason_str(int reason)
46 const char *str;
48 switch (reason)
50 case MPEG2_ALLOC_MPEG2DEC:
51 str = "MPEG2_ALLOC_MPEG2DEC";
52 break;
53 case MPEG2_ALLOC_CHUNK:
54 str = "MPEG2_ALLOC_CHUNK";
55 break;
56 case MPEG2_ALLOC_YUV:
57 str = "MPEG2_ALLOC_YUV";
58 break;
59 case MPEG2_ALLOC_CONVERT_ID:
60 str = "MPEG2_ALLOC_CONVERT_ID";
61 break;
62 case MPEG2_ALLOC_CONVERTED:
63 str = "MPEG2_ALLOC_CONVERTED";
64 break;
65 case MPEG_ALLOC_MPEG2_BUFFER:
66 str = "MPEG_ALLOC_MPEG2_BUFFER";
67 break;
68 case MPEG_ALLOC_AUDIOBUF:
69 str = "MPEG_ALLOC_AUDIOBUF";
70 break;
71 case MPEG_ALLOC_PCMOUT:
72 str = "MPEG_ALLOC_PCMOUT";
73 break;
74 case MPEG_ALLOC_DISKBUF:
75 str = "MPEG_ALLOC_DISKBUF";
76 break;
77 case MPEG_ALLOC_CODEC_MALLOC:
78 str = "MPEG_ALLOC_CODEC_MALLOC";
79 break;
80 case MPEG_ALLOC_CODEC_CALLOC:
81 str = "MPEG_ALLOC_CODEC_CALLOC";
82 break;
83 default:
84 str = "Unknown";
87 return str;
89 #endif
91 static void * mpeg_malloc_internal (unsigned char *mallocbuf,
92 off_t *mem_ptr,
93 size_t bufsize,
94 unsigned size,
95 int reason)
97 void *x;
99 DEBUGF("mpeg_alloc_internal: bs:%lu s:%u reason:%s (%d)\n",
100 bufsize, size, mpeg_get_reason_str(reason), reason);
102 if ((size_t) (*mem_ptr + size) > bufsize)
104 DEBUGF("OUT OF MEMORY\n");
105 return NULL;
108 x = &mallocbuf[*mem_ptr];
109 *mem_ptr += (size + 3) & ~3; /* Keep memory 32-bit aligned */
111 return x;
112 (void)reason;
115 void *mpeg_malloc(size_t size, mpeg2_alloc_t reason)
117 return mpeg_malloc_internal(mallocbuf, &mem_ptr, bufsize, size,
118 reason);
121 void *mpeg_malloc_all(size_t *size_out, mpeg2_alloc_t reason)
123 /* Can steal all but MIN_MEMMARGIN */
124 if (bufsize - mem_ptr < MIN_MEMMARGIN)
125 return NULL;
127 *size_out = bufsize - mem_ptr - MIN_MEMMARGIN;
128 return mpeg_malloc(*size_out, reason);
131 bool mpeg_alloc_init(unsigned char *buf, size_t mallocsize)
133 mem_ptr = 0;
134 /* Cache-align buffer or 4-byte align */
135 mallocbuf = buf;
136 bufsize = mallocsize;
137 ALIGN_BUFFER(mallocbuf, bufsize, CACHEALIGN_UP(4));
139 /* Separate allocator for video */
140 mpeg2_mem_ptr = 0;
141 mpeg2_mallocbuf = mallocbuf;
142 mpeg2_bufallocbuf = mallocbuf;
143 mpeg2_bufsize = CACHEALIGN_UP(LIBMPEG2_ALLOC_SIZE);
145 if (mpeg_malloc_internal(mallocbuf, &mem_ptr,
146 bufsize, mpeg2_bufsize,
147 MPEG_ALLOC_MPEG2_BUFFER) == NULL)
149 return false;
152 IF_COP(rb->cpucache_invalidate());
153 return true;
156 /* allocate non-dedicated buffer space which mpeg2_mem_reset will free */
157 void * mpeg2_malloc(unsigned size, mpeg2_alloc_t reason)
159 void *ptr = mpeg_malloc_internal(mpeg2_mallocbuf, &mpeg2_mem_ptr,
160 mpeg2_bufsize, size, reason);
161 /* libmpeg2 expects zero-initialized allocations */
162 if (ptr)
163 rb->memset(ptr, 0, size);
165 return ptr;
168 /* allocate dedicated buffer - memory behind buffer pointer becomes dedicated
169 so order is important */
170 void * mpeg2_bufalloc(unsigned size, mpeg2_alloc_t reason)
172 void *buf = mpeg2_malloc(size, reason);
174 if (buf == NULL)
175 return NULL;
177 mpeg2_bufallocbuf = &mpeg2_mallocbuf[mpeg2_mem_ptr];
178 return buf;
181 /* return unused buffer portion and size */
182 void * mpeg2_get_buf(size_t *size)
184 if ((size_t)mpeg2_mem_ptr + 32 >= mpeg2_bufsize)
185 return NULL;
187 *size = mpeg2_bufsize - mpeg2_mem_ptr;
188 return &mpeg2_mallocbuf[mpeg2_mem_ptr];
191 /* de-allocate all non-dedicated buffer space */
192 void mpeg2_mem_reset(void)
194 DEBUGF("mpeg2_mem_reset\n");
195 mpeg2_mem_ptr = mpeg2_bufallocbuf - mpeg2_mallocbuf;
198 /* The following are expected by libmad */
199 void * codec_malloc(size_t size)
201 void* ptr;
203 ptr = mpeg_malloc_internal(mallocbuf, &mem_ptr,
204 bufsize, size, MPEG_ALLOC_CODEC_MALLOC);
206 if (ptr)
207 rb->memset(ptr,0,size);
209 return ptr;
212 void * codec_calloc(size_t nmemb, size_t size)
214 void* ptr;
216 ptr = mpeg_malloc_internal(mallocbuf, &mem_ptr,
217 bufsize, nmemb*size,
218 MPEG_ALLOC_CODEC_CALLOC);
220 if (ptr)
221 rb->memset(ptr,0,size);
223 return ptr;
226 void codec_free(void* ptr)
228 DEBUGF("codec_free - %p\n", ptr);
229 #if 0
230 mem_ptr = (void *)ptr - (void *)mallocbuf;
231 #endif
232 (void)ptr;