1 /* radare - LGPL - Copyright 2007-2015 - ret2libc */ 2 3 #include <r_util.h> 4 r_queue_new(int n)5R_API RQueue *r_queue_new (int n) { 6 if (n <= 0) { 7 return NULL; 8 } 9 RQueue *q = R_NEW0 (RQueue); 10 if (!q) { 11 return NULL; 12 } 13 q->elems = R_NEWS0 (void *, n); 14 if (!q->elems){ 15 free (q); 16 return NULL; 17 } 18 q->front = 0; 19 q->rear = -1; 20 q->size = 0; 21 q->capacity = n; 22 return q; 23 } 24 r_queue_free(RQueue * q)25R_API void r_queue_free(RQueue *q) { 26 free (q->elems); 27 free (q); 28 } 29 is_full(RQueue * q)30static int is_full(RQueue *q) { 31 return q->size == q->capacity; 32 } 33 increase_capacity(RQueue * q)34static int increase_capacity(RQueue *q) { 35 unsigned int new_capacity = q->capacity * 2; 36 void **newelems; 37 int i, tmp_front; 38 39 newelems = R_NEWS0(void *, new_capacity); 40 if (!newelems) { 41 return false; 42 } 43 44 i = -1; 45 tmp_front = q->front; 46 while (i + 1 < q->size) { 47 i++; 48 newelems[i] = q->elems[tmp_front]; 49 tmp_front = (tmp_front + 1) % q->capacity; 50 } 51 52 free (q->elems); 53 q->elems = newelems; 54 q->front = 0; 55 q->rear = i; 56 q->capacity = new_capacity; 57 return true; 58 } 59 r_queue_enqueue(RQueue * q,void * el)60R_API int r_queue_enqueue(RQueue *q, void *el) { 61 if (is_full(q)) { 62 int res = increase_capacity (q); 63 if (!res) { 64 return false; 65 } 66 } 67 68 q->rear = (q->rear + 1) % q->capacity; 69 q->elems[q->rear] = el; 70 q->size++; 71 return true; 72 } 73 r_queue_dequeue(RQueue * q)74R_API void *r_queue_dequeue(RQueue *q) { 75 void *res; 76 77 if (r_queue_is_empty (q)) { 78 return NULL; 79 } 80 res = q->elems[q->front]; 81 q->front = (q->front + 1) % q->capacity; 82 q->size--; 83 return res; 84 } 85 r_queue_is_empty(RQueue * q)86R_API int r_queue_is_empty(RQueue *q) { 87 return q->size == 0; 88 } 89