Get the plugins synced up with the threading changes.
[kugel-rb.git] / apps / plugins / mpegplayer / alloc.c
blobb8f68458c07289391234e170ea319d06706b2634
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
24 #include "plugin.h"
26 #include "mpeg2.h"
28 extern struct plugin_api* rb;
30 /* Main allocator */
31 static off_t mem_ptr;
32 static size_t bufsize;
33 static unsigned char* mallocbuf;
35 /* libmpeg2 allocator */
36 static off_t mpeg2_mem_ptr;
37 static size_t mpeg2_bufsize;
38 static unsigned char *mpeg2_mallocbuf;
40 static void * mpeg_malloc_internal (unsigned char *mallocbuf,
41 off_t *mem_ptr,
42 size_t bufsize,
43 unsigned size,
44 int reason)
46 void *x;
48 if (*mem_ptr + size > bufsize)
50 DEBUGF("OUT OF MEMORY\n");
51 return NULL;
54 x = &mallocbuf[*mem_ptr];
55 *mem_ptr += (size + 3) & ~3; /* Keep memory 32-bit aligned */
57 return x;
58 (void)reason;
61 void *mpeg_malloc(size_t size, mpeg2_alloc_t reason)
63 return mpeg_malloc_internal(mallocbuf, &mem_ptr, bufsize, size,
64 reason);
67 size_t mpeg_alloc_init(unsigned char *buf, size_t mallocsize,
68 size_t libmpeg2size)
70 mem_ptr = 0;
71 bufsize = mallocsize;
72 /* Line-align buffer */
73 mallocbuf = (char *)(((intptr_t)buf + 15) & ~15);
74 /* Adjust for real size */
75 bufsize -= mallocbuf - buf;
76 rb->memset(buf,0,bufsize);
78 /* Separate allocator for video */
79 libmpeg2size = (libmpeg2size + 15) & ~15;
80 if (mpeg_malloc_internal(mallocbuf, &mem_ptr,
81 bufsize, libmpeg2size, 0) == NULL)
83 return 0;
86 mpeg2_mallocbuf = mallocbuf;
87 mpeg2_mem_ptr = 0;
88 mpeg2_bufsize = libmpeg2size;
90 #if NUM_CORES > 1
91 flush_icache();
92 #endif
94 return bufsize - mpeg2_bufsize;
97 /* gcc may want to use memcpy before rb is initialised, so here's a trivial
98 implementation */
100 void *memcpy(void *dest, const void *src, size_t n) {
101 size_t i;
102 char* d=(char*)dest;
103 char* s=(char*)src;
105 for (i=0;i<n;i++)
106 d[i]=s[i];
108 return dest;
111 void * mpeg2_malloc(unsigned size, mpeg2_alloc_t reason)
113 return mpeg_malloc_internal(mpeg2_mallocbuf, &mpeg2_mem_ptr,
114 mpeg2_bufsize, size, reason);
117 void mpeg2_free(void *ptr)
119 (void)ptr;
122 /* The following are expected by libmad */
123 void * codec_malloc(size_t size)
125 return mpeg_malloc_internal(mallocbuf, &mem_ptr,
126 bufsize, size, -3);
129 void * codec_calloc(size_t nmemb, size_t size)
131 void* ptr;
133 ptr = mpeg_malloc_internal(mallocbuf, &mem_ptr,
134 bufsize, nmemb*size, -3);
136 if (ptr)
137 rb->memset(ptr,0,size);
139 return ptr;
142 void codec_free(void* ptr)
144 (void)ptr;
147 void *memmove(void *dest, const void *src, size_t n)
149 return rb->memmove(dest,src,n);
152 void *memset(void *s, int c, size_t n)
154 return rb->memset(s,c,n);
157 void abort(void)
159 rb->lcd_putsxy(0,0,"ABORT!");
160 rb->lcd_update();
162 while (1);
163 /* Let's hope this is never called */