From bcf1439eb8d936d3b43ac817519cd47e9624210e Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Sun, 24 May 2026 09:04:26 -0400 Subject: completely redo lists --- Makefile.dep | 2 -- dc/dc.c | 4 +++- teensy/teensy_list.c | 12 ------------ teensy/teensy_list.h | 53 ++++++++++++++++++++-------------------------------- teensy/teensy_mem.c | 19 +++++++++++++++++++ teensy/teensy_mem.h | 2 ++ teensy/teensy_ui.c | 31 +++++++++++++++++------------- 7 files changed, 62 insertions(+), 61 deletions(-) delete mode 100644 teensy/teensy_list.c diff --git a/Makefile.dep b/Makefile.dep index 942d214..9f0fc61 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -7,8 +7,6 @@ teensy_context.o: teensy/teensy_context.c teensy/teensy_context.h \ teensy/teensy_common.h teensy/teensy_log.h teensy/teensy_mem.h \ teensy/teensy_renderer.h teensy/teensy.h teensy/teensy_list.h \ teensy/teensy_platform.h -teensy_list.o: teensy/teensy_list.c teensy/teensy_list.h \ - teensy/teensy_common.h teensy/teensy_log.h teensy/teensy_mem.h teensy_log.o: teensy/teensy_log.c teensy/teensy_log.h \ teensy/teensy_common.h teensy/teensy_mem.h teensy_math.o: teensy/teensy_math.c teensy/teensy.h \ diff --git a/dc/dc.c b/dc/dc.c index b0da900..bee27c7 100644 --- a/dc/dc.c +++ b/dc/dc.c @@ -69,7 +69,7 @@ void tick(void) if (tyui_begin_window("Test Window", ty_recti(5, 5, 100, 120), &winid1)) { tyui_button("before"); - tyui_push_layout((float[]){-49, -1, 0}); + tyui_push_layout((float[]){0.5, -1, 0}); tyui_push_layout((float[]){-1, 0}); tyui_text_ex(TYUI_ALIGN_CENTER, "Buttons"); tyui_button("test1"); @@ -83,6 +83,8 @@ void tick(void) tyui_text_ex(TYUI_ALIGN_CENTER, "Stats"); tyui_text_ex(TYUI_ALIGN_RIGHT, "%.2g ms", frame_time * 1000); tyui_text_ex(TYUI_ALIGN_RIGHT, "%.2d fps", (int)(1.0 / frame_time)); + tyui_text_ex(TYUI_ALIGN_RIGHT, "%d alloc", ty_alloc_count()); + tyui_text_ex(TYUI_ALIGN_RIGHT, "%dM mem", ty_alloc_size() / 1000); tyui_pop_layout(); tyui_pop_layout(); diff --git a/teensy/teensy_list.c b/teensy/teensy_list.c deleted file mode 100644 index 76de21a..0000000 --- a/teensy/teensy_list.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "teensy_list.h" - -#include -#include - -void *ty_list_create(void) -{ - ty_List_Header *header = ty_new(ty_List_Header); - header->cap = 0; - header->len = 0; - return (void*)(header + 1); -} diff --git a/teensy/teensy_list.h b/teensy/teensy_list.h index 56ae4b4..6d6e536 100644 --- a/teensy/teensy_list.h +++ b/teensy/teensy_list.h @@ -4,45 +4,32 @@ #include "teensy_common.h" #include "teensy_mem.h" -typedef struct { - size_t cap; - size_t len; -} ty_List_Header; - #define TY_LIST_MIN_CAP 8 #define TY_LIST_GROW_RATE 2 -#define ty_list_get_header(arr) ((ty_List_Header*)arr - 1) -#define ty_list_cap(arr) (ty_list_get_header(arr)->cap) -#define ty_list_len(arr) (ty_list_get_header(arr)->len) - -#define ty_list_last(arr) ((arr)[ty_list_len(arr) - 1]) - -#define ty_list_reserve(arr, amt) \ - do { \ - ty_List_Header *header = ty_list_get_header(arr); \ - if ((amt) > header->cap) { \ - header->cap = (amt) < TY_LIST_MIN_CAP ? TY_LIST_MIN_CAP : (amt); \ - header = ty_realloc( \ - header, \ - (sizeof(*(arr)) * (amt)) + sizeof(ty_List_Header) \ - ); \ - (arr) = (void*)(header + 1); \ - } \ +#define ty_list_last(arr) ((arr)->items[(arr)->len - 1]) + +#define ty_list_reserve(arr, amt) \ + do { \ + if ((amt) > (arr)->cap) { \ + (arr)->cap = ty_max(TY_LIST_MIN_CAP, (arr)->cap); \ + while ((arr)->cap < (amt)) \ + (arr)->cap *= TY_LIST_GROW_RATE; \ + \ + (arr)->items = ty_realloc( \ + (arr)->items, \ + sizeof(*(arr)->items) * (arr)->cap \ + ); \ + } \ } while (0) -#define ty_list_append(arr, elem) \ - do { \ - ty_List_Header *header = ty_list_get_header(arr); \ - ty_list_reserve(arr, header->len + 1); \ - (arr)[header->len++] = (elem); \ +#define ty_list_append(arr, elem) \ + do { \ + ty_list_reserve(arr, (arr)->len + 1); \ + (arr)->items[(arr)->len++] = (elem); \ } while (0) -#define ty_list_clear(arr) \ - (ty_list_get_header(arr)->len = 0) - -#define ty_list_free(arr) (ty_free(ty_list_get_header(arr))) - -void *ty_list_create(void); +#define ty_list_clear(arr) ((arr)->len = 0) +#define ty_list_free(arr) ty_free((arr)->items) #endif // TEENSY_LIST_H_ diff --git a/teensy/teensy_mem.c b/teensy/teensy_mem.c index 8c1022d..65b7486 100644 --- a/teensy/teensy_mem.c +++ b/teensy/teensy_mem.c @@ -126,6 +126,11 @@ void *ty_realloc(void *ptr, size_t new_size) return NULL; } + // The pointer will not be registered in the tracker by realloc if it does + // not yet exist. + if (ptr == NULL) + return ty_alloc(new_size); + void *new_ptr = realloc(ptr, new_size); if (!new_ptr) ty_log_fatal(TY_ERR_MEM, "(%s) ran out of memory", __func__); @@ -179,3 +184,17 @@ int ty_alloc_count(void) return -1; #endif } + +size_t ty_alloc_size(void) +{ +#ifdef TEENSY_DEBUG + size_t total = 0; + for (int i = 0; i < allocations_len; i++) { + Alloc *alloc = &allocations[i]; + total += alloc->size; + } + return total; +#else + return 0; +#endif +} diff --git a/teensy/teensy_mem.h b/teensy/teensy_mem.h index b25cad5..8639479 100644 --- a/teensy/teensy_mem.h +++ b/teensy/teensy_mem.h @@ -22,5 +22,7 @@ void ty_free(void *ptr); // Returns -1 in release builds. int ty_alloc_count(void); +// Returns 0 in release builds. +size_t ty_alloc_size(void); #endif // TEENSY_MEM_H_ diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c index 6a156bb..7ff2418 100644 --- a/teensy/teensy_ui.c +++ b/teensy/teensy_ui.c @@ -34,6 +34,12 @@ typedef struct { tyui_Align align; } Cmd; +typedef struct { + size_t len; + size_t cap; + Cmd *items; +} CmdList; + typedef struct { int idx; int columns; @@ -45,7 +51,7 @@ typedef struct { } Layout; typedef struct { - Cmd *cmds; + CmdList cmds; Layout *layout; Layout layout_stack[MAX_LAYOUTS]; ty_Recti rect; @@ -105,7 +111,7 @@ void rect_cmd(ty_Recti rect, ty_Color color) cmd.rect = rect; cmd.color = color; - ty_list_append(window()->cmds, cmd); + ty_list_append(&window()->cmds, cmd); } static @@ -117,7 +123,7 @@ void text_cmd(const char *text, ty_Recti rect, tyui_Align align) cmd.rect = rect; cmd.align = align; - ty_list_append(window()->cmds, cmd); + ty_list_append(&window()->cmds, cmd); } static @@ -127,7 +133,7 @@ void image_cmd(ty_Image img, ty_Recti rect) cmd.kind = CMD_IMAGE; cmd.img = img; cmd.rect = rect; - ty_list_append(window()->cmds, cmd); + ty_list_append(&window()->cmds, cmd); } static @@ -140,7 +146,7 @@ void clip_cmd(ty_Recti rect) rect = window()->rect; // This is a full screen clip cmd.rect = ty_recti_clamp(rect, window()->rect); - ty_list_append(window()->cmds, cmd); + ty_list_append(&window()->cmds, cmd); } static @@ -230,11 +236,10 @@ bool is_hovered(ty_Recti rect) static Window create_window(ty_Recti rect, uint32_t flags) { - Window win = {}; + Window win = {0}; win.rect = rect; win.flags = flags; - win.cmds = ty_list_create(); - ty_list_reserve(win.cmds, 64); + ty_list_reserve(&win.cmds, 8); return win; } @@ -258,9 +263,9 @@ void tyui_init(const ty_Font *font) void tyui_deinit(void) { - ty_list_free(tyui.root.cmds); + ty_list_free(&tyui.root.cmds); for (int i = 0; i < tyui.window_count; i++) - ty_list_free(tyui.windows[i].cmds); + ty_list_free(&tyui.windows[i].cmds); } void tyui_push_layout(const float *column_widths) @@ -562,8 +567,8 @@ void tyui_text_input(tyui_Text_Input *input) static void draw_window(Window *win) { - for (size_t i = 0; i < ty_list_len(win->cmds); i++) { - Cmd cmd = win->cmds[i]; + for (size_t i = 0; i < win->cmds.len; i++) { + Cmd cmd = win->cmds.items[i]; switch (cmd.kind) { case CMD_RECT: ty_draw_rect(cmd.rect, cmd.color); @@ -594,7 +599,7 @@ void draw_window(Window *win) } } ty_draw_set_clip(TY_CLIP_NONE); - ty_list_clear(win->cmds); + ty_list_clear(&win->cmds); } static -- cgit v1.3-2-g0d8e