queue.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #include "dbutil.h"
  26. #include "queue.h"
  27. void initqueue(struct Queue* queue) {
  28. queue->head = NULL;
  29. queue->tail = NULL;
  30. queue->count = 0;
  31. }
  32. int isempty(struct Queue* queue) {
  33. return (queue->head == NULL);
  34. }
  35. void* dequeue(struct Queue* queue) {
  36. void* ret;
  37. struct Link* oldhead;
  38. dropbear_assert(!isempty(queue));
  39. ret = queue->head->item;
  40. oldhead = queue->head;
  41. if (oldhead->link != NULL) {
  42. queue->head = oldhead->link;
  43. } else {
  44. queue->head = NULL;
  45. queue->tail = NULL;
  46. TRACE(("empty queue dequeing"))
  47. }
  48. m_free(oldhead);
  49. queue->count--;
  50. return ret;
  51. }
  52. void *examine(struct Queue* queue) {
  53. dropbear_assert(!isempty(queue));
  54. return queue->head->item;
  55. }
  56. void enqueue(struct Queue* queue, void* item) {
  57. struct Link* newlink;
  58. newlink = (struct Link*)m_malloc(sizeof(struct Link));
  59. newlink->item = item;
  60. newlink->link = NULL;
  61. if (queue->tail != NULL) {
  62. queue->tail->link = newlink;
  63. }
  64. queue->tail = newlink;
  65. if (queue->head == NULL) {
  66. queue->head = newlink;
  67. }
  68. queue->count++;
  69. }