src/queue.c: use xalloc.
This commit is contained in:
parent
2e2d6c7eee
commit
463d432104
1 changed files with 12 additions and 17 deletions
29
src/queue.c
29
src/queue.c
|
|
@ -11,9 +11,8 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <malloc.h>
|
#include "xalloc.h"
|
||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
|
|
||||||
#define BLOCK_SIZE (1<<7)
|
#define BLOCK_SIZE (1<<7)
|
||||||
|
|
@ -42,8 +41,7 @@ struct __queue {
|
||||||
|
|
||||||
static void alloc_node(struct ref *head) {
|
static void alloc_node(struct ref *head) {
|
||||||
|
|
||||||
struct node *n = malloc(sizeof(struct node));
|
struct node *n = xmalloc(sizeof(struct node));
|
||||||
assert(n != NULL);
|
|
||||||
|
|
||||||
n->next = NULL;
|
n->next = NULL;
|
||||||
head->n = head->n->next = n;
|
head->n = head->n->next = n;
|
||||||
|
|
@ -53,34 +51,31 @@ static void alloc_node(struct ref *head) {
|
||||||
static void dealloc_node(struct ref *tail) {
|
static void dealloc_node(struct ref *tail) {
|
||||||
|
|
||||||
struct node *next = tail->n->next;
|
struct node *next = tail->n->next;
|
||||||
assert(next != NULL);
|
|
||||||
|
|
||||||
free(tail->n);
|
if (next) {
|
||||||
|
xfree(tail->n);
|
||||||
tail->n = next;
|
tail->n = next;
|
||||||
tail->i = 0;
|
tail->i = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
queue_t queue_init() {
|
queue_t queue_init() {
|
||||||
|
|
||||||
queue_t q = malloc(sizeof(struct __queue));
|
queue_t q = xmalloc(sizeof(struct __queue));
|
||||||
|
|
||||||
if (q)
|
init(q);
|
||||||
init(q);
|
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_destroy(queue_t q) {
|
void queue_destroy(queue_t q) {
|
||||||
|
|
||||||
if (q)
|
xfree(q);
|
||||||
free(q);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_enqueue(queue_t q, void *obj) {
|
void queue_enqueue(queue_t q, void *obj) {
|
||||||
|
|
||||||
if (q->head.n == NULL) {
|
if (q->head.n == NULL) {
|
||||||
q->tail.n = q->head.n = malloc(sizeof(struct node));
|
q->tail.n = q->head.n = xmalloc(sizeof(struct node));
|
||||||
assert(q->tail.n != NULL);
|
|
||||||
} else if (q->head.i + 1 >= BLOCK_SIZE) {
|
} else if (q->head.i + 1 >= BLOCK_SIZE) {
|
||||||
alloc_node(&q->head);
|
alloc_node(&q->head);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -100,7 +95,7 @@ void* queue_dequeue(queue_t q) {
|
||||||
obj = q->tail.n->block[q->tail.i++];
|
obj = q->tail.n->block[q->tail.i++];
|
||||||
|
|
||||||
if (q->tail.n == q->head.n && q->tail.i > q->head.i) {
|
if (q->tail.n == q->head.n && q->tail.i > q->head.i) {
|
||||||
free(q->head.n);
|
xfree(q->head.n);
|
||||||
init(q);
|
init(q);
|
||||||
} else if (q->tail.i >= BLOCK_SIZE) {
|
} else if (q->tail.i >= BLOCK_SIZE) {
|
||||||
dealloc_node(&q->tail);
|
dealloc_node(&q->tail);
|
||||||
|
|
|
||||||
Reference in a new issue