1 /*
2 * Percepio Trace Recorder for Tracealyzer v4.8.1
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)
14 
15 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
16 
17 #if (TRC_USE_HEAPS == 1)
18 
19 /*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)20 traceResult xTraceHeapCreate(const char *szName, TraceUnsignedBaseType_t uxCurrent, TraceUnsignedBaseType_t uxHighWaterMark, TraceUnsignedBaseType_t uxMax, TraceHeapHandle_t *pxHeapHandle)
21 {
22 	TraceUnsignedBaseType_t uxStates[3];
23 
24 	uxStates[TRC_HEAP_STATE_INDEX_CURRENT] = uxCurrent;
25 	uxStates[TRC_HEAP_STATE_INDEX_HIGHWATERMARK] = uxHighWaterMark;
26 	uxStates[TRC_HEAP_STATE_INDEX_MAX] = uxMax;
27 
28 	return xTraceObjectRegisterInternal(PSF_EVENT_HEAP_CREATE, (void*)0, szName, 3u, uxStates, TRC_ENTRY_OPTION_HEAP, (TraceObjectHandle_t*)pxHeapHandle);
29 }
30 
xTraceHeapAlloc(TraceHeapHandle_t xHeapHandle,void * pvAddress,TraceUnsignedBaseType_t uxSize)31 traceResult xTraceHeapAlloc(TraceHeapHandle_t xHeapHandle, void *pvAddress, TraceUnsignedBaseType_t uxSize)
32 {
33 	TraceUnsignedBaseType_t uxCurrent, uxHighWaterMark;
34 
35 	if (xHeapHandle == 0)
36 	{
37 		/* This can happen */
38 		return TRC_FAIL;
39 	}
40 
41 	/* If the address is null we assume this was a failed alloc attempt */
42 	if (pvAddress != (void*)0)
43 	{
44 		/* This should never fail */
45 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntryGetState(xHeapHandle, TRC_HEAP_STATE_INDEX_CURRENT, &uxCurrent) == TRC_SUCCESS);
46 
47 		/* This should never fail */
48 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntryGetState(xHeapHandle, TRC_HEAP_STATE_INDEX_HIGHWATERMARK, &uxHighWaterMark) == TRC_SUCCESS);
49 
50 		uxCurrent += uxSize;
51 
52 		if (uxCurrent > uxHighWaterMark)
53 		{
54 			uxHighWaterMark = uxCurrent;
55 			/* This should never fail */
56 			TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetState(xHeapHandle, TRC_HEAP_STATE_INDEX_HIGHWATERMARK, uxHighWaterMark) == TRC_SUCCESS);
57 		}
58 
59 		/* This should never fail */
60 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetState(xHeapHandle, TRC_HEAP_STATE_INDEX_CURRENT, uxCurrent) == TRC_SUCCESS);
61 	}
62 
63 	(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*/
64 
65 	return TRC_SUCCESS;
66 }
67 
xTraceHeapFree(TraceHeapHandle_t xHeapHandle,void * pvAddress,TraceUnsignedBaseType_t uxSize)68 traceResult xTraceHeapFree(TraceHeapHandle_t xHeapHandle, void *pvAddress, TraceUnsignedBaseType_t uxSize)
69 {
70 	TraceUnsignedBaseType_t uxCurrent;
71 
72 	if (xHeapHandle == 0)
73 	{
74 		/* This can happen */
75 		return TRC_FAIL;
76 	}
77 
78 	/* If the address is null we assume this was a failed alloc attempt */
79 	if (pvAddress != (void*)0)
80 	{
81 		/* This should never fail */
82 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntryGetState(xHeapHandle, TRC_HEAP_STATE_INDEX_CURRENT, &uxCurrent) == TRC_SUCCESS);
83 
84 		uxCurrent -= uxSize;
85 
86 		/* This should never fail */
87 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetState(xHeapHandle, TRC_HEAP_STATE_INDEX_CURRENT, uxCurrent) == TRC_SUCCESS);
88 	}
89 
90 	(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*/
91 
92 	return TRC_SUCCESS;
93 }
94 
95 #endif /* (TRC_USE_HEAPS == 1) */
96 
97 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
98 
99 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
100