Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fcc_stats.c Source File

fcc_stats.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //  
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //  
00008 //     http://www.apache.org/licenses/LICENSE-2.0
00009 //  
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 // ----------------------------------------------------------------------------
00016 
00017 #ifdef FCC_MEM_STATS_ENABLED
00018 
00019 #include <stdlib.h>
00020 #include <inttypes.h>
00021 
00022 #include "fcc_malloc.h"
00023 #include "pv_log.h"
00024 
00025 typedef struct {
00026     uint32_t current_size;      /* Bytes allocated currently */
00027     uint32_t total_size;        /* Cumulative sum of bytes ever allocated. */
00028     uint32_t max_peak_size;     /* Max peak allocated at a certain time (e.g.: getting worst case memory usage) */
00029     uint32_t alloc_cnt;         /* Current number of allocations. */
00030     uint32_t free_cnt;          /* Current number of frees. */
00031     uint32_t alloc_fail_cnt;    /* Number of failed allocations. */
00032 } stats_heap_t;
00033 
00034 /* Size must be a multiple of 8 to keep alignment */
00035 typedef struct {
00036     uint32_t size;
00037     uint32_t pad;
00038 } alloc_info_t;
00039 
00040 static stats_heap_t g_fcc_heap_stats = { 0, 0, 0, 0, 0, 0 };
00041 
00042 void *fcc_malloc(size_t size)
00043 {
00044     void *ptr = NULL;
00045     alloc_info_t *alloc_info = (alloc_info_t *)malloc(sizeof(alloc_info_t) + size);
00046 
00047     if (alloc_info != NULL) {
00048         alloc_info->size = size;
00049         ptr = (void *)(alloc_info + 1);
00050 
00051         g_fcc_heap_stats.current_size += size;
00052         g_fcc_heap_stats.total_size += size;
00053         g_fcc_heap_stats.alloc_cnt += 1;
00054 
00055         if (g_fcc_heap_stats.current_size > g_fcc_heap_stats.max_peak_size) {
00056             g_fcc_heap_stats.max_peak_size = g_fcc_heap_stats.current_size;
00057         }
00058     } else {
00059         g_fcc_heap_stats.alloc_fail_cnt += 1;
00060     }
00061 
00062     return ptr;
00063 }
00064 
00065 void fcc_free(void *ptr)
00066 {
00067     alloc_info_t *alloc_info = NULL;
00068 
00069     if (ptr != NULL) {
00070         alloc_info = ((alloc_info_t *)ptr) - 1;
00071         g_fcc_heap_stats.current_size -= alloc_info->size;
00072         g_fcc_heap_stats.free_cnt += 1;
00073         free(alloc_info);
00074     }
00075 }
00076 
00077 void fcc_stats_print_summary(void)
00078 {
00079     // Use printf since this is printed after mbed trace has been destroyed.
00080     printf("  ********* FCC Heap Statistics *********\n");
00081     printf("  * Total bytes allocated:           %" PRIu32 "\n", g_fcc_heap_stats.total_size);
00082     printf("  * Max peak ever allocated:         %" PRIu32 "\n", g_fcc_heap_stats.max_peak_size);
00083     printf("  * Number of allocation succeeded:  %" PRIu32 "\n", g_fcc_heap_stats.alloc_cnt);
00084     printf("  * Number of frees succeeded:       %" PRIu32 "\n", g_fcc_heap_stats.free_cnt);
00085     printf("  * Number of allocation failed:     %" PRIu32 "\n", g_fcc_heap_stats.alloc_fail_cnt);
00086     printf("  ***************************************\n");
00087 }
00088 
00089 #endif //FCC_MEM_STATS_ENABLED