2015-06-08 13:13:06 +00:00
|
|
|
/* radare - LGPL - Copyright 2007-2015 - ret2libc */
|
|
|
|
|
|
|
|
#include <r_util.h>
|
|
|
|
|
|
|
|
R_API RQueue *r_queue_new (int n) {
|
2017-02-08 22:01:42 +00:00
|
|
|
if (n <= 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-08 13:13:06 +00:00
|
|
|
RQueue *q = R_NEW0 (RQueue);
|
2017-02-08 22:01:42 +00:00
|
|
|
if (!q) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-12 10:00:18 +00:00
|
|
|
q->elems = R_NEWS0 (void *, n);
|
2015-06-16 21:00:15 +00:00
|
|
|
if (!q->elems){
|
|
|
|
free (q);
|
2015-06-08 13:13:06 +00:00
|
|
|
return NULL;
|
2015-06-16 21:00:15 +00:00
|
|
|
}
|
2015-06-08 13:13:06 +00:00
|
|
|
q->front = 0;
|
|
|
|
q->rear = -1;
|
|
|
|
q->size = 0;
|
|
|
|
q->capacity = n;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2017-02-08 22:01:42 +00:00
|
|
|
R_API void r_queue_free(RQueue *q) {
|
2015-06-08 13:13:06 +00:00
|
|
|
free (q->elems);
|
|
|
|
free (q);
|
|
|
|
}
|
|
|
|
|
2017-02-08 22:01:42 +00:00
|
|
|
static int is_full(RQueue *q) {
|
2015-06-08 13:13:06 +00:00
|
|
|
return q->size == q->capacity;
|
|
|
|
}
|
|
|
|
|
2017-02-08 22:01:42 +00:00
|
|
|
static int increase_capacity(RQueue *q) {
|
2015-06-08 13:13:06 +00:00
|
|
|
unsigned int new_capacity = q->capacity * 2;
|
|
|
|
void **newelems;
|
|
|
|
int i, tmp_front;
|
|
|
|
|
2015-06-12 10:00:18 +00:00
|
|
|
newelems = R_NEWS0(void *, new_capacity);
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!newelems) {
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2015-06-08 13:13:06 +00:00
|
|
|
|
|
|
|
i = -1;
|
|
|
|
tmp_front = q->front;
|
|
|
|
while (i + 1 < q->size) {
|
|
|
|
i++;
|
|
|
|
newelems[i] = q->elems[tmp_front];
|
|
|
|
tmp_front = (tmp_front + 1) % q->capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (q->elems);
|
|
|
|
q->elems = newelems;
|
|
|
|
q->front = 0;
|
|
|
|
q->rear = i;
|
|
|
|
q->capacity = new_capacity;
|
2016-07-12 20:15:19 +00:00
|
|
|
return true;
|
2015-06-08 13:13:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 22:01:42 +00:00
|
|
|
R_API int r_queue_enqueue(RQueue *q, void *el) {
|
2015-06-08 13:13:06 +00:00
|
|
|
if (is_full(q)) {
|
|
|
|
int res = increase_capacity (q);
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!res) {
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2015-06-08 13:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
q->rear = (q->rear + 1) % q->capacity;
|
|
|
|
q->elems[q->rear] = el;
|
|
|
|
q->size++;
|
2016-07-12 20:15:19 +00:00
|
|
|
return true;
|
2015-06-08 13:13:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 22:01:42 +00:00
|
|
|
R_API void *r_queue_dequeue(RQueue *q) {
|
2015-06-08 13:13:06 +00:00
|
|
|
void *res;
|
|
|
|
|
2017-02-08 22:01:42 +00:00
|
|
|
if (r_queue_is_empty (q)) {
|
2015-06-08 13:13:06 +00:00
|
|
|
return NULL;
|
2017-02-08 22:01:42 +00:00
|
|
|
}
|
2015-06-08 13:13:06 +00:00
|
|
|
res = q->elems[q->front];
|
|
|
|
q->front = (q->front + 1) % q->capacity;
|
|
|
|
q->size--;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-02-08 22:01:42 +00:00
|
|
|
R_API int r_queue_is_empty(RQueue *q) {
|
2015-06-08 13:13:06 +00:00
|
|
|
return q->size == 0;
|
|
|
|
}
|