STAILQ_HEAD(lines, pakfire_log_line) lines;
+ size_t length;
size_t max_length;
};
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;
// 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;
}
// Append to the queue
STAILQ_INSERT_TAIL(&buffer->lines, l, nodes);
+ // The buffer is now longer
+ buffer->length++;
+
return 0;
ERROR:
// Remove the line
STAILQ_REMOVE_HEAD(&buffer->lines, nodes);
+ // The buffer is now shorter
+ buffer->length--;
+
// Free the line
pakfire_log_line_free(l);