comparison mem.c @ 603:880c6441f56a libavutil

Change semantic of CONFIG_*, HAVE_* and ARCH_*. They are now always defined to either 0 or 1.
author aurel
date Tue, 13 Jan 2009 23:44:16 +0000
parents b4abc96d1610
children ed203c477511
comparison
equal deleted inserted replaced
602:0b84593767d8 603:880c6441f56a
30 #undef malloc 30 #undef malloc
31 #undef free 31 #undef free
32 #undef realloc 32 #undef realloc
33 33
34 #include <stdlib.h> 34 #include <stdlib.h>
35 #ifdef HAVE_MALLOC_H 35 #if HAVE_MALLOC_H
36 #include <malloc.h> 36 #include <malloc.h>
37 #endif 37 #endif
38 38
39 /* you can redefine av_malloc and av_free in your project to use your 39 /* you can redefine av_malloc and av_free in your project to use your
40 memory allocator. You do not need to suppress this file because the 40 memory allocator. You do not need to suppress this file because the
41 linker will do it automatically */ 41 linker will do it automatically */
42 42
43 void *av_malloc(unsigned int size) 43 void *av_malloc(unsigned int size)
44 { 44 {
45 void *ptr = NULL; 45 void *ptr = NULL;
46 #ifdef CONFIG_MEMALIGN_HACK 46 #if CONFIG_MEMALIGN_HACK
47 long diff; 47 long diff;
48 #endif 48 #endif
49 49
50 /* let's disallow possible ambiguous cases */ 50 /* let's disallow possible ambiguous cases */
51 if(size > (INT_MAX-16) ) 51 if(size > (INT_MAX-16) )
52 return NULL; 52 return NULL;
53 53
54 #ifdef CONFIG_MEMALIGN_HACK 54 #if CONFIG_MEMALIGN_HACK
55 ptr = malloc(size+16); 55 ptr = malloc(size+16);
56 if(!ptr) 56 if(!ptr)
57 return ptr; 57 return ptr;
58 diff= ((-(long)ptr - 1)&15) + 1; 58 diff= ((-(long)ptr - 1)&15) + 1;
59 ptr = (char*)ptr + diff; 59 ptr = (char*)ptr + diff;
60 ((char*)ptr)[-1]= diff; 60 ((char*)ptr)[-1]= diff;
61 #elif defined (HAVE_POSIX_MEMALIGN) 61 #elif HAVE_POSIX_MEMALIGN
62 posix_memalign(&ptr,16,size); 62 posix_memalign(&ptr,16,size);
63 #elif defined (HAVE_MEMALIGN) 63 #elif HAVE_MEMALIGN
64 ptr = memalign(16,size); 64 ptr = memalign(16,size);
65 /* Why 64? 65 /* Why 64?
66 Indeed, we should align it: 66 Indeed, we should align it:
67 on 4 for 386 67 on 4 for 386
68 on 16 for 486 68 on 16 for 486
94 return ptr; 94 return ptr;
95 } 95 }
96 96
97 void *av_realloc(void *ptr, unsigned int size) 97 void *av_realloc(void *ptr, unsigned int size)
98 { 98 {
99 #ifdef CONFIG_MEMALIGN_HACK 99 #if CONFIG_MEMALIGN_HACK
100 int diff; 100 int diff;
101 #endif 101 #endif
102 102
103 /* let's disallow possible ambiguous cases */ 103 /* let's disallow possible ambiguous cases */
104 if(size > (INT_MAX-16) ) 104 if(size > (INT_MAX-16) )
105 return NULL; 105 return NULL;
106 106
107 #ifdef CONFIG_MEMALIGN_HACK 107 #if CONFIG_MEMALIGN_HACK
108 //FIXME this isn't aligned correctly, though it probably isn't needed 108 //FIXME this isn't aligned correctly, though it probably isn't needed
109 if(!ptr) return av_malloc(size); 109 if(!ptr) return av_malloc(size);
110 diff= ((char*)ptr)[-1]; 110 diff= ((char*)ptr)[-1];
111 return (char*)realloc((char*)ptr - diff, size + diff) + diff; 111 return (char*)realloc((char*)ptr - diff, size + diff) + diff;
112 #else 112 #else
116 116
117 void av_free(void *ptr) 117 void av_free(void *ptr)
118 { 118 {
119 /* XXX: this test should not be needed on most libcs */ 119 /* XXX: this test should not be needed on most libcs */
120 if (ptr) 120 if (ptr)
121 #ifdef CONFIG_MEMALIGN_HACK 121 #if CONFIG_MEMALIGN_HACK
122 free((char*)ptr - ((char*)ptr)[-1]); 122 free((char*)ptr - ((char*)ptr)[-1]);
123 #else 123 #else
124 free(ptr); 124 free(ptr);
125 #endif 125 #endif
126 } 126 }