From 963b95877a8e8c515d6ae775d1f1aeab2775a863 Mon Sep 17 00:00:00 2001 From: Laxmikant Rashinkar Date: Wed, 12 Mar 2014 19:43:14 -0700 Subject: added a FIFO implementation --- common/fifo.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 common/fifo.c (limited to 'common/fifo.c') diff --git a/common/fifo.c b/common/fifo.c new file mode 100644 index 00000000..5e94694d --- /dev/null +++ b/common/fifo.c @@ -0,0 +1,164 @@ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Laxmikant Rashinkar 2004-2014 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * FIFO implementation to store pointer to data struct + */ + +#include "fifo.h" +#include "common/os_calls.h" + +/** + * Create new fifo data struct + * + * @return pointer to new FIFO or NULL if system out of memory + *****************************************************************************/ + +FIFO *fifo_create() +{ + return (FIFO *) g_malloc(sizeof(FIFO), 1); +} + +/** + * Delete specified FIFO + *****************************************************************************/ + +void fifo_delete(FIFO *self) +{ + USER_DATA *udp; + + if (!self) + return; + + if (!self->head) + { + /* FIFO is empty */ + g_free(self); + return; + } + + if (self->head == self->tail) + { + /* only one item in FIFO */ + if (self->auto_free) + g_free(self->head->item); + + g_free(self->head); + g_free(self); + return; + } + + /* more then one item in FIFO */ + while (self->head) + { + udp = self->head; + + if (self->auto_free) + g_free(udp->item); + + self->head = udp->next; + g_free(udp); + } + + g_free(self); +} + +/** + * Add an item to the specified FIFO + * + * @param self FIFO to operate on + * @param item item to add to specified FIFO + * + * @return 0 on success, -1 on error + *****************************************************************************/ + +int fifo_add_item(FIFO *self, void *item) +{ + USER_DATA *udp; + + if (!self || !item) + return -1; + + if ((udp = (USER_DATA *) g_malloc(sizeof(USER_DATA), 0)) == 0) + return -1; + + udp->item = item; + udp->next = 0; + + /* if fifo is empty, add to head */ + if (!self->head) + { + self->head = udp; + self->tail = udp; + return 0; + } + + /* add to tail */ + self->tail->next = udp; + self->tail = udp; + + return 0; +} + +/** + * Return an item from top of FIFO + * + * @param self FIFO to operate on + * + * @return top item from FIFO or NULL if FIFO is empty + *****************************************************************************/ + +void *fifo_remove_item(FIFO *self) +{ + void *item; + USER_DATA *udp; + + if (!self || !self->head) + return 0; + + if (self->head == self->tail) + { + /* only one item in FIFO */ + item = self->head->item; + g_free(self->head); + self->head = 0; + self->tail = 0; + return item; + } + + /* more then one item in FIFO */ + udp = self->head; + item = self->head->item; + self->head = self->head->next; + g_free(udp); + return item; +} + +/** + * Return FIFO status + * + * @param self FIFO to operate on + * + * @return true if FIFO is empty, false otherwise + *****************************************************************************/ + +int fifo_is_empty(FIFO *self) +{ + if (!self) + return 1; + + return (self->head == 0); +} -- cgit v1.2.1 From b39c68bdc405c67699768c7159e2b98eca0244f0 Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Thu, 24 Jul 2014 22:31:47 -0700 Subject: common: fifo.c, os_call.h, don't need prefix dir --- common/fifo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common/fifo.c') diff --git a/common/fifo.c b/common/fifo.c index 5e94694d..165acf8e 100644 --- a/common/fifo.c +++ b/common/fifo.c @@ -19,7 +19,7 @@ */ #include "fifo.h" -#include "common/os_calls.h" +#include "os_calls.h" /** * Create new fifo data struct -- cgit v1.2.1 From 9e1e6d3c3eebb5f82fa6fb6207255eb99ab89ecc Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Fri, 25 Jul 2014 21:37:59 -0700 Subject: common: indent, not logic change --- common/fifo.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'common/fifo.c') diff --git a/common/fifo.c b/common/fifo.c index 165acf8e..ebd1ef4d 100644 --- a/common/fifo.c +++ b/common/fifo.c @@ -27,7 +27,8 @@ * @return pointer to new FIFO or NULL if system out of memory *****************************************************************************/ -FIFO *fifo_create() +FIFO * APP_CC +fifo_create(void) { return (FIFO *) g_malloc(sizeof(FIFO), 1); } @@ -36,7 +37,8 @@ FIFO *fifo_create() * Delete specified FIFO *****************************************************************************/ -void fifo_delete(FIFO *self) +void APP_CC +fifo_delete(FIFO *self) { USER_DATA *udp; @@ -85,7 +87,8 @@ void fifo_delete(FIFO *self) * @return 0 on success, -1 on error *****************************************************************************/ -int fifo_add_item(FIFO *self, void *item) +int APP_CC +fifo_add_item(FIFO *self, void *item) { USER_DATA *udp; @@ -121,7 +124,8 @@ int fifo_add_item(FIFO *self, void *item) * @return top item from FIFO or NULL if FIFO is empty *****************************************************************************/ -void *fifo_remove_item(FIFO *self) +void * APP_CC +fifo_remove_item(FIFO *self) { void *item; USER_DATA *udp; @@ -155,7 +159,8 @@ void *fifo_remove_item(FIFO *self) * @return true if FIFO is empty, false otherwise *****************************************************************************/ -int fifo_is_empty(FIFO *self) +int APP_CC +fifo_is_empty(FIFO *self) { if (!self) return 1; -- cgit v1.2.1 From c0ce0821ecaac93bc669778115f214fa6f4ea7d3 Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Wed, 27 Aug 2014 11:15:31 -0700 Subject: common: no logic change, remove spaces --- common/fifo.c | 62 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'common/fifo.c') diff --git a/common/fifo.c b/common/fifo.c index ebd1ef4d..e3f6f3bc 100644 --- a/common/fifo.c +++ b/common/fifo.c @@ -23,10 +23,10 @@ /** * Create new fifo data struct - * + * * @return pointer to new FIFO or NULL if system out of memory *****************************************************************************/ - + FIFO * APP_CC fifo_create(void) { @@ -36,12 +36,12 @@ fifo_create(void) /** * Delete specified FIFO *****************************************************************************/ - + void APP_CC fifo_delete(FIFO *self) { USER_DATA *udp; - + if (!self) return; @@ -51,56 +51,56 @@ fifo_delete(FIFO *self) g_free(self); return; } - + if (self->head == self->tail) { /* only one item in FIFO */ if (self->auto_free) g_free(self->head->item); - + g_free(self->head); g_free(self); return; } - + /* more then one item in FIFO */ while (self->head) { udp = self->head; - + if (self->auto_free) g_free(udp->item); - + self->head = udp->next; g_free(udp); } - + g_free(self); } /** * Add an item to the specified FIFO - * + * * @param self FIFO to operate on * @param item item to add to specified FIFO - * + * * @return 0 on success, -1 on error - *****************************************************************************/ - + *****************************************************************************/ + int APP_CC fifo_add_item(FIFO *self, void *item) { USER_DATA *udp; - + if (!self || !item) return -1; if ((udp = (USER_DATA *) g_malloc(sizeof(USER_DATA), 0)) == 0) return -1; - + udp->item = item; udp->next = 0; - + /* if fifo is empty, add to head */ if (!self->head) { @@ -108,31 +108,31 @@ fifo_add_item(FIFO *self, void *item) self->tail = udp; return 0; } - + /* add to tail */ self->tail->next = udp; self->tail = udp; - + return 0; } /** * Return an item from top of FIFO - * + * * @param self FIFO to operate on - * + * * @return top item from FIFO or NULL if FIFO is empty - *****************************************************************************/ - + *****************************************************************************/ + void * APP_CC fifo_remove_item(FIFO *self) { void *item; USER_DATA *udp; - + if (!self || !self->head) return 0; - + if (self->head == self->tail) { /* only one item in FIFO */ @@ -142,7 +142,7 @@ fifo_remove_item(FIFO *self) self->tail = 0; return item; } - + /* more then one item in FIFO */ udp = self->head; item = self->head->item; @@ -153,17 +153,17 @@ fifo_remove_item(FIFO *self) /** * Return FIFO status - * + * * @param self FIFO to operate on - * + * * @return true if FIFO is empty, false otherwise - *****************************************************************************/ - + *****************************************************************************/ + int APP_CC fifo_is_empty(FIFO *self) { if (!self) return 1; - + return (self->head == 0); } -- cgit v1.2.1