Monitor memory usage and panic on over 8MB used

This commit is contained in:
Michal Moskal 2017-12-13 15:28:52 +00:00
parent 3e2a1ec9e1
commit 67ec4accb9
3 changed files with 26 additions and 3 deletions

View File

@ -13,14 +13,35 @@
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <malloc.h>
#define THREAD_DBG(...)
#define MALLOC_LIMIT (8 * 1024 * 1024)
#define MALLOC_CHECK_PERIOD (1024 * 1024)
void *xmalloc(size_t sz) {
static size_t allocBytes = 0;
allocBytes += sz;
if (allocBytes >= MALLOC_CHECK_PERIOD) {
allocBytes = 0;
auto info = mallinfo();
DMESG("malloc used: %d kb", info.uordblks / 1024);
if (info.uordblks > MALLOC_LIMIT) {
target_panic(904);
}
}
auto r = malloc(sz);
if (r == NULL)
target_panic(905); // shouldn't happen
return r;
}
void *operator new(size_t size) {
return malloc(size);
return xmalloc(size);
}
void *operator new[](size_t size) {
return malloc(size);
return xmalloc(size);
}
void operator delete(void *p) {

View File

@ -88,7 +88,7 @@ Image unpackPNG(Buffer png) {
uint32_t byteW = (hd.width + 7) >> 3;
uint32_t expSize = (byteW + 1) * hd.height;
unsigned long sz = expSize;
uint8_t *tmp = (uint8_t *)malloc(sz);
uint8_t *tmp = (uint8_t *)xmalloc(sz);
int code = uncompress(tmp, &sz, png->data + sizeof(hd), hd.lenIDAT);
if (code != 0) {
DMESG("PNG: zlib failed: %d", code);

View File

@ -3,6 +3,8 @@
#include "pxtbase.h"
void *xmalloc(size_t sz);
namespace pxt {
void raiseEvent(int id, int event);
int allocateNotifyEvent();