Statistics
| Branch: | Revision:

ffmpeg / libavutil / mem.c @ 80a289b9

History | View | Annotate | Download (4.44 KB)

1 d01fe86d Fabrice Bellard
/*
2 cea8f6f3 Luca Abeni
 * default memory allocator for libavutil
3 d01fe86d Fabrice Bellard
 * Copyright (c) 2002 Fabrice Bellard.
4
 *
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
 * @file mem.c
24 cea8f6f3 Luca Abeni
 * default memory allocator for libavutil.
25 983e3246 Michael Niedermayer
 */
26 115329f1 Diego Biurrun
27 cea8f6f3 Luca Abeni
#include "common.h"
28 8e1e6f31 Fabrice Bellard
29
/* here we can use OS dependant allocation functions */
30
#undef malloc
31
#undef free
32
#undef realloc
33
34 d01fe86d Fabrice Bellard
#ifdef HAVE_MALLOC_H
35
#include <malloc.h>
36
#endif
37
38
/* you can redefine av_malloc and av_free in your project to use your
39
   memory allocator. You do not need to suppress this file because the
40
   linker will do it automatically */
41
42 115329f1 Diego Biurrun
/**
43 8e1e6f31 Fabrice Bellard
 * Memory allocation of size byte with alignment suitable for all
44
 * memory accesses (including vectors if available on the
45
 * CPU). av_malloc(0) must return a non NULL pointer.
46
 */
47 18f77016 Zdenek Kabelac
void *av_malloc(unsigned int size)
48 d01fe86d Fabrice Bellard
{
49
    void *ptr;
50 9ddbcae6 Måns Rullgård
#ifdef 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 9ddbcae6 Måns Rullgård
#ifdef 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 da9b170c Michael Niedermayer
    ptr += diff;
64
    ((char*)ptr)[-1]= diff;
65 115329f1 Diego Biurrun
#elif defined (HAVE_MEMALIGN)
66 8f2b21a8 Michael Niedermayer
    ptr = memalign(16,size);
67 115329f1 Diego Biurrun
    /* Why 64?
68 d01fe86d Fabrice Bellard
       Indeed, we should align it:
69
         on 4 for 386
70
         on 16 for 486
71 bb270c08 Diego Biurrun
         on 32 for 586, PPro - k6-III
72
         on 64 for K7 (maybe for P3 too).
73 d01fe86d Fabrice Bellard
       Because L1 and L2 caches are aligned on those values.
74
       But I don't want to code such logic here!
75
     */
76 8f2b21a8 Michael Niedermayer
     /* Why 16?
77
        because some cpus need alignment, for example SSE2 on P4, & most RISC cpus
78
        it will just trigger an exception and the unaligned load will be done in the
79
        exception handler or it will just segfault (SSE2 on P4)
80
        Why not larger? because i didnt see a difference in benchmarks ...
81
     */
82
     /* benchmarks with p3
83 bb270c08 Diego Biurrun
        memalign(64)+1          3071,3051,3032
84
        memalign(64)+2          3051,3032,3041
85
        memalign(64)+4          2911,2896,2915
86
        memalign(64)+8          2545,2554,2550
87
        memalign(64)+16         2543,2572,2563
88
        memalign(64)+32         2546,2545,2571
89
        memalign(64)+64         2570,2533,2558
90 115329f1 Diego Biurrun

91 8f2b21a8 Michael Niedermayer
        btw, malloc seems to do 8 byte alignment by default here
92
     */
93 d01fe86d Fabrice Bellard
#else
94
    ptr = malloc(size);
95
#endif
96
    return ptr;
97
}
98
99 b7a22d84 Michael Niedermayer
/**
100 8e1e6f31 Fabrice Bellard
 * av_realloc semantics (same as glibc): if ptr is NULL and size > 0,
101
 * identical to malloc(size). If size is zero, it is identical to
102 115329f1 Diego Biurrun
 * free(ptr) and NULL is returned.
103 b7a22d84 Michael Niedermayer
 */
104 8e1e6f31 Fabrice Bellard
void *av_realloc(void *ptr, unsigned int size)
105
{
106 9ddbcae6 Måns Rullgård
#ifdef CONFIG_MEMALIGN_HACK
107 0a7c36af Michael Niedermayer
    int diff;
108
#endif
109 88730be6 Måns Rullgård
110 7d77d5f6 Diego Biurrun
    /* let's disallow possible ambiguous cases */
111 a9493601 Herve W
    if(size > (INT_MAX-16) )
112 0ecca7a4 Michael Niedermayer
        return NULL;
113
114 9ddbcae6 Måns Rullgård
#ifdef CONFIG_MEMALIGN_HACK
115 0a7c36af Michael Niedermayer
    //FIXME this isn't aligned correctly, though it probably isn't needed
116
    if(!ptr) return av_malloc(size);
117
    diff= ((char*)ptr)[-1];
118
    return realloc(ptr - diff, size + diff) + diff;
119
#else
120
    return realloc(ptr, size);
121 da9b170c Michael Niedermayer
#endif
122 b7a22d84 Michael Niedermayer
}
123
124 1499e0be Michael Niedermayer
/**
125
 * Free memory which has been allocated with av_malloc(z)() or av_realloc().
126 4f59b684 Diego Biurrun
 * NOTE: ptr = NULL is explicetly allowed
127 da2e9781 Diego Biurrun
 * Note2: it is recommended that you use av_freep() instead
128 1499e0be Michael Niedermayer
 */
129 d01fe86d Fabrice Bellard
void av_free(void *ptr)
130
{
131
    /* XXX: this test should not be needed on most libcs */
132
    if (ptr)
133 9ddbcae6 Måns Rullgård
#ifdef CONFIG_MEMALIGN_HACK
134 da9b170c Michael Niedermayer
        free(ptr - ((char*)ptr)[-1]);
135
#else
136 d01fe86d Fabrice Bellard
        free(ptr);
137 da9b170c Michael Niedermayer
#endif
138 d01fe86d Fabrice Bellard
}
139
140 79e47000 Luca Barbato
/**
141
 * Frees memory and sets the pointer to NULL.
142
 * @param arg pointer to the pointer which should be freed
143
 */
144
void av_freep(void *arg)
145
{
146
    void **ptr= (void**)arg;
147
    av_free(*ptr);
148
    *ptr = NULL;
149
}
150
151
void *av_mallocz(unsigned int size)
152
{
153
    void *ptr;
154
155
    ptr = av_malloc(size);
156
    if (ptr)
157
        memset(ptr, 0, size);
158
    return ptr;
159
}
160
161
char *av_strdup(const char *s)
162
{
163
    char *ptr;
164
    int len;
165
    len = strlen(s) + 1;
166
    ptr = av_malloc(len);
167
    if (ptr)
168
        memcpy(ptr, s, len);
169
    return ptr;
170
}