8
8
queue_t * queue_create (int32_t capacity )
9
9
{
10
10
/* A capacity of at least 2 is required for an N-1 queue. */
11
- if (capacity < 2 )
11
+ if (unlikely ( capacity < 2 ) )
12
12
capacity = 2 ;
13
13
14
14
if (!ispowerof2 (capacity ))
15
15
capacity = nextpowerof2 (capacity );
16
16
17
17
queue_t * q = calloc (1 , sizeof (queue_t ));
18
- if (! q )
18
+ if (unlikely (! q ) )
19
19
return NULL ;
20
20
21
21
q -> buf = malloc (capacity * sizeof (void * ));
22
- if (!q -> buf ) {
22
+ if (unlikely ( !q -> buf ) ) {
23
23
free (q );
24
24
return NULL ;
25
25
}
@@ -37,7 +37,7 @@ queue_t *queue_create(int32_t capacity)
37
37
int32_t queue_destroy (queue_t * q )
38
38
{
39
39
/* Refuse to destroy a non-empty queue. */
40
- if (!queue_is_empty (q ))
40
+ if (unlikely ( !queue_is_empty (q ) ))
41
41
return ERR_FAIL ;
42
42
43
43
free (q -> buf );
@@ -48,10 +48,10 @@ int32_t queue_destroy(queue_t *q)
48
48
/* Adds an element to the tail of the queue. */
49
49
int32_t queue_enqueue (queue_t * q , void * ptr )
50
50
{
51
- /* Reject null pointer or enqueue into a full queue.
51
+ /* Reject null queue or enqueue into a full queue.
52
52
* The queue can only store up to 'size - 1' items.
53
53
*/
54
- if (!q || queue_is_full (q ))
54
+ if (unlikely ( !q || queue_is_full (q ) ))
55
55
return ERR_FAIL ;
56
56
57
57
q -> buf [q -> tail ] = ptr ;
@@ -62,7 +62,7 @@ int32_t queue_enqueue(queue_t *q, void *ptr)
62
62
/* Removes an element from the head of the queue. */
63
63
void * queue_dequeue (queue_t * q )
64
64
{
65
- if (queue_is_empty (q ))
65
+ if (unlikely ( queue_is_empty (q ) ))
66
66
return NULL ;
67
67
68
68
void * item = q -> buf [q -> head ];
@@ -73,7 +73,7 @@ void *queue_dequeue(queue_t *q)
73
73
/* Returns the element at the head of the queue without removing it. */
74
74
void * queue_peek (const queue_t * q )
75
75
{
76
- if (queue_is_empty (q ))
76
+ if (unlikely ( queue_is_empty (q ) ))
77
77
return NULL ;
78
78
return q -> buf [q -> head ];
79
79
}
0 commit comments