2016-04-20 19:20:10 -05:00
|
|
|
// 4 december 2014
|
|
|
|
#include "uipriv_windows.hpp"
|
|
|
|
|
2016-04-20 19:21:57 -05:00
|
|
|
typedef std::vector<uint8_t> byteArray;
|
2016-04-20 19:20:10 -05:00
|
|
|
|
2016-04-20 19:21:57 -05:00
|
|
|
static std::map<uint8_t *, byteArray *> heap;
|
|
|
|
static std::map<byteArray *, const char *> types;
|
2016-04-20 19:20:10 -05:00
|
|
|
|
|
|
|
void initAlloc(void)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
void uninitAlloc(void)
|
|
|
|
{
|
2016-05-13 22:29:41 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
|
|
|
|
if (heap.size() == 0)
|
|
|
|
return;
|
|
|
|
for (const auto &alloc : heap)
|
|
|
|
// note the void * cast; otherwise it'll be treated as a string
|
|
|
|
oss << (void *) (alloc.first) << " " << types[alloc.second] << "\n";
|
|
|
|
// TODO keep oss.str() alive?
|
|
|
|
userbug("Some data was leaked; either you left a uiControl lying around or there's a bug in libui itself. Leaked data:\n%s", oss.str().c_str());
|
2016-04-20 19:20:10 -05:00
|
|
|
}
|
|
|
|
|
2016-04-23 16:31:59 -05:00
|
|
|
#define rawBytes(pa) (&((*pa)[0]))
|
|
|
|
|
2016-04-20 19:20:10 -05:00
|
|
|
void *uiAlloc(size_t size, const char *type)
|
|
|
|
{
|
2016-04-20 19:21:57 -05:00
|
|
|
byteArray *out;
|
2016-04-20 19:20:10 -05:00
|
|
|
|
|
|
|
out = new byteArray(size, 0);
|
2016-04-23 16:31:59 -05:00
|
|
|
heap[rawBytes(out)] = out;
|
2016-04-20 19:20:10 -05:00
|
|
|
types[out] = type;
|
2016-04-23 16:31:59 -05:00
|
|
|
return rawBytes(out);
|
2016-04-20 19:20:10 -05:00
|
|
|
}
|
|
|
|
|
2016-04-23 16:31:59 -05:00
|
|
|
void *uiRealloc(void *_p, size_t size, const char *type)
|
2016-04-20 19:20:10 -05:00
|
|
|
{
|
2016-04-23 16:31:59 -05:00
|
|
|
uint8_t *p = (uint8_t *) _p;
|
2016-04-20 19:21:57 -05:00
|
|
|
byteArray *arr;
|
2016-04-20 19:20:10 -05:00
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
return uiAlloc(size, type);
|
|
|
|
arr = heap[p];
|
|
|
|
arr->resize(size, 0);
|
|
|
|
heap.erase(p);
|
2016-04-23 16:31:59 -05:00
|
|
|
heap[rawBytes(arr)] = arr;
|
|
|
|
return rawBytes(arr);
|
2016-04-20 19:20:10 -05:00
|
|
|
}
|
|
|
|
|
2016-04-23 16:31:59 -05:00
|
|
|
void uiFree(void *_p)
|
2016-04-20 19:20:10 -05:00
|
|
|
{
|
2016-04-23 16:31:59 -05:00
|
|
|
uint8_t *p = (uint8_t *) _p;
|
|
|
|
|
2016-04-20 19:20:10 -05:00
|
|
|
if (p == NULL)
|
2016-05-13 21:05:02 -05:00
|
|
|
implbug("attempt to uiFree(NULL)");
|
2016-04-20 19:20:10 -05:00
|
|
|
types.erase(heap[p]);
|
|
|
|
delete heap[p];
|
|
|
|
heap.erase(p);
|
|
|
|
}
|