2626
2727struct 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
4347void 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
5664int 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
6273int 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
6983int 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
93110int 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 ) {
0 commit comments