ffmpeg / libavutil / mem.c @ d71ad089
History | View | Annotate | Download (3.99 KB)
1 | d01fe86d | Fabrice Bellard | /*
|
---|---|---|---|
2 | cea8f6f3 | Luca Abeni | * default memory allocator for libavutil
|
3 | 406792e7 | Diego Biurrun | * Copyright (c) 2002 Fabrice Bellard
|
4 | d01fe86d | Fabrice Bellard | *
|
5 | b78e7197 | Diego Biurrun | * This file is part of FFmpeg.
|
6 | *
|
||
7 | * FFmpeg is free software; you can redistribute it and/or
|
||
8 | d01fe86d | Fabrice Bellard | * modify it under the terms of the GNU Lesser General Public
|
9 | * License as published by the Free Software Foundation; either
|
||
10 | b78e7197 | Diego Biurrun | * version 2.1 of the License, or (at your option) any later version.
|
11 | d01fe86d | Fabrice Bellard | *
|
12 | b78e7197 | Diego Biurrun | * FFmpeg is distributed in the hope that it will be useful,
|
13 | d01fe86d | Fabrice Bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
15 | * Lesser General Public License for more details.
|
||
16 | *
|
||
17 | * You should have received a copy of the GNU Lesser General Public
|
||
18 | b78e7197 | Diego Biurrun | * License along with FFmpeg; if not, write to the Free Software
|
19 | 5509bffa | Diego Biurrun | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
20 | d01fe86d | Fabrice Bellard | */
|
21 | 115329f1 | Diego Biurrun | |
22 | 983e3246 | Michael Niedermayer | /**
|
23 | bad5537e | Diego Biurrun | * @file libavutil/mem.c
|
24 | 89c9ff50 | Diego Biurrun | * default memory allocator for libavutil
|
25 | 983e3246 | Michael Niedermayer | */
|
26 | 115329f1 | Diego Biurrun | |
27 | dfcb6b56 | Diego Biurrun | #include "config.h" |
28 | 8e1e6f31 | Fabrice Bellard | |
29 | dfcb6b56 | Diego Biurrun | #include <limits.h> |
30 | 1f91cdce | Ramiro Polla | #include <stdlib.h> |
31 | dfcb6b56 | Diego Biurrun | #include <string.h> |
32 | b250f9c6 | Aurelien Jacobs | #if HAVE_MALLOC_H
|
33 | d01fe86d | Fabrice Bellard | #include <malloc.h> |
34 | #endif
|
||
35 | |||
36 | 77652a6a | Diego Biurrun | #include "mem.h" |
37 | |||
38 | 89c9ff50 | Diego Biurrun | /* here we can use OS-dependent allocation functions */
|
39 | 1e60e933 | Diego Biurrun | #undef free
|
40 | #undef malloc
|
||
41 | #undef realloc
|
||
42 | |||
43 | 89c9ff50 | Diego Biurrun | /* You can redefine av_malloc and av_free in your project to use your
|
44 | d01fe86d | Fabrice Bellard | memory allocator. You do not need to suppress this file because the
|
45 | 89c9ff50 | Diego Biurrun | linker will do it automatically. */
|
46 | d01fe86d | Fabrice Bellard | |
47 | 18f77016 | Zdenek Kabelac | void *av_malloc(unsigned int size) |
48 | d01fe86d | Fabrice Bellard | { |
49 | 1f91cdce | Ramiro Polla | void *ptr = NULL; |
50 | b250f9c6 | Aurelien Jacobs | #if CONFIG_MEMALIGN_HACK
|
51 | ed96aeea | Dieter | long diff;
|
52 | 88730be6 | Måns Rullgård | #endif
|
53 | 0ecca7a4 | Michael Niedermayer | |
54 | 7d77d5f6 | Diego Biurrun | /* let's disallow possible ambiguous cases */
|
55 | 0a7c36af | Michael Niedermayer | if(size > (INT_MAX-16) ) |
56 | 0ecca7a4 | Michael Niedermayer | return NULL; |
57 | 115329f1 | Diego Biurrun | |
58 | b250f9c6 | Aurelien Jacobs | #if CONFIG_MEMALIGN_HACK
|
59 | a9493601 | Herve W | ptr = malloc(size+16);
|
60 | if(!ptr)
|
||
61 | return ptr;
|
||
62 | ed96aeea | Dieter | diff= ((-(long)ptr - 1)&15) + 1; |
63 | 90d30570 | avcoder | ptr = (char*)ptr + diff;
|
64 | da9b170c | Michael Niedermayer | ((char*)ptr)[-1]= diff; |
65 | b250f9c6 | Aurelien Jacobs | #elif HAVE_POSIX_MEMALIGN
|
66 | a90de11d | Patrik Kullman | if (posix_memalign(&ptr,16,size)) |
67 | ptr = NULL;
|
||
68 | b250f9c6 | Aurelien Jacobs | #elif HAVE_MEMALIGN
|
69 | 8f2b21a8 | Michael Niedermayer | ptr = memalign(16,size);
|
70 | 115329f1 | Diego Biurrun | /* Why 64?
|
71 | d01fe86d | Fabrice Bellard | Indeed, we should align it:
|
72 | on 4 for 386
|
||
73 | on 16 for 486
|
||
74 | 89c9ff50 | Diego Biurrun | on 32 for 586, PPro - K6-III
|
75 | bb270c08 | Diego Biurrun | on 64 for K7 (maybe for P3 too).
|
76 | d01fe86d | Fabrice Bellard | Because L1 and L2 caches are aligned on those values.
|
77 | But I don't want to code such logic here!
|
||
78 | */
|
||
79 | 8f2b21a8 | Michael Niedermayer | /* Why 16?
|
80 | 7ce68923 | Diego Biurrun | Because some CPUs need alignment, for example SSE2 on P4, & most RISC CPUs
|
81 | 8f2b21a8 | Michael Niedermayer | it will just trigger an exception and the unaligned load will be done in the
|
82 | 89c9ff50 | Diego Biurrun | exception handler or it will just segfault (SSE2 on P4).
|
83 | 2cab6401 | Diego Biurrun | Why not larger? Because I did not see a difference in benchmarks ...
|
84 | 8f2b21a8 | Michael Niedermayer | */
|
85 | 89c9ff50 | Diego Biurrun | /* benchmarks with P3
|
86 | bb270c08 | Diego Biurrun | memalign(64)+1 3071,3051,3032
|
87 | memalign(64)+2 3051,3032,3041
|
||
88 | memalign(64)+4 2911,2896,2915
|
||
89 | memalign(64)+8 2545,2554,2550
|
||
90 | memalign(64)+16 2543,2572,2563
|
||
91 | memalign(64)+32 2546,2545,2571
|
||
92 | memalign(64)+64 2570,2533,2558
|
||
93 | 115329f1 | Diego Biurrun | |
94 | 89c9ff50 | Diego Biurrun | BTW, malloc seems to do 8-byte alignment by default here.
|
95 | 8f2b21a8 | Michael Niedermayer | */
|
96 | d01fe86d | Fabrice Bellard | #else
|
97 | ptr = malloc(size); |
||
98 | #endif
|
||
99 | return ptr;
|
||
100 | } |
||
101 | |||
102 | 8e1e6f31 | Fabrice Bellard | void *av_realloc(void *ptr, unsigned int size) |
103 | { |
||
104 | b250f9c6 | Aurelien Jacobs | #if CONFIG_MEMALIGN_HACK
|
105 | 0a7c36af | Michael Niedermayer | int diff;
|
106 | #endif
|
||
107 | 88730be6 | Måns Rullgård | |
108 | 7d77d5f6 | Diego Biurrun | /* let's disallow possible ambiguous cases */
|
109 | a9493601 | Herve W | if(size > (INT_MAX-16) ) |
110 | 0ecca7a4 | Michael Niedermayer | return NULL; |
111 | |||
112 | b250f9c6 | Aurelien Jacobs | #if CONFIG_MEMALIGN_HACK
|
113 | 0a7c36af | Michael Niedermayer | //FIXME this isn't aligned correctly, though it probably isn't needed
|
114 | if(!ptr) return av_malloc(size); |
||
115 | diff= ((char*)ptr)[-1]; |
||
116 | 90d30570 | avcoder | return (char*)realloc((char*)ptr - diff, size + diff) + diff; |
117 | 0a7c36af | Michael Niedermayer | #else
|
118 | return realloc(ptr, size);
|
||
119 | da9b170c | Michael Niedermayer | #endif
|
120 | b7a22d84 | Michael Niedermayer | } |
121 | |||
122 | d01fe86d | Fabrice Bellard | void av_free(void *ptr) |
123 | { |
||
124 | /* XXX: this test should not be needed on most libcs */
|
||
125 | if (ptr)
|
||
126 | b250f9c6 | Aurelien Jacobs | #if CONFIG_MEMALIGN_HACK
|
127 | 90d30570 | avcoder | free((char*)ptr - ((char*)ptr)[-1]); |
128 | da9b170c | Michael Niedermayer | #else
|
129 | d01fe86d | Fabrice Bellard | free(ptr); |
130 | da9b170c | Michael Niedermayer | #endif
|
131 | d01fe86d | Fabrice Bellard | } |
132 | |||
133 | 79e47000 | Luca Barbato | void av_freep(void *arg) |
134 | { |
||
135 | void **ptr= (void**)arg; |
||
136 | av_free(*ptr); |
||
137 | *ptr = NULL;
|
||
138 | } |
||
139 | |||
140 | void *av_mallocz(unsigned int size) |
||
141 | { |
||
142 | 11362767 | Michael Niedermayer | void *ptr = av_malloc(size);
|
143 | 79e47000 | Luca Barbato | if (ptr)
|
144 | memset(ptr, 0, size);
|
||
145 | return ptr;
|
||
146 | } |
||
147 | |||
148 | char *av_strdup(const char *s) |
||
149 | { |
||
150 | fdf35f26 | Michael Niedermayer | char *ptr= NULL; |
151 | if(s){
|
||
152 | 19757f61 | Michael Niedermayer | int len = strlen(s) + 1; |
153 | ptr = av_malloc(len); |
||
154 | if (ptr)
|
||
155 | memcpy(ptr, s, len); |
||
156 | fdf35f26 | Michael Niedermayer | } |
157 | 79e47000 | Luca Barbato | return ptr;
|
158 | } |