fx9860g3: move console_line_t allocation to new PRAM0 allocator

This commit is contained in:
Lephenixnoir 2024-02-04 22:59:54 +01:00
parent d4501baadb
commit af8bacd271
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 55 additions and 22 deletions

View File

@ -6,6 +6,7 @@
#include <gint/keyboard.h>
#include <gint/display.h>
#include <gint/kmalloc.h>
#include <gint/defs/util.h>
#include <stdlib.h>
#include <string.h>
@ -59,7 +60,12 @@ bool console_line_init(console_line_t *line, int prealloc_size)
void console_line_deinit(console_line_t *line)
{
free(line->data);
memset(line, 0, sizeof *line);
/* Manual memset to allow for PRAM0 storage */
line->data = NULL;
line->size = 0;
line->alloc_size = 0;
line->render_lines = 0;
line->prefix = 0;
}
bool console_line_alloc(console_line_t *line, int n)
@ -86,7 +92,7 @@ int console_line_capacity(console_line_t *line)
void console_line_set_prefix(console_line_t *line, int prefix_size)
{
line->prefix = min(max(0, prefix_size), line->size);
line->prefix = min(max(0, prefix_size), (int)line->size);
}
bool console_line_insert(console_line_t *line, int p, char const *str, int n)
@ -175,7 +181,7 @@ bool linebuf_init(linebuf_t *buf, int capacity, int backlog_size)
if(capacity <= 0)
return false;
buf->lines = malloc(capacity * sizeof *buf->lines);
buf->lines = kmalloc(capacity * sizeof *buf->lines, PE_CONSOLE_LINE_ALLOC);
if(!buf->lines)
return false;
@ -192,7 +198,7 @@ bool linebuf_init(linebuf_t *buf, int capacity, int backlog_size)
void linebuf_deinit(linebuf_t *buf)
{
free(buf->lines);
kfree((void *)buf->lines);
memset(buf, 0, sizeof *buf);
}

View File

@ -24,26 +24,34 @@
#include <gint/keyboard.h>
#include <gint/display.h>
#include <gint/defs/attributes.h>
#include <stdbool.h>
/* Maximum line length, to ensure the console can threshold its memory usage
while cleaning only entire lines. Lines longer than this get split. */
#define PE_CONSOLE_LINE_MAX_LENGTH 1024
/* Allocation arena for arrays of lines. */
#ifdef FX9860G
#define PE_CONSOLE_LINE_ALLOC "pram0"
#else
#define PE_CONSOLE_LINE_ALLOC NULL
#endif
//=== Dynamic console lines ===//
typedef struct
typedef volatile struct
{
/* Line contents, NUL-terminated. The buffer might be larger. */
char *data;
/* Size of contents (not counting the NUL). */
int16_t size;
int32_t size :16;
/* Allocated size (always ≥ size+1). */
int16_t alloc_size;
int32_t alloc_size :16;
/* Number or render lines used (updated on-demand). */
int16_t render_lines;
int32_t render_lines :16;
/* Number of initial characters that can't be edited. */
int16_t prefix;
int32_t prefix :16;
} console_line_t;
@ -93,7 +101,10 @@ typedef struct
- 0 <= size <= capacity
- 0 <= start < capacity
- When size is 0, start is undefined. */
int capacity, start, size;
int16_t capacity, start, size;
/* Total number of rendered lines for the buffer. */
int16_t total_rendered;
/* To keep track of lines' identity, the rotating array includes an extra
numbering system. Each line is assigned an *absolute* line number which
@ -117,8 +128,6 @@ typedef struct
for edition (ie. followed by another line). Lazy rendering can always
start at `absolute_rendered+1`. */
int absolute_rendered;
/* Total number of rendered lines for the buffer. */
int total_rendered;
} linebuf_t;
@ -167,15 +176,15 @@ typedef struct
linebuf_t lines;
/* Cursor position within the last line. */
int cursor;
int16_t cursor;
/* Whether new data has been added and a frame should be rendered. */
bool render_needed;
/* View geometry parameters from last console_compute_view(). */
font_t const *render_font;
int render_width;
int render_lines;
int16_t render_width;
int16_t render_lines;
} console_t;

View File

@ -84,16 +84,23 @@ mp_print_t const mp_debug_print = { NULL, print_strn };
void pe_debug_kmalloc(char const *prefix)
{
kmalloc_gint_stats_t *s;
kmalloc_gint_stats_t *s1, *s2;
s1 = kmalloc_get_gint_stats(kmalloc_get_arena("_uram"));
s = kmalloc_get_gint_stats(kmalloc_get_arena("_uram"));
pe_debug_printf("%s: [_uram] used=%d free=%d\n",
prefix, s->used_memory, s->free_memory);
#ifdef FX9860G
s2 = kmalloc_get_gint_stats(kmalloc_get_arena("pram0"));
pe_debug_printf("%s: _uram[used=%d free=%d] pram0[used=%d free=%d]\n",
prefix,
s1->used_memory, s1->free_memory,
s2->used_memory, s2->free_memory);
#endif
#ifdef FXCG50
s = kmalloc_get_gint_stats(kmalloc_get_arena("_ostk"));
pe_debug_printf("%s: [_ostk] used=%d free=%d\n",
prefix, s->used_memory, s->free_memory);
s2 = kmalloc_get_gint_stats(kmalloc_get_arena("_ostk"));
pe_debug_printf("%s: _uram[used=%d free=%d] _ostk[used=%d free=%d]\n",
prefix,
s1->used_memory, s1->free_memory,
s2->used_memory, s2->free_memory);
#endif
}

View File

@ -333,6 +333,17 @@ int pe_readline(vstr_t *line, char const *prompt)
int main(int argc, char **argv)
{
#ifdef FX9860G
/* Use PRAM0 as an arena for special allocs to save memory elsewhere */
kmalloc_arena_t arena_pram0 = { 0 };
arena_pram0.name = "pram0";
arena_pram0.is_default = false;
arena_pram0.start = (void *)0xfe200000;
arena_pram0.end = (void *)0xfe228000; /* 160 kB! */
kmalloc_init_arena(&arena_pram0, true);
kmalloc_add_arena(&arena_pram0);
#endif
pe_debug_init();
pe_debug_printf("---\n");
pe_debug_kmalloc("main");