|
15 | 15 | */ |
16 | 16 | #include <stdint.h> |
17 | 17 | #include <string.h> |
| 18 | +#undef NSDYNMEM_TRACKER_ENABLED |
18 | 19 | #include "nsdynmemLIB.h" |
19 | 20 | #include "platform/arm_hal_interrupt.h" |
20 | 21 | #include <stdlib.h> |
@@ -47,6 +48,7 @@ struct ns_mem_book { |
47 | 48 | ns_mem_heap_size_t temporary_alloc_heap_limit; /* Amount of reserved heap temporary alloc can't exceed */ |
48 | 49 | }; |
49 | 50 |
|
| 51 | +static mem_stat_t default_stats; |
50 | 52 | static ns_mem_book_t *default_book; // heap pointer for original "ns_" API use |
51 | 53 |
|
52 | 54 | // size of a hole_t in our word units |
@@ -162,12 +164,14 @@ ns_mem_book_t *ns_mem_init(void *heap, ns_mem_heap_size_t h_size, |
162 | 164 | ns_list_init(&book->holes_list); |
163 | 165 | ns_list_add_to_start(&book->holes_list, hole_from_block_start(book->heap_main[0])); |
164 | 166 |
|
165 | | - book->mem_stat_info_ptr = info_ptr; |
166 | | - //RESET Memory by Hea Len |
167 | 167 | if (info_ptr) { |
168 | | - memset(book->mem_stat_info_ptr, 0, sizeof(mem_stat_t)); |
169 | | - book->mem_stat_info_ptr->heap_sector_size = book->heap_size; |
| 168 | + book->mem_stat_info_ptr = info_ptr; |
| 169 | + } else { |
| 170 | + book->mem_stat_info_ptr = &default_stats; |
170 | 171 | } |
| 172 | + //RESET Memory by Heap Len |
| 173 | + memset(book->mem_stat_info_ptr, 0, sizeof(mem_stat_t)); |
| 174 | + book->mem_stat_info_ptr->heap_sector_size = book->heap_size; |
171 | 175 | book->temporary_alloc_heap_limit = book->heap_size / 100 * (100 - TEMPORARY_ALLOC_FREE_HEAP_THRESHOLD); |
172 | 176 | #endif |
173 | 177 | //There really is no support to standard malloc in this library anymore |
@@ -234,9 +238,7 @@ int ns_mem_region_add(ns_mem_book_t *book, void *region_ptr, ns_mem_heap_size_t |
234 | 238 | // adjust total heap size with new hole |
235 | 239 | book->heap_size += region_size; |
236 | 240 |
|
237 | | - if (book->mem_stat_info_ptr) { |
238 | | - book->mem_stat_info_ptr->heap_sector_size = book->heap_size; |
239 | | - } |
| 241 | + book->mem_stat_info_ptr->heap_sector_size = book->heap_size; |
240 | 242 |
|
241 | 243 | // adjust temporary allocation limits to match new heap |
242 | 244 | book->temporary_alloc_heap_limit = book->heap_size / 100 * (100 - TEMPORARY_ALLOC_FREE_HEAP_THRESHOLD); |
@@ -265,8 +267,8 @@ int ns_mem_set_temporary_alloc_free_heap_threshold(ns_mem_book_t *book, uint8_t |
265 | 267 | #ifndef STANDARD_MALLOC |
266 | 268 | ns_mem_heap_size_t heap_limit = 0; |
267 | 269 |
|
268 | | - if (!book || !book->mem_stat_info_ptr) { |
269 | | - // no book or mem_stats |
| 270 | + if (!book) { |
| 271 | + // no book |
270 | 272 | return -1; |
271 | 273 | } |
272 | 274 |
|
@@ -304,25 +306,25 @@ extern int ns_dyn_mem_set_temporary_alloc_free_heap_threshold(uint8_t free_heap_ |
304 | 306 | #ifndef STANDARD_MALLOC |
305 | 307 | static void dev_stat_update(mem_stat_t *mem_stat_info_ptr, mem_stat_update_t type, ns_mem_block_size_t size) |
306 | 308 | { |
307 | | - if (mem_stat_info_ptr) { |
308 | | - switch (type) { |
309 | | - case DEV_HEAP_ALLOC_OK: |
310 | | - mem_stat_info_ptr->heap_sector_alloc_cnt++; |
311 | | - mem_stat_info_ptr->heap_sector_allocated_bytes += size; |
312 | | - if (mem_stat_info_ptr->heap_sector_allocated_bytes_max < mem_stat_info_ptr->heap_sector_allocated_bytes) { |
313 | | - mem_stat_info_ptr->heap_sector_allocated_bytes_max = mem_stat_info_ptr->heap_sector_allocated_bytes; |
314 | | - } |
315 | | - mem_stat_info_ptr->heap_alloc_total_bytes += size; |
316 | | - break; |
317 | | - case DEV_HEAP_ALLOC_FAIL: |
318 | | - mem_stat_info_ptr->heap_alloc_fail_cnt++; |
319 | | - break; |
320 | | - case DEV_HEAP_FREE: |
321 | | - mem_stat_info_ptr->heap_sector_alloc_cnt--; |
322 | | - mem_stat_info_ptr->heap_sector_allocated_bytes -= size; |
323 | | - break; |
324 | | - } |
| 309 | + |
| 310 | + switch (type) { |
| 311 | + case DEV_HEAP_ALLOC_OK: |
| 312 | + mem_stat_info_ptr->heap_sector_alloc_cnt++; |
| 313 | + mem_stat_info_ptr->heap_sector_allocated_bytes += size; |
| 314 | + if (mem_stat_info_ptr->heap_sector_allocated_bytes_max < mem_stat_info_ptr->heap_sector_allocated_bytes) { |
| 315 | + mem_stat_info_ptr->heap_sector_allocated_bytes_max = mem_stat_info_ptr->heap_sector_allocated_bytes; |
| 316 | + } |
| 317 | + mem_stat_info_ptr->heap_alloc_total_bytes += size; |
| 318 | + break; |
| 319 | + case DEV_HEAP_ALLOC_FAIL: |
| 320 | + mem_stat_info_ptr->heap_alloc_fail_cnt++; |
| 321 | + break; |
| 322 | + case DEV_HEAP_FREE: |
| 323 | + mem_stat_info_ptr->heap_sector_alloc_cnt--; |
| 324 | + mem_stat_info_ptr->heap_sector_allocated_bytes -= size; |
| 325 | + break; |
325 | 326 | } |
| 327 | + |
326 | 328 | } |
327 | 329 |
|
328 | 330 | static ns_mem_word_size_t convert_allocation_size(ns_mem_book_t *book, ns_mem_block_size_t requested_bytes) |
@@ -363,7 +365,7 @@ static void *ns_mem_internal_alloc(ns_mem_book_t *book, const ns_mem_block_size_ |
363 | 365 | return NULL; |
364 | 366 | } |
365 | 367 |
|
366 | | - if (book->mem_stat_info_ptr && direction == 1) { |
| 368 | + if (direction == 1) { |
367 | 369 | if (book->mem_stat_info_ptr->heap_sector_allocated_bytes > book->temporary_alloc_heap_limit) { |
368 | 370 | /* Not enough heap for temporary memory allocation */ |
369 | 371 | dev_stat_update(book->mem_stat_info_ptr, DEV_HEAP_ALLOC_FAIL, 0); |
@@ -441,14 +443,14 @@ static void *ns_mem_internal_alloc(ns_mem_book_t *book, const ns_mem_block_size_ |
441 | 443 | block_ptr[1 + data_size] = data_size; |
442 | 444 |
|
443 | 445 | done: |
444 | | - if (book->mem_stat_info_ptr) { |
445 | | - if (block_ptr) { |
446 | | - //Update Allocate OK |
447 | | - dev_stat_update(book->mem_stat_info_ptr, DEV_HEAP_ALLOC_OK, (data_size + 2) * sizeof(ns_mem_word_size_t)); |
448 | | - } else { |
449 | | - //Update Allocate Fail, second parameter is used for stats |
450 | | - dev_stat_update(book->mem_stat_info_ptr, DEV_HEAP_ALLOC_FAIL, 0); |
451 | | - } |
| 446 | + |
| 447 | + |
| 448 | + if (block_ptr) { |
| 449 | + //Update Allocate OK |
| 450 | + dev_stat_update(book->mem_stat_info_ptr, DEV_HEAP_ALLOC_OK, (data_size + 2) * sizeof(ns_mem_word_size_t)); |
| 451 | + } else { |
| 452 | + //Update Allocate Fail, second parameter is used for stats |
| 453 | + dev_stat_update(book->mem_stat_info_ptr, DEV_HEAP_ALLOC_FAIL, 0); |
452 | 454 | } |
453 | 455 | platform_exit_critical(); |
454 | 456 |
|
@@ -619,10 +621,8 @@ void ns_mem_free(ns_mem_book_t *book, void *block) |
619 | 621 | heap_failure(book, NS_DYN_MEM_HEAP_SECTOR_CORRUPTED); |
620 | 622 | } else { |
621 | 623 | ns_mem_free_and_merge_with_adjacent_blocks(book, ptr, size); |
622 | | - if (book->mem_stat_info_ptr) { |
623 | | - //Update Free Counter |
624 | | - dev_stat_update(book->mem_stat_info_ptr, DEV_HEAP_FREE, (size + 2) * sizeof(ns_mem_word_size_t)); |
625 | | - } |
| 624 | + //Update Free Counter |
| 625 | + dev_stat_update(book->mem_stat_info_ptr, DEV_HEAP_FREE, (size + 2) * sizeof(ns_mem_word_size_t)); |
626 | 626 | } |
627 | 627 | } |
628 | 628 |
|
|
0 commit comments