util.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Name: util.c
  2. *
  3. * This file contains safe wrappers for common functions and implementations of
  4. * data structures
  5. *
  6. * Slitheen - a decoy routing system for censorship resistance
  7. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, version 3.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Additional permission under GNU GPL version 3 section 7
  22. *
  23. * If you modify this Program, or any covered work, by linking or combining
  24. * it with the OpenSSL library (or a modified version of that library),
  25. * containing parts covered by the terms of the OpenSSL Licence and the
  26. * SSLeay license, the licensors of this Program grant you additional
  27. * permission to convey the resulting work. Corresponding Source for a
  28. * non-source form of such a combination shall include the source code
  29. * for the parts of the OpenSSL library used as well as that of the covered
  30. * work.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "util.h"
  35. //malloc macro that exits on error
  36. void *emalloc(size_t size){
  37. void *ptr = malloc(size);
  38. if (ptr == NULL){
  39. fprintf(stderr, "Memory failure. Exiting...\n");
  40. exit(1);
  41. }
  42. return ptr;
  43. }
  44. //calloc macro that exits on error
  45. void *ecalloc(size_t nmemb, size_t size){
  46. void *ptr = calloc(nmemb, size);
  47. if(ptr == NULL){
  48. fprintf(stderr, "Memory failure. Exiting...\n");
  49. exit(1);
  50. }
  51. return ptr;
  52. }
  53. /**
  54. * Initializes a generic queue structure
  55. */
  56. queue *init_queue(){
  57. queue *new_queue = emalloc(sizeof(queue));
  58. new_queue->first = NULL;
  59. new_queue->last = NULL;
  60. return new_queue;
  61. }
  62. /**
  63. * Function to append a struct to the end of a list
  64. */
  65. void enqueue(queue *list, void *data){
  66. //Do not allow appending NULL data
  67. if(data == NULL){
  68. return;
  69. }
  70. element *new_elem = emalloc(sizeof(element));
  71. new_elem->data = data;
  72. new_elem->next = NULL;
  73. if(list->first == NULL){
  74. list->first = new_elem;
  75. list->last = new_elem;
  76. } else {
  77. list->last->next = new_elem;
  78. list->last = new_elem;
  79. }
  80. }
  81. /**
  82. * Removes and returns the first element from the front of the list. Returns NULL
  83. * if list is empty
  84. */
  85. void *dequeue(queue *list){
  86. if(list->first == NULL){
  87. return NULL;
  88. }
  89. void *data = list->first->data;
  90. element *target =list->first;
  91. list->first = target->next;
  92. free(target);
  93. return data;
  94. }
  95. /**
  96. * Returns the nth element of the queue (as provided)
  97. *
  98. * An input of -1 peeks at last element
  99. *
  100. * Returns data on success, NULL on failure
  101. */
  102. void *peek(queue *list, int32_t n){
  103. int32_t i;
  104. element *target = list->first;
  105. if(n == -1){
  106. target = list->last;
  107. }
  108. for(i=0; (i< n) && (target == NULL); i++){
  109. target = target->next;
  110. }
  111. if(target == NULL){
  112. return NULL;
  113. } else {
  114. return target->data;
  115. }
  116. }
  117. /**
  118. * Removes (frees the data in) all elements from the list and then frees the list itself
  119. */
  120. void remove_queue(queue *list){
  121. void *data = dequeue(list);
  122. while(data != NULL){
  123. free(data);
  124. data = dequeue(list);
  125. }
  126. free(list);
  127. }