mem.c 4.24 KB
Newer Older
1
/*
2
 * default memory allocator for libavutil
3
 * Copyright (c) 2002 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; 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
#include "config.h"
28

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

36
#include "avutil.h"
37 38
#include "mem.h"

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

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#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 */

60
/* You can redefine av_malloc and av_free in your project to use your
61
   memory allocator. You do not need to suppress this file because the
62
   linker will do it automatically. */
63

64
void *av_malloc(size_t size)
65
{
66
    void *ptr = NULL;
67
#if CONFIG_MEMALIGN_HACK
68
    long diff;
69
#endif
70

71
    /* let's disallow possible ambiguous cases */
72
    if(size > (INT_MAX-32) )
73
        return NULL;
74

75
#if CONFIG_MEMALIGN_HACK
76
    ptr = malloc(size+32);
77 78
    if(!ptr)
        return ptr;
79
    diff= ((-(long)ptr - 1)&31) + 1;
avcoder's avatar
avcoder committed
80
    ptr = (char*)ptr + diff;
81
    ((char*)ptr)[-1]= diff;
82
#elif HAVE_POSIX_MEMALIGN
83
    if (posix_memalign(&ptr,32,size))
84
        ptr = NULL;
85
#elif HAVE_MEMALIGN
86
    ptr = memalign(32,size);
87
    /* Why 64?
88 89 90
       Indeed, we should align it:
         on 4 for 386
         on 16 for 486
91
         on 32 for 586, PPro - K6-III
92
         on 64 for K7 (maybe for P3 too).
93 94 95
       Because L1 and L2 caches are aligned on those values.
       But I don't want to code such logic here!
     */
96 97
     /* Why 32?
        For AVX ASM. SSE / NEON needs only 16.
Diego Biurrun's avatar
Diego Biurrun committed
98
        Why not larger? Because I did not see a difference in benchmarks ...
Michael Niedermayer's avatar
Michael Niedermayer committed
99
     */
100
     /* benchmarks with P3
101 102 103 104 105 106 107
        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
108

109
        BTW, malloc seems to do 8-byte alignment by default here.
Michael Niedermayer's avatar
Michael Niedermayer committed
110
     */
111 112 113 114 115 116
#else
    ptr = malloc(size);
#endif
    return ptr;
}

117
void *av_realloc(void *ptr, size_t size)
118
{
119
#if CONFIG_MEMALIGN_HACK
120 121
    int diff;
#endif
122

123
    /* let's disallow possible ambiguous cases */
124
    if(size > (INT_MAX-16) )
125 126
        return NULL;

127
#if CONFIG_MEMALIGN_HACK
128 129 130
    //FIXME this isn't aligned correctly, though it probably isn't needed
    if(!ptr) return av_malloc(size);
    diff= ((char*)ptr)[-1];
avcoder's avatar
avcoder committed
131
    return (char*)realloc((char*)ptr - diff, size + diff) + diff;
132 133
#else
    return realloc(ptr, size);
134
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
135 136
}

137 138
void av_free(void *ptr)
{
139
#if CONFIG_MEMALIGN_HACK
140
    if (ptr)
avcoder's avatar
avcoder committed
141
        free((char*)ptr - ((char*)ptr)[-1]);
142
#else
143
    free(ptr);
144
#endif
145 146
}

147 148 149 150 151 152 153
void av_freep(void *arg)
{
    void **ptr= (void**)arg;
    av_free(*ptr);
    *ptr = NULL;
}

154
void *av_mallocz(size_t size)
155
{
156
    void *ptr = av_malloc(size);
157 158 159 160 161 162 163
    if (ptr)
        memset(ptr, 0, size);
    return ptr;
}

char *av_strdup(const char *s)
{
164 165
    char *ptr= NULL;
    if(s){
Michael Niedermayer's avatar
Michael Niedermayer committed
166 167 168 169
        int len = strlen(s) + 1;
        ptr = av_malloc(len);
        if (ptr)
            memcpy(ptr, s, len);
170
    }
171 172 173
    return ptr;
}