2015-12-10 12:30:14 -05:00
|
|
|
//
|
2015-12-10 12:26:56 -05:00
|
|
|
// FILE: avrheap.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-10-19 05:34:48 -04:00
|
|
|
// VERSION: 0.2.4
|
2020-11-27 05:10:47 -05:00
|
|
|
// PURPOSE: experimental library for heap Arduino UNO
|
|
|
|
// URL: https://github.com/RobTillaart/avrheap
|
2015-12-10 12:26:56 -05:00
|
|
|
//
|
2015-12-10 12:35:47 -05:00
|
|
|
// REFERENCES
|
|
|
|
// http://forum.arduino.cc/index.php?topic=27536.15
|
2020-11-27 05:10:47 -05:00
|
|
|
// http://forum.arduino.cc/index.php?topic=355660
|
2015-12-10 12:35:47 -05:00
|
|
|
//
|
2020-11-27 05:10:47 -05:00
|
|
|
// HISTORY
|
2021-12-13 15:05:26 -05:00
|
|
|
// 0.2.5 2021-12-13 update library.json, license
|
2021-10-19 05:34:48 -04:00
|
|
|
// 0.2.4 2021-10-19 update Arduino-CI + add badges
|
|
|
|
// 0.2.3 2021-05027 add Arduino-CI
|
2021-12-13 15:05:26 -05:00
|
|
|
// 0.2.2 2020-12-13 Arduino-CI + minimal unit tests
|
2021-10-19 05:34:48 -04:00
|
|
|
// 0.2.1 2020-05-27 update library.json
|
|
|
|
// 0.2.0 2020-03-27 Removed support for pre 1.0 version
|
|
|
|
// 0.1.5 fix typo #116 - Thanks to DMNC
|
2021-12-13 15:05:26 -05:00
|
|
|
// 0.1.04 new methods including PrintTo support - Thanks to Whandall
|
2021-10-19 05:34:48 -04:00
|
|
|
// !! breaking interface
|
|
|
|
// 0.1.03 refactoring
|
|
|
|
// 0.1.02 added followHeap()
|
|
|
|
// 0.1.01 refactor, added startAddress()
|
|
|
|
// 0.1.00 initial version
|
2015-12-10 12:26:56 -05:00
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2018-10-24 10:26:18 -04:00
|
|
|
#include "avrheap.h"
|
2015-12-10 12:26:56 -05:00
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 12:35:47 -05:00
|
|
|
struct __freelist
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
size_t size;
|
|
|
|
struct __freelist *next;
|
2015-12-10 12:26:56 -05:00
|
|
|
};
|
|
|
|
|
2015-12-10 12:35:47 -05:00
|
|
|
extern struct __freelist *__flp;
|
|
|
|
extern uint16_t __heap_start;
|
|
|
|
extern uint16_t *__brkval;
|
|
|
|
extern char *__malloc_heap_start;
|
|
|
|
extern char *__malloc_heap_end;
|
|
|
|
extern size_t __malloc_margin;
|
|
|
|
extern uint16_t __data_start;
|
|
|
|
extern uint16_t __data_end;
|
|
|
|
extern uint16_t __bss_start;
|
|
|
|
extern uint16_t __bss_end;
|
2015-12-10 14:19:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
size_t hNibble(Print& p, byte val)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return p.write(val + (val<10 ? '0' : 'A'-10));
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
size_t hByte(Print& p, byte val)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
size_t len = hNibble(p, val >> 4);
|
|
|
|
return len + hNibble(p, val & 0x0F);
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
size_t hWord(Print& p, uint16_t val)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
size_t len = hByte(p, (byte)(val >> 8));
|
|
|
|
return len + hByte(p, (byte)(val & 0xFF) );
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
size_t dumpR(Print& p, byte* adr, int len)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
size_t glen = 0;
|
|
|
|
byte idx;
|
|
|
|
if (!len)
|
|
|
|
{
|
|
|
|
len = 16;
|
|
|
|
}
|
|
|
|
for (; len > 0; len -= 16, adr += 16)
|
|
|
|
{
|
|
|
|
glen += hWord(p, (uint16_t)adr);
|
|
|
|
glen += p.print(F(": "));
|
|
|
|
for (idx = 0; idx < 16; idx++)
|
2015-12-10 14:19:02 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
if (idx < len )
|
|
|
|
{
|
|
|
|
glen += hByte(p, adr[idx]);
|
|
|
|
glen += p.write(' ');
|
|
|
|
} else {
|
|
|
|
glen += p.print(F(" "));
|
|
|
|
}
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
glen += p.write('\'');
|
|
|
|
for (idx = 0; (idx < 16) && (idx < len); idx++)
|
2015-12-10 14:19:02 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
glen += p.write(adr[idx] < 0x20 ? '.' : adr[idx]);
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
glen += p.write('\'');
|
|
|
|
glen += p.println();
|
|
|
|
}
|
|
|
|
return glen;
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
size_t dumpAlloced(byte *ptr, bool withDump)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return dumpAlloced(Serial, ptr, withDump);
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
size_t dumpAlloced(Print& p, byte *ptr, bool withDump)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
size_t len = hWord(p, (uint16_t)ptr);
|
|
|
|
if (!ptr)
|
|
|
|
{
|
|
|
|
len += p.println(F(": NULL"));
|
|
|
|
} else {
|
|
|
|
size_t size = *(size_t*)(ptr-sizeof(size_t));
|
|
|
|
if (size < __malloc_margin)
|
2015-12-10 14:19:02 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
len += p.print(F(": size "));
|
|
|
|
len += p.println(size);
|
2015-12-10 14:19:02 -05:00
|
|
|
} else {
|
2021-01-29 06:31:58 -05:00
|
|
|
len += p.print(F(": invalid size "));
|
|
|
|
len += p.println(size);
|
|
|
|
size = 16;
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
if (withDump)
|
|
|
|
{
|
|
|
|
len += dumpR(p, ptr, size);
|
|
|
|
len += p.println();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
2015-12-10 12:26:56 -05:00
|
|
|
|
|
|
|
|
2015-12-10 12:35:47 -05:00
|
|
|
Avrheap::Avrheap()
|
2015-12-10 12:26:56 -05:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 12:26:56 -05:00
|
|
|
bool Avrheap::isFragmented()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return freeListCount() > 0;
|
2015-12-10 12:26:56 -05:00
|
|
|
};
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 12:26:56 -05:00
|
|
|
uint16_t Avrheap::freeListCount()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
uint16_t count = 0;
|
|
|
|
for (struct __freelist* p = __flp; p; p = p->next) count++;
|
|
|
|
return count;
|
2015-12-10 12:26:56 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 12:26:56 -05:00
|
|
|
uint16_t Avrheap::freeListSize()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
uint16_t total = 0;
|
|
|
|
for (struct __freelist* p = __flp; p; p = p->next)
|
|
|
|
{
|
|
|
|
total += 2; // malloc size
|
|
|
|
total += (uint16_t) p->size;
|
|
|
|
}
|
|
|
|
return total;
|
2015-12-10 12:26:56 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
void Avrheap::freeListWalk(bool withDump)
|
2015-12-10 12:26:56 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
int elements = freeListCount();
|
|
|
|
Serial.print(F("\nFreeList: "));
|
|
|
|
Serial.print(isFragmented() ? F("fragmented") : F("clean"));
|
|
|
|
Serial.print(F(", count "));
|
|
|
|
Serial.print(elements);
|
|
|
|
Serial.print(F(", largest "));
|
|
|
|
Serial.print(freeListLargest());
|
|
|
|
Serial.print(F(", total size "));
|
|
|
|
Serial.println(freeListSize());
|
|
|
|
Serial.println();
|
|
|
|
if (elements)
|
|
|
|
{
|
|
|
|
for (struct __freelist* p = __flp; p; p = p->next)
|
2015-12-10 12:26:56 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
hWord(Serial, (uint16_t)p);
|
|
|
|
Serial.print(F(": size "));
|
|
|
|
Serial.print((uint16_t)p->size);
|
|
|
|
Serial.print(F(" next "));
|
|
|
|
hWord(Serial, (uint16_t)p->next);
|
|
|
|
Serial.println();
|
|
|
|
if (withDump)
|
|
|
|
{
|
|
|
|
dumpR(Serial, ((byte*)p)+2, p->size);
|
|
|
|
Serial.println();
|
|
|
|
}
|
2015-12-10 12:26:56 -05:00
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
2015-12-10 12:26:56 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 12:30:14 -05:00
|
|
|
uint16_t Avrheap::startAddress()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return (uint16_t) &__heap_start;
|
2015-12-10 12:30:14 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
void Avrheap::dumpHeap(uint16_t count)
|
2015-12-10 12:30:14 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
hWord(Serial, (uint16_t)RAMEND);
|
|
|
|
Serial.println(F(" RAMEND"));
|
|
|
|
hWord(Serial, (uint16_t)SP);
|
|
|
|
Serial.println(F(" SP"));
|
|
|
|
hWord(Serial, (uint16_t)__brkval);
|
|
|
|
Serial.println(F(" __brkval"));
|
|
|
|
hWord(Serial, (uint16_t)__malloc_heap_end);
|
|
|
|
Serial.println(F(" __malloc_heap_end"));
|
|
|
|
hWord(Serial, (uint16_t)__malloc_heap_start);
|
|
|
|
Serial.println(F(" __malloc_heap_start"));
|
|
|
|
hWord(Serial, (uint16_t)&__heap_start);
|
|
|
|
Serial.println(F(" __heap_start"));
|
|
|
|
hWord(Serial, (uint16_t)&__bss_end);
|
|
|
|
Serial.println(F(" __bss_end"));
|
|
|
|
hWord(Serial, (uint16_t)&__bss_start);
|
|
|
|
Serial.println(F(" __bss_start"));
|
|
|
|
hWord(Serial, (uint16_t)&__data_end);
|
|
|
|
Serial.println(F(" __data_end"));
|
|
|
|
hWord(Serial, (uint16_t)&__data_start);
|
|
|
|
Serial.println(F(" __data_start"));
|
|
|
|
hWord(Serial, (uint16_t)__malloc_margin);
|
|
|
|
Serial.println(F(" __malloc_margin"));
|
|
|
|
Serial.println();
|
|
|
|
Serial.println(F("start of heap"));
|
|
|
|
Serial.println();
|
|
|
|
dumpR(Serial, (byte*)startAddress(), count);
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
size_t Avrheap::heapWalk(bool withDump) {
|
2021-01-29 06:31:58 -05:00
|
|
|
return heapWalk(Serial, withDump);
|
2015-12-10 12:30:14 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
// EXPERIMENTAL
|
|
|
|
size_t Avrheap::heapWalk(Print& pr, bool withDump) const
|
2015-12-10 12:35:47 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
byte* p = (byte*) &__heap_start;
|
|
|
|
struct __freelist* fp = __flp;
|
2015-12-10 14:19:02 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
size_t len = pr.println(F("Heap\n"));
|
|
|
|
while ((int)p < (int)__brkval)
|
|
|
|
{
|
|
|
|
len += hWord(pr, (uint16_t)p); // p+2 ?
|
|
|
|
len += pr.write(' ');
|
|
|
|
len += pr.print(*p, DEC);
|
|
|
|
if ( (fp != NULL) && ((uint16_t)p == (uint16_t)fp))
|
2015-12-10 12:35:47 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
len += pr.print(F(" (free)"));
|
|
|
|
fp = fp->next;
|
|
|
|
}
|
|
|
|
len += pr.println();
|
|
|
|
if (withDump) {
|
|
|
|
len += dumpR(pr, p, *p+2);
|
|
|
|
len += pr.println();
|
2015-12-10 12:35:47 -05:00
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
p += (byte) *p + 2;
|
|
|
|
}
|
|
|
|
return len;
|
2015-12-10 12:35:47 -05:00
|
|
|
}
|
2015-12-10 12:26:56 -05:00
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 12:35:47 -05:00
|
|
|
bool Avrheap::inFreeList(uint16_t addr)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
for (struct __freelist* p = __flp; p; p = p->next)
|
|
|
|
{
|
|
|
|
if (addr == (uint16_t)p) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-12-10 12:35:47 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 12:35:47 -05:00
|
|
|
uint16_t Avrheap::freeListLargest()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
uint16_t largest = 0;
|
|
|
|
for (struct __freelist* p = __flp; p; p = p->next)
|
|
|
|
{
|
|
|
|
largest = max(largest, (uint16_t) p->size);
|
|
|
|
}
|
|
|
|
return largest;
|
2015-12-10 12:35:47 -05:00
|
|
|
}
|
2015-12-10 12:26:56 -05:00
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2015-12-10 14:19:02 -05:00
|
|
|
size_t Avrheap::printTo(Print& p) const
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
size_t len = heapWalk(p, true);
|
|
|
|
return len;
|
2015-12-10 14:19:02 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:20:16 -04:00
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
// --- END OF FILE ---
|