Skip to content

Commit

Permalink
queue: delay freeing overflow until after critical section
Browse files Browse the repository at this point in the history
I probably won't differ much, but all small bits help.

Signed-off-by: Fabian Groffen <[email protected]>
  • Loading branch information
grobian committed Apr 28, 2024
1 parent 3be6b90 commit c4df9c7
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions queue.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 Fabian Groffen
* Copyright 2013-2024 Fabian Groffen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,6 +85,8 @@ queue_destroy(queue *q)
void
queue_enqueue(queue *q, const char *p)
{
char *tofree = NULL;

/* queue normal:
* |=====-----------------------------| 4
* ^ ^
Expand All @@ -102,7 +104,7 @@ queue_enqueue(queue *q, const char *p)
if (q->len == q->end) {
if (q->read == q->end)
q->read = 0;
free((char *)(q->queue[q->read]));
tofree = (char *)(q->queue[q->read]);
q->read++;
q->len--;
}
Expand All @@ -112,6 +114,9 @@ queue_enqueue(queue *q, const char *p)
q->write++;
q->len++;
pthread_mutex_unlock(&q->lock);

if (tofree != NULL)
free(tofree);
}

/**
Expand Down

0 comments on commit c4df9c7

Please sign in to comment.