[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freebsd-hackers
Subject:    kernel assertions
From:       Michael Hancock <michaelh () cet ! co ! jp>
Date:       1996-07-29 11:59:27
[Download RAW message or body]

Any comments on introducing an assertion macro for kernel code that panics
if the expression is false and does nothing otherwise.  It would also be
very cool to preprocess it out with something like a -NDEBUG flag.  It
could be called KASSERT or KERN_ASSERT.

Benefits:

1) You write most of your code assuming the parameters are correct.  This
would get rid of a few if ... then ... else's. 

2) You define your function requirements clearly to callers of your
function. 

An example follows below:

Original version
----------------

/*
 * remove the buffer from the appropriate free list
 */
void
bremfree(struct buf * bp)
{
	int s = splbio();

	if (bp->b_qindex != QUEUE_NONE) {
		TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
		bp->b_qindex = QUEUE_NONE;
	} else {
		panic("bremfree: removing a buffer when not on a queue");
	}
	splx(s);
}

KERN_ASSERT version
---------------

/*
 * remove the buffer from the appropriate free list
 */
void
bremfree(struct buf * bp)
{
	int s = splbio();

  	KERN_ASSERT(bp->b_qindex != QUEUE_NONE)

	TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
	bp->b_qindex = QUEUE_NONE;

	splx(s);
}

This was a simple example but I'm sure it could make code a lot less
intricate in other places.

Confident performance freaks can preprocess away a few clock cycles.  This
could add up for frequently called functions. 

--
Mike Hancock

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic