xrdp/common/fifo.c

174 lines
3.5 KiB
C
Raw Normal View History

2014-03-13 06:43:14 +04:00
/**
* 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
*/
#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif
2014-03-13 06:43:14 +04:00
#include "fifo.h"
#include "os_calls.h"
2014-03-13 06:43:14 +04:00
/**
* Create new fifo data struct
2014-08-27 22:15:31 +04:00
*
2014-03-13 06:43:14 +04:00
* @return pointer to new FIFO or NULL if system out of memory
*****************************************************************************/
2014-08-27 22:15:31 +04:00
2014-07-26 08:37:59 +04:00
FIFO * APP_CC
fifo_create(void)
2014-03-13 06:43:14 +04:00
{
return (FIFO *) g_malloc(sizeof(FIFO), 1);
}
/**
* Delete specified FIFO
*****************************************************************************/
2014-08-27 22:15:31 +04:00
2014-07-26 08:37:59 +04:00
void APP_CC
fifo_delete(FIFO *self)
2014-03-13 06:43:14 +04:00
{
USER_DATA *udp;
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
if (!self)
return;
if (!self->head)
{
/* FIFO is empty */
g_free(self);
return;
}
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
if (self->head == self->tail)
{
/* only one item in FIFO */
if (self->auto_free)
g_free(self->head->item);
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
g_free(self->head);
g_free(self);
return;
}
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
/* more then one item in FIFO */
while (self->head)
{
udp = self->head;
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
if (self->auto_free)
g_free(udp->item);
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
self->head = udp->next;
g_free(udp);
}
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
g_free(self);
}
/**
* Add an item to the specified FIFO
2014-08-27 22:15:31 +04:00
*
2014-03-13 06:43:14 +04:00
* @param self FIFO to operate on
* @param item item to add to specified FIFO
2014-08-27 22:15:31 +04:00
*
2014-03-13 06:43:14 +04:00
* @return 0 on success, -1 on error
2014-08-27 22:15:31 +04:00
*****************************************************************************/
2014-07-26 08:37:59 +04:00
int APP_CC
fifo_add_item(FIFO *self, void *item)
2014-03-13 06:43:14 +04:00
{
USER_DATA *udp;
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
if (!self || !item)
return -1;
if ((udp = (USER_DATA *) g_malloc(sizeof(USER_DATA), 0)) == 0)
return -1;
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
udp->item = item;
udp->next = 0;
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
/* if fifo is empty, add to head */
if (!self->head)
{
self->head = udp;
self->tail = udp;
return 0;
}
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
/* add to tail */
self->tail->next = udp;
self->tail = udp;
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
return 0;
}
/**
* Return an item from top of FIFO
2014-08-27 22:15:31 +04:00
*
2014-03-13 06:43:14 +04:00
* @param self FIFO to operate on
2014-08-27 22:15:31 +04:00
*
2014-03-13 06:43:14 +04:00
* @return top item from FIFO or NULL if FIFO is empty
2014-08-27 22:15:31 +04:00
*****************************************************************************/
2014-07-26 08:37:59 +04:00
void * APP_CC
fifo_remove_item(FIFO *self)
2014-03-13 06:43:14 +04:00
{
void *item;
USER_DATA *udp;
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
if (!self || !self->head)
return 0;
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
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;
}
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
/* 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
2014-08-27 22:15:31 +04:00
*
2014-03-13 06:43:14 +04:00
* @param self FIFO to operate on
2014-08-27 22:15:31 +04:00
*
2014-03-13 06:43:14 +04:00
* @return true if FIFO is empty, false otherwise
2014-08-27 22:15:31 +04:00
*****************************************************************************/
2014-07-26 08:37:59 +04:00
int APP_CC
fifo_is_empty(FIFO *self)
2014-03-13 06:43:14 +04:00
{
if (!self)
return 1;
2014-08-27 22:15:31 +04:00
2014-03-13 06:43:14 +04:00
return (self->head == 0);
}