You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fifo.c 3.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * xrdp: A Remote Desktop Protocol server.
  3. *
  4. * Copyright (C) Laxmikant Rashinkar 2004-2014
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * FIFO implementation to store pointer to data struct
  19. */
  20. #if defined(HAVE_CONFIG_H)
  21. #include <config_ac.h>
  22. #endif
  23. #include "fifo.h"
  24. #include "os_calls.h"
  25. /**
  26. * Create new fifo data struct
  27. *
  28. * @return pointer to new FIFO or NULL if system out of memory
  29. *****************************************************************************/
  30. FIFO *
  31. fifo_create(void)
  32. {
  33. return (FIFO *) g_malloc(sizeof(FIFO), 1);
  34. }
  35. /**
  36. * Delete specified FIFO
  37. *****************************************************************************/
  38. void
  39. fifo_delete(FIFO *self)
  40. {
  41. USER_DATA *udp;
  42. if (!self)
  43. return;
  44. if (!self->head)
  45. {
  46. /* FIFO is empty */
  47. g_free(self);
  48. return;
  49. }
  50. if (self->head == self->tail)
  51. {
  52. /* only one item in FIFO */
  53. if (self->auto_free)
  54. g_free(self->head->item);
  55. g_free(self->head);
  56. g_free(self);
  57. return;
  58. }
  59. /* more then one item in FIFO */
  60. while (self->head)
  61. {
  62. udp = self->head;
  63. if (self->auto_free)
  64. g_free(udp->item);
  65. self->head = udp->next;
  66. g_free(udp);
  67. }
  68. g_free(self);
  69. }
  70. /**
  71. * Add an item to the specified FIFO
  72. *
  73. * @param self FIFO to operate on
  74. * @param item item to add to specified FIFO
  75. *
  76. * @return 0 on success, -1 on error
  77. *****************************************************************************/
  78. int
  79. fifo_add_item(FIFO *self, void *item)
  80. {
  81. USER_DATA *udp;
  82. if (!self || !item)
  83. return -1;
  84. if ((udp = (USER_DATA *) g_malloc(sizeof(USER_DATA), 0)) == 0)
  85. return -1;
  86. udp->item = item;
  87. udp->next = 0;
  88. /* if fifo is empty, add to head */
  89. if (!self->head)
  90. {
  91. self->head = udp;
  92. self->tail = udp;
  93. return 0;
  94. }
  95. /* add to tail */
  96. self->tail->next = udp;
  97. self->tail = udp;
  98. return 0;
  99. }
  100. /**
  101. * Return an item from top of FIFO
  102. *
  103. * @param self FIFO to operate on
  104. *
  105. * @return top item from FIFO or NULL if FIFO is empty
  106. *****************************************************************************/
  107. void *
  108. fifo_remove_item(FIFO *self)
  109. {
  110. void *item;
  111. USER_DATA *udp;
  112. if (!self || !self->head)
  113. return 0;
  114. if (self->head == self->tail)
  115. {
  116. /* only one item in FIFO */
  117. item = self->head->item;
  118. g_free(self->head);
  119. self->head = 0;
  120. self->tail = 0;
  121. return item;
  122. }
  123. /* more then one item in FIFO */
  124. udp = self->head;
  125. item = self->head->item;
  126. self->head = self->head->next;
  127. g_free(udp);
  128. return item;
  129. }
  130. /**
  131. * Return FIFO status
  132. *
  133. * @param self FIFO to operate on
  134. *
  135. * @return true if FIFO is empty, false otherwise
  136. *****************************************************************************/
  137. int
  138. fifo_is_empty(FIFO *self)
  139. {
  140. if (!self)
  141. return 1;
  142. return (self->head == 0);
  143. }