Statistics
| Branch: | Revision:

ffmpeg / libavutil / mem.c @ c957c854

History | View | Annotate | Download (3.99 KB)

1
/*
2
 * default memory allocator for libavutil
3
 * Copyright (c) 2002 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * 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
 * 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
 * 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

    
22
/**
23
 * @file libavutil/mem.c
24
 * default memory allocator for libavutil
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
#include <malloc.h>
34
#endif
35

    
36
#include "mem.h"
37

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

    
43
/* You can redefine av_malloc and av_free in your project to use your
44
   memory allocator. You do not need to suppress this file because the
45
   linker will do it automatically. */
46

    
47
void *av_malloc(unsigned int size)
48
{
49
    void *ptr = NULL;
50
#if CONFIG_MEMALIGN_HACK
51
    long diff;
52
#endif
53

    
54
    /* let's disallow possible ambiguous cases */
55
    if(size > (INT_MAX-16) )
56
        return NULL;
57

    
58
#if CONFIG_MEMALIGN_HACK
59
    ptr = malloc(size+16);
60
    if(!ptr)
61
        return ptr;
62
    diff= ((-(long)ptr - 1)&15) + 1;
63
    ptr = (char*)ptr + diff;
64
    ((char*)ptr)[-1]= diff;
65
#elif HAVE_POSIX_MEMALIGN
66
    if (posix_memalign(&ptr,16,size))
67
        ptr = NULL;
68
#elif HAVE_MEMALIGN
69
    ptr = memalign(16,size);
70
    /* Why 64?
71
       Indeed, we should align it:
72
         on 4 for 386
73
         on 16 for 486
74
         on 32 for 586, PPro - K6-III
75
         on 64 for K7 (maybe for P3 too).
76
       Because L1 and L2 caches are aligned on those values.
77
       But I don't want to code such logic here!
78
     */
79
     /* Why 16?
80
        Because some CPUs need alignment, for example SSE2 on P4, & most RISC CPUs
81
        it will just trigger an exception and the unaligned load will be done in the
82
        exception handler or it will just segfault (SSE2 on P4).
83
        Why not larger? Because I did not see a difference in benchmarks ...
84
     */
85
     /* benchmarks with P3
86
        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

94
        BTW, malloc seems to do 8-byte alignment by default here.
95
     */
96
#else
97
    ptr = malloc(size);
98
#endif
99
    return ptr;
100
}
101

    
102
void *av_realloc(void *ptr, unsigned int size)
103
{
104
#if CONFIG_MEMALIGN_HACK
105
    int diff;
106
#endif
107

    
108
    /* let's disallow possible ambiguous cases */
109
    if(size > (INT_MAX-16) )
110
        return NULL;
111

    
112
#if CONFIG_MEMALIGN_HACK
113
    //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
    return (char*)realloc((char*)ptr - diff, size + diff) + diff;
117
#else
118
    return realloc(ptr, size);
119
#endif
120
}
121

    
122
void av_free(void *ptr)
123
{
124
    /* XXX: this test should not be needed on most libcs */
125
    if (ptr)
126
#if CONFIG_MEMALIGN_HACK
127
        free((char*)ptr - ((char*)ptr)[-1]);
128
#else
129
        free(ptr);
130
#endif
131
}
132

    
133
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
    void *ptr = av_malloc(size);
143
    if (ptr)
144
        memset(ptr, 0, size);
145
    return ptr;
146
}
147

    
148
char *av_strdup(const char *s)
149
{
150
    char *ptr= NULL;
151
    if(s){
152
        int len = strlen(s) + 1;
153
        ptr = av_malloc(len);
154
        if (ptr)
155
            memcpy(ptr, s, len);
156
    }
157
    return ptr;
158
}
159