00001
00002
00003
00004
00005
00006
00007
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include "kb_fifo.h"
00011
00026
00032 Fifo kb_fifo_fifoEmpty() {
00033 Fifo F;
00034
00035 F = (Fifo)malloc(sizeof(struct _fifo));
00036 if (F == NULL) perror("Problem fifo is empty \n\r");
00037 F->length = 0;
00038 F->mutex = 0;
00039 F->head = F->queue = NULL;
00040 return(F);
00041 }
00042
00043
00050 int kb_fifo_length(Fifo F) {
00051 if (F == NULL) perror("Problem fifo length, F is null \n\r");
00052 return(F->length);
00053 }
00054
00055
00062 void kb_fifo_enqueue(Fifo F, int elt) {
00063 kb_cel cel;
00064
00065 if (F == NULL) perror("enqueue problem, F is Null \n\r");
00066 cel = (kb_cel)malloc(sizeof(struct _cel));
00067 if (cel == NULL) perror("enqueue problem, cel is Null");
00068 cel->elt = elt;
00069 cel->next = NULL;
00070 while((F->mutex) != 0);
00071 (F->mutex) = 1;
00072 if (kb_fifo_length(F) == 0)
00073 F->head = F->queue = cel;
00074 else {
00075 F->queue->next = cel;
00076 F->queue = cel;
00077 }
00078 ++(F->length);
00079 (F->mutex) = 0;
00080 }
00081
00082
00089 int kb_fifo_head(Fifo F) {
00090 if (F == NULL || kb_fifo_length(F) == 0) perror("head problem, F is Null or size = 0 \n\r");
00091 return(F->head->elt);
00092 }
00093
00094
00100 void kb_fifo_remove(Fifo F) {
00101 kb_cel cel;
00102
00103 if (F == NULL || kb_fifo_length(F) == 0) perror("dequeue problem, F is null or size = 0 \n\r");
00104
00105 while((F->mutex) != 0);
00106 (F->mutex) = 1;
00107 cel = F->head;
00108 if (kb_fifo_length(F) == 1)
00109 F->head = F->queue = NULL;
00110 else
00111 F->head = F->head->next;
00112 --(F->length);
00113 (F->mutex) = 0;
00114 free(cel);
00115 }
00116
00117
00124 int kb_fifo_dequeue(Fifo F) {
00125 kb_cel cel;
00126 int elt;
00127
00128 if (F == NULL || kb_fifo_length(F) == 0) perror("run problem, F is null or size = 0 \n\r");
00129
00130 while((F->mutex) != 0);
00131 (F->mutex) = 1;
00132 cel = F->head;
00133 elt = cel->elt;
00134 if (kb_fifo_length(F) == 1)
00135 F->head = F->queue = NULL;
00136 else
00137 F->head = F->head->next;
00138 free(cel);
00139 --(F->length);
00140 (F->mutex) = 0;
00141 return(elt);
00142 }
00143
00144
00151 Fifo kb_fifo_copy(Fifo F) {
00152 Fifo G, H;
00153 kb_cel cel;
00154
00155 if (F == NULL) perror("copy problem, F is null \n\r");
00156 H = kb_fifo_fifoEmpty();
00157 cel = F->head;
00158 while (cel != NULL) {
00159 kb_fifo_enqueue(H, cel->elt);
00160 cel = cel->next;
00161 }
00162 G = kb_fifo_fifoEmpty();
00163 cel = H->head;
00164 while (cel != NULL) {
00165 kb_fifo_enqueue(G, cel->elt);
00166 cel = cel->next;
00167 }
00168 return(G);
00169 }
00170
00171
00177 void kb_fifo_printFifo(Fifo F) {
00178 kb_cel cel;
00179
00180 if (F == NULL) perror("print problem, F is null \n\r");
00181 cel = F->head;
00182 while (cel != NULL) {
00183 printf("%d ", cel->elt);
00184 cel = cel->next;
00185 }
00186 printf("\n\r");
00187 }