util.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. element *new_elem = emalloc(sizeof(element));
  67. new_elem->data = data;
  68. new_elem->next = NULL;
  69. if(list->first == NULL){
  70. list->first = new_elem;
  71. list->last = new_elem;
  72. } else {
  73. list->last->next = new_elem;
  74. list->last = new_elem;
  75. }
  76. }
  77. /**
  78. * Removes and returns the first element from the front of the list. Returns NULL
  79. * if list is empty
  80. */
  81. void *dequeue(queue *list){
  82. if(list->first == NULL){
  83. return NULL;
  84. }
  85. void *data = list->first->data;
  86. element *target =list->first;
  87. list->first = target->next;
  88. free(target);
  89. return data;
  90. }
  91. /**
  92. * Returns the nth element of the queue (as provided)
  93. *
  94. * An input of -1 peeks at last element
  95. *
  96. * Returns data on success, NULL on failure
  97. */
  98. void *peek(queue *list, int32_t n){
  99. int32_t i;
  100. element *target = list->first;
  101. if(n == -1){
  102. target = list->last;
  103. }
  104. for(i=0; (i< n) && (target == NULL); i++){
  105. target = target->next;
  106. }
  107. if(target == NULL){
  108. return NULL;
  109. } else {
  110. return target->data;
  111. }
  112. }
  113. /**
  114. * Removes (frees the data in) all elements from the list and then frees the list itself
  115. */
  116. void remove_queue(queue *list){
  117. void *data = dequeue(list);
  118. while(data != NULL){
  119. free(data);
  120. data = dequeue(list);
  121. }
  122. free(list);
  123. }