diff options
Diffstat (limited to 'teensy')
| -rw-r--r-- | teensy/teensy_list.c | 12 | ||||
| -rw-r--r-- | teensy/teensy_list.h | 51 | ||||
| -rw-r--r-- | teensy/teensy_mem.c | 19 | ||||
| -rw-r--r-- | teensy/teensy_mem.h | 2 | ||||
| -rw-r--r-- | teensy/teensy_ui.c | 31 |
5 files changed, 58 insertions, 57 deletions
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 <stdio.h> -#include <assert.h> - -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_last(arr) ((arr)->items[(arr)->len - 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_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 @@ -35,6 +35,12 @@ typedef struct { } Cmd; typedef struct { + size_t len; + size_t cap; + Cmd *items; +} CmdList; + +typedef struct { int idx; int columns; float column_widths[TYUI_MAX_LAYOUT_SIZE]; @@ -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 |
