Developer World
Spresense SDK Library v3.3.0-375c679
al_memalloc.h
1/****************************************************************************
2 * modules/include/audiolite/al_memalloc.h
3 *
4 * Copyright 2023 Sony Semiconductor Solutions Corporation
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
17 * the names of its contributors may be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36#ifndef __INCLUDE_AUDIOLITE_MEMALLOC_H
37#define __INCLUDE_AUDIOLITE_MEMALLOC_H
38
39/****************************************************************************
40 * Included Files
41 ****************************************************************************/
42
43#include <stdlib.h>
44#include <stdint.h>
45#include <string.h>
46
47#include <sys/time.h>
48
49#include <nuttx/audio/audio.h>
50
51#include <mossfw/mossfw_memoryallocator.h>
52#include <mossfw/mossfw_lock.h>
53#include <mossfw/mossfw_data.h>
54
55/****************************************************************************
56 * Class Pre-definitions
57 ****************************************************************************/
58
59class audiolite_mem;
60
61/****************************************************************************
62 * Class Definitions
63 ****************************************************************************/
64
65/****************************************************************************
66 * class: audiolite_timeprofile
67 ****************************************************************************/
68
70{
71 public:
72 struct timeval timekeeper;
73 int min_remain;
74 uint32_t max_zero_timeus;
75
77 {
78 reflesh(0);
79 };
80
81 void reflesh(int rem);
82 void update_remain(int rem);
83
84 uint32_t measure_interval();
85 void measure_start();
86 void measure_stop();
87
88 uint32_t zero_time() { return max_zero_timeus; };
89 int minimum_remain() { return min_remain; };
90};
91
92/****************************************************************************
93 * class: audiolite_mempool
94 ****************************************************************************/
95
97{
98 public:
99 virtual ~audiolite_mempool(){};
100 virtual audiolite_mem *allocate(bool blocking = true) = 0;
101 virtual void memfree(audiolite_mem *mem) = 0;
102 virtual void disable_pool() = 0;
103 virtual void enable_pool() = 0;
104 virtual int remaining() = 0;
105};
106
107/****************************************************************************
108 * class: audiolite_mem
109 ****************************************************************************/
110
111class audiolite_mem : public mossfw_data_t
112{
113 private:
114 mossfw_allocator_t _alloc;
115
116 protected:
117 int _sz;
118
119 public:
120 audiolite_mem(void);
121 virtual ~audiolite_mem(void){};
122
123 protected:
124 virtual void setup_instance(int sz, char *mem,
125 audiolite_mempool *pool);
126 int unrefer();
127
128 private:
129 audiolite_mem *operator=(audiolite_mem *mem);
130 static void mempoolmossfw_free(void *priv, mossfw_data_t *mem);
131
132 public:
133
134 void release(void);
135 virtual audiolite_mem *reference(void);
136
137 void *get_data(void) { return (void *)data.xc; };
138 int get_fs(void) { return fs; };
139 void set_fs(int hz) { fs = hz; };
140 int get_storedsize(void) { return data_bytes; };
141 void set_storedsize(int sz) { data_bytes = sz; };
142 int get_fullsize(void) { return _sz; };
143 int get_typebytes(void);
144 virtual void set_eof() = 0;
145 virtual void clear_eof() = 0;
146 virtual bool is_eof() = 0;
147
148 friend audiolite_mempool;
149};
150
151/****************************************************************************
152 * class: audiolite_memapbuf
153 ****************************************************************************/
154
156{
157 protected:
158 struct ap_buffer_s _abuf;
159
160 public:
161 audiolite_memapbuf(void);
162 void setup_instance(int sz, char *mem,
163 audiolite_mempool *pool);
164 void reset_audiodata(void);
165 void set_fs(int hz);
166 void set_storedsize(int sz);
167 void set_channels(int ch);
168 int get_channels(void);
169 void set_eof();
170 void clear_eof();
171 bool is_eof();
172 struct ap_buffer_s *get_raw_abuf();
173 dq_entry_t *get_link(void);
174
175 static audiolite_memapbuf *local_cast(dq_entry_t *ent);
176};
177
178/****************************************************************************
179 * class: audiolite_mempoolapbuf
180 ****************************************************************************/
181
183{
184 private:
185 audiolite_memapbuf *_insts;
186 struct dq_queue_s _free_mem;
187 int _blknum;
188 char * _ownmem;
189 bool _pool_enable;
190 mossfw_lock_t _lock;
191 mossfw_condition_t _cond;
192
193 void _add_freemem(audiolite_memapbuf *mem);
194 bool _create_instance(int block_size, int block_num,
195 char *mem, int memsize);
196
197 public:
200 bool create_instance(int block_size, int block_num);
201 bool create_instance(int block_size, int block_num,
202 char *mem, int memsize);
203 virtual audiolite_mem *allocate(bool blocking = true);
204 virtual void memfree(audiolite_mem *mem);
205 void disable_pool();
206 void enable_pool();
207 int remaining();
208};
209
210/****************************************************************************
211 * class: audiolite_sysmsg
212 ****************************************************************************/
213
215{
216 dq_entry_t link;
217 int evtid;
218 void *issuer;
219 unsigned long arg;
220};
221
223{
224 protected:
225 struct audiolite_evtmsg_s _msg;
226
227 public:
228 audiolite_sysmsg(void);
229
230 void setup_instance(audiolite_mempool *pool);
231 int get_evtid() { return _msg.evtid; };
232 void *get_issuer() { return _msg.issuer; };
233 unsigned long get_arg() { return _msg.arg; };
234 void set_msgcontent(int evtid, void *issuer, unsigned long arg);
235
236 dq_entry_t *get_link(void) { return &_msg.link; };
237
238 void set_eof() {};
239 void clear_eof() {};
240 bool is_eof() { return false; };
241
242 static audiolite_sysmsg *local_cast(dq_entry_t *ent);
243};
244
245/****************************************************************************
246 * class: audiolite_mempoolsysmsg
247 ****************************************************************************/
248
250{
251 private:
252 audiolite_sysmsg *_insts;
253 int _msgnum;
254 struct dq_queue_s _free_mem;
255 bool _pool_enable;
256 mossfw_lock_t _lock;
257 mossfw_condition_t _cond;
258
259 void _add_freemem(audiolite_sysmsg *mem);
260
261 public:
264 bool create_instance(int msgnum);
265 virtual audiolite_mem *allocate(bool blocking = true);
266 virtual void memfree(audiolite_mem *mem);
267 void disable_pool();
268 void enable_pool();
269 int remaining();
270};
271
272#endif /* __INCLUDE_AUDIOLITE_MEMALLOC_H */
Definition: al_memalloc.h:112
Definition: al_memalloc.h:156
Definition: al_memalloc.h:97
Definition: al_memalloc.h:183
Definition: al_memalloc.h:250
Definition: al_memalloc.h:223
Definition: al_memalloc.h:70
Definition: al_memalloc.h:215