1 /*
2 * Percepio Trace Recorder for Tracealyzer v4.9.2
3 * Copyright 2023 Percepio AB
4 * www.percepio.com
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * The implementation for heaps.
9 */
10 
11 #include <trcRecorder.h>
12 
13 #if (TRC_USE_TRACEALYZER_RECORDER == 1) && (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) && (TRC_USE_HEAPS == 1)
14 
15 /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/
xTraceHeapCreate(const char * szName,TraceUnsignedBaseType_t uxCurrent,TraceUnsignedBaseType_t uxHighWaterMark,TraceUnsignedBaseType_t uxMax,TraceHeapHandle_t * pxHeapHandle)16 traceResult xTraceHeapCreate(const char *szName, TraceUnsignedBaseType_t uxCurrent, TraceUnsignedBaseType_t uxHighWaterMark, TraceUnsignedBaseType_t uxMax, TraceHeapHandle_t *pxHeapHandle)
17 {
18 	TraceUnsignedBaseType_t uxStates[3];
19 
20 	uxStates[TRC_HEAP_STATE_INDEX_CURRENT] = uxCurrent;
21 	uxStates[TRC_HEAP_STATE_INDEX_HIGHWATERMARK] = uxHighWaterMark;
22 	uxStates[TRC_HEAP_STATE_INDEX_MAX] = uxMax;
23 
24 	return xTraceObjectRegisterInternal(PSF_EVENT_HEAP_CREATE, (void*)0, szName, 3u, uxStates, TRC_ENTRY_OPTION_HEAP, (TraceObjectHandle_t*)pxHeapHandle);
25 }
26 
xTraceHeapAlloc(TraceHeapHandle_t xHeapHandle,void * pvAddress,TraceUnsignedBaseType_t uxSize)27 traceResult xTraceHeapAlloc(TraceHeapHandle_t xHeapHandle, void *pvAddress, TraceUnsignedBaseType_t uxSize)
28 {
29 	TraceUnsignedBaseType_t uxCurrent, uxHighWaterMark;
30 
31 	if (xHeapHandle == 0)
32 	{
33 		/* This can happen */
34 		return TRC_FAIL;
35 	}
36 
37 	/* If the address is null we assume this was a failed alloc attempt */
38 	if (pvAddress != (void*)0)
39 	{
40 		/* This should never fail */
41 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntryGetState(xHeapHandle, TRC_HEAP_STATE_INDEX_CURRENT, &uxCurrent) == TRC_SUCCESS);
42 
43 		/* This should never fail */
44 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntryGetState(xHeapHandle, TRC_HEAP_STATE_INDEX_HIGHWATERMARK, &uxHighWaterMark) == TRC_SUCCESS);
45 
46 		uxCurrent += uxSize;
47 
48 		if (uxCurrent > uxHighWaterMark)
49 		{
50 			uxHighWaterMark = uxCurrent;
51 			/* This should never fail */
52 			TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetState(xHeapHandle, TRC_HEAP_STATE_INDEX_HIGHWATERMARK, uxHighWaterMark) == TRC_SUCCESS);
53 		}
54 
55 		/* This should never fail */
56 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetState(xHeapHandle, TRC_HEAP_STATE_INDEX_CURRENT, uxCurrent) == TRC_SUCCESS);
57 	}
58 
59 	(void)xTraceEventCreate2((pvAddress != (void*)0) ? PSF_EVENT_MALLOC : PSF_EVENT_MALLOC_FAILED, (TraceUnsignedBaseType_t)pvAddress, uxSize);  /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 !MISRAC2012-Rule-11.6 Suppress conversion from pointer to integer check*/
60 
61 	return TRC_SUCCESS;
62 }
63 
xTraceHeapFree(TraceHeapHandle_t xHeapHandle,void * pvAddress,TraceUnsignedBaseType_t uxSize)64 traceResult xTraceHeapFree(TraceHeapHandle_t xHeapHandle, void *pvAddress, TraceUnsignedBaseType_t uxSize)
65 {
66 	TraceUnsignedBaseType_t uxCurrent;
67 
68 	if (xHeapHandle == 0)
69 	{
70 		/* This can happen */
71 		return TRC_FAIL;
72 	}
73 
74 	/* If the address is null we assume this was a failed alloc attempt */
75 	if (pvAddress != (void*)0)
76 	{
77 		/* This should never fail */
78 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntryGetState(xHeapHandle, TRC_HEAP_STATE_INDEX_CURRENT, &uxCurrent) == TRC_SUCCESS);
79 
80 		uxCurrent -= uxSize;
81 
82 		/* This should never fail */
83 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetState(xHeapHandle, TRC_HEAP_STATE_INDEX_CURRENT, uxCurrent) == TRC_SUCCESS);
84 	}
85 
86 	(void)xTraceEventCreate2((pvAddress != (void*)0) ? PSF_EVENT_FREE : PSF_EVENT_FREE_FAILED, (TraceUnsignedBaseType_t)pvAddress, uxSize);  /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 !MISRAC2012-Rule-11.6 Suppress conversion from pointer to integer check*/
87 
88 	return TRC_SUCCESS;
89 }
90 
91 #endif
92