leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2016-2017 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 5 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 //
leothedragon 0:8f0bb79ddd48 8 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 // limitations under the License.
leothedragon 0:8f0bb79ddd48 15 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 16
leothedragon 0:8f0bb79ddd48 17 #ifdef FCC_MEM_STATS_ENABLED
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #include <stdlib.h>
leothedragon 0:8f0bb79ddd48 20 #include <inttypes.h>
leothedragon 0:8f0bb79ddd48 21
leothedragon 0:8f0bb79ddd48 22 #include "fcc_malloc.h"
leothedragon 0:8f0bb79ddd48 23 #include "pv_log.h"
leothedragon 0:8f0bb79ddd48 24
leothedragon 0:8f0bb79ddd48 25 typedef struct {
leothedragon 0:8f0bb79ddd48 26 uint32_t current_size; /* Bytes allocated currently */
leothedragon 0:8f0bb79ddd48 27 uint32_t total_size; /* Cumulative sum of bytes ever allocated. */
leothedragon 0:8f0bb79ddd48 28 uint32_t max_peak_size; /* Max peak allocated at a certain time (e.g.: getting worst case memory usage) */
leothedragon 0:8f0bb79ddd48 29 uint32_t alloc_cnt; /* Current number of allocations. */
leothedragon 0:8f0bb79ddd48 30 uint32_t free_cnt; /* Current number of frees. */
leothedragon 0:8f0bb79ddd48 31 uint32_t alloc_fail_cnt; /* Number of failed allocations. */
leothedragon 0:8f0bb79ddd48 32 } stats_heap_t;
leothedragon 0:8f0bb79ddd48 33
leothedragon 0:8f0bb79ddd48 34 /* Size must be a multiple of 8 to keep alignment */
leothedragon 0:8f0bb79ddd48 35 typedef struct {
leothedragon 0:8f0bb79ddd48 36 uint32_t size;
leothedragon 0:8f0bb79ddd48 37 uint32_t pad;
leothedragon 0:8f0bb79ddd48 38 } alloc_info_t;
leothedragon 0:8f0bb79ddd48 39
leothedragon 0:8f0bb79ddd48 40 static stats_heap_t g_fcc_heap_stats = { 0, 0, 0, 0, 0, 0 };
leothedragon 0:8f0bb79ddd48 41
leothedragon 0:8f0bb79ddd48 42 void *fcc_malloc(size_t size)
leothedragon 0:8f0bb79ddd48 43 {
leothedragon 0:8f0bb79ddd48 44 void *ptr = NULL;
leothedragon 0:8f0bb79ddd48 45 alloc_info_t *alloc_info = (alloc_info_t *)malloc(sizeof(alloc_info_t) + size);
leothedragon 0:8f0bb79ddd48 46
leothedragon 0:8f0bb79ddd48 47 if (alloc_info != NULL) {
leothedragon 0:8f0bb79ddd48 48 alloc_info->size = size;
leothedragon 0:8f0bb79ddd48 49 ptr = (void *)(alloc_info + 1);
leothedragon 0:8f0bb79ddd48 50
leothedragon 0:8f0bb79ddd48 51 g_fcc_heap_stats.current_size += size;
leothedragon 0:8f0bb79ddd48 52 g_fcc_heap_stats.total_size += size;
leothedragon 0:8f0bb79ddd48 53 g_fcc_heap_stats.alloc_cnt += 1;
leothedragon 0:8f0bb79ddd48 54
leothedragon 0:8f0bb79ddd48 55 if (g_fcc_heap_stats.current_size > g_fcc_heap_stats.max_peak_size) {
leothedragon 0:8f0bb79ddd48 56 g_fcc_heap_stats.max_peak_size = g_fcc_heap_stats.current_size;
leothedragon 0:8f0bb79ddd48 57 }
leothedragon 0:8f0bb79ddd48 58 } else {
leothedragon 0:8f0bb79ddd48 59 g_fcc_heap_stats.alloc_fail_cnt += 1;
leothedragon 0:8f0bb79ddd48 60 }
leothedragon 0:8f0bb79ddd48 61
leothedragon 0:8f0bb79ddd48 62 return ptr;
leothedragon 0:8f0bb79ddd48 63 }
leothedragon 0:8f0bb79ddd48 64
leothedragon 0:8f0bb79ddd48 65 void fcc_free(void *ptr)
leothedragon 0:8f0bb79ddd48 66 {
leothedragon 0:8f0bb79ddd48 67 alloc_info_t *alloc_info = NULL;
leothedragon 0:8f0bb79ddd48 68
leothedragon 0:8f0bb79ddd48 69 if (ptr != NULL) {
leothedragon 0:8f0bb79ddd48 70 alloc_info = ((alloc_info_t *)ptr) - 1;
leothedragon 0:8f0bb79ddd48 71 g_fcc_heap_stats.current_size -= alloc_info->size;
leothedragon 0:8f0bb79ddd48 72 g_fcc_heap_stats.free_cnt += 1;
leothedragon 0:8f0bb79ddd48 73 free(alloc_info);
leothedragon 0:8f0bb79ddd48 74 }
leothedragon 0:8f0bb79ddd48 75 }
leothedragon 0:8f0bb79ddd48 76
leothedragon 0:8f0bb79ddd48 77 void fcc_stats_print_summary(void)
leothedragon 0:8f0bb79ddd48 78 {
leothedragon 0:8f0bb79ddd48 79 // Use printf since this is printed after mbed trace has been destroyed.
leothedragon 0:8f0bb79ddd48 80 printf(" ********* FCC Heap Statistics *********\n");
leothedragon 0:8f0bb79ddd48 81 printf(" * Total bytes allocated: %" PRIu32 "\n", g_fcc_heap_stats.total_size);
leothedragon 0:8f0bb79ddd48 82 printf(" * Max peak ever allocated: %" PRIu32 "\n", g_fcc_heap_stats.max_peak_size);
leothedragon 0:8f0bb79ddd48 83 printf(" * Number of allocation succeeded: %" PRIu32 "\n", g_fcc_heap_stats.alloc_cnt);
leothedragon 0:8f0bb79ddd48 84 printf(" * Number of frees succeeded: %" PRIu32 "\n", g_fcc_heap_stats.free_cnt);
leothedragon 0:8f0bb79ddd48 85 printf(" * Number of allocation failed: %" PRIu32 "\n", g_fcc_heap_stats.alloc_fail_cnt);
leothedragon 0:8f0bb79ddd48 86 printf(" ***************************************\n");
leothedragon 0:8f0bb79ddd48 87 }
leothedragon 0:8f0bb79ddd48 88
leothedragon 0:8f0bb79ddd48 89 #endif //FCC_MEM_STATS_ENABLED