Skip to content

Commit

Permalink
replace assertion macros with plain asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorton committed Apr 2, 2012
1 parent 9ec2193 commit 413994e
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 124 deletions.
48 changes: 24 additions & 24 deletions src/rt/circular_buffer.cpp
Expand Up @@ -12,26 +12,26 @@ 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);
assert(_buffer);
W(kernel, _unread == 0,
"freeing circular_buffer with %d unread bytes", _unread);
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 +40,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 +53,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 +69,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 +82,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 +109,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 +153,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
10 changes: 5 additions & 5 deletions src/rt/memory_region.cpp
Expand Up @@ -55,7 +55,7 @@ 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);
Expand Down Expand Up @@ -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 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
18 changes: 6 additions & 12 deletions src/rt/rust_builtin.cpp
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
12 changes: 0 additions & 12 deletions src/rt/rust_internal.h
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
12 changes: 6 additions & 6 deletions src/rt/rust_kernel.cpp
Expand Up @@ -67,13 +67,13 @@ rust_kernel::create_scheduler(size_t num_threads) {
// the scheduler reaper.
bool start_reaper = sched_table.empty();
id = max_sched_id++;
K(srv, id != INTPTR_MAX, "Hit the maximum scheduler id");
assert(id != INTPTR_MAX && "Hit the maximum scheduler id");
sched = new (this, "rust_scheduler")
rust_scheduler(this, srv, num_threads, id);
bool is_new = sched_table
.insert(std::pair<rust_sched_id,
rust_scheduler*>(id, sched)).second;
A(this, is_new, "Reusing a sched id?");
assert(is_new && "Reusing a sched id?");
if (start_reaper) {
sched_reaper.start();
}
Expand Down Expand Up @@ -118,7 +118,7 @@ rust_kernel::wait_for_schedulers()
rust_sched_id id = join_list.back();
join_list.pop_back();
sched_map::iterator iter = sched_table.find(id);
I(this, iter != sched_table.end());
assert(iter != sched_table.end());
rust_scheduler *sched = iter->second;
sched_table.erase(iter);
sched->join_task_threads();
Expand Down Expand Up @@ -175,7 +175,7 @@ rust_kernel::fail() {
rust_task_id
rust_kernel::generate_task_id() {
rust_task_id id = sync::increment(max_task_id);
K(srv, id != INTPTR_MAX, "Hit the maximum task id");
assert(id != INTPTR_MAX && "Hit the maximum task id");
return id;
}

Expand All @@ -189,7 +189,7 @@ rust_kernel::register_port(rust_port *port) {
port_table.put(new_port_id, port);
new_live_ports = port_table.count();
}
K(srv, new_port_id != INTPTR_MAX, "Hit the maximum port id");
assert(new_port_id != INTPTR_MAX && "Hit the maximum port id");
KLOG_("Registered port %" PRIdPTR, new_port_id);
KLOG_("Total outstanding ports: %d", new_live_ports);
return new_port_id;
Expand Down Expand Up @@ -233,7 +233,7 @@ rust_kernel::win32_require(LPCTSTR fn, BOOL ok) {
(LPTSTR) &buf, 0, NULL );
KLOG_ERR_(dom, "%s failed with error %ld: %s", fn, err, buf);
LocalFree((HLOCAL)buf);
I(this, ok);
assert(ok);
}
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/rt/rust_port.cpp
Expand Up @@ -51,7 +51,7 @@ void rust_port::end_detach() {
// Just take the lock to make sure that the thread that signaled
// the detach_cond isn't still holding it
scoped_lock with(ref_lock);
I(task->sched_loop, ref_count == 0);
assert(ref_count == 0);
}

void rust_port::send(void *sptr) {
Expand All @@ -61,8 +61,8 @@ void rust_port::send(void *sptr) {

buffer.enqueue(sptr);

A(kernel, !buffer.is_empty(),
"rust_chan::transmit with nothing to send.");
assert(!buffer.is_empty() &&
"rust_chan::transmit with nothing to send.");

if (task->blocked_on(this)) {
KLOG(kernel, comm, "dequeued in rendezvous_ptr");
Expand Down
16 changes: 8 additions & 8 deletions src/rt/rust_port_selector.cpp
Expand Up @@ -10,12 +10,12 @@ rust_port_selector::select(rust_task *task, rust_port **dptr,
rust_port **ports,
size_t n_ports, uintptr_t *yield) {

I(task->sched_loop, this->ports == NULL);
I(task->sched_loop, this->n_ports == 0);
I(task->sched_loop, dptr != NULL);
I(task->sched_loop, ports != NULL);
I(task->sched_loop, n_ports != 0);
I(task->sched_loop, yield != NULL);
assert(this->ports == NULL);
assert(this->n_ports == 0);
assert(dptr != NULL);
assert(ports != NULL);
assert(n_ports != 0);
assert(yield != NULL);

*yield = false;
size_t locks_taken = 0;
Expand All @@ -31,7 +31,7 @@ rust_port_selector::select(rust_task *task, rust_port **dptr,
for (size_t i = 0; i < n_ports; i++) {
size_t k = (i + j) % n_ports;
rust_port *port = ports[k];
I(task->sched_loop, port != NULL);
assert(port != NULL);

port->lock.lock();
locks_taken++;
Expand All @@ -46,7 +46,7 @@ rust_port_selector::select(rust_task *task, rust_port **dptr,
if (!found_msg) {
this->ports = ports;
this->n_ports = n_ports;
I(task->sched_loop, task->rendezvous_ptr == NULL);
assert(task->rendezvous_ptr == NULL);
task->rendezvous_ptr = (uintptr_t*)dptr;
task->block(this, "waiting for select rendezvous");

Expand Down

0 comments on commit 413994e

Please sign in to comment.