39
39
#include "system/lwmem_sys.h"
40
40
#endif /* LWMEM_CFG_OS */
41
41
42
- #define LWMEM_MEMSET memset
43
- #define LWMEM_MEMCPY memcpy
44
- #define LWMEM_MEMMOVE memmove
45
-
46
42
/**
47
43
* \brief Transform alignment number (power of `2`) to bits
48
44
*/
@@ -176,19 +172,19 @@ static lwmem_t lwmem_default;
176
172
* \brief Get region aligned start address and aligned size
177
173
* \param[in] region: Region to check for size and address
178
174
* \param[out] msa: Memory start address output variable
179
- * \param[out] ms : Memory size output variable
175
+ * \param[out] msz : Memory size output variable
180
176
* \return `1` if region valid, `0` otherwise
181
177
*/
182
178
static uint8_t
183
- prv_get_region_addr_size (const lwmem_region_t * region , uint8_t * * msa , size_t * ms ) {
179
+ prv_get_region_addr_size (const lwmem_region_t * region , uint8_t * * msa , size_t * msz ) {
184
180
size_t mem_size ;
185
181
uint8_t * mem_start_addr ;
186
182
187
- if (region == NULL || msa == NULL || ms == NULL ) {
183
+ if (region == NULL || msa == NULL || msz == NULL ) {
188
184
return 0 ;
189
185
}
190
186
* msa = NULL ;
191
- * ms = 0 ;
187
+ * msz = 0 ;
192
188
193
189
/* Check region size and align it to config bits */
194
190
mem_size = region -> size & ~LWMEM_ALIGN_BITS ; /* Size does not include lower bits */
@@ -209,7 +205,7 @@ prv_get_region_addr_size(const lwmem_region_t* region, uint8_t** msa, size_t* ms
209
205
/* Check final memory size */
210
206
if (mem_size >= (2 * LWMEM_BLOCK_MIN_SIZE )) {
211
207
* msa = mem_start_addr ;
212
- * ms = mem_size ;
208
+ * msz = mem_size ;
213
209
214
210
return 1 ;
215
211
}
@@ -219,22 +215,22 @@ prv_get_region_addr_size(const lwmem_region_t* region, uint8_t** msa, size_t* ms
219
215
/**
220
216
* \brief Insert free block to linked list of free blocks
221
217
* \param[in] lwobj: LwMEM instance. Set to `NULL` to use default instance
222
- * \param[in] nb : New free block to insert into linked list
218
+ * \param[in] nblk : New free block to insert into linked list
223
219
*/
224
220
static void
225
- prv_insert_free_block (lwmem_t * const lwobj , lwmem_block_t * nb ) {
221
+ prv_insert_free_block (lwmem_t * const lwobj , lwmem_block_t * nblk ) {
226
222
lwmem_block_t * prev ;
227
223
228
224
/* Check valid inputs */
229
- if (nb == NULL ) {
225
+ if (nblk == NULL ) {
230
226
return ;
231
227
}
232
228
233
229
/*
234
230
* Try to find position to put new block in-between
235
231
* Search until all free block addresses are lower than entry block
236
232
*/
237
- for (prev = & (lwobj -> start_block ); prev != NULL && prev -> next < nb ; prev = prev -> next ) {}
233
+ for (prev = & (lwobj -> start_block ); prev != NULL && prev -> next < nblk ; prev = prev -> next ) {}
238
234
239
235
/* This is hard error with wrong memory usage */
240
236
if (prev == NULL ) {
@@ -254,10 +250,10 @@ prv_insert_free_block(lwmem_t* const lwobj, lwmem_block_t* nb) {
254
250
* By doing this, we protect data left by app
255
251
* and we make sure new allocations cannot see old information
256
252
*/
257
- if (nb != NULL ) {
258
- void * p = LWMEM_GET_PTR_FROM_BLOCK (nb );
259
- if (p != NULL ) {
260
- LWMEM_MEMSET (p , 0x00 , nb -> size - LWMEM_BLOCK_META_SIZE );
253
+ if (nblk != NULL ) {
254
+ void * ptr = LWMEM_GET_PTR_FROM_BLOCK (nblk );
255
+ if (ptr != NULL ) {
256
+ LWMEM_MEMSET (ptr , 0x00 , nblk -> size - LWMEM_BLOCK_META_SIZE );
261
257
}
262
258
}
263
259
#endif /* LWMEM_CFG_RESET_MEMORY */
@@ -266,10 +262,10 @@ prv_insert_free_block(lwmem_t* const lwobj, lwmem_block_t* nb) {
266
262
* Check if previous block and input block together create one big contiguous block
267
263
* If this is the case, merge blocks together and increase previous block by input block size
268
264
*/
269
- if ((LWMEM_TO_BYTE_PTR (prev ) + prev -> size ) == LWMEM_TO_BYTE_PTR (nb )) {
270
- prev -> size += nb -> size ; /* Increase current block by size of new block */
271
- nb = prev ; /* New block and current are now the same thing */
272
- /*
265
+ if ((LWMEM_TO_BYTE_PTR (prev ) + prev -> size ) == LWMEM_TO_BYTE_PTR (nblk )) {
266
+ prev -> size += nblk -> size ; /* Increase current block by size of new block */
267
+ nblk = prev ; /* New block and current are now the same thing */
268
+ /*
273
269
* It is important to set new block as current one
274
270
* as this allows merging previous and next blocks together with new block
275
271
* at the same time; follow next steps
@@ -281,25 +277,24 @@ prv_insert_free_block(lwmem_t* const lwobj, lwmem_block_t* nb) {
281
277
* Do not merge with "end of region" indication (commented part of if statement)
282
278
*/
283
279
if (prev -> next != NULL && prev -> next -> size > 0 /* Do not remove "end of region" indicator in each region */
284
- && (LWMEM_TO_BYTE_PTR (nb ) + nb -> size ) == LWMEM_TO_BYTE_PTR (prev -> next )) {
280
+ && (LWMEM_TO_BYTE_PTR (nblk ) + nblk -> size ) == LWMEM_TO_BYTE_PTR (prev -> next )) {
285
281
if (prev -> next == lwobj -> end_block ) { /* Does it points to the end? */
286
- nb -> next = lwobj -> end_block ; /* Set end block pointer */
282
+ nblk -> next = lwobj -> end_block ; /* Set end block pointer */
287
283
} else {
288
- nb -> size +=
289
- prev -> next
290
- -> size ; /* Expand of current block for size of next free block which is right behind new block */
291
- nb -> next = prev -> next -> next ; /* Next free is pointed to the next one of previous next */
284
+ /* Expand of current block for size of next free block which is right behind new block */
285
+ nblk -> size += prev -> next -> size ;
286
+ nblk -> next = prev -> next -> next ; /* Next free is pointed to the next one of previous next */
292
287
}
293
288
} else {
294
- nb -> next = prev -> next ; /* Set next of input block as next of current one */
289
+ nblk -> next = prev -> next ; /* Set next of input block as next of current one */
295
290
}
296
291
297
292
/*
298
293
* If new block has not been set as current (and expanded),
299
294
* then link them together, otherwise ignore as it would point to itself
300
295
*/
301
- if (prev != nb ) {
302
- prev -> next = nb ;
296
+ if (prev != nblk ) {
297
+ prev -> next = nblk ;
303
298
}
304
299
}
305
300
0 commit comments