Pulled rust_srv in its own file. Some cleanup, and added varargs to assertion macros.

This commit is contained in:
Michael Bebenita
2010-08-16 14:13:08 -07:00
parent 7e62aa6801
commit b40a9fa787
7 changed files with 168 additions and 130 deletions

View File

@@ -1,110 +1,5 @@
#include "rust_internal.h"
#define TRACK_ALLOCATIONS
rust_srv::rust_srv() :
live_allocs(0)
{
}
rust_srv::~rust_srv()
{
if (live_allocs != 0) {
char msg[128];
snprintf(msg, sizeof(msg),
"leaked memory in rust main loop (%" PRIuPTR " objects)",
live_allocs);
#ifdef TRACK_ALLOCATIONS
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]);
}
}
#endif
fatal(msg, __FILE__, __LINE__);
}
}
void
rust_srv::log(char const *str)
{
printf("rt: %s\n", str);
}
void *
rust_srv::malloc(size_t bytes)
{
++live_allocs;
void * val = ::malloc(bytes);
#ifdef TRACK_ALLOCATIONS
allocation_list.append(val);
#endif
return val;
}
void *
rust_srv::realloc(void *p, size_t bytes)
{
if (!p) {
live_allocs++;
}
void * val = ::realloc(p, bytes);
#ifdef TRACK_ALLOCATIONS
if (allocation_list.replace(p, val) == false) {
printf("realloc: ptr 0x%" PRIxPTR " is not in allocation_list\n",
(uintptr_t) p);
fatal("not in allocation_list", __FILE__, __LINE__);
}
#endif
return val;
}
void
rust_srv::free(void *p)
{
#ifdef TRACK_ALLOCATIONS
if (allocation_list.replace(p, NULL) == false) {
printf("free: ptr 0x%" PRIxPTR " is not in allocation_list\n",
(uintptr_t) p);
fatal("not in allocation_list", __FILE__, __LINE__);
}
#endif
if (live_allocs < 1) {
fatal("live_allocs < 1", __FILE__, __LINE__);
}
live_allocs--;
::free(p);
}
void
rust_srv::fatal(char const *expr, char const *file, size_t line)
{
char buf[1024];
snprintf(buf, sizeof(buf),
"fatal, '%s' failed, %s:%d",
expr, file, (int)line);
log(buf);
exit(1);
}
void
rust_srv::warning(char const *expr, char const *file, size_t line)
{
char buf[1024];
snprintf(buf, sizeof(buf),
"warning: '%s', at: %s:%d",
expr, file, (int)line);
log(buf);
}
rust_srv *
rust_srv::clone()
{
return new rust_srv();
}
struct
command_line_args