{
struct ncb_blk blk;
+ if (ncb_is_null(buf))
+ return NCB_BLK_NULL;
+
blk.st = ncb_head(buf);
blk.sz_ptr = ncb_reserved(buf);
{
struct ncb_blk blk;
+ if (ncb_is_null(buf))
+ return NCB_BLK_NULL;
+
BUG_ON_HOT(off >= ncb_size(buf));
for (blk = ncb_blk_first(buf); off > blk.off + blk.sz;
}
/* Initialize or reset <buf> by clearing all data. Its size is untouched.
- * Buffer is positioned to <head> offset. Use 0 to realign it.
+ * Buffer is positioned to <head> offset. Use 0 to realign it. <buf> must not
+ * be NCBUF_NULL.
*/
void ncb_init(struct ncbuf *buf, ncb_sz_t head)
{
+ BUG_ON_HOT(ncb_is_null(buf));
+
BUG_ON_HOT(head >= buf->size);
buf->head = head;
*/
ncb_sz_t ncb_size(const struct ncbuf *buf)
{
+ if (ncb_is_null(buf))
+ return 0;
+
return buf->size - NCB_RESERVED_SZ;
}
/* Returns true if there is no data anywhere in <buf>. */
int ncb_is_empty(const struct ncbuf *buf)
{
+ if (ncb_is_null(buf))
+ return 1;
+
BUG_ON_HOT(*ncb_reserved(buf) + *ncb_head(buf) > ncb_size(buf));
return *ncb_reserved(buf) == 0 && *ncb_head(buf) == ncb_size(buf);
}
/* Returns true if no more data can be inserted in <buf>. */
int ncb_is_full(const struct ncbuf *buf)
{
+ if (ncb_is_null(buf))
+ return 0;
+
BUG_ON_HOT(ncb_read_off(buf, ncb_reserved(buf)) > ncb_size(buf));
return ncb_read_off(buf, ncb_reserved(buf)) == ncb_size(buf);
}
struct ncb_blk blk = ncb_blk_find(buf, off);
ncb_sz_t off_blk = ncb_blk_off(blk, off);
+ if (ncb_blk_is_null(blk))
+ return 0;
+
/* if <off> at the frontier between two and <blk> is gap, retrieve the
* next data block.
*/
bufarea = malloc(bufsize);
+ fprintf(stderr, "running unit tests\n");
+
+ b = NCBUF_NULL;
+ BUG_ON(!ncb_is_null(&b));
+ NCB_DATA_EQ(&b, 0, 0);
+ NCB_TOTAL_DATA_EQ(&b, 0);
+ BUG_ON(ncb_size(&b) != 0);
+ BUG_ON(!ncb_is_empty(&b));
+ BUG_ON(ncb_is_full(&b));
+
b.area = bufarea;
b.size = bufsize;
b.head = head;
NCB_INIT(&b);
- fprintf(stderr, "running unit tests\n");
-
/* insertion test suite */
NCB_INIT(&b);
NCB_DATA_EQ(&b, 0, 0); NCB_DATA_EQ(&b, bufsize - NCB_RESERVED_SZ - 1, 0); /* first and last offset */