- eliminate the string library's hidden dependency on ; make explicit - use ainc() and adec() for reference counting rather than a lock. Reference: /n/atom/patch/applied/stringbio2 Date: Sun May 25 04:12:23 CES 2014 Signed-off-by: quanstro@quanstro.net --- /sys/include/String.h Sun May 25 04:10:50 2014 +++ /sys/include/String.h Sun May 25 04:10:51 2014 @@ -3,11 +3,10 @@ /* extensible Strings */ typedef struct String { - Lock; char *base; /* base of String */ char *end; /* end of allocated space+1 */ char *ptr; /* ptr into String */ - short ref; + int ref; uchar fixed; } String; @@ -33,7 +32,6 @@ extern String* s_unique(String*); extern String* s_grow(String*, int); -#ifdef BGETC extern int s_read(Biobuf*, String*, int); extern char *s_read_line(Biobuf*, String*); extern char *s_getline(Biobuf*, String*); @@ -42,4 +40,3 @@ extern char *s_rdinstack(Sinstack*, String*); extern Sinstack *s_allocinstack(char*); extern void s_freeinstack(Sinstack*); -#endif /* BGETC */ --- /sys/include/bio.h Sun May 25 04:10:53 2014 +++ /sys/include/bio.h Sun May 25 04:10:53 2014 @@ -6,7 +6,7 @@ enum { - Bsize = 8*1024, + Bsize = 6*8*1024, Bungetsize = 4, /* space for ungetc */ Bmagic = 0x314159, Beof = -1, @@ -39,9 +39,6 @@ Biobufhdr; uchar b[Bungetsize+Bsize]; }; - -/* Dregs, the string library has a sneaky ifdef */ -#define BGETC fixthestringlibrary int Bbuffered(Biobufhdr*); int Bfildes(Biobufhdr*); --- /sys/src/libString/s_alloc.c Sun May 25 04:10:55 2014 +++ /sys/src/libString/s_alloc.c Sun May 25 04:10:56 2014 @@ -1,20 +1,15 @@ #include #include -#include "String.h" +#include +#include #define STRLEN 128 -extern void +void s_free(String *sp) { - if (sp == nil) + if(sp == nil || adec(&sp->ref) > 0) return; - lock(sp); - if(--(sp->ref) != 0){ - unlock(sp); - return; - } - unlock(sp); if(sp->fixed == 0 && sp->base != nil) free(sp->base); @@ -22,18 +17,15 @@ } /* get another reference to a string */ -extern String * +String * s_incref(String *sp) { - lock(sp); - sp->ref++; - unlock(sp); - + ainc(&sp->ref); return sp; } /* allocate a String head */ -extern String * +String * _s_alloc(void) { String *s; @@ -47,7 +39,7 @@ } /* create a new `short' String */ -extern String * +String * s_newalloc(int len) { String *sp; @@ -69,7 +61,7 @@ } /* create a new `short' String */ -extern String * +String * s_new(void) { String *sp; --- /sys/src/libString/s_append.c Sun May 25 04:10:57 2014 +++ /sys/src/libString/s_append.c Sun May 25 04:10:58 2014 @@ -1,14 +1,15 @@ #include #include -#include "String.h" +#include +#include /* append a char array to a String */ String * s_append(String *to, char *from) { - if (to == 0) + if (to == nil) to = s_new(); - if (from == 0) + if (from == nil) return to; for(; *from; from++) s_putc(to, *from); --- /sys/src/libString/s_array.c Sun May 25 04:11:00 2014 +++ /sys/src/libString/s_array.c Sun May 25 04:11:01 2014 @@ -1,15 +1,17 @@ #include #include -#include "String.h" +#include +#include extern String* _s_alloc(void); /* return a String containing a character array (this had better not grow) */ -extern String * +String * s_array(char *cp, int len) { - String *sp = _s_alloc(); + String *sp; + sp = _s_alloc(); sp->base = sp->ptr = cp; sp->end = sp->base + len; sp->fixed = 1; --- /sys/src/libString/s_copy.c Sun May 25 04:11:03 2014 +++ /sys/src/libString/s_copy.c Sun May 25 04:11:04 2014 @@ -1,10 +1,11 @@ #include #include -#include "String.h" +#include +#include /* return a String containing a copy of the passed char array */ -extern String* +String* s_copy(char *cp) { String *sp; --- /sys/src/libString/s_getline.c Sun May 25 04:11:05 2014 +++ /sys/src/libString/s_getline.c Sun May 25 04:11:06 2014 @@ -1,7 +1,7 @@ #include #include #include -#include "String.h" +#include /* Append an input line to a String. * @@ -10,11 +10,11 @@ * * Empty lines and lines starting with '#' are ignored. */ -extern char * +char * s_getline(Biobuf *fp, String *to) { int c; - int len=0; + int len; s_terminate(to); @@ -46,7 +46,7 @@ } /* gather up a line */ - for(;;) { + for(len = 0;;) { len++; switch(c) { case -1: --- /sys/src/libString/s_grow.c Sun May 25 04:11:08 2014 +++ /sys/src/libString/s_grow.c Sun May 25 04:11:09 2014 @@ -1,9 +1,10 @@ #include #include -#include "String.h" +#include +#include /* grow a String's allocation by at least `incr' bytes */ -extern String* +String* s_grow(String *s, int incr) { char *cp; @@ -23,7 +24,7 @@ size += size/2; cp = realloc(s->base, size); - if (cp == 0) + if (cp == nil) sysfatal("s_grow: %r"); s->ptr = (s->ptr - s->base) + cp; s->end = cp + size; --- /sys/src/libString/s_memappend.c Sun May 25 04:11:11 2014 +++ /sys/src/libString/s_memappend.c Sun May 25 04:11:12 2014 @@ -1,6 +1,7 @@ #include #include -#include "String.h" +#include +#include /* append a char array ( of up to n characters) to a String */ String * @@ -8,9 +9,9 @@ { char *e; - if (to == 0) + if (to == nil) to = s_new(); - if (from == 0) + if (from == nil) return to; for(e = from + n; from < e; from++) s_putc(to, *from); --- /sys/src/libString/s_nappend.c Sun May 25 04:11:13 2014 +++ /sys/src/libString/s_nappend.c Sun May 25 04:11:14 2014 @@ -1,14 +1,15 @@ #include #include -#include "String.h" +#include +#include /* append a char array ( of up to n characters) to a String */ String * s_nappend(String *to, char *from, int n) { - if (to == 0) + if(to == nil) to = s_new(); - if (from == 0) + if(from == nil) return to; for(; n && *from; from++, n--) s_putc(to, *from); --- /sys/src/libString/s_parse.c Sun May 25 04:11:15 2014 +++ /sys/src/libString/s_parse.c Sun May 25 04:11:16 2014 @@ -1,6 +1,7 @@ #include #include -#include "String.h" +#include +#include #define isspace(c) ((c)==' ' || (c)=='\t' || (c)=='\n') @@ -11,8 +12,8 @@ s_parse(String *from, String *to) { if (*from->ptr == '\0') - return 0; - if (to == 0) + return nil; + if (to == nil) to = s_new(); if (*from->ptr == '\'') { from->ptr++; --- /sys/src/libString/s_putc.c Sun May 25 04:11:17 2014 +++ /sys/src/libString/s_putc.c Sun May 25 04:11:19 2014 @@ -1,6 +1,7 @@ #include #include -#include "String.h" +#include +#include void s_putc(String *s, int c) --- /sys/src/libString/s_rdinstack.c Sun May 25 04:11:20 2014 +++ /sys/src/libString/s_rdinstack.c Sun May 25 04:11:21 2014 @@ -1,8 +1,7 @@ - #include #include #include -#include "String.h" +#include struct Sinstack{ int depth; @@ -10,7 +9,7 @@ }; /* initialize */ -extern Sinstack * +Sinstack * s_allocinstack(char *file) { Sinstack *sp; @@ -26,7 +25,7 @@ return sp; } -extern void +void s_freeinstack(Sinstack *sp) { while(sp->depth >= 0) @@ -38,7 +37,7 @@ * * Empty lines and leading whitespace are removed. */ -static char * +char * rdline(Biobuf *fp, String *to) { int c; @@ -90,7 +89,7 @@ * Lines starting with #include cause us to descend into the new file. * Empty lines and other lines starting with '#' are ignored. */ -extern char * +char * s_rdinstack(Sinstack *sp, String *to) { char *p; --- /sys/src/libString/s_read.c Sun May 25 04:11:23 2014 +++ /sys/src/libString/s_read.c Sun May 25 04:11:24 2014 @@ -12,7 +12,7 @@ * * Returns the number of characters read. */ -extern int +int s_read(Biobuf *fp, String *to, int len) { int rv; --- /sys/src/libString/s_read_line.c Sun May 25 04:11:25 2014 +++ /sys/src/libString/s_read_line.c Sun May 25 04:11:26 2014 @@ -8,7 +8,7 @@ * Returns a pointer to the character string (or 0). * Trailing newline is left on. */ -extern char * +char * s_read_line(Biobuf *fp, String *to) { char *cp; @@ -18,8 +18,8 @@ sysfatal("can't s_read_line a shared string"); s_terminate(to); cp = Brdline(fp, '\n'); - if(cp == 0) - return 0; + if(cp == nil) + return nil; llen = Blinelen(fp); if(to->end - to->ptr < llen) s_grow(to, llen); --- /sys/src/libString/s_reset.c Sun May 25 04:11:28 2014 +++ /sys/src/libString/s_reset.c Sun May 25 04:11:29 2014 @@ -1,6 +1,7 @@ #include #include -#include "String.h" +#include +#include String* s_reset(String *s) --- /sys/src/libString/s_terminate.c Sun May 25 04:11:30 2014 +++ /sys/src/libString/s_terminate.c Sun May 25 04:11:31 2014 @@ -1,6 +1,7 @@ #include #include -#include "String.h" +#include +#include void s_terminate(String *s) --- /sys/src/libString/s_tolower.c Sun May 25 04:11:32 2014 +++ /sys/src/libString/s_tolower.c Sun May 25 04:11:33 2014 @@ -1,6 +1,7 @@ #include #include -#include "String.h" +#include +#include /* convert String to lower case */ --- /sys/src/libString/s_unique.c Sun May 25 04:11:35 2014 +++ /sys/src/libString/s_unique.c Sun May 25 04:11:36 2014 @@ -1,6 +1,7 @@ #include #include -#include "String.h" +#include +#include String* s_unique(String *s)