Developer World
Spresense SDK Library v3.2.0-ebc0364
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};
105
106/****************************************************************************
107 * class: audiolite_mem
108 ****************************************************************************/
109
110class audiolite_mem : public mossfw_data_t
111{
112 private:
113 mossfw_allocator_t _alloc;
114
115 protected:
116 int _sz;
117
118 public:
119 audiolite_mem(void);
120 virtual ~audiolite_mem(void){};
121
122 protected:
123 virtual void setup_instance(int sz, char *mem,
124 audiolite_mempool *pool);
125 int unrefer();
126
127 private:
128 audiolite_mem *operator=(audiolite_mem *mem);
129 static void mempoolmossfw_free(void *priv, mossfw_data_t *mem);
130
131 public:
132
133 void release(void);
134 virtual audiolite_mem *reference(void);
135
136 void *get_data(void) { return (void *)data.xc; };
137 int get_fs(void) { return fs; };
138 void set_fs(int hz) { fs = hz; };
139 int get_storedsize(void) { return data_bytes; };
140 void set_storedsize(int sz) { data_bytes = sz; };
141 int get_fullsize(void) { return _sz; };
142 int get_typebytes(void);
143 virtual void set_eof() = 0;
144 virtual void clear_eof() = 0;
145 virtual bool is_eof() = 0;
146
147 friend audiolite_mempool;
148};
149
150/****************************************************************************
151 * class: audiolite_memapbuf
152 ****************************************************************************/
153
155{
156 protected:
157 struct ap_buffer_s _abuf;
158
159 public:
160 audiolite_memapbuf(void);
161 void setup_instance(int sz, char *mem,
162 audiolite_mempool *pool);
163 void reset_audiodata(void);
164 void set_fs(int hz);
165 void set_storedsize(int sz);
166 void set_channels(int ch);
167 int get_channels(void);
168 void set_eof();
169 void clear_eof();
170 bool is_eof();
171 struct ap_buffer_s *get_raw_abuf();
172 dq_entry_t *get_link(void);
173
174 static audiolite_memapbuf *local_cast(dq_entry_t *ent);
175};
176
177/****************************************************************************
178 * class: audiolite_mempoolapbuf
179 ****************************************************************************/
180
182{
183 private:
184 audiolite_memapbuf *_insts;
185 struct dq_queue_s _free_mem;
186 int _blknum;
187 char * _ownmem;
188 bool _pool_enable;
189 mossfw_lock_t _lock;
190 mossfw_condition_t _cond;
191
192 void _add_freemem(audiolite_memapbuf *mem);
193 bool _create_instance(int block_size, int block_num,
194 char *mem, int memsize);
195
196 public:
199 bool create_instance(int block_size, int block_num);
200 bool create_instance(int block_size, int block_num,
201 char *mem, int memsize);
202 virtual audiolite_mem *allocate(bool blocking = true);
203 virtual void memfree(audiolite_mem *mem);
204 void disable_pool();
205 void enable_pool();
206};
207
208/****************************************************************************
209 * class: audiolite_sysmsg
210 ****************************************************************************/
211
213{
214 dq_entry_t link;
215 int evtid;
216 void *issuer;
217 unsigned long arg;
218};
219
221{
222 protected:
223 struct audiolite_evtmsg_s _msg;
224
225 public:
226 audiolite_sysmsg(void);
227
228 void setup_instance(audiolite_mempool *pool);
229 int get_evtid() { return _msg.evtid; };
230 void *get_issuer() { return _msg.issuer; };
231 unsigned long get_arg() { return _msg.arg; };
232 void set_msgcontent(int evtid, void *issuer, unsigned long arg);
233
234 dq_entry_t *get_link(void) { return &_msg.link; };
235
236 void set_eof() {};
237 void clear_eof() {};
238 bool is_eof() { return false; };
239
240 static audiolite_sysmsg *local_cast(dq_entry_t *ent);
241};
242
243/****************************************************************************
244 * class: audiolite_mempoolsysmsg
245 ****************************************************************************/
246
248{
249 private:
250 audiolite_sysmsg *_insts;
251 int _msgnum;
252 struct dq_queue_s _free_mem;
253 bool _pool_enable;
254 mossfw_lock_t _lock;
255 mossfw_condition_t _cond;
256
257 void _add_freemem(audiolite_sysmsg *mem);
258
259 public:
262 bool create_instance(int msgnum);
263 virtual audiolite_mem *allocate(bool blocking = true);
264 virtual void memfree(audiolite_mem *mem);
265 void disable_pool();
266 void enable_pool();
267};
268
269#endif /* __INCLUDE_AUDIOLITE_MEMALLOC_H */
Definition: al_memalloc.h:111
Definition: al_memalloc.h:155
Definition: al_memalloc.h:97
Definition: al_memalloc.h:182
Definition: al_memalloc.h:248
Definition: al_memalloc.h:221
Definition: al_memalloc.h:70
Definition: al_memalloc.h:213