Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!munnari.oz.au!uniwa!DIALix!metapro!bernie From: bernie@metapro.DIALix.oz.au (Bernd Felsche) Newsgroups: comp.sys.amiga.programmer Subject: Re: C string library? Keywords: strings, C Message-ID: <1991Feb15.050640.15436@metapro.DIALix.oz.au> Date: 15 Feb 91 05:06:40 GMT References: <874@cbmger.UUCP> Organization: MetaPro Systems, Perth, Western Australia Lines: 134 In <874@cbmger.UUCP> peterk@cbmger.UUCP (Peter Kittel GERMANY) writes: >Now it should be possible to have a set of functions which initialize >a memory area and allocate dynamically all my strings in that space >just as a Basic interpreter does. Plus a good and fast garbage collection. >And plus all the nice string functions that make Basic programming such >a fun. And if you would add further flexibility over the Basic approach, >then one could add dynamic growth of the whole string area, where you >in Basic once and for all must decide for a certain amount by CLEAR. >So, is there such a beast out there? This is what I use under UNIX, though it should work under AmigaDOS as well. It doesn't do any garbage collection, but it's fast! Also, there is no mechanism to grow a string, though that should be a bit easier to implement (for somebody else :-)). It works by having a linked list pointing to a cluster of malloc'd memory chunks, the size of which can be "defined" at the start. When addstring() is called, it stores the nominated string in an allocated memory chunks, before returning a pointer to it. If there is insufficient space in existing chunks, then a new one is allocated, along with another linked-list element. If the string to be stored is larger than the nominal chunks size, then it is stored in its own chunk. The bad news is that there is no garbage collection, or indeed code to free up space. See my notes at the end of the listing. I'm relying on malloc'd chunks being cleaned up automatically upon exit. P.S. the missing fatal() and sysfatal() routines do nothing more than displaying the error text somewhere convenient, and exiting with an error condition. ___________________________________space.c_____________________________________ /* * Buffer some strings in dynamically allocated RAM chunks */ #define NULL 0 #include #include char *malloc(); /* * allocated in ALLOC_SPACE chunks, and expanded * dynamically, if it isn't eno...ugh */ #define ALLOC_SPACE 8000 #define ALLOC_LOW 16 struct p_space { char *free; /* next free space in this chunk */ int avail; /* amount of available space in this chunk*/ struct p_space *next; /* next p_space chunk */ char *stuff; /* this space chunk */ }; static struct p_space *MEMLIST; /* the head of the linked list */ static struct p_space *MEMLAST; /* the head of the mostly - free linked list */ char *addstring(string) char *string; /* text string */ { struct p_space *chunk; int length; char *place; length = strlen(string) + 1; /* remember null! */ if( MEMLIST == NULL ) { /* no space yet allocated */ if( !(MEMLIST = (struct p_space *) malloc(sizeof(struct p_space))) ) sysfatal("cannot allocate space for string"); MEMLIST->avail = (length < ALLOC_SPACE ? ALLOC_SPACE : length); if( !(MEMLIST->stuff = malloc(MEMLIST->avail)) ) sysfatal("cannot allocate chunk"); MEMLIST->free = MEMLIST->stuff; MEMLIST->next = NULL; MEMLAST = MEMLIST; chunk = MEMLAST; } chunk = MEMLAST; /* search for enough free space in any chunk */ while ( length >= chunk->avail && chunk->next ) { if( chunk->avail < ALLOC_LOW ) MEMLAST = chunk->next; /* speedup if low water reached */ chunk = chunk->next; } if( !chunk->next && length >= chunk->avail ) { /* can't find enough space */ if( !(chunk->next = (struct p_space *) malloc(sizeof(struct p_space))) ) sysfatal("cannot allocate space for string"); chunk = chunk->next; chunk->avail = (length < ALLOC_SPACE ? ALLOC_SPACE : length); if( !(chunk->stuff = malloc(chunk->avail)) ) sysfatal("cannot allocate chunk"); chunk->free = chunk->stuff; chunk->next = NULL; } /* copy string to chunk */ place = strcpy(chunk->free, string); if ( place != chunk->free ) fatal("error in string copy"); chunk->free += length; chunk->avail -= length; return(place); } char *delstring(string) char *string; { /* an exercise for the programmer :-) */ } ___________________________________the.end_____________________________________ How's that for some pointers in C? -- _--_|\ Bernd Felsche #include / \ Metapro Systems, 328 Albany Highway, Victoria Park, Western Australia \_.--._/ Fax: +61 9 472 3337 Phone: +61 9 362 9355 TZ=WST-8 v E-Mail: bernie@metapro.DIALix.oz.au | bernie@DIALix.oz.au