]> git.ipfire.org Git - pakfire.git/commitdiff
log buffer: Keep count of the current number of lines
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Jan 2025 15:43:30 +0000 (15:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Jan 2025 15:43:30 +0000 (15:43 +0000)
This avoids the rather expensive counting operation.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/log_buffer.c

index a25a26bf52d3c5e9ac4a7064bc31da889850df4d..91de15532424526658e9b483dfe467d47aba5046 100644 (file)
@@ -39,6 +39,7 @@ struct pakfire_log_buffer {
 
        STAILQ_HEAD(lines, pakfire_log_line) lines;
 
+       size_t length;
        size_t max_length;
 };
 
@@ -95,16 +96,6 @@ struct pakfire_log_buffer* pakfire_log_buffer_unref(struct pakfire_log_buffer* b
        return NULL;
 }
 
-static size_t pakfire_log_buffer_length(struct pakfire_log_buffer* buffer) {
-       struct pakfire_log_line* line = NULL;
-       size_t length = 0;
-
-       STAILQ_FOREACH(line, &buffer->lines, nodes)
-               length++;
-
-       return length;
-}
-
 int pakfire_log_buffer_enqueue(struct pakfire_log_buffer* buffer, int priority, const char* line, ssize_t length) {
        struct pakfire_log_line* l = NULL;
 
@@ -114,7 +105,7 @@ int pakfire_log_buffer_enqueue(struct pakfire_log_buffer* buffer, int priority,
 
        // Fail if the buffer is full
        if (buffer->max_length > 0) {
-               if (pakfire_log_buffer_length(buffer) >= buffer->max_length)
+               if (buffer->length >= buffer->max_length)
                        return -ENOBUFS;
        }
 
@@ -141,6 +132,9 @@ int pakfire_log_buffer_enqueue(struct pakfire_log_buffer* buffer, int priority,
        // Append to the queue
        STAILQ_INSERT_TAIL(&buffer->lines, l, nodes);
 
+       // The buffer is now longer
+       buffer->length++;
+
        return 0;
 
 ERROR:
@@ -185,6 +179,9 @@ int pakfire_log_buffer_dequeue(struct pakfire_log_buffer* buffer, int* priority,
        // Remove the line
        STAILQ_REMOVE_HEAD(&buffer->lines, nodes);
 
+       // The buffer is now shorter
+       buffer->length--;
+
        // Free the line
        pakfire_log_line_free(l);