libisdn
|
00001 /* 00002 * 00003 * 00004 */ 00005 #ifdef HAVE_CONFIG_H 00006 #include "config.h" 00007 #endif 00008 00009 #include <stdlib.h> 00010 #include <string.h> 00011 00012 #include "msgb.h" 00013 #include "msgb_fifo.h" 00014 00015 /******************************************************************************* 00016 * 00017 *******************************************************************************/ 00018 00019 int msgb_fifo_init(struct msgb_fifo *fifo, const int size) 00020 { 00021 if (!fifo || size <= 1) 00022 return -1; 00023 00024 memset(fifo, 0, sizeof(*fifo)); 00025 dlist_init_head(&fifo->entries); 00026 fifo->size = size; 00027 return 0; 00028 } 00029 00030 struct msgb_fifo *msgb_fifo_alloc(const int size) 00031 { 00032 struct msgb_fifo *fifo = NULL; 00033 00034 if (size <= 1) 00035 return NULL; 00036 00037 fifo = malloc(sizeof(*fifo)); 00038 if (!fifo) 00039 return NULL; 00040 00041 if (msgb_fifo_init(fifo, size) < 0) { 00042 free(fifo); 00043 return NULL; 00044 } 00045 00046 return fifo; 00047 } 00048 00049 int msgb_fifo_destroy(struct msgb_fifo *fifo) 00050 { 00051 if (!fifo) 00052 return -1; 00053 free(fifo); 00054 return 0; 00055 } 00056 00057 /******************************************************************************* 00058 * 00059 *******************************************************************************/ 00060 00061 int msgb_fifo_size(const struct msgb_fifo *fifo) 00062 { 00063 return (fifo) ? fifo->size : -1; 00064 } 00065 00066 int msgb_fifo_length(const struct msgb_fifo *fifo) 00067 { 00068 return (fifo) ? fifo->nr_entries : -1; 00069 } 00070 00071 int msgb_fifo_empty(const struct msgb_fifo *fifo) 00072 { 00073 return !!(!fifo || !fifo->nr_entries); 00074 } 00075 00076 int msgb_fifo_full(const struct msgb_fifo *fifo) 00077 { 00078 return !!(!fifo || fifo->nr_entries == fifo->size); 00079 } 00080 00081 00082 /******************************************************************************* 00083 * 00084 *******************************************************************************/ 00085 00086 int msgb_fifo_enqueue(struct msgb_fifo *fifo, struct msgb *msg) 00087 { 00088 if (!fifo || !msg) 00089 return -1; 00090 if (fifo->nr_entries == fifo->size) 00091 return -1; 00092 00093 dlist_insert_tail(&fifo->entries, &msg->list); 00094 fifo->nr_entries++; 00095 return 0; 00096 } 00097 00098 struct msgb *msgb_fifo_dequeue(struct msgb_fifo *fifo) 00099 { 00100 struct dlist_head *entry = NULL; 00101 00102 if (!fifo || !fifo->nr_entries) 00103 return NULL; 00104 if ((entry = dlist_pop_head(&fifo->entries)) == NULL) 00105 return NULL; 00106 00107 fifo->nr_entries--; 00108 return dlist_entry(entry, struct msgb, list); 00109 }