--- al.c 2002/10/18 09:09:18 1.20
+++ al.c 2002/10/18 10:20:26 1.21
@@ -102,8 +102,8 @@
/* number of bytes of a span that are stored in a chunk */
#define AL_CHUNK_SPAN(alc, off, n) \
- ((n) > AL_CHUNK_LEN(alc) - (off) ? \
- AL_CHUNK_LEN(alc) - (off) : (n))
+ ((n) > (AL_CHUNK_LEN(alc) - (off)) ? \
+ (AL_CHUNK_LEN(alc) - (off)) : (n))
/*
* number of bytes a chunk can be grown to the end
@@ -111,9 +111,11 @@
* we do not track other chunks sharing the buffer
*/
#define AL_CHUNK_RESERVE(alc) \
- ((alc) \
- ? (alc)->buf->usecount > 1 ? 0 : (alc)->buf->size - (alc)->end \
- : 0)
+ ( (alc) != NULL \
+ ? ( (alc)->buf->usecount > 1 \
+ ? 0 \
+ : (alc)->buf->size - (alc)->end) \
+ : 0)
/* grow chunk to the end */
#define AL_RESIZE(al, alc, n) \
@@ -125,9 +127,11 @@
* we do not track other chunks sharing the buffer
*/
#define AL_CHUNK_PRESERVE(alc) \
- ((alc) \
- ? (alc)->buf->usecount > 1 ? 0 : (alc)->begin \
- : 0)
+ ( (alc) != NULL \
+ ? ( (alc)->buf->usecount > 1 \
+ ? 0 \
+ : (alc)->begin) \
+ : 0)
/* grow chunk to the beginning */
#define AL_PRESIZE(al, alc, n) \
@@ -138,8 +142,8 @@
*
* can be freed when usecount drops to zero
*/
-static
-al_rc_t new_buffer(al_t *al, al_buffer_t **bufp)
+static al_rc_t
+new_buffer(al_t *al, al_buffer_t **bufp)
{
size_t n = al->m.new_buffersize;
al_buffer_t *buf;
@@ -160,8 +164,8 @@
return AL_OK;
}
-static
-al_rc_t dispose_buffer(al_t *al, al_buffer_t *buf)
+static al_rc_t
+dispose_buffer(al_t *al, al_buffer_t *buf)
{
/* must not happen */
if (buf->usecount != 0)
@@ -179,8 +183,8 @@
* and attach to existing memory
* only the control structure will be freed later
*/
-static
-al_rc_t make_buffer(al_t *al, char *p, size_t n, al_buffer_t **bufp)
+static al_rc_t
+make_buffer(al_t *al, char *p, size_t n, al_buffer_t **bufp)
{
al_buffer_t *buf;
@@ -206,15 +210,13 @@
* the case where new_chunk fails and the buffer has no other
* users
*/
-static
-struct {
+static struct {
LIST(al_chunk_t) chunks;
} alc_freelist;
-static
-int alc_freecount = 0;
+static int alc_freecount = 0;
-static
-al_rc_t new_chunk(al_t *al, al_buffer_t *buf, al_chunk_t **alcp)
+static al_rc_t
+new_chunk(al_t *al, al_buffer_t *buf, al_chunk_t **alcp)
{
al_chunk_t *alc;
@@ -239,14 +241,14 @@
alc->begin = 0;
alc->end = 0;
- ++buf->usecount;
+ buf->usecount++;
*alcp = alc;
return AL_OK;
}
-static
-al_rc_t split_chunk(al_t *al, al_chunk_t *orig, size_t off, al_chunk_t **alcp)
+static al_rc_t
+split_chunk(al_t *al, al_chunk_t *orig, size_t off, al_chunk_t **alcp)
{
al_rc_t rc;
al_chunk_t *alc;
@@ -256,7 +258,8 @@
return AL_ERR_ARG;
rc = new_chunk(al, orig->buf, &alc);
- if (rc != AL_OK) return rc;
+ if (rc != AL_OK)
+ return rc;
alc->begin = orig->begin;
alc->end = orig->begin+off;
@@ -266,21 +269,21 @@
return AL_OK;
}
-static
-void dispose_chunk(al_t *al, al_chunk_t *alc)
+static void
+dispose_chunk(al_t *al, al_chunk_t *alc)
{
- --alc->buf->usecount;
- if (alc->buf->usecount == 0)
- dispose_buffer(al,alc->buf);
- alc->buf = NULL;
-
- /* stop freelist from growing infinitely */
- if (alc_freecount >= al->m.max_freechunks) {
- (al->m.free)(alc);
- } else {
- ADDTAIL(&alc_freelist, chunks, alc);
- ++alc_freecount;
- }
+ alc->buf->usecount--;
+ if (alc->buf->usecount == 0)
+ dispose_buffer(al,alc->buf);
+ alc->buf = NULL;
+
+ /* stop freelist from growing infinitely */
+ if (alc_freecount >= al->m.max_freechunks)
+ (al->m.free)(alc);
+ else {
+ ADDTAIL(&alc_freelist, chunks, alc);
+ alc_freecount++;
+ }
}
/*
@@ -291,8 +294,8 @@
* return AL_OK and *alcp == NULL if positioned exactly to end of list
* return AL_ERR_EOF when no such chunk can be found
*/
-static
-al_rc_t al_seek(al_t *al, size_t off, al_chunk_t **alcp, size_t *skipp)
+static al_rc_t
+al_seek(al_t *al, size_t off, al_chunk_t **alcp, size_t *skipp)
{
al_chunk_t *cur;
size_t pos, end;
@@ -343,8 +346,8 @@
}
#ifdef TEST
-static
-void dump(al_t *al)
+static void
+dump(al_t *al)
{
al_chunk_t *cur;
size_t total;
@@ -370,18 +373,19 @@
* allocate an empty assembly line
* dispose all chunks and all allocated backing store
*/
-al_rc_t al_create(al_t **alp)
+al_rc_t
+al_create(al_t **alp)
{
al_t *al;
/* argument sanity check(s) */
if (alp == NULL)
- return AL_RC(AL_ERR_ARG);
+ return AL_RC(AL_ERR_ARG);
/* allocate and initialize new assembly line object */
/* XXX - what malloc ? */
if ((al = (al_t *)malloc(sizeof(al_t))) == NULL)
- return AL_RC(AL_ERR_MEM);
+ return AL_RC(AL_ERR_MEM);
LISTINIT(al,chunks);
al->bytes = 0;
@@ -404,13 +408,14 @@
return AL_OK;
}
-al_rc_t al_destroy(al_t *al)
+al_rc_t
+al_destroy(al_t *al)
{
al_chunk_t *cur, *pred;
/* argument sanity check(s) */
if (al == NULL)
- return AL_RC(AL_ERR_ARG);
+ return AL_RC(AL_ERR_ARG);
/* free chunks and backing store */
FOREACHD(al,chunks,cur,pred) {
@@ -431,7 +436,8 @@
* stops copy operation when a new buffer cannot be created
* but leaves data structure consistent
*/
-al_rc_t al_append_bytes(al_t *al, const char *src, size_t n)
+al_rc_t
+al_append_bytes(al_t *al, const char *src, size_t n)
{
al_rc_t rc;
al_chunk_t *cur;
@@ -441,7 +447,7 @@
/* argument sanity check(s) */
if (al == NULL || src == NULL || n < 0)
- return AL_RC(AL_ERR_ARG);
+ return AL_RC(AL_ERR_ARG);
cur = TAIL(al,chunks);
res = AL_CHUNK_RESERVE(cur);
@@ -449,9 +455,11 @@
while (n > 0) {
if (res == 0) {
rc = new_buffer(al, &buf);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
rc = new_chunk(al,buf,&cur);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
res = AL_CHUNK_RESERVE(cur);
ADDTAIL(al, chunks, cur);
}
@@ -477,7 +485,8 @@
* stops copy operation when a new buffer cannot be created
* but leaves data structure consistent
*/
-al_rc_t al_prepend_bytes(al_t *al, const char *src, size_t n)
+al_rc_t
+al_prepend_bytes(al_t *al, const char *src, size_t n)
{
al_rc_t rc;
al_chunk_t *cur;
@@ -487,7 +496,7 @@
/* argument sanity check(s) */
if (al == NULL || src == NULL || n < 0)
- return AL_RC(AL_ERR_ARG);
+ return AL_RC(AL_ERR_ARG);
cur = HEAD(al,chunks);
res = AL_CHUNK_PRESERVE(cur);
@@ -497,9 +506,11 @@
while (n > 0) {
if (res == 0) {
rc = new_buffer(al, &buf);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
rc = new_chunk(al,buf,&cur);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
res = AL_CHUNK_PRESERVE(cur);
ADDHEAD(al, chunks, cur);
}
@@ -527,7 +538,8 @@
* XXX - some list operations modify the buffer
*
*/
-al_rc_t al_attach_buffer(al_t *al, char *p, size_t n)
+al_rc_t
+al_attach_buffer(al_t *al, char *p, size_t n)
{
al_rc_t rc;
al_buffer_t *buf;
@@ -535,12 +547,14 @@
/* argument sanity check(s) */
if (al == NULL || p == NULL || n < 0)
- return AL_RC(AL_ERR_ARG);
+ return AL_RC(AL_ERR_ARG);
rc = make_buffer(al, p, n, &buf);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
rc = new_chunk(al,buf, &cur);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
ADDTAIL(al, chunks, cur);
/* validate data in buffer */
@@ -560,7 +574,8 @@
* -> the replacement data is moved and not copied.
*
*/
-al_rc_t al_splice(al_t *al, size_t off, size_t n, al_t *nal, al_t *tal)
+al_rc_t
+al_splice(al_t *al, size_t off, size_t n, al_t *nal, al_t *tal)
{
al_rc_t rc;
al_chunk_t *cur, *next, *ins, *splitbuf;
@@ -569,7 +584,7 @@
/* argument sanity check(s) */
if (al == NULL || n < 0)
- return AL_RC(AL_ERR_ARG);
+ return AL_RC(AL_ERR_ARG);
/* optimization: treat an empty list as no insertion at all */
doinsert = (nal != NULL) && !ISEMPTY(nal,chunks);
@@ -579,8 +594,9 @@
* EOD must be a valid seek position so that we can append data
*/
rc = al_seek(al, off, &cur, &skip);
- if (rc != AL_OK) return AL_RC(rc);
-
+ if (rc != AL_OK)
+ return AL_RC(rc);
+
/*
* remember insertion point
*
@@ -611,7 +627,8 @@
if (doinsert) {
if (ins != NULL && skip > 0) {
rc = split_chunk(al, ins, skip, &splitbuf);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
INSERT(al,chunks,ins,splitbuf);
}
}
@@ -642,14 +659,12 @@
size_t before = tal->bytes;
rc = al_append_bytes(tal, AL_CHUNK_PTR(cur, skip), step);
- if (rc != AL_OK) {
+ if (rc != AL_OK)
/* correct step size to actual size */
step = tal->bytes - before;
- }
- } else {
+ } else
rc = AL_OK;
- }
/*
* cut span from src chunk
@@ -657,9 +672,9 @@
* if skip > 0, compute number of bytes to preserve,
* align buffer and shrink chunk from the end
*/
- if (skip == 0) {
+ if (skip == 0)
AL_PRESIZE(al, cur, -step);
- } else {
+ else {
/* align trailing bytes */
if (len > (skip+step)) {
memmove(
@@ -675,10 +690,10 @@
* bail out from failed al_append_bytes operation above
*
*/
- if (rc != AL_OK) return AL_RC(rc);
-
- } else {
-
+ if (rc != AL_OK)
+ return AL_RC(rc);
+ }
+ else {
/*
* the whole chunk has to be moved, simply move it
* to the target chain
@@ -699,13 +714,12 @@
ins = next;
al->bytes -= step;
- if (tal == NULL) {
+ if (tal == NULL)
dispose_chunk(al, cur);
- } else {
+ else {
ADDTAIL(tal, chunks, cur);
tal->bytes += step;
}
-
}
n -= step;
pos += step;
@@ -717,7 +731,6 @@
* now splice in replacement chunks
*/
if (doinsert) {
-
if (ins != NULL) {
/*
* complex case where list is inserted
@@ -745,7 +758,8 @@
* assembly line traversal requires a context. It needs to be
* malloced to keep its inner structure private
*/
-al_rc_t al_txalloc(al_t *al, al_tx_t **txp)
+al_rc_t
+al_txalloc(al_t *al, al_tx_t **txp)
{
al_tx_t *tx;
@@ -760,7 +774,8 @@
/*
* free traversal context using proper policy function
*/
-al_rc_t al_txfree(al_t *al, al_tx_t *tx)
+al_rc_t
+al_txfree(al_t *al, al_tx_t *tx)
{
(al->m.free)(tx);
return AL_OK;
@@ -772,14 +787,16 @@
* - do initial seek, fail if position does not exist
* - save traversal parameters
*/
-al_rc_t al_traverse(al_t *al, size_t off, size_t n, al_td_t dir, al_tx_t *tx)
+al_rc_t
+al_traverse(al_t *al, size_t off, size_t n, al_td_t dir, al_tx_t *tx)
{
al_rc_t rc;
tx->cur = NULL;
rc = al_seek(al, off, &tx->cur, &tx->skip);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
tx->dir = dir;
tx->togo = n;
@@ -794,7 +811,8 @@
* clip view chunk to traversal bounds
* advance chunk cursor according to traversal direction
*/
-al_rc_t al_traverse_next(al_t *al, al_tx_t *tx, al_chunk_t **alcp)
+al_rc_t
+al_traverse_next(al_t *al, al_tx_t *tx, al_chunk_t **alcp)
{
size_t step;
@@ -820,14 +838,14 @@
tx->view.end = tx->view.begin + step;
switch (tx->dir) {
- case AL_FORWARD:
- tx->cur = NEXT(tx->cur,chunks);
- tx->togo -= step;
- break;
- case AL_BACKWARD:
- tx->cur = PREV(tx->cur,chunks);
- tx->togo -= step;
- break;
+ case AL_FORWARD:
+ tx->cur = NEXT(tx->cur,chunks);
+ tx->togo -= step;
+ break;
+ case AL_BACKWARD:
+ tx->cur = PREV(tx->cur,chunks);
+ tx->togo -= step;
+ break;
}
*alcp = &tx->view;
@@ -853,15 +871,17 @@
*
* traversal context is kept on stack (XXX ?)
*/
-al_rc_t al_traverse_cb(al_t *al, size_t off, size_t n, al_td_t dir,
- al_rc_t (*cb)(al_chunk_t *, void *), void *u)
+al_rc_t
+al_traverse_cb(al_t *al, size_t off, size_t n, al_td_t dir,
+ al_rc_t (*cb)(al_chunk_t *, void *), void *u)
{
al_rc_t rc;
al_tx_t tx; /* XXX - private tx structure on stack */
al_chunk_t *view;
rc = al_traverse(al, off, n, dir, &tx);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
while ((rc = al_traverse_next(al, &tx, &view)) == AL_OK) {
rc = cb(view, u);
@@ -869,7 +889,7 @@
break;
}
- (void) al_traverse_end(al, &tx, 1);
+ al_traverse_end(al, &tx, 1);
if (rc != AL_ERR_EOF)
return AL_RC(rc);
@@ -884,7 +904,8 @@
*
* traversal context is kept on stack (XXX ?)
*/
-al_rc_t al_flatten(al_t *al, size_t off, size_t n, char *dst, size_t *lenp)
+al_rc_t
+al_flatten(al_t *al, size_t off, size_t n, char *dst, size_t *lenp)
{
al_rc_t rc;
al_tx_t tx; /* XXX - private tx structure on stack */
@@ -894,18 +915,19 @@
*lenp = 0; /* keep caller on safe side */
rc = al_traverse(al, off, n, AL_FORWARD, &tx);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
total = 0;
while ((rc = al_traverse_next(al, &tx, &view)) == AL_OK) {
step = AL_CHUNK_LEN(view);
- memmove(dst, AL_CHUNK_PTR(view,0), step);
+ memmove(dst, AL_CHUNK_PTR(view, 0), step);
dst += step;
total += step;
}
*lenp = total;
- (void) al_traverse_end(al, &tx, 1);
+ al_traverse_end(al, &tx, 1);
if (rc != AL_ERR_EOF)
return AL_RC(rc);
@@ -918,7 +940,8 @@
*
* traversal context is kept on stack (XXX ?)
*/
-al_rc_t al_copy(al_t *al, size_t off, size_t n, al_t *tal)
+al_rc_t
+al_copy(al_t *al, size_t off, size_t n, al_t *tal)
{
al_rc_t rc;
al_tx_t tx; /* XXX - private tx structure on stack */
@@ -926,14 +949,15 @@
size_t step;
rc = al_traverse(al, off, n, AL_FORWARD, &tx);
- if (rc != AL_OK) return AL_RC(rc);
+ if (rc != AL_OK)
+ return AL_RC(rc);
while ((rc = al_traverse_next(al, &tx, &view)) == AL_OK) {
step = AL_CHUNK_LEN(view);
- al_append_bytes(tal, AL_CHUNK_PTR(view,0), step);
+ al_append_bytes(tal, AL_CHUNK_PTR(view, 0), step);
}
- (void) al_traverse_end(al, &tx, 1);
+ al_traverse_end(al, &tx, 1);
if (rc != AL_ERR_EOF)
return AL_RC(rc);
@@ -951,19 +975,27 @@
*
* off must be a valid offset within (0,len(
*/
-size_t al_bytes(const al_t *al)
+
+size_t
+al_bytes(const al_t *al)
{
return al->bytes;
}
-size_t al_chunk_len(al_chunk_t *alc)
+
+size_t
+al_chunk_len(al_chunk_t *alc)
{
return AL_CHUNK_LEN(alc);
}
-size_t al_chunk_span(al_chunk_t *alc, size_t off, size_t n)
+
+size_t
+al_chunk_span(al_chunk_t *alc, size_t off, size_t n)
{
return AL_CHUNK_SPAN(alc, off, n);
}
-char *al_chunk_ptr(al_chunk_t *alc, size_t off)
+
+char *
+al_chunk_ptr(al_chunk_t *alc, size_t off)
{
return AL_CHUNK_PTR(alc, off);
}
@@ -971,17 +1003,18 @@
/*
* convert error code into human readable form
*/
-const char *al_error(al_rc_t rc)
+const char *
+al_error(al_rc_t rc)
{
const char *mess;
switch (rc) {
- case AL_OK: mess = "Everything Ok"; break;
- case AL_ERR_ARG: mess = "Invalid Argument"; break;
- case AL_ERR_MEM: mess = "Not Enough Memory"; break;
- case AL_ERR_EOF: mess = "End Of Data"; break;
- case AL_ERR_INT: mess = "Internal Error"; break;
- default: mess = "Invalid Result Code"; break;
+ case AL_OK: mess = "Everything Ok"; break;
+ case AL_ERR_ARG: mess = "Invalid Argument"; break;
+ case AL_ERR_MEM: mess = "Not Enough Memory"; break;
+ case AL_ERR_EOF: mess = "End Of Data"; break;
+ case AL_ERR_INT: mess = "Internal Error"; break;
+ default: mess = "Invalid Result Code"; break;
}
return mess;
|