[#36] Use dynamic buffer size to prevent unnecessary buffer cleanups #37
@@ -1,37 +1,32 @@
|
|||||||
/*
|
/*
|
||||||
* buffer.c
|
* buffer.c
|
||||||
* vim: expandtab:ts=4:sts=4:sw=4
|
*
|
||||||
|
* Message buffer implementation for CProof.
|
||||||
|
*
|
||||||
|
* This module provides an in-memory buffer for managing active chat entries in the
|
||||||
|
* console-based XMPP client. Separate from persistent SQLite storage, it holds
|
||||||
|
* messages and metadata solely for UI rendering, ensuring responsive scrolling and
|
||||||
|
* display without exceeding ncurses pad limits (10k lines). By tracking rendered
|
||||||
|
* line counts per entry, it proactively trims content to prevent overflow, enabling
|
||||||
|
* seamless integration with ncurses for position-aware rendering.
|
||||||
|
*
|
||||||
|
* Key features and operations:
|
||||||
|
* - Append/prepend entries (messages) with automatic line-based cleanup.
|
||||||
|
* - Track cumulative lines for overflow prevention and efficient buffer sizing.
|
||||||
|
* - Remove entries by ID or index; mark delivery receipts as received.
|
||||||
|
* - Retrieve entries by index or ID for rendering and history queries.
|
||||||
|
* - Store metadata including timestamps, senders, themes, and ncurses y-positions.
|
||||||
|
*
|
||||||
|
* CProof. Fork of Profanity (since 2025).
|
||||||
*
|
*
|
||||||
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
||||||
* Copyright (C) 2019 - 2025 Michael Vetter <jubalh@iodoru.org>
|
* Copyright (C) 2019 - 2025 Michael Vetter <jubalh@iodoru.org>
|
||||||
|
* Copyright (C) 2025 CProof Developers
|
||||||
*
|
*
|
||||||
* This file is part of Profanity.
|
* Licensed under the GNU General Public License, version 3,
|
||||||
*
|
* with the OpenSSL exception. See LICENSE for details.
|
||||||
* Profanity is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* Profanity is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* In addition, as a special exception, the copyright holders give permission to
|
|
||||||
* link the code of portions of this program with the OpenSSL library under
|
|
||||||
* certain conditions as described in each individual source file, and
|
|
||||||
* distribute linked combinations including the two.
|
|
||||||
*
|
|
||||||
* You must obey the GNU General Public License in all respects for all of the
|
|
||||||
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
|
||||||
* may extend this exception to your version of the file(s), but you are not
|
|
||||||
* obligated to do so. If you do not wish to do so, delete this exception
|
|
||||||
* statement from your version. If you delete this exception statement from all
|
|
||||||
* source files in the program, then also delete it here.
|
|
||||||
*
|
*
|
||||||
|
* vim: expandtab:ts=4:sts=4:sw=4
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
@@ -54,7 +49,6 @@
|
|||||||
#include "ui/window.h"
|
#include "ui/window.h"
|
||||||
#include "ui/buffer.h"
|
#include "ui/buffer.h"
|
||||||
|
|
||||||
#define MAX_BUFFER_SIZE 200
|
|
||||||
#define STRDUP_OR_NULL(str) ((str) ? strdup(str) : NULL)
|
#define STRDUP_OR_NULL(str) ((str) ? strdup(str) : NULL)
|
||||||
|
|
||||||
struct prof_buff_t
|
struct prof_buff_t
|
||||||
@@ -108,7 +102,7 @@ _buffer_add(ProfBuff buffer, const char* show_char, int pad_indent, GDateTime* t
|
|||||||
|
|
||||||
buffer->lines += e->_lines;
|
buffer->lines += e->_lines;
|
||||||
|
|
||||||
while (g_slist_length(buffer->entries) >= MAX_BUFFER_SIZE) {
|
while (buffer->lines >= PAD_SIZE) {
|
||||||
// Delete message from the opposite size to free buffer
|
// Delete message from the opposite size to free buffer
|
||||||
GSList* buffer_entry_to_delete = append ? buffer->entries : g_slist_last(buffer->entries);
|
GSList* buffer_entry_to_delete = append ? buffer->entries : g_slist_last(buffer->entries);
|
||||||
ProfBuffEntry* entry_to_delete = (ProfBuffEntry*)buffer_entry_to_delete->data;
|
ProfBuffEntry* entry_to_delete = (ProfBuffEntry*)buffer_entry_to_delete->data;
|
||||||
@@ -118,8 +112,8 @@ _buffer_add(ProfBuff buffer, const char* show_char, int pad_indent, GDateTime* t
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (from_jid && y_end_pos == y_start_pos) {
|
if (from_jid && y_end_pos == y_start_pos) {
|
||||||
log_warning("Ncurses Overflow! From: %s, position: %d, ID: %s, append: %s, used message buffer size: %d, message buffer size: %d, rendered lines buffer size: %d",
|
log_warning("Ncurses Overflow! From: %s, position: %d, ID: %s, append: %s, used message buffer size: %d, rendered lines buffer size: %d",
|
||||||
from_jid, y_start_pos, id, append ? "TRUE" : "FALSE", g_slist_length(buffer->entries), MAX_BUFFER_SIZE, PAD_SIZE);
|
from_jid, y_start_pos, id, append ? "TRUE" : "FALSE", g_slist_length(buffer->entries), PAD_SIZE);
|
||||||
// At this point we have a message that caused overflow of the render buffer.
|
// At this point we have a message that caused overflow of the render buffer.
|
||||||
// Ideally, we want to clean other messages to rerender everything properly. To do so,
|
// Ideally, we want to clean other messages to rerender everything properly. To do so,
|
||||||
// first we need to determine the size of the message that we are trying to display,
|
// first we need to determine the size of the message that we are trying to display,
|
||||||
|
|||||||
Reference in New Issue
Block a user