Difference between revisions of "Util.c"

From Organic Design wiki
(if any spare indexes in free-list, use that and don't ++pitem)
(merge free-list ops into insert/remove-pointer functions since private)
Line 71: Line 71:
 
fitem *flist = NULL;
 
fitem *flist = NULL;
  
int freePush(int data) {
+
int insertPointer(void *ptr) {
fitem *newitem = (fitem*)malloc(sizeof(fitem));
+
int index;
newitem->next = flist ? flist : NULL;
+
if (index = freePop()) return index;
flist = newitem;
 
return newitem->data = data;
 
}
 
  
int freePop() {
 
 
if (flist == NULL) return NULL;
 
if (flist == NULL) return NULL;
 
void *kill = flist;
 
void *kill = flist;
Line 85: Line 81:
 
free(kill);
 
free(kill);
 
return data;
 
return data;
}
 
  
int insertPointer(void *ptr) {
 
int index;
 
if (index = freePop()) return index;
 
 
if (++pitem > psize) realloc(plist,psize += 100);
 
if (++pitem > psize) realloc(plist,psize += 100);
 
return pitem;
 
return pitem;
 
}
 
}
  
 +
// push the index onto the free-list, returning the pointer at that index
 
void *removePointer(int index) {
 
void *removePointer(int index) {
void *ptr;
+
fitem *newitem = (fitem*)malloc(sizeof(fitem));
freePush(index);
+
newitem->next = flist ? flist : NULL;
return ptr;
+
flist = newitem;
 +
newitem->data = index;
 +
return plist[ptr];
 
}
 
}

Revision as of 05:40, 27 July 2006

int fileRead(char* filename) { }

int fileWrite(char* filename, char* content) { }

char *logAdd(char *msg) { // prepend with a timestamp // append to logfile printf(msg); printf("\n"); return msg; }

// Enter a blank line in log as a marker of new peerd session logAdd("");

int logErr(char *msg) { logAdd(msg); // should prepend "ERROR:" return(errno = EXIT_FAILURE); }

// Same as logErr but allows error message to contain an arg int logErrMsg(char *fmt, char *msg2) { char *msg = malloc(100); sprintf(msg,fmt,msg2); logErr(msg); free(msg); return(errno = EXIT_FAILURE); }

// Same as logErr but allows error message to contain an arg int logErrNum(char *fmt, int num) { char *msg = malloc(100); sprintf(msg,fmt,num); logErr(msg); free(msg); return(errno = EXIT_FAILURE); }

// Return an array of strings resulting from splitting passed text at passed character // - the resulting strings are formed from the passed string char **split(char c,char *text) { int len = strlen(text), items = 0, size = 10; char **list = malloc(size); char *i = malloc(len+1), *j = i, *k = i, *item = i; while(*j++ = *text++); while(i <= k+len) { if (*i == c) *i = '\0'; if ((*i++ == '\0')&&strlen(item)) { if (items>size-2) realloc(list,size+=10); list[items++] = item; list[items] = NULL; item = i; } } return list; }

// ----------------------------------------------------------------------------------------- // // Global Pointer List // - use insertPointer() and removePointer() to access the dynamically allocating pointer-array // - internally uses freePush() and freePop() to maintain a linked-list of free array slots

int psize = 100, pitem = 0; void *plist = malloc(psize*sizeof(void*)); typedef struct { void *data; struct fitem *next; } fitem; fitem *flist = NULL;

int insertPointer(void *ptr) { int index; if (index = freePop()) return index;

if (flist == NULL) return NULL; void *kill = flist; flist = kill->next; int data = kill->data; free(kill); return data;

if (++pitem > psize) realloc(plist,psize += 100); return pitem; }

// push the index onto the free-list, returning the pointer at that index void *removePointer(int index) { fitem *newitem = (fitem*)malloc(sizeof(fitem)); newitem->next = flist ? flist : NULL; flist = newitem; newitem->data = index; return plist[ptr]; }