| 1 |
|
| /* |
| 2 |
|
| * default memory allocator for libavcodec |
| 3 |
|
| * Copyright (c) 2002 Fabrice Bellard. |
| 4 |
|
| * |
| 5 |
|
| * This library is free software; you can redistribute it and/or |
| 6 |
|
| * modify it under the terms of the GNU Lesser General Public |
| 7 |
|
| * License as published by the Free Software Foundation; either |
| 8 |
|
| * version 2 of the License, or (at your option) any later version. |
| 9 |
|
| * |
| 10 |
|
| * This library is distributed in the hope that it will be useful, |
| 11 |
|
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
|
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
|
| * Lesser General Public License for more details. |
| 14 |
|
| * |
| 15 |
|
| * You should have received a copy of the GNU Lesser General Public |
| 16 |
|
| * License along with this library; if not, write to the Free Software |
| 17 |
|
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
|
| */ |
| 19 |
|
| |
| 20 |
|
| |
| 21 |
|
| typedef struct a a; |
| 22 |
|
| |
| 23 |
|
| struct a { |
| 24 |
|
| int b,c; |
| 25 |
|
| a *d; |
| 26 |
|
| }; |
| 27 |
|
| |
| 28 |
|
| |
| 29 |
|
| /** |
| 30 |
|
| * @file mem.c |
| 31 |
|
| * default memory allocator for libavcodec. |
| 32 |
|
| */ |
| 33 |
|
| |
| 34 |
|
| #include "avcodec.h" |
| 35 |
|
| |
| 36 |
|
| /* here we can use OS dependant allocation functions */ |
| 37 |
|
| #undef malloc |
| 38 |
|
| #undef free |
| 39 |
|
| #undef realloc |
| 40 |
|
| |
| 41 |
|
| #ifdef HAVE_MALLOC_H |
| 42 |
|
| #include <malloc.h> |
| 43 |
|
| #endif |
| 44 |
|
| |
| 45 |
|
| /* you can redefine av_malloc and av_free in your project to use your |
| 46 |
|
| memory allocator. You do not need to suppress this file because the |
| 47 |
|
| linker will do it automatically */ |
| 48 |
|
| |
| 49 |
|
| /** |
| 50 |
|
| * Memory allocation of size byte with alignment suitable for all |
| 51 |
|
| * memory accesses (including vectors if available on the |
| 52 |
|
| * CPU). av_malloc(0) must return a non NULL pointer. |
| 53 |
|
| */ |
| 54 |
0 |
| void *av_malloc(unsigned int size) |
| 55 |
|
| { |
| 56 |
|
| void *ptr; |
| 57 |
|
| |
| 58 |
|
| #if defined (HAVE_MEMALIGN) |
| 59 |
|
| ptr = memalign(16,size); |
| 60 |
|
| /* Why 64? |
| 61 |
|
| Indeed, we should align it: |
| 62 |
|
| on 4 for 386 |
| 63 |
|
| on 16 for 486 |
| 64 |
|
| on 32 for 586, PPro - k6-III |
| 65 |
|
| on 64 for K7 (maybe for P3 too). |
| 66 |
|
| Because L1 and L2 caches are aligned on those values. |
| 67 |
|
| But I don't want to code such logic here! |
| 68 |
|
| */ |
| 69 |
|
| /* Why 16? |
| 70 |
|
| because some cpus need alignment, for example SSE2 on P4, & most RISC cpus |
| 71 |
|
| it will just trigger an exception and the unaligned load will be done in the |
| 72 |
|
| exception handler or it will just segfault (SSE2 on P4) |
| 73 |
|
| Why not larger? because i didnt see a difference in benchmarks ... |
| 74 |
|
| */ |
| 75 |
|
| /* benchmarks with p3 |
| 76 |
|
| memalign(64)+1 3071,3051,3032 |
| 77 |
|
| memalign(64)+2 3051,3032,3041 |
| 78 |
|
| memalign(64)+4 2911,2896,2915 |
| 79 |
|
| memalign(64)+8 2545,2554,2550 |
| 80 |
|
| memalign(64)+16 2543,2572,2563 |
| 81 |
|
| memalign(64)+32 2546,2545,2571 |
| 82 |
|
| memalign(64)+64 2570,2533,2558 |
| 83 |
|
| |
| 84 |
|
| btw, malloc seems to do 8 byte alignment by default here |
| 85 |
|
| */ |
| 86 |
|
| #else |
| 87 |
0 |
| ptr = malloc(size); |
| 88 |
|
| #endif |
| 89 |
0 |
| return ptr; |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| /** |
| 93 |
|
| * av_realloc semantics (same as glibc): if ptr is NULL and size > 0, |
| 94 |
|
| * identical to malloc(size). If size is zero, it is identical to |
| 95 |
|
| * free(ptr) and NULL is returned. |
| 96 |
|
| */ |
| 97 |
0 |
| void *av_realloc(void *ptr, unsigned int size) |
| 98 |
|
| { |
| 99 |
0 |
| return realloc(ptr, size); |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| /* NOTE: ptr = NULL is explicetly allowed */ |
| 103 |
0 |
| void av_free(void *ptr) |
| 104 |
|
| { |
| 105 |
|
| /* XXX: this test should not be needed on most libcs */ |
| 106 |
0 |
▶ | if (ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr) |
0 |
0 |
0 |
|
| 107 |
0 |
| free(ptr); |
| 108 |
0 |
| } |
| 109 |
|
| |
| 110 |
0 |
| void *av_mallocz(unsigned int size) |
| 111 |
|
| { |
| 112 |
0 |
| void *ptr = av_malloc(size); |
| 113 |
0 |
▶ | if (ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr) |
0 |
0 |
0 |
|
| 114 |
0 |
| memset(ptr, 0, size); |
| 115 |
0 |
| return ptr; |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| |
| 119 |
|
| /* allocation of static arrays - do not use for normal allocation */ |
| 120 |
|
| static unsigned int last_static = 0; |
| 121 |
|
| static char*** array_static = NULL; |
| 122 |
|
| static const unsigned int grow_static = 64; // ^2 |
| 123 |
0 |
| void *__av_mallocz_static(void** location, unsigned int size) |
| 124 |
|
| { |
| 125 |
0 |
| unsigned int l = (last_static + grow_static) & ~(grow_static - 1); |
| 126 |
0 |
| void *ptr = av_mallocz(size); |
| 127 |
0 |
▶ | if (!ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ptr) |
0 |
0 |
0 |
|
| 128 |
0 |
| return NULL; |
| 129 |
|
| |
| 130 |
0 |
▶ | if (location) |
Branch: 1 branch
| Branch | True | False | Total |
| if (location) |
0 |
0 |
0 |
|
| 131 |
|
| { |
| 132 |
0 |
▶ | if (l > last_static) |
Branch: 1 branch
| Branch | True | False | Total |
| if (l > last_static) |
0 |
0 |
0 |
|
| 133 |
0 |
| array_static = av_realloc(array_static, l); |
| 134 |
0 |
| array_static[last_static++] = (char**) location; |
| 135 |
0 |
| *location = ptr; |
| 136 |
|
| } |
| 137 |
0 |
| return ptr; |
| 138 |
|
| } |
| 139 |
|
| /* free all static arrays and reset pointers to 0 */ |
| 140 |
0 |
| void av_free_static(void) |
| 141 |
|
| { |
| 142 |
0 |
▶ | if (array_static) |
Branch: 1 branch
| Branch | True | False | Total |
| if (array_static) |
0 |
0 |
0 |
|
| 143 |
|
| { |
| 144 |
|
| unsigned i; |
| 145 |
0 |
▶ | for (i = 0; i < last_static; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < last_static; i++) |
0 |
0 |
0 |
|
| 146 |
|
| { |
| 147 |
0 |
| av_free(*array_static[i]); |
| 148 |
0 |
| *array_static[i] = NULL; |
| 149 |
|
| } |
| 150 |
0 |
| av_free(array_static); |
| 151 |
0 |
| array_static = 0; |
| 152 |
|
| } |
| 153 |
0 |
| last_static = 0; |
| 154 |
0 |
| } |