Coverage Report

Created: 2025-08-26 06:24

/src/dav1d/src/mem.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2018, VideoLAN and dav1d authors
3
 * Copyright © 2018, Two Orioles, LLC
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#ifndef DAV1D_SRC_MEM_H
29
#define DAV1D_SRC_MEM_H
30
31
#define TRACK_HEAP_ALLOCATIONS 0
32
33
#include <stdlib.h>
34
35
#if defined(_WIN32) || HAVE_MEMALIGN
36
#include <malloc.h>
37
#endif
38
39
#include "dav1d/dav1d.h"
40
41
#include "common/attributes.h"
42
43
#include "src/thread.h"
44
45
enum AllocationType {
46
    ALLOC_BLOCK,
47
    ALLOC_CDEF,
48
    ALLOC_CDF,
49
    ALLOC_COEF,
50
    ALLOC_COMMON_CTX,
51
    ALLOC_DAV1DDATA,
52
    ALLOC_IPRED,
53
    ALLOC_LF,
54
    ALLOC_LR,
55
    ALLOC_OBU_HDR,
56
    ALLOC_OBU_META,
57
    ALLOC_PAL,
58
    ALLOC_PIC,
59
    ALLOC_PIC_CTX,
60
    ALLOC_REFMVS,
61
    ALLOC_SEGMAP,
62
    ALLOC_THREAD_CTX,
63
    ALLOC_TILE,
64
    N_ALLOC_TYPES,
65
};
66
67
typedef struct Dav1dMemPoolBuffer {
68
    void *data;
69
    struct Dav1dMemPoolBuffer *next;
70
} Dav1dMemPoolBuffer;
71
72
typedef struct Dav1dMemPool {
73
    pthread_mutex_t lock;
74
    Dav1dMemPoolBuffer *buf;
75
    int ref_cnt;
76
    int end;
77
#if TRACK_HEAP_ALLOCATIONS
78
    enum AllocationType type;
79
#endif
80
} Dav1dMemPool;
81
82
// TODO: Move this to a common location?
83
#define ROUND_UP(x,a) (((x)+((a)-1)) & ~((a)-1))
84
85
/*
86
 * Allocate align-byte aligned memory. The return value can be released
87
 * by calling the dav1d_free_aligned() function.
88
 */
89
1.11M
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
1.11M
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
1.11M
    void *ptr;
95
1.11M
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
1.11M
    return ptr;
97
#elif HAVE_MEMALIGN
98
    return memalign(align, sz);
99
#elif HAVE_ALIGNED_ALLOC
100
    // The C11 standard specifies that the size parameter
101
    // must be an integral multiple of alignment.
102
    return aligned_alloc(align, ROUND_UP(sz, align));
103
#else
104
#error No aligned allocation functions are available
105
#endif
106
1.11M
}
lib.c:dav1d_alloc_aligned_internal
Line
Count
Source
89
72.8k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
72.8k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
72.8k
    void *ptr;
95
72.8k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
72.8k
    return ptr;
97
#elif HAVE_MEMALIGN
98
    return memalign(align, sz);
99
#elif HAVE_ALIGNED_ALLOC
100
    // The C11 standard specifies that the size parameter
101
    // must be an integral multiple of alignment.
102
    return aligned_alloc(align, ROUND_UP(sz, align));
103
#else
104
#error No aligned allocation functions are available
105
#endif
106
72.8k
}
mem.c:dav1d_alloc_aligned_internal
Line
Count
Source
89
209k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
209k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
209k
    void *ptr;
95
209k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
209k
    return ptr;
97
#elif HAVE_MEMALIGN
98
    return memalign(align, sz);
99
#elif HAVE_ALIGNED_ALLOC
100
    // The C11 standard specifies that the size parameter
101
    // must be an integral multiple of alignment.
102
    return aligned_alloc(align, ROUND_UP(sz, align));
103
#else
104
#error No aligned allocation functions are available
105
#endif
106
209k
}
Unexecuted instantiation: obu.c:dav1d_alloc_aligned_internal
Unexecuted instantiation: picture.c:dav1d_alloc_aligned_internal
ref.c:dav1d_alloc_aligned_internal
Line
Count
Source
89
520k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
520k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
520k
    void *ptr;
95
520k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
520k
    return ptr;
97
#elif HAVE_MEMALIGN
98
    return memalign(align, sz);
99
#elif HAVE_ALIGNED_ALLOC
100
    // The C11 standard specifies that the size parameter
101
    // must be an integral multiple of alignment.
102
    return aligned_alloc(align, ROUND_UP(sz, align));
103
#else
104
#error No aligned allocation functions are available
105
#endif
106
520k
}
refmvs.c:dav1d_alloc_aligned_internal
Line
Count
Source
89
36.6k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
36.6k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
36.6k
    void *ptr;
95
36.6k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
36.6k
    return ptr;
97
#elif HAVE_MEMALIGN
98
    return memalign(align, sz);
99
#elif HAVE_ALIGNED_ALLOC
100
    // The C11 standard specifies that the size parameter
101
    // must be an integral multiple of alignment.
102
    return aligned_alloc(align, ROUND_UP(sz, align));
103
#else
104
#error No aligned allocation functions are available
105
#endif
106
36.6k
}
Unexecuted instantiation: thread_task.c:dav1d_alloc_aligned_internal
Unexecuted instantiation: cdf.c:dav1d_alloc_aligned_internal
Unexecuted instantiation: data.c:dav1d_alloc_aligned_internal
decode.c:dav1d_alloc_aligned_internal
Line
Count
Source
89
272k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
272k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
272k
    void *ptr;
95
272k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
272k
    return ptr;
97
#elif HAVE_MEMALIGN
98
    return memalign(align, sz);
99
#elif HAVE_ALIGNED_ALLOC
100
    // The C11 standard specifies that the size parameter
101
    // must be an integral multiple of alignment.
102
    return aligned_alloc(align, ROUND_UP(sz, align));
103
#else
104
#error No aligned allocation functions are available
105
#endif
106
272k
}
Unexecuted instantiation: recon_tmpl.c:dav1d_alloc_aligned_internal
Unexecuted instantiation: cdef_apply_tmpl.c:dav1d_alloc_aligned_internal
Unexecuted instantiation: lf_apply_tmpl.c:dav1d_alloc_aligned_internal
Unexecuted instantiation: lr_apply_tmpl.c:dav1d_alloc_aligned_internal
107
108
1.65M
static inline void dav1d_free_aligned_internal(void *ptr) {
109
#ifdef _WIN32
110
    _aligned_free(ptr);
111
#else
112
1.65M
    free(ptr);
113
1.65M
#endif
114
1.65M
}
lib.c:dav1d_free_aligned_internal
Line
Count
Source
108
616k
static inline void dav1d_free_aligned_internal(void *ptr) {
109
#ifdef _WIN32
110
    _aligned_free(ptr);
111
#else
112
616k
    free(ptr);
113
616k
#endif
114
616k
}
mem.c:dav1d_free_aligned_internal
Line
Count
Source
108
209k
static inline void dav1d_free_aligned_internal(void *ptr) {
109
#ifdef _WIN32
110
    _aligned_free(ptr);
111
#else
112
209k
    free(ptr);
113
209k
#endif
114
209k
}
Unexecuted instantiation: obu.c:dav1d_free_aligned_internal
Unexecuted instantiation: picture.c:dav1d_free_aligned_internal
ref.c:dav1d_free_aligned_internal
Line
Count
Source
108
520k
static inline void dav1d_free_aligned_internal(void *ptr) {
109
#ifdef _WIN32
110
    _aligned_free(ptr);
111
#else
112
520k
    free(ptr);
113
520k
#endif
114
520k
}
refmvs.c:dav1d_free_aligned_internal
Line
Count
Source
108
36.6k
static inline void dav1d_free_aligned_internal(void *ptr) {
109
#ifdef _WIN32
110
    _aligned_free(ptr);
111
#else
112
36.6k
    free(ptr);
113
36.6k
#endif
114
36.6k
}
Unexecuted instantiation: thread_task.c:dav1d_free_aligned_internal
Unexecuted instantiation: cdf.c:dav1d_free_aligned_internal
Unexecuted instantiation: data.c:dav1d_free_aligned_internal
decode.c:dav1d_free_aligned_internal
Line
Count
Source
108
274k
static inline void dav1d_free_aligned_internal(void *ptr) {
109
#ifdef _WIN32
110
    _aligned_free(ptr);
111
#else
112
274k
    free(ptr);
113
274k
#endif
114
274k
}
Unexecuted instantiation: recon_tmpl.c:dav1d_free_aligned_internal
Unexecuted instantiation: cdef_apply_tmpl.c:dav1d_free_aligned_internal
Unexecuted instantiation: lf_apply_tmpl.c:dav1d_free_aligned_internal
Unexecuted instantiation: lr_apply_tmpl.c:dav1d_free_aligned_internal
115
116
#if TRACK_HEAP_ALLOCATIONS
117
void *dav1d_malloc(enum AllocationType type, size_t sz);
118
void *dav1d_realloc(enum AllocationType type, void *ptr, size_t sz);
119
void *dav1d_alloc_aligned(enum AllocationType type, size_t sz, size_t align);
120
void dav1d_free(void *ptr);
121
void dav1d_free_aligned(void *ptr);
122
void dav1d_log_alloc_stats(Dav1dContext *c);
123
#else
124
291k
#define dav1d_mem_pool_init(type, pool) dav1d_mem_pool_init(pool)
125
580k
#define dav1d_malloc(type, sz) malloc(sz)
126
87.1k
#define dav1d_realloc(type, ptr, sz) realloc(ptr, sz)
127
1.11M
#define dav1d_alloc_aligned(type, sz, align) dav1d_alloc_aligned_internal(sz, align)
128
1.42M
#define dav1d_free(ptr) free(ptr)
129
1.65M
#define dav1d_free_aligned(ptr) dav1d_free_aligned_internal(ptr)
130
#endif /* TRACK_HEAP_ALLOCATIONS */
131
132
void dav1d_mem_pool_push(Dav1dMemPool *pool, Dav1dMemPoolBuffer *buf);
133
Dav1dMemPoolBuffer *dav1d_mem_pool_pop(Dav1dMemPool *pool, size_t size);
134
int dav1d_mem_pool_init(enum AllocationType type, Dav1dMemPool **pool);
135
void dav1d_mem_pool_end(Dav1dMemPool *pool);
136
137
26.4k
static inline void dav1d_freep_aligned(void *ptr) {
138
26.4k
    void **mem = (void **) ptr;
139
26.4k
    if (*mem) {
140
26.4k
        dav1d_free_aligned(*mem);
141
26.4k
        *mem = NULL;
142
26.4k
    }
143
26.4k
}
lib.c:dav1d_freep_aligned
Line
Count
Source
137
24.2k
static inline void dav1d_freep_aligned(void *ptr) {
138
24.2k
    void **mem = (void **) ptr;
139
24.2k
    if (*mem) {
140
24.2k
        dav1d_free_aligned(*mem);
141
24.2k
        *mem = NULL;
142
24.2k
    }
143
24.2k
}
Unexecuted instantiation: mem.c:dav1d_freep_aligned
Unexecuted instantiation: obu.c:dav1d_freep_aligned
Unexecuted instantiation: picture.c:dav1d_freep_aligned
Unexecuted instantiation: ref.c:dav1d_freep_aligned
Unexecuted instantiation: refmvs.c:dav1d_freep_aligned
Unexecuted instantiation: thread_task.c:dav1d_freep_aligned
Unexecuted instantiation: cdf.c:dav1d_freep_aligned
Unexecuted instantiation: data.c:dav1d_freep_aligned
decode.c:dav1d_freep_aligned
Line
Count
Source
137
2.19k
static inline void dav1d_freep_aligned(void *ptr) {
138
2.19k
    void **mem = (void **) ptr;
139
2.19k
    if (*mem) {
140
2.19k
        dav1d_free_aligned(*mem);
141
2.19k
        *mem = NULL;
142
2.19k
    }
143
2.19k
}
Unexecuted instantiation: recon_tmpl.c:dav1d_freep_aligned
Unexecuted instantiation: cdef_apply_tmpl.c:dav1d_freep_aligned
Unexecuted instantiation: lf_apply_tmpl.c:dav1d_freep_aligned
Unexecuted instantiation: lr_apply_tmpl.c:dav1d_freep_aligned
144
145
#endif /* DAV1D_SRC_MEM_H */