2010-08-18 01:40:07 -05:00
|
|
|
#include "rust_internal.h"
|
|
|
|
#include "memory_region.h"
|
|
|
|
|
2011-03-02 15:43:33 -06:00
|
|
|
// NB: please do not commit code with this uncommented. It's
|
|
|
|
// hugely expensive and should only be used as a last resort.
|
|
|
|
//
|
|
|
|
// #define TRACK_ALLOCATIONS
|
2010-08-18 01:40:07 -05:00
|
|
|
|
|
|
|
memory_region::memory_region(rust_srv *srv, bool synchronized) :
|
|
|
|
_srv(srv), _parent(NULL), _live_allocations(0),
|
2010-11-30 19:10:51 -06:00
|
|
|
_detailed_leaks(getenv("RUST_DETAILED_LEAKS") != NULL),
|
2010-08-18 01:40:07 -05:00
|
|
|
_synchronized(synchronized) {
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_region::memory_region(memory_region *parent) :
|
|
|
|
_srv(parent->_srv), _parent(parent), _live_allocations(0),
|
2010-11-30 19:10:51 -06:00
|
|
|
_detailed_leaks(parent->_detailed_leaks),
|
2010-08-18 01:40:07 -05:00
|
|
|
_synchronized(parent->_synchronized) {
|
|
|
|
}
|
|
|
|
|
2011-07-07 13:53:08 -05:00
|
|
|
void memory_region::add_alloc() {
|
|
|
|
//_live_allocations++;
|
|
|
|
sync::increment(_live_allocations);
|
|
|
|
}
|
|
|
|
|
|
|
|
void memory_region::dec_alloc() {
|
|
|
|
//_live_allocations--;
|
|
|
|
sync::decrement(_live_allocations);
|
|
|
|
}
|
|
|
|
|
2010-08-18 01:40:07 -05:00
|
|
|
void memory_region::free(void *mem) {
|
2011-07-07 13:53:08 -05:00
|
|
|
// printf("free: ptr 0x%" PRIxPTR" region=%p\n", (uintptr_t) mem, this);
|
2011-06-16 13:13:23 -05:00
|
|
|
if (!mem) { return; }
|
2010-08-18 01:40:07 -05:00
|
|
|
if (_synchronized) { _lock.lock(); }
|
|
|
|
#ifdef TRACK_ALLOCATIONS
|
2011-07-07 18:24:35 -05:00
|
|
|
int index = ((int *)mem)[-1];
|
|
|
|
if (_allocation_list[index] != (uint8_t *)mem - sizeof(int)) {
|
2010-08-18 01:40:07 -05:00
|
|
|
printf("free: ptr 0x%" PRIxPTR " is not in allocation_list\n",
|
2011-07-07 18:24:35 -05:00
|
|
|
(uintptr_t) mem);
|
2010-08-18 01:40:07 -05:00
|
|
|
_srv->fatal("not in allocation_list", __FILE__, __LINE__, "");
|
|
|
|
}
|
2011-07-07 18:24:35 -05:00
|
|
|
else {
|
|
|
|
// printf("freed index %d\n", index);
|
|
|
|
_allocation_list[index] = NULL;
|
|
|
|
}
|
|
|
|
mem = (void*)((uint8_t*)mem - sizeof(int));
|
2010-08-18 01:40:07 -05:00
|
|
|
#endif
|
|
|
|
if (_live_allocations < 1) {
|
|
|
|
_srv->fatal("live_allocs < 1", __FILE__, __LINE__, "");
|
|
|
|
}
|
2011-07-07 13:53:08 -05:00
|
|
|
dec_alloc();
|
2010-08-18 01:40:07 -05:00
|
|
|
_srv->free(mem);
|
|
|
|
if (_synchronized) { _lock.unlock(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
memory_region::realloc(void *mem, size_t size) {
|
|
|
|
if (_synchronized) { _lock.lock(); }
|
|
|
|
if (!mem) {
|
2011-07-07 13:53:08 -05:00
|
|
|
add_alloc();
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
2011-07-07 18:24:35 -05:00
|
|
|
#ifdef TRACK_ALLOCATIONS
|
|
|
|
size += sizeof(int);
|
|
|
|
mem = (void*)((uint8_t*)mem - sizeof(int));
|
|
|
|
int index = *(int *)mem;
|
|
|
|
#endif
|
2010-08-18 01:40:07 -05:00
|
|
|
void *newMem = _srv->realloc(mem, size);
|
|
|
|
#ifdef TRACK_ALLOCATIONS
|
2011-07-07 18:24:35 -05:00
|
|
|
if (_allocation_list[index] != mem) {
|
2011-07-13 17:44:09 -05:00
|
|
|
printf("at index %d, found %p, expected %p\n",
|
2011-07-07 18:24:35 -05:00
|
|
|
index, _allocation_list[index], mem);
|
2010-08-18 01:40:07 -05:00
|
|
|
printf("realloc: ptr 0x%" PRIxPTR " is not in allocation_list\n",
|
|
|
|
(uintptr_t) mem);
|
|
|
|
_srv->fatal("not in allocation_list", __FILE__, __LINE__, "");
|
|
|
|
}
|
2011-07-07 18:24:35 -05:00
|
|
|
else {
|
|
|
|
_allocation_list[index] = newMem;
|
|
|
|
(*(int*)newMem) = index;
|
2011-07-13 17:44:09 -05:00
|
|
|
// printf("realloc: stored %p at index %d, replacing %p\n",
|
2011-07-07 18:24:35 -05:00
|
|
|
// newMem, index, mem);
|
|
|
|
}
|
2010-08-18 01:40:07 -05:00
|
|
|
#endif
|
|
|
|
if (_synchronized) { _lock.unlock(); }
|
2011-07-07 18:24:35 -05:00
|
|
|
#ifdef TRACK_ALLOCATIONS
|
|
|
|
newMem = (void *)((uint8_t*)newMem + sizeof(int));
|
|
|
|
#endif
|
2010-08-18 01:40:07 -05:00
|
|
|
return newMem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
memory_region::malloc(size_t size) {
|
|
|
|
if (_synchronized) { _lock.lock(); }
|
2011-07-07 13:53:08 -05:00
|
|
|
add_alloc();
|
2011-07-07 18:24:35 -05:00
|
|
|
#ifdef TRACK_ALLOCATIONS
|
|
|
|
size += sizeof(int);
|
|
|
|
#endif
|
2010-08-18 01:40:07 -05:00
|
|
|
void *mem = _srv->malloc(size);
|
|
|
|
#ifdef TRACK_ALLOCATIONS
|
2011-07-07 18:24:35 -05:00
|
|
|
int index = _allocation_list.append(mem);
|
|
|
|
int *p = (int *)mem;
|
|
|
|
*p = index;
|
|
|
|
// printf("malloc: stored %p at index %d\n", mem, index);
|
2010-08-18 01:40:07 -05:00
|
|
|
#endif
|
2011-07-13 17:44:09 -05:00
|
|
|
// printf("malloc: ptr 0x%" PRIxPTR " region=%p\n",
|
2011-07-07 13:53:08 -05:00
|
|
|
// (uintptr_t) mem, this);
|
2010-08-18 01:40:07 -05:00
|
|
|
if (_synchronized) { _lock.unlock(); }
|
2011-07-07 18:24:35 -05:00
|
|
|
#ifdef TRACK_ALLOCATIONS
|
|
|
|
mem = (void*)((uint8_t*)mem + sizeof(int));
|
|
|
|
#endif
|
2010-08-18 01:40:07 -05:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
memory_region::calloc(size_t size) {
|
|
|
|
if (_synchronized) { _lock.lock(); }
|
2011-07-07 13:53:08 -05:00
|
|
|
add_alloc();
|
2011-07-07 18:24:35 -05:00
|
|
|
#ifdef TRACK_ALLOCATIONS
|
|
|
|
size += sizeof(int);
|
|
|
|
#endif
|
2010-08-18 01:40:07 -05:00
|
|
|
void *mem = _srv->malloc(size);
|
|
|
|
memset(mem, 0, size);
|
|
|
|
#ifdef TRACK_ALLOCATIONS
|
2011-07-07 18:24:35 -05:00
|
|
|
int index = _allocation_list.append(mem);
|
|
|
|
int *p = (int *)mem;
|
|
|
|
*p = index;
|
|
|
|
// printf("calloc: stored %p at index %d\n", mem, index);
|
2010-08-18 01:40:07 -05:00
|
|
|
#endif
|
|
|
|
if (_synchronized) { _lock.unlock(); }
|
2011-07-07 18:24:35 -05:00
|
|
|
#ifdef TRACK_ALLOCATIONS
|
|
|
|
mem = (void*)((uint8_t*)mem + sizeof(int));
|
|
|
|
#endif
|
2010-08-18 01:40:07 -05:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_region::~memory_region() {
|
2011-07-07 18:24:35 -05:00
|
|
|
if (_synchronized) { _lock.lock(); }
|
2010-08-18 01:40:07 -05:00
|
|
|
if (_live_allocations == 0) {
|
2011-07-07 18:24:35 -05:00
|
|
|
if (_synchronized) { _lock.unlock(); }
|
2010-08-18 01:40:07 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
char msg[128];
|
|
|
|
snprintf(msg, sizeof(msg),
|
2010-11-30 19:10:51 -06:00
|
|
|
"leaked memory in rust main loop (%" PRIuPTR " objects)",
|
|
|
|
_live_allocations);
|
2010-08-18 01:40:07 -05:00
|
|
|
#ifdef TRACK_ALLOCATIONS
|
2010-11-30 19:10:51 -06:00
|
|
|
if (_detailed_leaks) {
|
|
|
|
for (size_t i = 0; i < _allocation_list.size(); i++) {
|
|
|
|
if (_allocation_list[i] != NULL) {
|
|
|
|
printf("allocation 0x%" PRIxPTR " was not freed\n",
|
|
|
|
(uintptr_t) _allocation_list[i]);
|
|
|
|
}
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-20 01:33:37 -05:00
|
|
|
if (!_hack_allow_leaks) {
|
|
|
|
_srv->fatal(msg, __FILE__, __LINE__,
|
|
|
|
"%d objects", _live_allocations);
|
|
|
|
} else {
|
|
|
|
_srv->warning(msg, __FILE__, __LINE__,
|
|
|
|
"%d objects", _live_allocations);
|
|
|
|
}
|
2011-07-07 18:24:35 -05:00
|
|
|
if (_synchronized) { _lock.unlock(); }
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
2010-11-30 19:10:51 -06:00
|
|
|
|
2011-07-20 01:33:37 -05:00
|
|
|
void
|
|
|
|
memory_region::hack_allow_leaks() {
|
|
|
|
_hack_allow_leaks = true;
|
|
|
|
}
|
|
|
|
|
2010-11-30 19:10:51 -06:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-07-13 15:51:20 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-11-30 19:10:51 -06:00
|
|
|
// End:
|
|
|
|
//
|