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
28 extern struct plugin_api
* rb
;
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
,
48 if (*mem_ptr
+ size
> bufsize
)
50 DEBUGF("OUT OF MEMORY\n");
54 x
= &mallocbuf
[*mem_ptr
];
55 *mem_ptr
+= (size
+ 3) & ~3; /* Keep memory 32-bit aligned */
61 void *mpeg_malloc(size_t size
, mpeg2_alloc_t reason
)
63 return mpeg_malloc_internal(mallocbuf
, &mem_ptr
, bufsize
, size
,
67 size_t mpeg_alloc_init(unsigned char *buf
, size_t 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
)
86 mpeg2_mallocbuf
= mallocbuf
;
88 mpeg2_bufsize
= libmpeg2size
;
94 return bufsize
- mpeg2_bufsize
;
97 /* gcc may want to use memcpy before rb is initialised, so here's a trivial
100 void *memcpy(void *dest
, const void *src
, size_t n
) {
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
)
122 /* The following are expected by libmad */
123 void * codec_malloc(size_t size
)
125 return mpeg_malloc_internal(mallocbuf
, &mem_ptr
,
129 void * codec_calloc(size_t nmemb
, size_t size
)
133 ptr
= mpeg_malloc_internal(mallocbuf
, &mem_ptr
,
134 bufsize
, nmemb
*size
, -3);
137 rb
->memset(ptr
,0,size
);
142 void codec_free(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
);
159 rb
->lcd_putsxy(0,0,"ABORT!");
163 /* Let's hope this is never called */