]> git.sesse.net Git - vlc/blob - modules/codec/xvmc/alloc.c
081a4b6d34b4cc9dec3d154ee9989eca19cc739b
[vlc] / modules / codec / xvmc / alloc.c
1 /* $Id$
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>
5  *
6  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7  * See http://libmpeg2.sourceforge.net/ for updates.
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <inttypes.h>
25
26 #include "mpeg2.h"
27
28 static void * (* malloc_hook) (unsigned size, mpeg2_alloc_t reason) = NULL;
29 static int (* free_hook) (void * buf) = NULL;
30
31 void * mpeg2_malloc( unsigned size, mpeg2_alloc_t reason )
32 {
33     char *buf = NULL;
34
35     if (malloc_hook)
36     {
37         buf = (char *) malloc_hook (size, reason);
38         if (buf)
39             return buf;
40     }
41
42     if (size)
43     {
44         buf = (char *) malloc (size + 63 + sizeof (void **));
45         if (buf)
46         {
47             char * align_buf = NULL;
48
49             align_buf = buf + 63 + sizeof (void **);
50             align_buf -= (long)align_buf & 63;
51             *(((void **)align_buf) - 1) = buf;
52             return align_buf;
53         }
54     }
55     return NULL;
56 }
57
58 void mpeg2_free( void * buf )
59 {
60     if (free_hook && free_hook (buf))
61         return;
62
63     if (buf)
64         free (*(((void **)buf) - 1));
65 }
66
67 void mpeg2_malloc_hooks( void * malloc (unsigned, mpeg2_alloc_t),
68                          int free (void *) )
69 {
70     malloc_hook = malloc;
71     free_hook = free;
72 }