Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
alloc.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "config.h"
00025
00026 #include <stdlib.h>
00027 #include <inttypes.h>
00028
00029 #include "mpeg2.h"
00030 #include "mpeg2_internal.h"
00031
00032 #if defined(HAVE_MEMALIGN) && !defined(__cplusplus)
00033
00034 void * memalign (size_t align, size_t size);
00035 #endif
00036
00037 void * (* mpeg2_malloc_hook) (int size, int reason) = NULL;
00038 int (* mpeg2_free_hook) (void * buf) = NULL;
00039
00040 void * mpeg2_malloc (int size, int reason)
00041 {
00042 char * buf;
00043
00044 if (mpeg2_malloc_hook) {
00045 buf = (char *) mpeg2_malloc_hook (size, reason);
00046 if (buf)
00047 return buf;
00048 }
00049
00050 #if defined(HAVE_MEMALIGN) && !defined(__cplusplus) && !defined(DEBUG)
00051 return memalign (16, size);
00052 #else
00053 buf = (char *) malloc (size + 15 + sizeof (void **));
00054 if (buf) {
00055 char * align_buf;
00056
00057 align_buf = buf + 15 + sizeof (void **);
00058 align_buf -= (long)align_buf & 15;
00059 *(((void **)align_buf) - 1) = buf;
00060 return align_buf;
00061 }
00062 return NULL;
00063 #endif
00064 }
00065
00066 void mpeg2_free (void * buf)
00067 {
00068 if (mpeg2_free_hook && mpeg2_free_hook (buf))
00069 return;
00070
00071 #if defined(HAVE_MEMALIGN) && !defined(__cplusplus) && !defined(DEBUG)
00072 free (buf);
00073 #else
00074 free (*(((void **)buf) - 1));
00075 #endif
00076 }