Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

nsrealloc.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <malloc.h>
00004 #include <neuro/nsrealloc.h>
00005 
00006 #define STARTINGBUFSIZE 256
00007 
00008 struct NSByteHolder
00009 {
00010   int alloc, used;
00011   char *buf;
00012 };
00013 
00014 static void ensureMoreSpace(struct NSByteHolder *nsb)
00015 {
00016   if (nsb->used == nsb->alloc)
00017     {
00018     int newSize;
00019     void *newbuf;
00020     newSize = nsb->alloc * 2;
00021     newbuf = calloc(1, newSize);
00022     memcpy(newbuf, nsb->buf, nsb->used);
00023     free(nsb->buf);
00024     nsb->buf = newbuf;
00025     nsb->alloc = newSize;
00026     }
00027 }
00028 
00029 struct NSByteHolder *newNSByteHolder(void)
00030 {
00031   struct NSByteHolder *nsb;
00032   nsb = calloc(1, sizeof(struct NSByteHolder));
00033   nsb->buf = calloc(1, STARTINGBUFSIZE);
00034   nsb->alloc = STARTINGBUFSIZE;
00035   nsb->used = 0;
00036   return nsb;
00037 }
00038 
00039 void addBlock(struct NSByteHolder *nsb, char *buf, int len)
00040 {
00041   int i;
00042   for (i = 0; i < len; i += 1)
00043     addCharacter(nsb, buf[i])
00044 }
00045 
00046 void addCharacter(struct NSByteHolder *nsb, char c)
00047 {
00048   ensureMoreSpace(nsb);
00049   nsb->buf[nsb->used] = c;
00050   nsb->used += 1;
00051 }
00052 
00053 int getSize(struct NSByteHolder *nsb)
00054 {
00055   return nsb->used;
00056 }
00057 
00058 const char *getData(struct NSByteHolder *nsb)
00059 {
00060   return nsb->buf;
00061 }
00062 
00063 void freeNSByteHolder(struct NSByteHolder *nsb)
00064 {
00065   nsb->alloc = 0;
00066   nsb->used = 0;
00067   free(nsb->buf);
00068   nsb->buf = NULL;
00069   free(nsb);
00070 }
00071 

Generated on Tue Feb 8 00:05:16 2005 for Neuroserver by doxygen 1.3.3