Difference between revisions of "Util.c"

From Organic Design wiki
(merge free-list ops into insert/remove-pointer functions since private)
m (bugs)
Line 71: Line 71:
 
fitem *flist = NULL;
 
fitem *flist = NULL;
  
 +
// add a new pointer to the list, try using slots from the free-list before extending array
 
int insertPointer(void *ptr) {
 
int insertPointer(void *ptr) {
 
int index;
 
int index;
if (index = freePop()) return index;
+
if (flist) {
 
+
void *kill = flist;
if (flist == NULL) return NULL;
+
flist = kill->next;
void *kill = flist;
+
index = kill->data;
flist = kill->next;
+
free(kill);
int data = kill->data;
+
}
free(kill);
+
else if ((index = ++pitem) > psize) realloc(plist,psize += 100);
return data;
+
plist[index] = ptr;
 
+
return index;
if (++pitem > psize) realloc(plist,psize += 100);
 
return pitem;
 
 
}
 
}
  
 
// push the index onto the free-list, returning the pointer at that index
 
// push the index onto the free-list, returning the pointer at that index
 
void *removePointer(int index) {
 
void *removePointer(int index) {
fitem *newitem = (fitem*)malloc(sizeof(fitem));
+
fitem *newfree = (fitem*)malloc(sizeof(fitem));
newitem->next = flist ? flist : NULL;  
+
newfree->next = flist ? flist : NULL;  
flist = newitem;
+
flist = newfree;
newitem->data = index;
+
return plist[newfree->data = index];
return plist[ptr];
 
 
}
 
}

Revision as of 05:56, 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;

// add a new pointer to the list, try using slots from the free-list before extending array int insertPointer(void *ptr) { int index; if (flist) { void *kill = flist; flist = kill->next; index = kill->data; free(kill); } else if ((index = ++pitem) > psize) realloc(plist,psize += 100); plist[index] = ptr; return index; }

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