Coverage Report

Created: 2026-05-30 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/mem.h
Line
Count
Source
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
    struct Dav1dMemPoolBuffer *next;
69
    size_t size;
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.42M
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
1.42M
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
1.42M
    void *ptr;
95
1.42M
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
1.42M
    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
    void *const buf = malloc(sz + align + sizeof(void *));
105
    if (!buf) return NULL;
106
107
    void *const ptr = (void *)(((uintptr_t)buf + sizeof(void *) + align - 1) & ~(align - 1));
108
    ((void **)ptr)[-1] = buf;
109
    return ptr;
110
#endif
111
1.42M
}
lib.c:dav1d_alloc_aligned_internal
Line
Count
Source
89
196k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
196k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
196k
    void *ptr;
95
196k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
196k
    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
    void *const buf = malloc(sz + align + sizeof(void *));
105
    if (!buf) return NULL;
106
107
    void *const ptr = (void *)(((uintptr_t)buf + sizeof(void *) + align - 1) & ~(align - 1));
108
    ((void **)ptr)[-1] = buf;
109
    return ptr;
110
#endif
111
196k
}
Unexecuted instantiation: log.c:dav1d_alloc_aligned_internal
mem.c:dav1d_alloc_aligned_internal
Line
Count
Source
89
509k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
509k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
509k
    void *ptr;
95
509k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
509k
    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
    void *const buf = malloc(sz + align + sizeof(void *));
105
    if (!buf) return NULL;
106
107
    void *const ptr = (void *)(((uintptr_t)buf + sizeof(void *) + align - 1) & ~(align - 1));
108
    ((void **)ptr)[-1] = buf;
109
    return ptr;
110
#endif
111
509k
}
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
64.4k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
64.4k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
64.4k
    void *ptr;
95
64.4k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
64.4k
    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
    void *const buf = malloc(sz + align + sizeof(void *));
105
    if (!buf) return NULL;
106
107
    void *const ptr = (void *)(((uintptr_t)buf + sizeof(void *) + align - 1) & ~(align - 1));
108
    ((void **)ptr)[-1] = buf;
109
    return ptr;
110
#endif
111
64.4k
}
refmvs.c:dav1d_alloc_aligned_internal
Line
Count
Source
89
40.5k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
40.5k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
40.5k
    void *ptr;
95
40.5k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
40.5k
    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
    void *const buf = malloc(sz + align + sizeof(void *));
105
    if (!buf) return NULL;
106
107
    void *const ptr = (void *)(((uintptr_t)buf + sizeof(void *) + align - 1) & ~(align - 1));
108
    ((void **)ptr)[-1] = buf;
109
    return ptr;
110
#endif
111
40.5k
}
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
612k
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
90
612k
    assert(!(align & (align - 1)));
91
#ifdef _WIN32
92
    return _aligned_malloc(sz, align);
93
#elif HAVE_POSIX_MEMALIGN
94
612k
    void *ptr;
95
612k
    if (posix_memalign(&ptr, align, sz)) return NULL;
96
612k
    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
    void *const buf = malloc(sz + align + sizeof(void *));
105
    if (!buf) return NULL;
106
107
    void *const ptr = (void *)(((uintptr_t)buf + sizeof(void *) + align - 1) & ~(align - 1));
108
    ((void **)ptr)[-1] = buf;
109
    return ptr;
110
#endif
111
612k
}
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
112
113
4.95M
static inline void dav1d_free_aligned_internal(void *ptr) {
114
#ifdef _WIN32
115
    _aligned_free(ptr);
116
#elif HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_ALLOC
117
    free(ptr);
118
#else
119
    if (ptr) free(((void **)ptr)[-1]);
120
#endif
121
4.95M
}
lib.c:dav1d_free_aligned_internal
Line
Count
Source
113
3.72M
static inline void dav1d_free_aligned_internal(void *ptr) {
114
#ifdef _WIN32
115
    _aligned_free(ptr);
116
#elif HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_ALLOC
117
    free(ptr);
118
#else
119
    if (ptr) free(((void **)ptr)[-1]);
120
#endif
121
3.72M
}
Unexecuted instantiation: log.c:dav1d_free_aligned_internal
mem.c:dav1d_free_aligned_internal
Line
Count
Source
113
509k
static inline void dav1d_free_aligned_internal(void *ptr) {
114
#ifdef _WIN32
115
    _aligned_free(ptr);
116
#elif HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_ALLOC
117
    free(ptr);
118
#else
119
    if (ptr) free(((void **)ptr)[-1]);
120
#endif
121
509k
}
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
113
64.4k
static inline void dav1d_free_aligned_internal(void *ptr) {
114
#ifdef _WIN32
115
    _aligned_free(ptr);
116
#elif HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_ALLOC
117
    free(ptr);
118
#else
119
    if (ptr) free(((void **)ptr)[-1]);
120
#endif
121
64.4k
}
refmvs.c:dav1d_free_aligned_internal
Line
Count
Source
113
40.5k
static inline void dav1d_free_aligned_internal(void *ptr) {
114
#ifdef _WIN32
115
    _aligned_free(ptr);
116
#elif HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_ALLOC
117
    free(ptr);
118
#else
119
    if (ptr) free(((void **)ptr)[-1]);
120
#endif
121
40.5k
}
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
113
612k
static inline void dav1d_free_aligned_internal(void *ptr) {
114
#ifdef _WIN32
115
    _aligned_free(ptr);
116
#elif HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_ALLOC
117
    free(ptr);
118
#else
119
    if (ptr) free(((void **)ptr)[-1]);
120
#endif
121
612k
}
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
122
123
#if TRACK_HEAP_ALLOCATIONS
124
void *dav1d_malloc(enum AllocationType type, size_t sz);
125
void *dav1d_realloc(enum AllocationType type, void *ptr, size_t sz);
126
void *dav1d_alloc_aligned(enum AllocationType type, size_t sz, size_t align);
127
void dav1d_free(void *ptr);
128
void dav1d_free_aligned(void *ptr);
129
void dav1d_log_alloc_stats(Dav1dContext *c);
130
#else
131
784k
#define dav1d_mem_pool_init(type, pool) dav1d_mem_pool_init(pool)
132
1.43M
#define dav1d_malloc(type, sz) malloc(sz)
133
322k
#define dav1d_realloc(type, ptr, sz) realloc(ptr, sz)
134
1.42M
#define dav1d_alloc_aligned(type, sz, align) dav1d_alloc_aligned_internal(sz, align)
135
6.60M
#define dav1d_free(ptr) free(ptr)
136
4.95M
#define dav1d_free_aligned(ptr) dav1d_free_aligned_internal(ptr)
137
#endif /* TRACK_HEAP_ALLOCATIONS */
138
139
void dav1d_mem_pool_push(Dav1dMemPool *pool, void *ptr);
140
void *dav1d_mem_pool_pop(Dav1dMemPool *pool, size_t size);
141
int dav1d_mem_pool_init(enum AllocationType type, Dav1dMemPool **pool);
142
void dav1d_mem_pool_end(Dav1dMemPool *pool);
143
144
65.8k
static inline void dav1d_freep_aligned(void *ptr) {
145
65.8k
    void **mem = (void **) ptr;
146
65.8k
    if (*mem) {
147
65.8k
        dav1d_free_aligned(*mem);
148
        *mem = NULL;
149
65.8k
    }
150
65.8k
}
lib.c:dav1d_freep_aligned
Line
Count
Source
144
65.3k
static inline void dav1d_freep_aligned(void *ptr) {
145
65.3k
    void **mem = (void **) ptr;
146
65.3k
    if (*mem) {
147
65.3k
        dav1d_free_aligned(*mem);
148
        *mem = NULL;
149
65.3k
    }
150
65.3k
}
Unexecuted instantiation: log.c:dav1d_freep_aligned
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
144
504
static inline void dav1d_freep_aligned(void *ptr) {
145
504
    void **mem = (void **) ptr;
146
504
    if (*mem) {
147
504
        dav1d_free_aligned(*mem);
148
        *mem = NULL;
149
504
    }
150
504
}
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
151
152
#endif /* DAV1D_SRC_MEM_H */