head 1.20; access; symbols; locks; strict; comment @ * @; 1.20 date 2002.12.17.13.43.19; author mlelstv; state dead; branches; next 1.19; 1.19 date 2002.11.28.17.44.14; author mlelstv; state Exp; branches; next 1.18; 1.18 date 2002.11.28.16.11.49; author mlelstv; state Exp; branches; next 1.17; 1.17 date 2002.11.28.15.12.40; author mlelstv; state Exp; branches; next 1.16; 1.16 date 2002.11.05.14.52.24; author mlelstv; state Exp; branches; next 1.15; 1.15 date 2002.10.23.16.49.29; author mlelstv; state Exp; branches; next 1.14; 1.14 date 2002.10.22.15.33.16; author mlelstv; state Exp; branches; next 1.13; 1.13 date 2002.10.18.15.53.17; author rse; state Exp; branches; next 1.12; 1.12 date 2002.10.18.12.24.53; author mlelstv; state Exp; branches; next 1.11; 1.11 date 2002.10.18.11.03.07; author rse; state Exp; branches; next 1.10; 1.10 date 2002.10.18.11.01.59; author rse; state Exp; branches; next 1.9; 1.9 date 2002.10.18.09.10.10; author mlelstv; state Exp; branches; next 1.8; 1.8 date 2002.10.17.15.02.49; author rse; state Exp; branches; next 1.7; 1.7 date 2002.10.16.14.20.48; author ms; state Exp; branches; next 1.6; 1.6 date 2002.10.16.12.37.08; author rse; state Exp; branches; next 1.5; 1.5 date 2002.10.14.15.41.37; author mlelstv; state Exp; branches; next 1.4; 1.4 date 2002.10.14.15.26.04; author mlelstv; state Exp; branches; next 1.3; 1.3 date 2002.10.14.13.34.25; author mlelstv; state Exp; branches; next 1.2; 1.2 date 2002.10.14.12.32.16; author mlelstv; state Exp; branches; next 1.1; 1.1 date 2002.10.14.08.04.46; author mlelstv; state Exp; branches; next ; desc @@ 1.20 log @moved into its own al project PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @/* ** OSSP al -- Assembly Line ** Copyright (c) 2002 The OSSP Project ** Copyright (c) 2002 Cable & Wireless Deutschland ** Copyright (c) 2002 Ralf S. Engelschall ** Copyright (c) 2002 Michael van Elst ** ** This file is part of OSSP al, an abstract datatype of a data buffer ** that can assemble, move and truncate data but avoids actual copying. ** ** Permission to use, copy, modify, and distribute this software for ** any purpose with or without fee is hereby granted, provided that ** the above copyright notice and this permission notice appear in all ** copies. ** ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** SUCH DAMAGE. ** ** al_test.c: assembly line library, minimal test suite */ #include #include #include #include #include "al.h" /* get unique pointers */ static char label, label2, label3; #define LABEL ((al_label_t)&label) #define LABEL2 ((al_label_t)&label2) #define LABEL3 ((al_label_t)&label3) #define S(s) s, strlen(s) static const char *fill(char *p, int n) { static char buf[4 * 80 + 1]; int max = sizeof(buf)-5; int k; k=0; while (n > 0 && k < max) { if (isprint(*p)) { buf[k] = *p; k += 1; } else { sprintf(buf+k,"<%2.2x>",*p); k += 4; } ++p; --n; } buf[k] = '\0'; return (const char *)buf; } static al_rc_t printchunk(al_chunk_t *alc, void *u) { size_t len, pre, post; len = al_chunk_len(alc); pre = 16; if (pre > len) pre = len; post = 16; if (post > len-pre) post = len-pre; printf("C: %08lx + %-6d [%08lx] = ", (long)al_chunk_ptr(alc, 0), al_chunk_len(alc), (long)al_chunk_label(alc)); fputs(fill(al_chunk_ptr(alc,0),pre), stdout); if (post > 0) { fputs(" .. ", stdout); fputs(fill(al_chunk_ptr(alc,len-post),post), stdout); } fputs("\n", stdout); return AL_OK; } #define DUMP(tag,al) do {\ printf("+DUMP(%s)\n",tag);\ al_traverse_cb(al, 0, al_bytes(al), AL_FORWARD, NULL, printchunk, NULL);\ printf("-DUMP(%s)\n\n",tag);\ } while (0) static void print(const char *tag, al_t *al) { #if 0 char *buf; size_t n, len; n = al_bytes(al); buf = (char *)malloc(n); if (buf == NULL) abort(); al_flatten(al, 0, n, AL_FORWARD, LABEL, buf, &len); printf("%s = %d of %d\n",tag,len,n); fwrite(">>", 2, 1, stdout); fwrite(buf, len, 1, stdout); fwrite("<<", 2, 1, stdout); printf("\n\n"); free(buf); #endif } static void checklen(const char *tag, al_t *al) { al_tx_t *tx; al_chunk_t *cur; size_t total, total2; total = 0; al_txalloc(al, &tx); al_traverse(al, 0, al_bytes(al), AL_FORWARD, NULL, tx); while (al_traverse_next(al, tx, &cur) == AL_OK) total += al_chunk_len(cur); al_traverse_end(al, tx, 1); al_txfree(al, tx); total2 = al_bytes(al); if (total != total2) printf("ERROR: al_bytes(%s=%p): count %d != sum %d\n", tag,(void *)al,total,total2); } static void reclaim(char *p, size_t n, void *u) { printf("*** reclaiming buffer %p size %d ***\n",p,n); } int main(int argc, char *argv[]) { al_rc_t rc; al_t *al, *al2, *al3, *al4; char baf[] = "Mittendrin\n"; int i; size_t off, span; al_create(&al); al_create(&al2); al_create(&al3); al_append_bytes(al, S("Hello world\n"), LABEL); al_attach_buffer(al, S(baf), LABEL, reclaim, NULL); for (i=0; i<500; ++i) al_append_bytes(al, S("Huhu world\n"), LABEL); al_append_bytes(al2, S("Hello world\n"), LABEL); al_append_bytes(al3, S("HUHU WORLD\n"), LABEL2); DUMP("DATA",al); DUMP("BUFFER",al2); DUMP("REPLACEMENT", al3); rc = al_splice(al, al_bytes(al)-500, 500, al3, al2); printf("splice result: %d (%s)\n\n",rc,al_error(rc)); checklen("SPLICED",al); checklen("BUFFER",al2); checklen("REPLACEMENT",al3); al_create(&al4); rc = al_copy(al, al_bytes(al)-42, 38, LABEL, al4); printf("copy result: %d (%s)\n\n",rc,al_error(rc)); checklen("SPLICED",al); checklen("COPY",al4); rc = al_setlabel(al4, 7, 2, NULL, LABEL3); printf("setlabel result: %d (%s)\n\n",rc,al_error(rc)); rc = al_setlabel(al4, 5, 9, LABEL, LABEL2); printf("setlabel result: %d (%s)\n\n",rc,al_error(rc)); checklen("COPY",al4); rc = al_spanlabel(al4, 8, al_bytes(al), LABEL, &off, &span); printf("spanlabel result: %d (%s)\n\n",rc,al_error(rc)); printf("offset = %d, span = %d\n",off,span); DUMP("SPLICED",al); print("SPLICED", al); DUMP("BUFFER",al2); print("BUFFER", al2); DUMP("REPLACEMENT", al3); print("REPLACEMENT", al3); printf("free REPLACEMENT\n"); fflush(stdout); al_destroy(al3); printf("free BUFFER\n"); fflush(stdout); al_destroy(al2); printf("free SPLICED\n"); fflush(stdout); al_destroy(al); DUMP("COPY", al4); print("COPY", al4); printf("free COPY\n"); fflush(stdout); al_destroy(al4); return 0; } @ 1.19 log @more labelling test PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @@ 1.18 log @new al_spanlabel method PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d40 1 a40 1 static char label, label2; d43 1 d186 3 a188 1 rc = al_setlabel(al4, 5, 9, LABEL2); @ 1.17 log @new al_setlabel method to modify the data label within an assembly line. PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d152 1 d188 4 @ 1.16 log @fix parameter remove compiler warning (signed/unsigned comparison) PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d93 1 a93 1 al_traverse_cb(al, 0, al_bytes(al), AL_FORWARD, LABEL, printchunk, NULL);\ d181 6 @ 1.15 log @new span traversal mode add direction to al_flatten to support span traversal utility method al_firstlabel PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d49 1 d53 1 a53 1 while (n > 0 && k < sizeof(buf)-5) { d99 1 a102 2 return; d116 1 d128 1 a128 1 al_traverse(al, 0, -1, AL_FORWARD, NULL, tx); @ 1.14 log @significant API change. now support data labels. PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d107 1 a107 1 al_flatten(al, 0, n, LABEL, buf, &len); @ 1.13 log @shutdown warning @ text @d39 5 d73 2 a74 2 pre = 20; if (pre > len) pre = len; post = 20; if (post > len-pre) post = len-pre; d76 1 a76 1 printf("C: %08lx + %-6d = ", d78 2 a79 1 al_chunk_len(alc)); d92 1 a92 1 al_traverse_cb(al, 0, al_bytes(al), AL_FORWARD, printchunk, NULL);\ d101 2 d107 1 a107 1 al_flatten(al, 0, n, buf, &len); d127 1 a127 1 al_traverse(al, 0, -1, AL_FORWARD, tx); d156 2 a157 2 al_append_bytes(al, S("Hello world\n")); al_attach_buffer(al, S(baf), reclaim, NULL); d160 1 a160 1 al_append_bytes(al, S("Huhu world\n")); d162 1 a162 1 al_append_bytes(al2, S("Hello world\n")); d164 1 a164 1 al_append_bytes(al3, S("HUHU WORLD\n")); d178 1 a178 1 rc = al_copy(al, al_bytes(al)-42, 38, al4); @ 1.12 log @change in al_attach_buffer API back out rse's change to al_traverse parameters. parameter order is: object ptr(if any), input parameters, output parameters PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d132 1 a132 1 void reclaim(char *p, size_t n, void *u) @ 1.11 log @shutdown remaining gcc warnings @ text @d119 1 a119 1 al_traverse(al, tx, 0, -1, AL_FORWARD); d132 5 d149 1 a149 1 al_attach_buffer(al, S(baf)); @ 1.10 log @Be pedantic about consistent argument orders and move al_tx_t argument to second position where it is for all other al_tx_t based functions: -al_traverse(al_t *al, size_t off, size_t n, al_td_t dir, al_tx_t *tx) +al_traverse(al_t *al, al_tx_t *tx, size_t off, size_t n, al_td_t dir) @ text @d41 1 a41 1 const char *fill(char *p, int n) d63 1 a63 1 al_rc_t printchunk(al_chunk_t *alc, void *u) d90 1 a90 1 void print(const char *tag, al_t *al) d110 1 a110 1 void checklen(const char *tag, al_t *al) d129 1 a129 1 tag,al,total,total2); d132 1 a132 1 int main() @ 1.9 log @use al_traverse_end() as required by API PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d119 1 a119 1 al_traverse(al, 0, -1, AL_FORWARD, tx); @ 1.8 log @Ok, as arranged with Michael: use 'assembly line' as the official long name @ text @d122 1 @ 1.7 log @I assume this is a copy and paste error, but is not syntactically wrong in any case. @ text @d2 1 a2 1 ** OSSP al -- Assembly Lists d29 1 a29 1 ** al_test.c: assembly lists library, minimal test suite @ 1.6 log @add the usual amount of license fun @ text @d148 1 a148 1 al_append_bytes(al, S("Hello world\n")); @ 1.5 log @al_copy function, like al_flatten but into a target assembly list PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d1 31 @ 1.4 log @INSERT/REMOVE didn't maintain list header test code PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d103 1 a103 1 al_t *al, *al2, *al3; a124 1 #if 1 a125 1 #endif d132 4 d157 8 @ 1.3 log @code cleanup get rid of AL_ALL_BYTES, not very useful PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d79 1 a79 1 void checklen(al_t *al) d96 2 a97 1 printf("ERROR: al_bytes(%p): %d != %d\n",al,total,total2); d130 3 a132 3 checklen(al); checklen(al2); checklen(al3); d143 2 d146 3 d150 3 @ 1.2 log @splice operation traversal needs alloc/free function for context lost of comments PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d10 1 a10 1 al_rc_t printchunk(al_chunk_t *alc, void *u) d12 2 a13 4 char buf[40]; char *p = al_chunk_ptr(alc,0); size_t n = al_chunk_len(alc); int k; d18 2 a19 1 buf[k++] = *p; d29 12 a40 1 printf("C: %08lx + %-6d = %s\n", d42 7 a48 2 al_chunk_len(alc), buf); d61 1 a61 1 char buf[10000]; a63 1 printf("%s\n",tag); d65 3 a67 1 if (n > sizeof(buf)) n = sizeof(buf); d69 2 d75 2 d101 1 a119 1 d125 1 a125 3 al_splice(al, 102, 200, al3, al2); #else al_splice(al, 102, 200, NULL, NULL); d127 1 d134 1 a134 1 print("RESULT", al); @ 1.1 log @first iteration through buffer data type PR: Submitted by: Reviewed by: Approved by: Obtained from: @ text @d4 1 d12 22 a33 4 printf("C: %8p = %8p + %d\n", alc, al_chunk_ptr(alc, 0), al_chunk_len(alc)); d44 35 d81 1 a81 3 al_t *al, *al2; char buf[10000]; size_t n; d87 1 d97 6 a103 1 DUMP("ALL",al); d105 1 a105 1 al_splice(al, 102, 200, al2); d107 1 a107 1 al_splice(al, 102, 200, NULL); d109 5 d115 2 d118 1 d120 2 a121 16 printf("RESULT\n"); n = al_bytes(al); if (n > 10000) n = 10000; al_flatten(al, 0, n, buf); fwrite(">>", 2, 1, stdout); fwrite(buf, n, 1, stdout); fwrite("<<", 2, 1, stdout); printf("BUFFER\n"); n = al_bytes(al2); if (n > 10000) n = 10000; al_flatten(al2, 0, n, buf); fwrite(">>", 2, 1, stdout); fwrite(buf, n, 1, stdout); fwrite("<<", 2, 1, stdout); d123 1 @