Skip to content

Commit d297e3d

Browse files
committed
[libqueue] up
1 parent 788b2ed commit d297e3d

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

libqueue/libqueue.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@
2626

2727
struct item *item_alloc(struct queue *q, void *data, size_t len)
2828
{
29+
if (!q) {
30+
return NULL;
31+
}
2932
struct item *item = CALLOC(1, struct item);
3033
if (!item) {
3134
printf("malloc failed!\n");
3235
return NULL;
3336
}
3437
if (q->alloc_hook) {
35-
item->opaque = (q->alloc_hook)(data, len);
38+
item->opaque.iov_base = (q->alloc_hook)(data, len);
39+
item->opaque.iov_len = len;
3640
} else {
3741
item->data.iov_base = memdup(data, len);
3842
item->data.iov_len = len;
@@ -42,11 +46,15 @@ struct item *item_alloc(struct queue *q, void *data, size_t len)
4246

4347
void item_free(struct queue *q, struct item *item)
4448
{
49+
if (!q) {
50+
return;
51+
}
4552
if (!item) {
4653
return;
4754
}
4855
if (q->free_hook) {
49-
(q->free_hook)(item->opaque);
56+
(q->free_hook)(item->opaque.iov_base);
57+
item->opaque.iov_len = 0;
5058
} else {
5159
free(item->data.iov_base);
5260
}
@@ -55,19 +63,28 @@ void item_free(struct queue *q, struct item *item)
5563

5664
int queue_set_mode(struct queue *q, enum queue_mode mode)
5765
{
66+
if (!q) {
67+
return -1;
68+
}
5869
q->mode = mode;
5970
return 0;
6071
}
6172

6273
int queue_set_hook(struct queue *q, alloc_hook *alloc_cb, free_hook *free_cb)
6374
{
75+
if (!q) {
76+
return -1;
77+
}
6478
q->alloc_hook = alloc_cb;
6579
q->free_hook = free_cb;
6680
return 0;
6781
}
6882

6983
int queue_set_depth(struct queue *q, int depth)
7084
{
85+
if (!q) {
86+
return -1;
87+
}
7188
q->max_depth = depth;
7289
return 0;
7390
}
@@ -92,6 +109,9 @@ struct queue *queue_create()
92109

93110
int queue_flush(struct queue *q)
94111
{
112+
if (!q) {
113+
return -1;
114+
}
95115
struct item *item, *next;
96116
pthread_mutex_lock(&q->lock);
97117
list_for_each_entry_safe(item, next, &q->head, entry) {

libqueue/libqueue.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ enum queue_mode {
3636
struct item {
3737
struct iovec data;
3838
struct list_head entry;
39-
void *opaque;
39+
struct iovec opaque;
4040
};
4141

4242
typedef void *(alloc_hook)(void *data, size_t len);
4343
typedef void (free_hook)(void *data);
4444

45+
4546
struct queue {
4647
struct list_head head;
4748
int depth;
@@ -59,12 +60,12 @@ void item_free(struct queue *q, struct item *item);
5960
struct queue *queue_create();
6061
void queue_destroy(struct queue *q);
6162
int queue_set_depth(struct queue *q, int depth);
63+
int queue_get_depth(struct queue *q);
6264
int queue_set_mode(struct queue *q, enum queue_mode mode);
6365
int queue_set_hook(struct queue *q, alloc_hook *alloc_cb, free_hook *free_cb);
6466
struct item *queue_pop(struct queue *q);
6567
int queue_push(struct queue *q, struct item *item);
6668
int queue_flush(struct queue *q);
67-
int queue_get_depth(struct queue *q);
6869

6970
#ifdef __cplusplus
7071
}

0 commit comments

Comments
 (0)