annotate mem.c @ 82:8fb151c4d4c7 libavutil

Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
author lucabe
date Wed, 19 Jul 2006 07:28:58 +0000
parents
children 7ac2b9550c6b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
82
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
1 /*
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
2 * default memory allocator for libavutil
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
3 * Copyright (c) 2002 Fabrice Bellard.
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
4 *
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
9 *
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
13 * Lesser General Public License for more details.
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
14 *
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
18 */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
19
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
20 /**
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
21 * @file mem.c
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
22 * default memory allocator for libavutil.
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
23 */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
24
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
25 #include "common.h"
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
26
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
27 /* here we can use OS dependant allocation functions */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
28 #undef malloc
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
29 #undef free
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
30 #undef realloc
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
31
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
32 #ifdef HAVE_MALLOC_H
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
33 #include <malloc.h>
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
34 #endif
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
35
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
36 /* you can redefine av_malloc and av_free in your project to use your
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
37 memory allocator. You do not need to suppress this file because the
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
38 linker will do it automatically */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
39
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
40 /**
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
41 * Memory allocation of size byte with alignment suitable for all
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
42 * memory accesses (including vectors if available on the
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
43 * CPU). av_malloc(0) must return a non NULL pointer.
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
44 */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
45 void *av_malloc(unsigned int size)
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
46 {
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
47 void *ptr;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
48 #ifdef MEMALIGN_HACK
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
49 long diff;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
50 #endif
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
51
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
52 /* let's disallow possible ambiguous cases */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
53 if(size > (INT_MAX-16) )
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
54 return NULL;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
55
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
56 #ifdef MEMALIGN_HACK
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
57 ptr = malloc(size+16);
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
58 if(!ptr)
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
59 return ptr;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
60 diff= ((-(long)ptr - 1)&15) + 1;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
61 ptr += diff;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
62 ((char*)ptr)[-1]= diff;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
63 #elif defined (HAVE_MEMALIGN)
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
64 ptr = memalign(16,size);
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
65 /* Why 64?
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
66 Indeed, we should align it:
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
67 on 4 for 386
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
68 on 16 for 486
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
69 on 32 for 586, PPro - k6-III
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
70 on 64 for K7 (maybe for P3 too).
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
71 Because L1 and L2 caches are aligned on those values.
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
72 But I don't want to code such logic here!
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
73 */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
74 /* Why 16?
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
75 because some cpus need alignment, for example SSE2 on P4, & most RISC cpus
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
76 it will just trigger an exception and the unaligned load will be done in the
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
77 exception handler or it will just segfault (SSE2 on P4)
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
78 Why not larger? because i didnt see a difference in benchmarks ...
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
79 */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
80 /* benchmarks with p3
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
81 memalign(64)+1 3071,3051,3032
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
82 memalign(64)+2 3051,3032,3041
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
83 memalign(64)+4 2911,2896,2915
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
84 memalign(64)+8 2545,2554,2550
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
85 memalign(64)+16 2543,2572,2563
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
86 memalign(64)+32 2546,2545,2571
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
87 memalign(64)+64 2570,2533,2558
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
88
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
89 btw, malloc seems to do 8 byte alignment by default here
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
90 */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
91 #else
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
92 ptr = malloc(size);
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
93 #endif
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
94 return ptr;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
95 }
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
96
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
97 /**
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
98 * av_realloc semantics (same as glibc): if ptr is NULL and size > 0,
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
99 * identical to malloc(size). If size is zero, it is identical to
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
100 * free(ptr) and NULL is returned.
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
101 */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
102 void *av_realloc(void *ptr, unsigned int size)
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
103 {
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
104 #ifdef MEMALIGN_HACK
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
105 int diff;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
106 #endif
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
107
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
108 /* let's disallow possible ambiguous cases */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
109 if(size > (INT_MAX-16) )
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
110 return NULL;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
111
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
112 #ifdef MEMALIGN_HACK
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
113 //FIXME this isn't aligned correctly, though it probably isn't needed
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
114 if(!ptr) return av_malloc(size);
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
115 diff= ((char*)ptr)[-1];
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
116 return realloc(ptr - diff, size + diff) + diff;
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
117 #else
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
118 return realloc(ptr, size);
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
119 #endif
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
120 }
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
121
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
122 /**
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
123 * Free memory which has been allocated with av_malloc(z)() or av_realloc().
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
124 * NOTE: ptr = NULL is explicetly allowed
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
125 * Note2: it is recommended that you use av_freep() instead
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
126 */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
127 void av_free(void *ptr)
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
128 {
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
129 /* XXX: this test should not be needed on most libcs */
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
130 if (ptr)
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
131 #ifdef MEMALIGN_HACK
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
132 free(ptr - ((char*)ptr)[-1]);
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
133 #else
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
134 free(ptr);
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
135 #endif
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
136 }
8fb151c4d4c7 Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff changeset
137