From b40a9fa787dd3b7d979cdf3e156284d6667fe9d2 Mon Sep 17 00:00:00 2001 From: Michael Bebenita Date: Mon, 16 Aug 2010 14:13:08 -0700 Subject: Pulled rust_srv in its own file. Some cleanup, and added varargs to assertion macros. --- src/rt/circular_buffer.cpp | 2 +- src/rt/rust.cpp | 105 --------------------------------------- src/rt/rust.h | 16 +----- src/rt/rust_internal.h | 16 +++--- src/rt/rust_srv.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++++ src/rt/rust_srv.h | 32 ++++++++++++ 6 files changed, 164 insertions(+), 128 deletions(-) create mode 100644 src/rt/rust_srv.cpp create mode 100644 src/rt/rust_srv.h (limited to 'src/rt') diff --git a/src/rt/circular_buffer.cpp b/src/rt/circular_buffer.cpp index caa95359..e5432bd9 100644 --- a/src/rt/circular_buffer.cpp +++ b/src/rt/circular_buffer.cpp @@ -33,7 +33,7 @@ circular_buffer::circular_buffer(rust_dom *dom, size_t unit_sz) : circular_buffer::~circular_buffer() { dom->log(rust_log::MEM, "~circular_buffer 0x%" PRIxPTR, this); I(dom, _buffer); - W(dom, _unread == 0, "~circular_buffer with unread messages."); + W(dom, _unread == 0, "~circular_buffer with %d unread bytes", _unread); dom->free(_buffer); } diff --git a/src/rt/rust.cpp b/src/rt/rust.cpp index d0215edc..905b0c8a 100644 --- a/src/rt/rust.cpp +++ b/src/rt/rust.cpp @@ -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 diff --git a/src/rt/rust.h b/src/rt/rust.h index 9a61dca7..97f5c0d4 100644 --- a/src/rt/rust.h +++ b/src/rt/rust.h @@ -19,21 +19,7 @@ #include "util/array_list.h" -struct rust_srv { - size_t live_allocs; - array_list allocation_list; - - virtual void log(char const *); - virtual void fatal(char const *, char const *, size_t); - virtual void warning(char const *, char const *, size_t); - virtual void *malloc(size_t); - virtual void *realloc(void *, size_t); - virtual void free(void *); - virtual rust_srv *clone(); - - rust_srv(); - virtual ~rust_srv(); -}; +#include "rust_srv.h" inline void *operator new(size_t size, rust_srv *srv) { diff --git a/src/rt/rust_internal.h b/src/rt/rust_internal.h index 05463c53..67787719 100644 --- a/src/rt/rust_internal.h +++ b/src/rt/rust_internal.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -46,13 +47,14 @@ extern "C" { #error "Target CPU not supported." #endif -#define I(dom, e) ((e) ? (void)0 : \ - (dom)->srv->fatal(#e, __FILE__, __LINE__)) -#define W(dom, e, s) ((e) ? (void)0 : \ - (dom)->srv->warning(#e " : " #s, __FILE__, __LINE__)) +#define I(dom, e) ((e) ? (void)0 : \ + (dom)->srv->fatal(#e, __FILE__, __LINE__, "")) -#define A(dom, e, s) ((e) ? (void)0 : \ - (dom)->srv->fatal(#e " : " #s, __FILE__, __LINE__)) +#define W(dom, e, s, ...) ((e) ? (void)0 : \ + (dom)->srv->warning(#e, __FILE__, __LINE__, s, ## __VA_ARGS__)) + +#define A(dom, e, s, ...) ((e) ? (void)0 : \ + (dom)->srv->fatal(#e, __FILE__, __LINE__, s, ## __VA_ARGS__)) struct rust_task; struct rust_port; @@ -164,7 +166,7 @@ template inline T check_null(rust_dom *dom, T value, char const *expr, char const *file, size_t line) { if (value == NULL) { - dom->srv->fatal(expr, file, line); + dom->srv->fatal(expr, file, line, "is null"); } return value; } diff --git a/src/rt/rust_srv.cpp b/src/rt/rust_srv.cpp new file mode 100644 index 00000000..9239f643 --- /dev/null +++ b/src/rt/rust_srv.cpp @@ -0,0 +1,121 @@ +/* + * + */ + +#include "rust_internal.h" +#include "rust_srv.h" + +#define TRACK_ALLOCATIONS + +rust_srv::rust_srv() : _live_allocations(0) { + // Nop. +} + +rust_srv::~rust_srv() { + if (_live_allocations != 0) { + char msg[128]; + snprintf(msg, sizeof(msg), + "leaked memory in rust main loop (%" PRIuPTR " objects)", + _live_allocations); +#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::malloc(size_t bytes) { + ++_live_allocations; + 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_allocations++; + } + 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_allocations < 1) { + fatal("live_allocs < 1", __FILE__, __LINE__, ""); + } + _live_allocations--; + ::free(p); +} + +void +rust_srv::log(char const *msg) { + printf("rt: %s\n", msg); +} + +void +rust_srv::fatal(const char *expression, + const char *file, + size_t line, + const char *format, + ...) { + char buf[1024]; + va_list args; + va_start(args, format); + vsnprintf(buf, sizeof(buf), format, args); + va_end(args); + + char msg[1024]; + snprintf(msg, sizeof(msg), + "fatal, '%s' failed, %s:%d %s", + expression, file, (int)line, buf); + log(msg); + exit(1); +} + +void +rust_srv::warning(char const *expression, + char const *file, + size_t line, + const char *format, + ...) { + char buf[1024]; + va_list args; + va_start(args, format); + vsnprintf(buf, sizeof(buf), format, args); + va_end(args); + + char msg[1024]; + snprintf(msg, sizeof(msg), + "warning: '%s', at: %s:%d %s", + expression, file, (int)line, buf); + log(msg); +} + +rust_srv * +rust_srv::clone() { + return new rust_srv(); +} diff --git a/src/rt/rust_srv.h b/src/rt/rust_srv.h new file mode 100644 index 00000000..b25e5e75 --- /dev/null +++ b/src/rt/rust_srv.h @@ -0,0 +1,32 @@ +/* + * + */ + +#ifndef RUST_SRV_H +#define RUST_SRV_H + +class rust_srv { +private: + size_t _live_allocations; + array_list _allocation_list; +public: + virtual void log(char const *msg); + virtual void fatal(char const *expression, + char const *file, + size_t line, + char const *format, + ...); + virtual void warning(char const *expression, + char const *file, + size_t line, + char const *format, + ...); + virtual void *malloc(size_t); + virtual void *realloc(void *, size_t); + virtual void free(void *); + virtual rust_srv *clone(); + rust_srv(); + virtual ~rust_srv(); +}; + +#endif /* RUST_SRV_H */ -- cgit v1.2.3