Skip to content

Commit 648653a

Browse files
committed
Add unlikely() branch prediction hints
1 parent 60d5521 commit 648653a

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/queue.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
queue_t *queue_create(int32_t capacity)
99
{
1010
/* A capacity of at least 2 is required for an N-1 queue. */
11-
if (capacity < 2)
11+
if (unlikely(capacity < 2))
1212
capacity = 2;
1313

1414
if (!ispowerof2(capacity))
1515
capacity = nextpowerof2(capacity);
1616

1717
queue_t *q = calloc(1, sizeof(queue_t));
18-
if (!q)
18+
if (unlikely(!q))
1919
return NULL;
2020

2121
q->buf = malloc(capacity * sizeof(void *));
22-
if (!q->buf) {
22+
if (unlikely(!q->buf)) {
2323
free(q);
2424
return NULL;
2525
}
@@ -37,7 +37,7 @@ queue_t *queue_create(int32_t capacity)
3737
int32_t queue_destroy(queue_t *q)
3838
{
3939
/* Refuse to destroy a non-empty queue. */
40-
if (!queue_is_empty(q))
40+
if (unlikely(!queue_is_empty(q)))
4141
return ERR_FAIL;
4242

4343
free(q->buf);
@@ -48,10 +48,10 @@ int32_t queue_destroy(queue_t *q)
4848
/* Adds an element to the tail of the queue. */
4949
int32_t queue_enqueue(queue_t *q, void *ptr)
5050
{
51-
/* Reject null pointer or enqueue into a full queue.
51+
/* Reject null queue or enqueue into a full queue.
5252
* The queue can only store up to 'size - 1' items.
5353
*/
54-
if (!q || queue_is_full(q))
54+
if (unlikely(!q || queue_is_full(q)))
5555
return ERR_FAIL;
5656

5757
q->buf[q->tail] = ptr;
@@ -62,7 +62,7 @@ int32_t queue_enqueue(queue_t *q, void *ptr)
6262
/* Removes an element from the head of the queue. */
6363
void *queue_dequeue(queue_t *q)
6464
{
65-
if (queue_is_empty(q))
65+
if (unlikely(queue_is_empty(q)))
6666
return NULL;
6767

6868
void *item = q->buf[q->head];
@@ -73,7 +73,7 @@ void *queue_dequeue(queue_t *q)
7373
/* Returns the element at the head of the queue without removing it. */
7474
void *queue_peek(const queue_t *q)
7575
{
76-
if (queue_is_empty(q))
76+
if (unlikely(queue_is_empty(q)))
7777
return NULL;
7878
return q->buf[q->head];
7979
}

0 commit comments

Comments
 (0)