Skip to content

Refactor asserts, remove rust_srv #2102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion mk/rt.mk
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ RUNTIME_CS_$(1) := \
rt/rust_port_selector.cpp \
rt/circular_buffer.cpp \
rt/isaac/randport.cpp \
rt/rust_srv.cpp \
rt/rust_kernel.cpp \
rt/rust_shape.cpp \
rt/rust_abi.cpp \
Expand Down
56 changes: 30 additions & 26 deletions src/rt/circular_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@ circular_buffer::circular_buffer(rust_kernel *kernel, size_t unit_sz) :
_unread(0),
_buffer((uint8_t *)kernel->malloc(_buffer_sz, "circular_buffer")) {

A(kernel, unit_sz, "Unit size must be larger than zero.");
assert(unit_sz && "Unit size must be larger than zero.");

KLOG(kernel, mem, "new circular_buffer(buffer_sz=%d, unread=%d)"
"-> circular_buffer=0x%" PRIxPTR,
_buffer_sz, _unread, this);

A(kernel, _buffer, "Failed to allocate buffer.");
assert(_buffer && "Failed to allocate buffer.");
}

circular_buffer::~circular_buffer() {
KLOG(kernel, mem, "~circular_buffer 0x%" PRIxPTR, this);
I(kernel, _buffer);
W(kernel, _unread == 0,
"freeing circular_buffer with %d unread bytes", _unread);
assert(_buffer);
if (_unread != 0) {
fprintf(stderr, "warning: freeing circular_buffer with"
" %lu unread bytes", _unread);
fflush(stderr);
}

kernel->free(_buffer);
}

size_t
circular_buffer::initial_size() {
I(kernel, unit_sz > 0);
assert(unit_sz > 0);
return INITIAL_CIRCULAR_BUFFER_SIZE_IN_UNITS * unit_sz;
}

Expand All @@ -40,8 +44,8 @@ circular_buffer::initial_size() {
*/
void
circular_buffer::transfer(void *dst) {
I(kernel, dst);
I(kernel, _unread <= _buffer_sz);
assert(dst);
assert(_unread <= _buffer_sz);

uint8_t *ptr = (uint8_t *) dst;

Expand All @@ -53,13 +57,13 @@ circular_buffer::transfer(void *dst) {
} else {
head_sz = _buffer_sz - _next;
}
I(kernel, _next + head_sz <= _buffer_sz);
assert(_next + head_sz <= _buffer_sz);
memcpy(ptr, _buffer + _next, head_sz);

// Then copy any other items from the beginning of the buffer
I(kernel, _unread >= head_sz);
assert(_unread >= head_sz);
size_t tail_sz = _unread - head_sz;
I(kernel, head_sz + tail_sz <= _buffer_sz);
assert(head_sz + tail_sz <= _buffer_sz);
memcpy(ptr + head_sz, _buffer, tail_sz);
}

Expand All @@ -69,9 +73,9 @@ circular_buffer::transfer(void *dst) {
*/
void
circular_buffer::enqueue(void *src) {
I(kernel, src);
I(kernel, _unread <= _buffer_sz);
I(kernel, _buffer);
assert(src);
assert(_unread <= _buffer_sz);
assert(_buffer);

// Grow if necessary.
if (_unread == _buffer_sz) {
Expand All @@ -82,20 +86,20 @@ circular_buffer::enqueue(void *src) {
"unread: %d, next: %d, buffer_sz: %d, unit_sz: %d",
_unread, _next, _buffer_sz, unit_sz);

I(kernel, _unread < _buffer_sz);
I(kernel, _unread + unit_sz <= _buffer_sz);
assert(_unread < _buffer_sz);
assert(_unread + unit_sz <= _buffer_sz);

// Copy data
size_t dst_idx = _next + _unread;
I(kernel, dst_idx >= _buffer_sz || dst_idx + unit_sz <= _buffer_sz);
assert(dst_idx >= _buffer_sz || dst_idx + unit_sz <= _buffer_sz);
if (dst_idx >= _buffer_sz) {
dst_idx -= _buffer_sz;

I(kernel, _next >= unit_sz);
I(kernel, dst_idx <= _next - unit_sz);
assert(_next >= unit_sz);
assert(dst_idx <= _next - unit_sz);
}

I(kernel, dst_idx + unit_sz <= _buffer_sz);
assert(dst_idx + unit_sz <= _buffer_sz);
memcpy(&_buffer[dst_idx], src, unit_sz);
_unread += unit_sz;

Expand All @@ -109,17 +113,17 @@ circular_buffer::enqueue(void *src) {
*/
void
circular_buffer::dequeue(void *dst) {
I(kernel, unit_sz > 0);
I(kernel, _unread >= unit_sz);
I(kernel, _unread <= _buffer_sz);
I(kernel, _buffer);
assert(unit_sz > 0);
assert(_unread >= unit_sz);
assert(_unread <= _buffer_sz);
assert(_buffer);

KLOG(kernel, mem,
"circular_buffer dequeue "
"unread: %d, next: %d, buffer_sz: %d, unit_sz: %d",
_unread, _next, _buffer_sz, unit_sz);

I(kernel, _next + unit_sz <= _buffer_sz);
assert(_next + unit_sz <= _buffer_sz);
if (dst != NULL) {
memcpy(dst, &_buffer[_next], unit_sz);
}
Expand Down Expand Up @@ -153,7 +157,7 @@ circular_buffer::grow() {
void
circular_buffer::shrink() {
size_t new_buffer_sz = _buffer_sz / 2;
I(kernel, initial_size() <= new_buffer_sz);
assert(initial_size() <= new_buffer_sz);
KLOG(kernel, mem, "circular_buffer is shrinking to %d bytes",
new_buffer_sz);
void *new_buffer = kernel->malloc(new_buffer_sz,
Expand Down
26 changes: 13 additions & 13 deletions src/rt/memory_region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ void *memory_region::get_data(alloc_header *ptr) {
return (void*)((char *)ptr + HEADER_SIZE);
}

memory_region::memory_region(rust_srv *srv, bool synchronized) :
_srv(srv), _parent(NULL), _live_allocations(0),
_detailed_leaks(srv->env->detailed_leaks),
memory_region::memory_region(rust_env *env, bool synchronized) :
_env(env), _parent(NULL), _live_allocations(0),
_detailed_leaks(env->detailed_leaks),
_synchronized(synchronized) {
}

memory_region::memory_region(memory_region *parent) :
_srv(parent->_srv), _parent(parent), _live_allocations(0),
_env(parent->_env), _parent(parent), _live_allocations(0),
_detailed_leaks(parent->_detailed_leaks),
_synchronized(parent->_synchronized) {
}
Expand All @@ -55,11 +55,11 @@ void memory_region::free(void *mem) {
# endif

if (_live_allocations < 1) {
_srv->fatal("live_allocs < 1", __FILE__, __LINE__, "");
assert(false && "live_allocs < 1");
}
release_alloc(mem);
maybe_poison(mem);
_srv->free(alloc);
::free(alloc);
}

void *
Expand All @@ -74,7 +74,7 @@ memory_region::realloc(void *mem, size_t orig_size) {
# endif

size_t size = orig_size + HEADER_SIZE;
alloc_header *newMem = (alloc_header *)_srv->realloc(alloc, size);
alloc_header *newMem = (alloc_header *)::realloc(alloc, size);

# if RUSTRT_TRACK_ALLOCATIONS >= 1
assert(newMem->magic == MAGIC);
Expand All @@ -88,7 +88,7 @@ memory_region::realloc(void *mem, size_t orig_size) {
alloc->index, _allocation_list[alloc->index], alloc);
printf("realloc: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
(uintptr_t) get_data(alloc), alloc->tag);
_srv->fatal("not in allocation_list", __FILE__, __LINE__, "");
assert(false && "not in allocation_list");
}
else {
_allocation_list[newMem->index] = newMem;
Expand All @@ -105,7 +105,7 @@ void *
memory_region::malloc(size_t size, const char *tag, bool zero) {
size_t old_size = size;
size += HEADER_SIZE;
alloc_header *mem = (alloc_header *)_srv->malloc(size);
alloc_header *mem = (alloc_header *)::malloc(size);

# if RUSTRT_TRACK_ALLOCATIONS >= 1
mem->magic = MAGIC;
Expand Down Expand Up @@ -166,8 +166,8 @@ memory_region::~memory_region() {
# endif

if (_live_allocations > 0) {
_srv->fatal(msg, __FILE__, __LINE__,
"%d objects", _live_allocations);
fprintf(stderr, "%s\n", msg);
assert(false);
}
if (_synchronized) { _lock.unlock(); }
}
Expand All @@ -184,7 +184,7 @@ memory_region::release_alloc(void *mem) {
if (_allocation_list[alloc->index] != alloc) {
printf("free: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
(uintptr_t) get_data(alloc), alloc->tag);
_srv->fatal("not in allocation_list", __FILE__, __LINE__, "");
assert(false && "not in allocation_list");
}
else {
// printf("freed index %d\n", index);
Expand Down Expand Up @@ -222,7 +222,7 @@ memory_region::claim_alloc(void *mem) {
void
memory_region::maybe_poison(void *mem) {

if (!_srv->env->poison_on_free)
if (!_env->poison_on_free)
return;

# if RUSTRT_TRACK_ALLOCATIONS >= 1
Expand Down
6 changes: 3 additions & 3 deletions src/rt/memory_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// hugely expensive and should only be used as a last resort.
#define RUSTRT_TRACK_ALLOCATIONS 0

class rust_srv;
class rust_env;

class memory_region {
private:
Expand All @@ -42,7 +42,7 @@ class memory_region {
inline alloc_header *get_header(void *mem);
inline void *get_data(alloc_header *);

rust_srv *_srv;
rust_env *_env;
memory_region *_parent;
int _live_allocations;
array_list<alloc_header *> _allocation_list;
Expand All @@ -58,7 +58,7 @@ class memory_region {
void claim_alloc(void *mem);

public:
memory_region(rust_srv *srv, bool synchronized);
memory_region(rust_env *env, bool synchronized);
memory_region(memory_region *parent);
void *malloc(size_t size, const char *tag, bool zero = true);
void *calloc(size_t size, const char *tag);
Expand Down
4 changes: 1 addition & 3 deletions src/rt/rust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {
update_log_settings(crate_map, env->logspec);
check_claims = env->check_claims;

rust_srv *srv = new rust_srv(env);
rust_kernel *kernel = new rust_kernel(srv);
rust_kernel *kernel = new rust_kernel(env);
rust_sched_id sched_id = kernel->create_scheduler(env->num_sched_threads);
rust_scheduler *sched = kernel->get_scheduler_by_id(sched_id);
rust_task *root_task = sched->create_task(NULL, "main");
Expand All @@ -96,7 +95,6 @@ rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {
int ret = kernel->wait_for_exit();
delete args;
delete kernel;
delete srv;

free_env(env);

Expand Down
18 changes: 6 additions & 12 deletions src/rt/rust_builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,7 @@ rust_get_sched_id() {
extern "C" CDECL rust_sched_id
rust_new_sched(uintptr_t threads) {
rust_task *task = rust_sched_loop::get_task();
A(task->sched_loop, threads > 0,
"Can't create a scheduler with no threads, silly!");
assert(threads > 0 && "Can't create a scheduler with no threads, silly!");
return task->kernel->create_scheduler(threads);
}

Expand Down Expand Up @@ -606,36 +605,31 @@ rust_dbg_lock_create() {

extern "C" CDECL void
rust_dbg_lock_destroy(lock_and_signal *lock) {
rust_task *task = rust_sched_loop::get_task();
I(task->sched_loop, lock);
assert(lock);
delete lock;
}

extern "C" CDECL void
rust_dbg_lock_lock(lock_and_signal *lock) {
rust_task *task = rust_sched_loop::get_task();
I(task->sched_loop, lock);
assert(lock);
lock->lock();
}

extern "C" CDECL void
rust_dbg_lock_unlock(lock_and_signal *lock) {
rust_task *task = rust_sched_loop::get_task();
I(task->sched_loop, lock);
assert(lock);
lock->unlock();
}

extern "C" CDECL void
rust_dbg_lock_wait(lock_and_signal *lock) {
rust_task *task = rust_sched_loop::get_task();
I(task->sched_loop, lock);
assert(lock);
lock->wait();
}

extern "C" CDECL void
rust_dbg_lock_signal(lock_and_signal *lock) {
rust_task *task = rust_sched_loop::get_task();
I(task->sched_loop, lock);
assert(lock);
lock->signal();
}

Expand Down
13 changes: 0 additions & 13 deletions src/rt/rust_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ typedef intptr_t rust_sched_id;
typedef intptr_t rust_task_id;
typedef intptr_t rust_port_id;

#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, __FILE__, __LINE__, s, ## __VA_ARGS__))

#define A(dom, e, s, ...) ((e) ? (void)0 : \
(dom)->srv->fatal(#e, __FILE__, __LINE__, s, ## __VA_ARGS__))

#define K(srv, e, s, ...) ((e) ? (void)0 : \
srv->fatal(#e, __FILE__, __LINE__, s, ## __VA_ARGS__))

#define PTR "0x%" PRIxPTR

// This drives our preemption scheme.
Expand Down Expand Up @@ -107,7 +95,6 @@ template <typename T> struct region_owned {
struct rust_cond { };

#include "memory_region.h"
#include "rust_srv.h"
#include "rust_log.h"
#include "rust_kernel.h"
#include "rust_sched_loop.h"
Expand Down
Loading