Difference between revisions of "How to use SystemView Heap Monitoring - Custom Heap"

From SEGGER Wiki
Jump to: navigation, search
Line 2: Line 2:
   
 
Note that the size reported to SystemView should be the internally allocated size minus the meta data, not the size requested buy a user.
 
Note that the size reported to SystemView should be the internally allocated size minus the meta data, not the size requested buy a user.
  +
The <code>__heap_start__</code> is a symbol that is defined if the heap region is allocated in the linker script.
  +
  +
Allocating the memory should be accompanied by a call to <code>SEGGER_SYSVIEW_HeapAlloc</code>:
 
<pre>
 
<pre>
 
static void* _MyMalloc(size_t size) {
 
static void* _MyMalloc(size_t size) {
void* p = malloc(size);
+
void* p = ... // a call to a custom alloc routine
 
// SystemView needs the real internal heap block size, not the amount requested by the user
 
// SystemView needs the real internal heap block size, not the amount requested by the user
 
SEGGER_SYSVIEW_HeapAlloc(__heap_start__, p, _GetAllocBlockSize(size));
 
SEGGER_SYSVIEW_HeapAlloc(__heap_start__, p, _GetAllocBlockSize(size));
Line 11: Line 14:
 
</pre>
 
</pre>
   
Since there is no dedicated realloc SystemView API function, a combination of <code>SEGGER_SYSVIEW_HeapFree</code> and <code>SEGGER_SYSVIEW_HeapAlloc</code> is needed:
+
Freeing the memory should be accompanied by a call to <code>SEGGER_SYSVIEW_HeapFree</code>:
 
<pre>
 
<pre>
static void* _MyRealloc(void* p, size_t size) {
+
static void _MyFree(void* p) {
  +
free(p);
void* rp = realloc(p, size);
 
 
SEGGER_SYSVIEW_HeapFree(__heap_start__, p);
 
SEGGER_SYSVIEW_HeapFree(__heap_start__, p);
// SystemView needs the real internal heap block size, not the amount requested by the user
 
SEGGER_SYSVIEW_HeapAlloc(__heap_start__, rp, _GetAllocBlockSize(size));
 
return rp;
 
 
}
 
}
 
</pre>
 
</pre>
   
  +
Since there is no dedicated realloc SystemView API function, a combination of <code>SEGGER_SYSVIEW_HeapFree</code> and <code>SEGGER_SYSVIEW_HeapAlloc</code> is needed:
 
<pre>
 
<pre>
static void _MyFree(void* p) {
+
static void* _MyRealloc(void* p, size_t size) {
  +
void* rp = ... // a call to a custom realloc routine
free(p);
 
 
SEGGER_SYSVIEW_HeapFree(__heap_start__, p);
 
SEGGER_SYSVIEW_HeapFree(__heap_start__, p);
  +
// SystemView needs the real internal heap block size, not the amount requested by the user
  +
SEGGER_SYSVIEW_HeapAlloc(__heap_start__, rp, _GetAllocBlockSize(size));
  +
return rp;
 
}
 
}
 
</pre>
 
</pre>

Revision as of 09:41, 19 July 2023

This article assumes a working SystemView setup. To gain some more info on this subject, please consult either the main article (How to use SystemView Heap_Monitoring) or the SystemView documentation.

Note that the size reported to SystemView should be the internally allocated size minus the meta data, not the size requested buy a user. The __heap_start__ is a symbol that is defined if the heap region is allocated in the linker script.

Allocating the memory should be accompanied by a call to SEGGER_SYSVIEW_HeapAlloc:

static void* _MyMalloc(size_t size) {
  void* p = ... // a call to a custom alloc routine
  // SystemView needs the real internal heap block size, not the amount requested by the user
  SEGGER_SYSVIEW_HeapAlloc(__heap_start__, p, _GetAllocBlockSize(size));
  return p;
}

Freeing the memory should be accompanied by a call to SEGGER_SYSVIEW_HeapFree:

static void _MyFree(void* p) {
  free(p);
  SEGGER_SYSVIEW_HeapFree(__heap_start__, p);
}

Since there is no dedicated realloc SystemView API function, a combination of SEGGER_SYSVIEW_HeapFree and SEGGER_SYSVIEW_HeapAlloc is needed:

static void* _MyRealloc(void* p, size_t size) {
  void* rp = ... // a call to a custom realloc routine
  SEGGER_SYSVIEW_HeapFree(__heap_start__, p);
  // SystemView needs the real internal heap block size, not the amount requested by the user
  SEGGER_SYSVIEW_HeapAlloc(__heap_start__, rp, _GetAllocBlockSize(size));
  return rp;
}