--- al.c 2002/10/14 13:45:47 1.5
+++ al.c 2002/10/14 15:26:04 1.6
@@ -1,6 +1,10 @@
+#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
+#ifdef TEST
+#include <stdio.h>
+#endif
/****************************************************************************/
@@ -82,16 +86,24 @@
do { \
if (PREV(n,l)) \
NEXT(PREV(n,l),l) = NEXT(n,l); \
+ else \
+ HEAD(q,l) = NEXT(n,l); \
if (NEXT(n,l)) \
PREV(NEXT(n,l),l) = PREV(n,l); \
+ else \
+ TAIL(q,l) = PREV(n,l); \
+ NEXT(n,l) = NULL; \
+ PREV(n,l) = NULL; \
} while (0)
#define INSERT(q,l,i,n) \
do { \
if (PREV(i,l)) { \
- PREV(n,l) = PREV(i,l); \
NEXT(PREV(i,l),l) = (n); \
+ } else { \
+ HEAD(q,l) = (n); \
} \
+ PREV(n,l) = PREV(i,l); \
PREV(i,l) = (n); \
NEXT(n,l) = (i); \
} while (0)
@@ -342,6 +354,8 @@
static
void dispose_chunk(al_t *al, al_chunk_t *alc)
{
+ assert(NEXT(alc,chunks) == NULL && PREV(alc,chunks) == NULL);
+
--alc->buf->usecount;
if (alc->buf->usecount == 0)
dispose_buffer(al,alc->buf);
@@ -411,20 +425,24 @@
return AL_ERR_EOF;
}
-#if 0
-#include <stdio.h>
+#ifdef TEST
static
void dump(al_t *al)
{
al_chunk_t *cur;
+ size_t total;
- printf("AL: %x\n", al);
+ printf("AL: %p\n", al);
+ total = 0;
FOREACH(al,chunks,cur) {
- printf(" C: %x (%d @ %x + %d < %d)\n",
+ printf(" C: %p (%d @ %p + %d < %d (use=%d))\n",
cur,
cur->buf->size,cur->buf->mem,
- cur->begin,cur->end);
+ cur->begin,cur->end,
+ cur->buf->usecount);
+ total += AL_CHUNK_LEN(cur);
}
+ printf("size = %d == %d\n",al->bytes,total);
printf("--\n\n");
}
#endif
@@ -456,7 +474,11 @@
al->m.free = free;
al->m.balloc = malloc; /* buffer allocation */
al->m.bfree = free;
+#ifdef TEST
+ al->m.new_buffersize = 42;
+#else
al->m.new_buffersize = 4096;
+#endif
al->m.max_freechunks = 500;
/* pass object to caller */
@@ -475,6 +497,7 @@
/* free chunks and backing store */
FOREACHD(al,chunks,cur,pred) {
+ REMOVE(al,chunks,cur);
dispose_chunk(al,cur);
}
@@ -658,11 +681,11 @@
step = len;
REMOVE(al, chunks, cur);
al->bytes -= step;
- if (tal != NULL) {
+ if (tal == NULL) {
+ dispose_chunk(al, cur);
+ } else {
ADDTAIL(tal, chunks, cur);
tal->bytes += step;
- } else {
- dispose_chunk(al, cur);
}
}
|