0.1.00 - initial version

This commit is contained in:
rob tillaart 2015-12-10 18:26:56 +01:00
parent 8e5236540e
commit 91dc550cc7
3 changed files with 236 additions and 0 deletions

View File

@ -0,0 +1,106 @@
//
// FILE: avrheap.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: library for avrheap Arduino
// URL:
//
// Released to the public domain
//
// 0.1.00 - initial version
#include "Avrheap.h"
struct __freelist {
size_t size;
struct __freelist *next;
};
extern struct __freelist *__flp;
extern int __heap_start, *__brkval;
extern char *__malloc_heap_start;
extern char *__malloc_heap_end;
Avrheap::Avrheap()
{
};
size_t Avrheap::printTo(Print& p) const
{
size_t n = 0;
for (int i = 0; i < count; )
{
n += p.print('\t');
n += p.print((uint8_t) * ((&__heap_start) + i) );
if (++i % 16 == 0) n += p.println();
}
n += p.println();
return n;
};
bool Avrheap::isFragmented()
{
return this->freeListCount() > 1;
};
// PRINTTO
void Avrheap::dump(uint16_t count)
{
for (int i = 0; i < count; )
{
Serial.print('\t');
Serial.print((uint8_t) * ((&__heap_start) + i) );
if (++i % 16 == 0) Serial.println();
}
Serial.println();
}
uint16_t Avrheap::freeListCount()
{
uint16_t count = 0;
for (struct __freelist* p = __flp; p; p = p->next)
{
count++;
}
return count;
}
uint16_t Avrheap::freeListSize()
{
uint16_t total = 0;
for (struct __freelist* p = __flp; p; p = p->next)
{
total += 2; // malloc size
total += (uint16_t) p->size;
}
return total;
}
void Avrheap::freeListDump()
{
for (struct __freelist* p = __flp; p; p = p->next)
{
Serial.print((uint16_t)p);
Serial.print("\t");
Serial.println((uint16_t)p->size);
}
Serial.println();
}
// void followHeap()
// {
// Serial.println("followHeap");
// int *p = (int*)&__heap_start;
// int x = 0;
// for (int i = 0; i < 10; i++)
// {
// Serial.print(i);
// Serial.print('\t');
// Serial.print(*p, DEC);
// Serial.println();
// p += (int) * p;
// }
// }
// --- END OF FILE ---

View File

@ -0,0 +1,39 @@
#ifndef AVRHEAP_H
#define AVRHEAP_H
//
// FILE: Avrheap.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1.00
// PURPOSE: heap library for Arduino (AVR)
// HISTORY: See avrheap.cpp
//
// Released to the public domain
//
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Printable.h"
#define AVRHEAP_LIB_VERSION "0.1.00"
class Avrheap
{
public:
Avrheap();
size_t printTo(Print& p) const;
// uint32_t freeRAM(); // should this be in ?
bool isFragmented();
uint16_t freeListCount();
uint16_t freeListSize();
void freeListDump();
void dump(uint16_t count);
private:
};
#endif

View File

@ -0,0 +1,91 @@
//
// FILE: heapdemo.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: heapdemo
// DATE: 2015-10-25
// URL:
//
// Released to the public domain
//
#include "avrheap.h"
Avrheap myheap;
int *par[10];
void setup()
{
Serial.begin(115200);
Serial.print("Start ");
Serial.println(__FILE__);
Serial.println(AVRHEAP_LIB_VERSION);
Serial.println();
Serial.print("HEAP ADDR:\t");
Serial.println(myheap.startAddress());
Serial.println();
// allocate 10 chunks
Serial.println("ptr\taddr");
for (int i = 0; i < 10; i++)
{
par[i] = (int*) malloc(i * 3); // all different sizes
*par[i] = 0;
Serial.print(i);
Serial.print('\t');
Serial.println((int)par[i], DEC);
}
Serial.println();
Serial.println();
myheap.dump(80);
Serial.println("\nfollowHeap");
myheap.followHeap();
Serial.print("fragmented: ");
Serial.println(myheap.isFragmented() ? "True" : "False");
Serial.print("count: ");
Serial.println(myheap.freeListCount());
Serial.print("size: ");
Serial.println(myheap.freeListSize());
Serial.println("dump: ");
myheap.freeListDump();
Serial.println("free 3 pointers");
free(par[3]);
free(par[5]);
free(par[7]);
Serial.print("fragmented: ");
Serial.println(myheap.isFragmented() ? "True" : "False");
Serial.print("count: ");
Serial.println(myheap.freeListCount());
Serial.print("size: ");
Serial.println(myheap.freeListSize());
Serial.println("dump: ");
myheap.freeListDump();
Serial.println("1 malloc");
par[3] = (int*) malloc(10);
Serial.print("fragmented:\t");
Serial.println(myheap.isFragmented() ? "True" : "False");
Serial.print("count:\t");
Serial.println(myheap.freeListCount());
Serial.print("size:\t");
Serial.println(myheap.freeListSize());
Serial.println("dump: ");
myheap.freeListDump();
Serial.println();
myheap.dump(80);
Serial.println("\nfollowHeap");
myheap.followHeap();
Serial.println("\ndone");
}
void loop()
{}