2012-04-02 22:18:01 -05:00
|
|
|
|
|
|
|
#include "sync/sync.h"
|
2010-08-18 01:40:07 -05:00
|
|
|
#include "memory_region.h"
|
2012-04-02 22:18:01 -05:00
|
|
|
#include "rust_env.h"
|
2010-08-18 01:40:07 -05:00
|
|
|
|
2012-03-26 20:58:40 -05:00
|
|
|
#if RUSTRT_TRACK_ALLOCATIONS >= 3
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2011-11-07 19:19:29 -06:00
|
|
|
#if RUSTRT_TRACK_ALLOCATIONS >= 1
|
2011-12-12 20:25:17 -06:00
|
|
|
// For some platforms, 16 byte alignment is required.
|
|
|
|
# define PTR_SIZE 16
|
2011-11-07 19:19:29 -06:00
|
|
|
# define ALIGN_PTR(x) (((x)+PTR_SIZE-1)/PTR_SIZE*PTR_SIZE)
|
|
|
|
# define HEADER_SIZE ALIGN_PTR(sizeof(alloc_header))
|
|
|
|
# define MAGIC 0xbadc0ffe
|
|
|
|
#else
|
|
|
|
# define HEADER_SIZE 0
|
|
|
|
#endif
|
2011-07-18 14:02:26 -05:00
|
|
|
|
|
|
|
memory_region::alloc_header *memory_region::get_header(void *mem) {
|
2011-11-07 18:48:18 -06:00
|
|
|
return (alloc_header *)((char *)mem - HEADER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *memory_region::get_data(alloc_header *ptr) {
|
|
|
|
return (void*)((char *)ptr + HEADER_SIZE);
|
2011-07-18 14:02:26 -05:00
|
|
|
}
|
2010-08-18 01:40:07 -05:00
|
|
|
|
2012-04-01 22:18:40 -05:00
|
|
|
memory_region::memory_region(rust_env *env, bool synchronized) :
|
|
|
|
_env(env), _parent(NULL), _live_allocations(0),
|
|
|
|
_detailed_leaks(env->detailed_leaks),
|
2011-09-08 18:59:23 -05:00
|
|
|
_synchronized(synchronized) {
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
memory_region::memory_region(memory_region *parent) :
|
2012-04-01 22:18:40 -05:00
|
|
|
_env(parent->_env), _parent(parent), _live_allocations(0),
|
2010-11-30 19:10:51 -06:00
|
|
|
_detailed_leaks(parent->_detailed_leaks),
|
2011-09-08 18:59:23 -05:00
|
|
|
_synchronized(parent->_synchronized) {
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
|
|
|
|
2011-07-07 13:53:08 -05:00
|
|
|
void memory_region::add_alloc() {
|
2011-08-12 18:36:17 -05:00
|
|
|
//_live_allocations++;
|
|
|
|
sync::increment(_live_allocations);
|
2011-07-07 13:53:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void memory_region::dec_alloc() {
|
2011-08-12 18:36:17 -05:00
|
|
|
//_live_allocations--;
|
|
|
|
sync::decrement(_live_allocations);
|
2011-07-07 13:53:08 -05:00
|
|
|
}
|
|
|
|
|
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; }
|
2011-07-18 14:02:26 -05:00
|
|
|
alloc_header *alloc = get_header(mem);
|
2011-11-07 19:19:29 -06:00
|
|
|
|
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 1
|
2011-07-18 14:02:26 -05:00
|
|
|
assert(alloc->magic == MAGIC);
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
|
|
|
|
2010-08-18 01:40:07 -05:00
|
|
|
if (_live_allocations < 1) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(false && "live_allocs < 1");
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
2011-08-12 18:36:17 -05:00
|
|
|
release_alloc(mem);
|
2011-09-06 16:15:01 -05:00
|
|
|
maybe_poison(mem);
|
2012-04-01 22:18:40 -05:00
|
|
|
::free(alloc);
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2011-11-07 19:19:29 -06:00
|
|
|
memory_region::realloc(void *mem, size_t orig_size) {
|
2010-08-18 01:40:07 -05:00
|
|
|
if (!mem) {
|
2011-07-07 13:53:08 -05:00
|
|
|
add_alloc();
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
2011-11-07 19:19:29 -06:00
|
|
|
|
2011-07-18 14:02:26 -05:00
|
|
|
alloc_header *alloc = get_header(mem);
|
2011-12-12 20:25:17 -06:00
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 1
|
|
|
|
assert(alloc->magic == MAGIC);
|
|
|
|
# endif
|
|
|
|
|
2011-11-07 19:19:29 -06:00
|
|
|
size_t size = orig_size + HEADER_SIZE;
|
2012-04-01 22:18:40 -05:00
|
|
|
alloc_header *newMem = (alloc_header *)::realloc(alloc, size);
|
2011-11-07 19:19:29 -06:00
|
|
|
|
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 1
|
2011-12-12 20:25:17 -06:00
|
|
|
assert(newMem->magic == MAGIC);
|
2011-11-07 19:19:29 -06:00
|
|
|
newMem->size = orig_size;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 2
|
2012-02-17 00:40:28 -06:00
|
|
|
if (_synchronized) { _lock.lock(); }
|
2011-07-18 14:02:26 -05:00
|
|
|
if (_allocation_list[newMem->index] != alloc) {
|
2011-07-13 17:44:09 -05:00
|
|
|
printf("at index %d, found %p, expected %p\n",
|
2011-07-18 14:02:26 -05:00
|
|
|
alloc->index, _allocation_list[alloc->index], alloc);
|
2011-08-03 22:33:22 -05:00
|
|
|
printf("realloc: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
|
2011-11-07 18:48:18 -06:00
|
|
|
(uintptr_t) get_data(alloc), alloc->tag);
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(false && "not in allocation_list");
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
2011-07-07 18:24:35 -05:00
|
|
|
else {
|
2011-07-18 14:02:26 -05:00
|
|
|
_allocation_list[newMem->index] = newMem;
|
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);
|
|
|
|
}
|
2012-02-17 00:40:28 -06:00
|
|
|
if (_synchronized) { _lock.unlock(); }
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
|
|
|
|
2011-11-07 18:48:18 -06:00
|
|
|
return get_data(newMem);
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2011-07-18 14:02:26 -05:00
|
|
|
memory_region::malloc(size_t size, const char *tag, bool zero) {
|
|
|
|
size_t old_size = size;
|
2011-11-07 18:48:18 -06:00
|
|
|
size += HEADER_SIZE;
|
2012-04-01 22:18:40 -05:00
|
|
|
alloc_header *mem = (alloc_header *)::malloc(size);
|
2011-11-07 19:19:29 -06:00
|
|
|
|
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 1
|
2011-07-18 14:02:26 -05:00
|
|
|
mem->magic = MAGIC;
|
|
|
|
mem->tag = tag;
|
2011-08-12 18:36:17 -05:00
|
|
|
mem->index = -1;
|
2011-09-06 16:15:01 -05:00
|
|
|
mem->size = old_size;
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
2011-11-07 18:48:18 -06:00
|
|
|
|
|
|
|
void *data = get_data(mem);
|
|
|
|
claim_alloc(data);
|
2011-07-18 14:02:26 -05:00
|
|
|
|
|
|
|
if(zero) {
|
2011-11-07 18:48:18 -06:00
|
|
|
memset(data, 0, old_size);
|
2011-07-18 14:02:26 -05:00
|
|
|
}
|
|
|
|
|
2011-11-07 18:48:18 -06:00
|
|
|
return data;
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2011-07-18 14:02:26 -05:00
|
|
|
memory_region::calloc(size_t size, const char *tag) {
|
|
|
|
return malloc(size, tag, true);
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
memory_region::~memory_region() {
|
2011-07-07 18:24:35 -05:00
|
|
|
if (_synchronized) { _lock.lock(); }
|
2011-08-12 18:36:17 -05:00
|
|
|
if (_live_allocations == 0 && !_detailed_leaks) {
|
2011-07-07 18:24:35 -05:00
|
|
|
if (_synchronized) { _lock.unlock(); }
|
2010-08-18 01:40:07 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
char msg[128];
|
2011-08-12 18:36:17 -05:00
|
|
|
if(_live_allocations > 0) {
|
|
|
|
snprintf(msg, sizeof(msg),
|
|
|
|
"leaked memory in rust main loop (%d objects)",
|
|
|
|
_live_allocations);
|
|
|
|
}
|
2011-11-07 19:19:29 -06:00
|
|
|
|
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 2
|
2010-11-30 19:10:51 -06:00
|
|
|
if (_detailed_leaks) {
|
2011-08-16 12:28:09 -05:00
|
|
|
int leak_count = 0;
|
2010-11-30 19:10:51 -06:00
|
|
|
for (size_t i = 0; i < _allocation_list.size(); i++) {
|
|
|
|
if (_allocation_list[i] != NULL) {
|
2011-07-28 10:19:00 -05:00
|
|
|
alloc_header *header = (alloc_header*)_allocation_list[i];
|
|
|
|
printf("allocation (%s) 0x%" PRIxPTR " was not freed\n",
|
|
|
|
header->tag,
|
2011-12-12 20:25:17 -06:00
|
|
|
(uintptr_t) get_data(header));
|
2011-08-12 18:36:17 -05:00
|
|
|
++leak_count;
|
2012-03-26 20:58:40 -05:00
|
|
|
|
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 3
|
|
|
|
if (_detailed_leaks) {
|
|
|
|
backtrace_symbols_fd(header->bt + 1,
|
|
|
|
header->btframes - 1, 2);
|
|
|
|
}
|
|
|
|
# endif
|
2010-11-30 19:10:51 -06:00
|
|
|
}
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
2011-08-12 18:36:17 -05:00
|
|
|
assert(leak_count == _live_allocations);
|
2010-08-18 01:40:07 -05:00
|
|
|
}
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
|
|
|
|
2011-09-08 18:59:23 -05:00
|
|
|
if (_live_allocations > 0) {
|
2012-04-01 21:14:16 -05:00
|
|
|
fprintf(stderr, "%s\n", msg);
|
|
|
|
assert(false);
|
2011-07-20 01:33:37 -05:00
|
|
|
}
|
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-08-12 18:36:17 -05:00
|
|
|
void
|
|
|
|
memory_region::release_alloc(void *mem) {
|
2011-11-07 19:19:29 -06:00
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 1
|
2011-08-12 18:36:17 -05:00
|
|
|
alloc_header *alloc = get_header(mem);
|
|
|
|
assert(alloc->magic == MAGIC);
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
2011-08-12 18:36:17 -05:00
|
|
|
|
2011-11-07 19:19:29 -06:00
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 2
|
2011-09-01 16:18:38 -05:00
|
|
|
if (_synchronized) { _lock.lock(); }
|
2011-08-12 18:36:17 -05:00
|
|
|
if (_allocation_list[alloc->index] != alloc) {
|
|
|
|
printf("free: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
|
2011-12-12 20:25:17 -06:00
|
|
|
(uintptr_t) get_data(alloc), alloc->tag);
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(false && "not in allocation_list");
|
2011-08-12 18:36:17 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// printf("freed index %d\n", index);
|
|
|
|
_allocation_list[alloc->index] = NULL;
|
|
|
|
alloc->index = -1;
|
|
|
|
}
|
2011-09-01 16:18:38 -05:00
|
|
|
if (_synchronized) { _lock.unlock(); }
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
|
|
|
|
2011-08-12 18:36:17 -05:00
|
|
|
dec_alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
memory_region::claim_alloc(void *mem) {
|
2011-11-07 19:19:29 -06:00
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 1
|
2011-08-12 18:36:17 -05:00
|
|
|
alloc_header *alloc = get_header(mem);
|
|
|
|
assert(alloc->magic == MAGIC);
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 2
|
2011-09-01 16:18:38 -05:00
|
|
|
if (_synchronized) { _lock.lock(); }
|
2011-08-12 18:36:17 -05:00
|
|
|
alloc->index = _allocation_list.append(alloc);
|
2011-09-01 16:18:38 -05:00
|
|
|
if (_synchronized) { _lock.unlock(); }
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
|
|
|
|
2012-03-26 20:58:40 -05:00
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 3
|
|
|
|
if (_detailed_leaks) {
|
|
|
|
alloc->btframes = ::backtrace(alloc->bt, 32);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2011-08-12 18:36:17 -05:00
|
|
|
add_alloc();
|
|
|
|
}
|
|
|
|
|
2011-09-06 16:15:01 -05:00
|
|
|
void
|
|
|
|
memory_region::maybe_poison(void *mem) {
|
|
|
|
|
2012-04-01 22:18:40 -05:00
|
|
|
if (!_env->poison_on_free)
|
2011-09-06 16:15:01 -05:00
|
|
|
return;
|
|
|
|
|
2011-11-07 19:19:29 -06:00
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 1
|
2011-09-06 16:15:01 -05:00
|
|
|
alloc_header *alloc = get_header(mem);
|
|
|
|
memset(mem, '\xcd', alloc->size);
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
2011-09-06 16:15:01 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// End:
|
|
|
|
//
|