libui/zOLD_windows/alloc.cpp

65 lines
1.4 KiB
C++
Raw Normal View History

// 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:21:57 -05:00
static std::map<uint8_t *, byteArray *> heap;
static std::map<byteArray *, const char *> types;
void initAlloc(void)
{
// do nothing
}
void uninitAlloc(void)
{
2016-05-13 22:29:41 -05:00
std::ostringstream oss;
2016-05-29 12:07:48 -05:00
std::string ossstr; // keep alive, just to be safe
2016-05-13 22:29:41 -05:00
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";
2016-05-29 12:07:48 -05:00
ossstr = oss.str();
uiprivUserBug("Some data was leaked; either you left a uiControl lying around or there's a bug in libui itself. Leaked data:\n%s", ossstr.c_str());
}
#define rawBytes(pa) (&((*pa)[0]))
void *uiprivAlloc(size_t size, const char *type)
{
2016-04-20 19:21:57 -05:00
byteArray *out;
out = new byteArray(size, 0);
heap[rawBytes(out)] = out;
types[out] = type;
return rawBytes(out);
}
void *uiprivRealloc(void *_p, size_t size, const char *type)
{
uint8_t *p = (uint8_t *) _p;
2016-04-20 19:21:57 -05:00
byteArray *arr;
if (p == NULL)
return uiprivAlloc(size, type);
arr = heap[p];
// TODO does this fill in?
arr->resize(size, 0);
heap.erase(p);
heap[rawBytes(arr)] = arr;
return rawBytes(arr);
}
void uiprivFree(void *_p)
{
uint8_t *p = (uint8_t *) _p;
if (p == NULL)
uiprivImplBug("attempt to uiprivFree(NULL)");
types.erase(heap[p]);
delete heap[p];
heap.erase(p);
}