Mercurial > libavutil.hg
annotate mem.c @ 525:0d4beab5e3c9 libavutil
intreadwrite: support DEC compiler __unaligned type qualifier
author | mru |
---|---|
date | Fri, 18 Jul 2008 01:18:59 +0000 |
parents | 8e2f8b81f59f |
children | b4abc96d1610 |
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 * |
116
d76a36742464
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
113
diff
changeset
|
5 * This file is part of FFmpeg. |
d76a36742464
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
113
diff
changeset
|
6 * |
d76a36742464
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
113
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
8 * 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
|
9 * License as published by the Free Software Foundation; either |
116
d76a36742464
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
113
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
11 * |
116
d76a36742464
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
113
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
13 * 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
|
14 * 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
|
15 * Lesser General Public License for more details. |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
16 * |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
116
d76a36742464
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
113
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
19 * 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
|
20 */ |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
21 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
22 /** |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
23 * @file mem.c |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
24 * default memory allocator for libavutil. |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
25 */ |
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 #include "common.h" |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
28 |
249
d1e99b40b1c9
cosmetics: Fix another common typo, dependAnt --> dependEnt.
diego
parents:
135
diff
changeset
|
29 /* here we can use OS dependent allocation functions */ |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
30 #undef malloc |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
31 #undef free |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
32 #undef realloc |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
33 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
34 #ifdef HAVE_MALLOC_H |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
35 #include <malloc.h> |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
36 #endif |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
37 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
38 /* 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
|
39 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
|
40 linker will do it automatically */ |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
41 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
42 void *av_malloc(unsigned int size) |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
43 { |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
44 void *ptr; |
135
ebc5bc38c1b5
rename MEMALIGN_HACK to CONFIG_MEMALIGN_HACK, use common code in configure
mru
parents:
116
diff
changeset
|
45 #ifdef CONFIG_MEMALIGN_HACK |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
46 long diff; |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
47 #endif |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
48 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
49 /* let's disallow possible ambiguous cases */ |
96
7d343463873b
revert aligned realloc() changesm this should be identical to r5784
michael
parents:
95
diff
changeset
|
50 if(size > (INT_MAX-16) ) |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
51 return NULL; |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
52 |
135
ebc5bc38c1b5
rename MEMALIGN_HACK to CONFIG_MEMALIGN_HACK, use common code in configure
mru
parents:
116
diff
changeset
|
53 #ifdef CONFIG_MEMALIGN_HACK |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
54 ptr = malloc(size+16); |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
55 if(!ptr) |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
56 return ptr; |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
57 diff= ((-(long)ptr - 1)&15) + 1; |
451 | 58 ptr = (char*)ptr + diff; |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
59 ((char*)ptr)[-1]= diff; |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
60 #elif defined (HAVE_MEMALIGN) |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
61 ptr = memalign(16,size); |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
62 /* Why 64? |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
63 Indeed, we should align it: |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
64 on 4 for 386 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
65 on 16 for 486 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
66 on 32 for 586, PPro - k6-III |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 */ |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
71 /* Why 16? |
466 | 72 Because some CPUs need alignment, for example SSE2 on P4, & most RISC CPUs |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
73 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
|
74 exception handler or it will just segfault (SSE2 on P4) |
467 | 75 Why not larger? Because I did not see a difference in benchmarks ... |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
76 */ |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
77 /* benchmarks with p3 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
78 memalign(64)+1 3071,3051,3032 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
79 memalign(64)+2 3051,3032,3041 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
80 memalign(64)+4 2911,2896,2915 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
81 memalign(64)+8 2545,2554,2550 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
82 memalign(64)+16 2543,2572,2563 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
83 memalign(64)+32 2546,2545,2571 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
84 memalign(64)+64 2570,2533,2558 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
85 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
86 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
|
87 */ |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
88 #else |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
89 ptr = malloc(size); |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
90 #endif |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
91 return ptr; |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
92 } |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
93 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
94 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
|
95 { |
135
ebc5bc38c1b5
rename MEMALIGN_HACK to CONFIG_MEMALIGN_HACK, use common code in configure
mru
parents:
116
diff
changeset
|
96 #ifdef CONFIG_MEMALIGN_HACK |
96
7d343463873b
revert aligned realloc() changesm this should be identical to r5784
michael
parents:
95
diff
changeset
|
97 int diff; |
7d343463873b
revert aligned realloc() changesm this should be identical to r5784
michael
parents:
95
diff
changeset
|
98 #endif |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
99 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
100 /* let's disallow possible ambiguous cases */ |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
101 if(size > (INT_MAX-16) ) |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
102 return NULL; |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
103 |
135
ebc5bc38c1b5
rename MEMALIGN_HACK to CONFIG_MEMALIGN_HACK, use common code in configure
mru
parents:
116
diff
changeset
|
104 #ifdef CONFIG_MEMALIGN_HACK |
96
7d343463873b
revert aligned realloc() changesm this should be identical to r5784
michael
parents:
95
diff
changeset
|
105 //FIXME this isn't aligned correctly, though it probably isn't needed |
7d343463873b
revert aligned realloc() changesm this should be identical to r5784
michael
parents:
95
diff
changeset
|
106 if(!ptr) return av_malloc(size); |
7d343463873b
revert aligned realloc() changesm this should be identical to r5784
michael
parents:
95
diff
changeset
|
107 diff= ((char*)ptr)[-1]; |
451 | 108 return (char*)realloc((char*)ptr - diff, size + diff) + diff; |
96
7d343463873b
revert aligned realloc() changesm this should be identical to r5784
michael
parents:
95
diff
changeset
|
109 #else |
7d343463873b
revert aligned realloc() changesm this should be identical to r5784
michael
parents:
95
diff
changeset
|
110 return realloc(ptr, size); |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
111 #endif |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
112 } |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
113 |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
114 void av_free(void *ptr) |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
115 { |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
116 /* 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
|
117 if (ptr) |
135
ebc5bc38c1b5
rename MEMALIGN_HACK to CONFIG_MEMALIGN_HACK, use common code in configure
mru
parents:
116
diff
changeset
|
118 #ifdef CONFIG_MEMALIGN_HACK |
451 | 119 free((char*)ptr - ((char*)ptr)[-1]); |
82
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
120 #else |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
121 free(ptr); |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
122 #endif |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
123 } |
8fb151c4d4c7
Move av_malloc(), av_realloc(), and av_free() from libavcodec to libavutil
lucabe
parents:
diff
changeset
|
124 |
113 | 125 void av_freep(void *arg) |
126 { | |
127 void **ptr= (void**)arg; | |
128 av_free(*ptr); | |
129 *ptr = NULL; | |
130 } | |
131 | |
132 void *av_mallocz(unsigned int size) | |
133 { | |
488 | 134 void *ptr = av_malloc(size); |
113 | 135 if (ptr) |
136 memset(ptr, 0, size); | |
137 return ptr; | |
138 } | |
139 | |
140 char *av_strdup(const char *s) | |
141 { | |
508 | 142 char *ptr= NULL; |
143 if(s){ | |
509 | 144 int len = strlen(s) + 1; |
145 ptr = av_malloc(len); | |
146 if (ptr) | |
147 memcpy(ptr, s, len); | |
508 | 148 } |
113 | 149 return ptr; |
150 } | |
151 |