util.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 [name of library's license],
  26. * the licensors of this Program grant you additional permission to convey
  27. * the resulting work. {Corresponding Source for a non-source form of such
  28. * a combination shall include the source code for the parts of the OpenSSL
  29. * library used as well as that of the covered work.}
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "util.h"
  34. //malloc macro that exits on error
  35. void *emalloc(size_t size){
  36. void *ptr = malloc(size);
  37. if (ptr == NULL){
  38. fprintf(stderr, "Memory failure. Exiting...\n");
  39. exit(1);
  40. }
  41. return ptr;
  42. }
  43. //calloc macro that exits on error
  44. void *ecalloc(size_t nmemb, size_t size){
  45. void *ptr = calloc(nmemb, size);
  46. if(ptr == NULL){
  47. fprintf(stderr, "Memory failure. Exiting...\n");
  48. exit(1);
  49. }
  50. return ptr;
  51. }
  52. /**
  53. * Initializes a generic queue structure
  54. */
  55. queue *init_queue(){
  56. queue *new_queue = emalloc(sizeof(queue));
  57. new_queue->first = NULL;
  58. new_queue->last = NULL;
  59. return new_queue;
  60. }
  61. /**
  62. * Function to append a struct to the end of a list
  63. */
  64. void enqueue(queue *list, void *data){
  65. element *new_elem = emalloc(sizeof(element));
  66. new_elem->data = data;
  67. new_elem->next = NULL;
  68. if(list->first == NULL){
  69. list->first = new_elem;
  70. list->last = new_elem;
  71. } else {
  72. list->last->next = new_elem;
  73. list->last = new_elem;
  74. }
  75. }
  76. /**
  77. * Removes and returns the first element from the front of the list. Returns NULL
  78. * if list is empty
  79. */
  80. void *dequeue(queue *list){
  81. if(list->first == NULL){
  82. return NULL;
  83. }
  84. void *data = list->first->data;
  85. element *target =list->first;
  86. list->first = target->next;
  87. free(target);
  88. return data;
  89. }
  90. /**
  91. * Returns the nth element of the queue (as provided)
  92. *
  93. * An input of -1 peeks at last element
  94. *
  95. * Returns data on success, NULL on failure
  96. */
  97. void *peek(queue *list, int32_t n){
  98. int32_t i;
  99. element *target = list->first;
  100. if(n == -1){
  101. target = list->last;
  102. }
  103. for(i=0; (i< n) && (target == NULL); i++){
  104. target = target->next;
  105. }
  106. if(target == NULL){
  107. return NULL;
  108. } else {
  109. return target->data;
  110. }
  111. }
  112. /**
  113. * Removes (frees the data in) all elements from the list and then frees the list itself
  114. */
  115. void remove_queue(queue *list){
  116. void *data = dequeue(list);
  117. while(data != NULL){
  118. free(data);
  119. data = dequeue(list);
  120. }
  121. free(list);
  122. }