mem.c 5.46 KB
Newer Older
1
/*
2
 * default memory allocator for libavutil
3
 * Copyright (c) 2002 Fabrice Bellard
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

Michael Niedermayer's avatar
Michael Niedermayer committed
22
/**
23
 * @file
24
 * default memory allocator for libavutil
Michael Niedermayer's avatar
Michael Niedermayer committed
25
 */
26

27 28
#define _XOPEN_SOURCE 600

29
#include "config.h"
30

31
#include <limits.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#if HAVE_MALLOC_H
35 36 37
#include <malloc.h>
#endif

38
#include "avutil.h"
39 40
#include "mem.h"

41
/* here we can use OS-dependent allocation functions */
42 43 44 45
#undef free
#undef malloc
#undef realloc

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#ifdef MALLOC_PREFIX

#define malloc         AV_JOIN(MALLOC_PREFIX, malloc)
#define memalign       AV_JOIN(MALLOC_PREFIX, memalign)
#define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
#define realloc        AV_JOIN(MALLOC_PREFIX, realloc)
#define free           AV_JOIN(MALLOC_PREFIX, free)

void *malloc(size_t size);
void *memalign(size_t align, size_t size);
int   posix_memalign(void **ptr, size_t align, size_t size);
void *realloc(void *ptr, size_t size);
void  free(void *ptr);

#endif /* MALLOC_PREFIX */

62 63
#define ALIGN (HAVE_AVX ? 32 : 16)

64
/* You can redefine av_malloc and av_free in your project to use your
65
   memory allocator. You do not need to suppress this file because the
66
   linker will do it automatically. */
67

68 69
#define MAX_MALLOC_SIZE INT_MAX

70
void *av_malloc(size_t size)
71
{
72
    void *ptr = NULL;
73
#if CONFIG_MEMALIGN_HACK
74
    long diff;
75
#endif
76

77
    /* let's disallow possible ambiguous cases */
78
    if (size > (MAX_MALLOC_SIZE-32))
79
        return NULL;
80

81
#if CONFIG_MEMALIGN_HACK
82
    ptr = malloc(size+ALIGN);
83 84
    if(!ptr)
        return ptr;
85
    diff= ((-(long)ptr - 1)&(ALIGN-1)) + 1;
avcoder's avatar
avcoder committed
86
    ptr = (char*)ptr + diff;
87
    ((char*)ptr)[-1]= diff;
88
#elif HAVE_POSIX_MEMALIGN
89
    if (size) //OSX on SDK 10.6 has a broken posix_memalign implementation
90
    if (posix_memalign(&ptr,ALIGN,size))
91
        ptr = NULL;
92
#elif HAVE_MEMALIGN
93
    ptr = memalign(ALIGN,size);
94
    /* Why 64?
95 96 97
       Indeed, we should align it:
         on 4 for 386
         on 16 for 486
98
         on 32 for 586, PPro - K6-III
99
         on 64 for K7 (maybe for P3 too).
100 101 102
       Because L1 and L2 caches are aligned on those values.
       But I don't want to code such logic here!
     */
103 104
     /* Why 32?
        For AVX ASM. SSE / NEON needs only 16.
Diego Biurrun's avatar
Diego Biurrun committed
105
        Why not larger? Because I did not see a difference in benchmarks ...
Michael Niedermayer's avatar
Michael Niedermayer committed
106
     */
107
     /* benchmarks with P3
108 109 110 111 112 113 114
        memalign(64)+1          3071,3051,3032
        memalign(64)+2          3051,3032,3041
        memalign(64)+4          2911,2896,2915
        memalign(64)+8          2545,2554,2550
        memalign(64)+16         2543,2572,2563
        memalign(64)+32         2546,2545,2571
        memalign(64)+64         2570,2533,2558
115

116
        BTW, malloc seems to do 8-byte alignment by default here.
Michael Niedermayer's avatar
Michael Niedermayer committed
117
     */
118 119 120
#else
    ptr = malloc(size);
#endif
121 122
    if(!ptr && !size)
        ptr= av_malloc(1);
123 124 125
    return ptr;
}

126
void *av_realloc(void *ptr, size_t size)
127
{
128
#if CONFIG_MEMALIGN_HACK
129 130
    int diff;
#endif
131

132
    /* let's disallow possible ambiguous cases */
133
    if (size > (MAX_MALLOC_SIZE-16))
134 135
        return NULL;

136
#if CONFIG_MEMALIGN_HACK
137 138 139
    //FIXME this isn't aligned correctly, though it probably isn't needed
    if(!ptr) return av_malloc(size);
    diff= ((char*)ptr)[-1];
140 141 142
    ptr= realloc((char*)ptr - diff, size + diff);
    if(ptr) ptr = (char*)ptr + diff;
    return ptr;
143
#else
144
    return realloc(ptr, size + !size);
145
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
146 147
}

Nicolas George's avatar
Nicolas George committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
{
    size_t size;
    void *r;

    if (av_size_mult(elsize, nelem, &size)) {
        av_free(ptr);
        return NULL;
    }
    r = av_realloc(ptr, size);
    if (!r && size)
        av_free(ptr);
    return r;
}

163 164
void av_free(void *ptr)
{
165
#if CONFIG_MEMALIGN_HACK
166
    if (ptr)
avcoder's avatar
avcoder committed
167
        free((char*)ptr - ((char*)ptr)[-1]);
168
#else
169
    free(ptr);
170
#endif
171 172
}

173 174 175 176 177 178 179
void av_freep(void *arg)
{
    void **ptr= (void**)arg;
    av_free(*ptr);
    *ptr = NULL;
}

180
void *av_mallocz(size_t size)
181
{
182
    void *ptr = av_malloc(size);
183 184 185 186 187
    if (ptr)
        memset(ptr, 0, size);
    return ptr;
}

Laurent Aimar's avatar
Laurent Aimar committed
188 189 190 191 192 193 194
void *av_calloc(size_t nmemb, size_t size)
{
    if (size <= 0 || nmemb >= INT_MAX / size)
        return NULL;
    return av_mallocz(nmemb * size);
}

195 196
char *av_strdup(const char *s)
{
197 198
    char *ptr= NULL;
    if(s){
Michael Niedermayer's avatar
Michael Niedermayer committed
199 200 201 202
        int len = strlen(s) + 1;
        ptr = av_malloc(len);
        if (ptr)
            memcpy(ptr, s, len);
203
    }
204 205 206
    return ptr;
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
/* add one element to a dynamic array */
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
{
    /* see similar ffmpeg.c:grow_array() */
    int nb, nb_alloc;
    intptr_t *tab;

    nb = *nb_ptr;
    tab = *(intptr_t**)tab_ptr;
    if ((nb & (nb - 1)) == 0) {
        if (nb == 0)
            nb_alloc = 1;
        else
            nb_alloc = nb * 2;
        tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
        *(intptr_t**)tab_ptr = tab;
    }
    tab[nb++] = (intptr_t)elem;
    *nb_ptr = nb;
}