00001 00002 /* 00003 * $Id: myfifo.c,v 1.3 2005/08/08 09:43:05 murrayma Exp $ 00004 * 00005 * Author: Markus Stenberg <fingon@iki.fi> 00006 * 00007 * Copyright (c) 1996 Markus Stenberg 00008 * All rights reserved 00009 * 00010 * Created: Sun Dec 1 11:46:18 1996 fingon 00011 * Last modified: Sun Dec 1 12:43:01 1996 fingon 00012 * 00013 */ 00014 00015 00016 #include <stdio.h> 00017 #include <stdlib.h> 00018 #include "myfifo.h" 00019 00020 /* A little shortcut to save me some typing */ 00021 #define PFOO (*foo) 00022 00023 void check_fifo(myfifo ** foo) 00024 { 00025 if (PFOO == NULL) { 00026 PFOO = (myfifo *) malloc(sizeof(myfifo)); 00027 PFOO->first = NULL; 00028 PFOO->last = NULL; 00029 PFOO->count = 0; 00030 } 00031 } 00032 00033 int myfifo_length(myfifo ** foo) 00034 { 00035 check_fifo(foo); 00036 return PFOO->count; 00037 } 00038 00039 void *myfifo_pop(myfifo ** foo) 00040 { 00041 void *tmpd; 00042 myfifo_e *tmp; 00043 00044 check_fifo(foo); 00045 tmp = PFOO->last; 00046 /* Is the list empty? */ 00047 if (tmp != NULL) { 00048 /* Are we removeing the only element? */ 00049 if (PFOO->first == PFOO->last) { 00050 PFOO->first = NULL; 00051 PFOO->last = NULL; 00052 } else 00053 tmp->prev->next = NULL; 00054 PFOO->last = tmp->prev; 00055 /* Are we going down to only one element? */ 00056 if (PFOO->last->prev == NULL) 00057 PFOO->first = PFOO->last; 00058 PFOO->count--; 00059 tmpd = tmp->data; 00060 free(tmp); 00061 return tmpd; 00062 } else 00063 return NULL; 00064 } 00065 00066 void myfifo_push(myfifo ** foo, void *data) 00067 { 00068 myfifo_e *tmp; 00069 00070 check_fifo(foo); 00071 tmp = (myfifo_e *) malloc(sizeof(myfifo_e)); 00072 tmp->data = data; 00073 tmp->next = PFOO->first; 00074 tmp->prev = NULL; 00075 PFOO->count++; 00076 if (PFOO->first == NULL) { 00077 PFOO->first = tmp; 00078 PFOO->last = tmp; 00079 } else 00080 PFOO->first->prev = tmp; 00081 PFOO->first = tmp; 00082 } 00083 00084 void myfifo_trav(myfifo ** foo, void (*func) ()) 00085 { 00086 myfifo_e *tmp; 00087 00088 check_fifo(foo); 00089 for (tmp = PFOO->first; tmp != NULL; tmp = tmp->next) 00090 func(tmp->data); 00091 } 00092 00093 void myfifo_trav_r(myfifo ** foo, void (*func) ()) 00094 { 00095 myfifo_e *tmp; 00096 00097 check_fifo(foo); 00098 for (tmp = PFOO->last; tmp != NULL; tmp = tmp->prev) 00099 func(tmp->data); 00100 }