audio_mem_mgnt.c
Go to the documentation of this file.
1 
18 #include "audio_mem_mgnt.h"
19 #include <stdlib.h> /* malloc, free, rand */
20 #define AUDIO_MEM_UNUSED_VAR(x) ((void)(x))
21 
22 #if defined(AUDIO_CONF_TRACK_MALLOC)
23 #define AUDIO_MAX_MEMBLOCKS_NB 3000
24 
25 typedef struct
26 {
27  int32_t size;
28  void *ptr;
29 } AudioMemBlock_t;
30 
31 typedef struct
32 {
33  int32_t totalSize;
34  AudioMemBlock_t memBlocks[AUDIO_MAX_MEMBLOCKS_NB];
35 } AudioMemoryCurrent_t;
36 
37 int32_t AFE_currentBlock = 0;
38 AudioMemoryCurrent_t AudioMem;
39 #endif
40 
41 /* cache flush */
42 #define _CCSIDR_LSSHIFT(x) (((x) & SCB_CCSIDR_LINESIZE_Msk) >> SCB_CCSIDR_LINESIZE_Pos)
43 
44 void CPU_CACHE_Flush(uint8_t *const buffer, uint32_t const length_in_bytes)
45 {
46 #ifdef __DCACHE_PRESENT
47  uint32_t ccsidr;
48  uint32_t smask;
49  uint32_t sshift;
50  uint32_t ways;
51  uint32_t wshift;
52  uint32_t ssize;
53  uint32_t sets;
54  uint32_t sw;
55  uint32_t start;
56  uint32_t end;
57 
58  start = (uint32_t)buffer;
59  end = start + (length_in_bytes * 4);
60  /* Get the characteristics of the D-Cache */
61 
62  ccsidr = SCB->CCSIDR;
63  smask = CCSIDR_SETS(ccsidr); /* (Number of sets) - 1 */
64  sshift = _CCSIDR_LSSHIFT(ccsidr) + 4; /* log2(cache-line-size-in-bytes) */
65  ways = CCSIDR_WAYS(ccsidr); /* (Number of ways) - 1 */
66 
67  /* Calculate the bit offset for the way field in the DCCISW register by
68  * counting the number of leading zeroes. For example:
69  *
70  * Number of Value of ways Field
71  * Ways 'ways' Offset
72  * 2 1 31
73  * 4 3 30
74  * 8 7 29
75  * ...
76  */
77 
78  wshift = __CLZ(ways) & 0x1f;
79 
80  /* Clean and invalidate the D-Cache over the range of addresses */
81 
82  ssize = (1 << sshift);
83  start &= ~(ssize - 1);
84  __DSB();
85 
86  do
87  {
88  int32_t tmpways = ways;
89 
90  /* Isolate the cache line associated with this address. For example
91  * if the cache line size is 32 bytes and the cache size is 16KB, then
92  *
93  * sshift = 5 : Offset to the beginning of the set field
94  * smask = 0x007f : Mask of the set field
95  */
96 
97  sets = ((uint32_t)start >> sshift) & smask;
98 
99  /* Clean and invalidate each way for this cacheline */
100 
101  do
102  {
103  sw = ((tmpways << wshift) | (sets << sshift));
104  SCB->DCCISW=sw;
105 
106  }
107  while (tmpways--);
108 
109  /* Increment the address by the size of one cache line. */
110 
111  start += ssize;
112  }
113  while (start < end);
114 
115  __DSB();
116  __ISB();
117 #endif
118 }
119 
120 
121 __weak void *AudioMalloc(size_t const size, int32_t const type)
122 {
123  void *ptr;
124 
125 #if defined(AUDIO_CONF_TRACK_MALLOC)
126  if (AFE_currentBlock < AUDIO_MAX_MEMBLOCKS_NB)
127  {
128  AudioMem.totalSize += size;
129  AudioMem.memBlocks[AFE_currentBlock].size = size;
130  AudioMem.memBlocks[AFE_currentBlock].ptr = malloc(size);
131  ptr = AudioMem.memBlocks[AFE_currentBlock++].ptr;
132  }
133  else
134  {
135  /* more memory blocks than estimated are needed */
136  ptr = NULL;
137  }
138 #else
139 // AUDIO_MEM_UNUSED_VAR(type);
140  ptr = malloc(size);
141 #endif
142 
143  return ptr;
144 }
145 
146 __weak void AudioFree(void *const pMemToFree, int32_t const type)
147 {
148  if (pMemToFree != NULL)
149  {
150 #if defined(AUDIO_CONF_TRACK_MALLOC)
151  int32_t i;
152 
153  for (i = 0; i < AFE_currentBlock; i++)
154  {
155  if (AudioMem.memBlocks[i].ptr == pMemToFree)
156  {
157  AudioMem.totalSize -= AudioMem.memBlocks[i].size;
158  free(pMemToFree);
159  AFE_currentBlock--;
160  break;
161  }
162  }
163  /* This block hasn't been allocated through AFE_malloc() */
164  return; /*Error*/
165 #else
166  free(pMemToFree);
167 #endif
168  }
169 }
170 
171 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
SCB
#define SCB
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:1778
CCSIDR_SETS
#define CCSIDR_SETS(x)
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:2232
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
AudioFree
__weak void AudioFree(void *const pMemToFree, int32_t const type)
Definition: audio_mem_mgnt.c:146
__DSB
__STATIC_FORCEINLINE void __DSB(void)
Data Synchronization Barrier.
Definition: imxrt1050/imxrt1050-evkb/CMSIS/cmsis_gcc.h:944
__CLZ
__STATIC_FORCEINLINE uint8_t __CLZ(uint32_t value)
Count leading zeros.
Definition: imxrt1050/imxrt1050-evkb/CMSIS/cmsis_armclang.h:937
__ISB
__STATIC_FORCEINLINE void __ISB(void)
Instruction Synchronization Barrier.
Definition: imxrt1050/imxrt1050-evkb/CMSIS/cmsis_gcc.h:933
CPU_CACHE_Flush
void CPU_CACHE_Flush(uint8_t *const buffer, uint32_t const length_in_bytes)
Definition: audio_mem_mgnt.c:44
AudioMalloc
__weak void * AudioMalloc(size_t const size, int32_t const type)
Definition: audio_mem_mgnt.c:121
CCSIDR_WAYS
#define CCSIDR_WAYS(x)
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:2231
audio_mem_mgnt.h
malloc management
start
ROSCPP_DECL void start()
_CCSIDR_LSSHIFT
#define _CCSIDR_LSSHIFT(x)
Definition: audio_mem_mgnt.c:42


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:47