forked from felipec/gst-dsp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmm_buffer.h
178 lines (160 loc) · 3.91 KB
/
dmm_buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (C) 2009 Felipe Contreras
* Copyright (C) 2008-2009 Nokia Corporation.
*
* Authors:
* Felipe Contreras <felipe.contreras@nokia.com>
* Marco Ballesio <marco.ballesio@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* 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
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef DMM_BUFFER_H
#define DMM_BUFFER_H
#include <stdlib.h> /* for calloc, free */
#include <unistd.h> /* for getpagesize */
#include "dsp_bridge.h"
#include "log.h"
#define ROUND_UP(num, scale) (((num) + ((scale) - 1)) & ~((scale) - 1))
typedef struct {
int handle;
dsp_node_t *node;
void *data;
void *allocated_data;
size_t size;
size_t len;
void *reserve;
void *map;
bool need_copy;
size_t alignment;
void *user_data;
bool used;
bool keyframe;
} dmm_buffer_t;
static inline dmm_buffer_t *
dmm_buffer_new(int handle,
dsp_node_t *node)
{
dmm_buffer_t *b;
b = calloc(1, sizeof(*b));
pr_debug(NULL, "%p", b);
b->handle = handle;
b->node = node;
b->alignment = 128;
return b;
}
static inline void
dmm_buffer_free(dmm_buffer_t *b)
{
pr_debug(NULL, "%p", b);
if (!b)
return;
if (b->map)
dsp_unmap(b->handle, b->node, b->map);
if (b->reserve)
dsp_unreserve(b->handle, b->node, b->reserve);
free(b->allocated_data);
free(b);
}
static inline void
dmm_buffer_map(dmm_buffer_t *b)
{
pr_debug(NULL, "%p", b);
if (b->map)
dsp_unmap(b->handle, b->node, b->map);
dsp_map(b->handle, b->node, b->data, b->size, b->reserve, &b->map, 0);
}
static inline void
dmm_buffer_unmap(dmm_buffer_t *b)
{
pr_debug(NULL, "%p", b);
if (!b->map)
return;
dsp_unmap(b->handle, b->node, b->map);
b->map = NULL;
}
static inline void
dmm_buffer_clean(dmm_buffer_t *b,
size_t len)
{
pr_debug(NULL, "%p", b);
if (G_UNLIKELY(len > b->size))
g_error("wrong cache flush");
dsp_flush(b->handle, b->node, b->data, len, 1);
}
static inline void
dmm_buffer_invalidate(dmm_buffer_t *b,
size_t len)
{
pr_debug(NULL, "%p", b);
if (G_UNLIKELY(len > b->size))
g_error("wrong cache flush");
dsp_invalidate(b->handle, b->node, b->data, len);
}
static inline void
dmm_buffer_flush(dmm_buffer_t *b,
size_t len)
{
pr_debug(NULL, "%p", b);
if (G_UNLIKELY(len > b->size))
g_error("wrong cache flush");
dsp_flush(b->handle, b->node, b->data, len, 0);
}
static inline void
dmm_buffer_reserve(dmm_buffer_t *b,
size_t size)
{
size_t to_reserve;
size_t page_size;
page_size = getpagesize();
if (b->reserve) {
if (ROUND_UP(size, page_size) <= ROUND_UP(b->size, page_size))
goto leave;
dsp_unreserve(b->handle, b->node, b->reserve);
}
to_reserve = ROUND_UP(size, page_size) + page_size;
dsp_reserve(b->handle, b->node, to_reserve, &b->reserve);
leave:
b->size = size;
}
static inline void
dmm_buffer_allocate(dmm_buffer_t *b,
size_t size)
{
pr_debug(NULL, "%p", b);
dmm_buffer_unmap(b);
free(b->allocated_data);
if (b->alignment != 0) {
if (posix_memalign(&b->allocated_data, b->alignment, ROUND_UP(size, b->alignment)) != 0)
b->allocated_data = NULL;
b->data = b->allocated_data;
}
else
b->data = b->allocated_data = malloc(size);
dmm_buffer_reserve(b, size);
dmm_buffer_map(b);
}
static inline void
dmm_buffer_use(dmm_buffer_t *b,
void *data,
size_t size)
{
pr_debug(NULL, "%p", b);
dmm_buffer_unmap(b);
b->data = data;
dmm_buffer_reserve(b, size);
dmm_buffer_map(b);
}
#endif /* DMM_BUFFER_H */