diff --git a/stage23/console.c b/stage23/console.c index ee4556de..8249e4cb 100644 --- a/stage23/console.c +++ b/stage23/console.c @@ -22,10 +22,7 @@ static void console_help(void) { void console(void) { print("Welcome to the Limine console.\nType 'help' for more information.\n\n"); - static char *prompt = NULL; - if (prompt == NULL) { - prompt = ext_mem_alloc(256); - } + char *prompt = ext_mem_alloc(256); for (;;) { print(">>> "); @@ -34,8 +31,7 @@ void console(void) { if (strcmp(prompt, "help") == 0) { console_help(); } else if (strcmp(prompt, "exit") == 0) { - reset_term(); - return; + break; } else if (strcmp(prompt, "version") == 0) { print(LIMINE_VERSION "\n"); } else if (strcmp(prompt, "copyright") == 0) { @@ -47,4 +43,7 @@ void console(void) { print("Invalid command: `%s`.\n", prompt); } } + + reset_term(); + pmm_free(prompt, 256); } diff --git a/stage23/drivers/disk.s2.c b/stage23/drivers/disk.s2.c index 08017dd6..4aa41a66 100644 --- a/stage23/drivers/disk.s2.c +++ b/stage23/drivers/disk.s2.c @@ -469,6 +469,8 @@ void disk_create_index(void) { find_unique_sectors(); find_part_handles(handles, handle_count); + + pmm_free(handles, handles_size); } #endif diff --git a/stage23/drivers/edid.c b/stage23/drivers/edid.c index 292a37c7..2b098a39 100644 --- a/stage23/drivers/edid.c +++ b/stage23/drivers/edid.c @@ -61,7 +61,7 @@ struct edid_info_struct *get_edid_info(void) { status = gBS->LocateHandle(ByProtocol, &gop_guid, NULL, &handles_size, handles); if (status && status != EFI_BUFFER_TOO_SMALL) - goto fail; + goto fail_n; handles = ext_mem_alloc(handles_size); @@ -88,10 +88,13 @@ struct edid_info_struct *get_edid_info(void) { goto success; fail: + pmm_free(handles, handles_size); +fail_n: printv("edid: Could not fetch EDID data.\n"); return NULL; success: + pmm_free(handles, handles_size); printv("edid: Success.\n"); return buf; } diff --git a/stage23/lib/bmp.c b/stage23/lib/bmp.c index 1dada797..c7ad75db 100644 --- a/stage23/lib/bmp.c +++ b/stage23/lib/bmp.c @@ -28,16 +28,16 @@ struct bmp_header { uint32_t blue_mask; } __attribute__((packed)); -int bmp_open_image(struct image *image, struct file_handle *file) { +bool bmp_open_image(struct image *image, struct file_handle *file) { struct bmp_header header; fread(file, &header, 0, sizeof(struct bmp_header)); if (memcmp(&header.bf_signature, "BM", 2) != 0) - return -1; + return false; // We don't support bpp lower than 8 if (header.bi_bpp % 8 != 0) - return -1; + return false; image->img = ext_mem_alloc(header.bf_size); @@ -50,6 +50,8 @@ int bmp_open_image(struct image *image, struct file_handle *file) { fread(file, image->img, header.bf_offset, bf_size); + image->allocated_size = header.bf_size; + image->x_size = header.bi_width; image->y_size = header.bi_height; image->pitch = ALIGN_UP(header.bi_width * header.bi_bpp, 32) / 8; @@ -57,5 +59,5 @@ int bmp_open_image(struct image *image, struct file_handle *file) { image->img_width = header.bi_width; image->img_height = header.bi_height; - return 0; + return true; } diff --git a/stage23/lib/bmp.h b/stage23/lib/bmp.h index 5292bdc4..8763d6b4 100644 --- a/stage23/lib/bmp.h +++ b/stage23/lib/bmp.h @@ -5,6 +5,6 @@ #include #include -int bmp_open_image(struct image *image, struct file_handle *file); +bool bmp_open_image(struct image *image, struct file_handle *file); #endif diff --git a/stage23/lib/gterm.c b/stage23/lib/gterm.c index ecc3bde3..94681c3d 100644 --- a/stage23/lib/gterm.c +++ b/stage23/lib/gterm.c @@ -19,7 +19,6 @@ #define DEFAULT_FONT_WIDTH 8 #define DEFAULT_FONT_HEIGHT 16 -static size_t last_vga_font_bool = 0; static size_t vga_font_width; static size_t vga_font_height; static size_t glyph_width = 8; @@ -40,6 +39,7 @@ static uint16_t gterm_bpp; extern symbol _binary_font_bin_start; static uint8_t *vga_font_bits = NULL; +static size_t vga_font_bool_size = 0; static bool *vga_font_bool = NULL; static uint32_t ansi_colours[8]; @@ -48,7 +48,7 @@ static uint32_t default_fg, default_bg; static struct image *background; -static size_t last_bg_canvas_size = 0; +static size_t bg_canvas_size = 0; static uint32_t *bg_canvas = NULL; static size_t rows; @@ -56,9 +56,9 @@ static size_t cols; static size_t margin; static size_t margin_gradient; -static size_t last_grid_size = 0; -static size_t last_queue_size = 0; -static size_t last_map_size = 0; +static size_t grid_size = 0; +static size_t queue_size = 0; +static size_t map_size = 0; struct gterm_char { uint32_t c; @@ -673,10 +673,7 @@ bool gterm_init(size_t *_rows, size_t *_cols, size_t width, size_t height) { if (background_path != NULL) { struct file_handle *bg_file; if ((bg_file = uri_open(background_path)) != NULL) { - background = ext_mem_alloc(sizeof(struct image)); - if (open_image(background, bg_file)) { - background = NULL; - } + background = image_open(bg_file); fclose(bg_file); } } @@ -714,9 +711,7 @@ bool gterm_init(size_t *_rows, size_t *_cols, size_t width, size_t height) { vga_font_width = DEFAULT_FONT_WIDTH, vga_font_height = DEFAULT_FONT_HEIGHT; size_t font_bytes = (vga_font_width * vga_font_height * VGA_FONT_GLYPHS) / 8; - if (vga_font_bits == NULL) { - vga_font_bits = ext_mem_alloc(VGA_FONT_MAX); - } + vga_font_bits = ext_mem_alloc(VGA_FONT_MAX); memcpy(vga_font_bits, (void *)_binary_font_bin_start, VGA_FONT_MAX); @@ -766,11 +761,8 @@ no_load_font:; vga_font_width += font_spacing; - size_t this_vga_font_bool = VGA_FONT_GLYPHS * vga_font_height * vga_font_width * sizeof(bool); - if (last_vga_font_bool < this_vga_font_bool) { - vga_font_bool = ext_mem_alloc(this_vga_font_bool); - last_vga_font_bool = this_vga_font_bool; - } + vga_font_bool_size = VGA_FONT_GLYPHS * vga_font_height * vga_font_width * sizeof(bool); + vga_font_bool = ext_mem_alloc(vga_font_bool_size); for (size_t i = 0; i < VGA_FONT_GLYPHS; i++) { uint8_t *glyph = &vga_font_bits[i * vga_font_height]; @@ -825,34 +817,18 @@ no_load_font:; offset_x = margin + ((gterm_width - margin * 2) % glyph_width) / 2; offset_y = margin + ((gterm_height - margin * 2) % glyph_height) / 2; - size_t new_grid_size = rows * cols * sizeof(struct gterm_char); - if (new_grid_size > last_grid_size) { - grid = ext_mem_alloc(new_grid_size); - last_grid_size = new_grid_size; - } else { - memset(grid, 0, new_grid_size); - } + grid_size = rows * cols * sizeof(struct gterm_char); + grid = ext_mem_alloc(grid_size); - size_t new_queue_size = rows * cols * sizeof(struct queue_item); - if (new_queue_size > last_queue_size) { - queue = ext_mem_alloc(new_queue_size); - last_queue_size = new_queue_size; - } + queue_size = rows * cols * sizeof(struct queue_item); + queue = ext_mem_alloc(queue_size); queue_i = 0; - size_t new_map_size = rows * cols * sizeof(struct queue_item *); - if (new_map_size > last_map_size) { - map = ext_mem_alloc(new_map_size); - last_map_size = new_map_size; - } else { - memset(map, 0, new_map_size); - } + map_size = rows * cols * sizeof(struct queue_item *); + map = ext_mem_alloc(map_size); - size_t new_bg_canvas_size = gterm_width * gterm_height * sizeof(uint32_t); - if (new_bg_canvas_size > last_bg_canvas_size) { - bg_canvas = ext_mem_alloc(new_bg_canvas_size); - last_bg_canvas_size = new_bg_canvas_size; - } + bg_canvas_size = gterm_width * gterm_height * sizeof(uint32_t); + bg_canvas = ext_mem_alloc(bg_canvas_size); gterm_generate_canvas(); gterm_clear(true); @@ -861,11 +837,23 @@ no_load_font:; return true; } +void gterm_deinit(void) { + if (background != NULL) { + image_close(background); + } + pmm_free(vga_font_bits, VGA_FONT_MAX); + pmm_free(vga_font_bool, vga_font_bool_size); + pmm_free(grid, grid_size); + pmm_free(queue, queue_size); + pmm_free(map, map_size); + pmm_free(bg_canvas, bg_canvas_size); +} + uint64_t gterm_context_size(void) { uint64_t ret = 0; ret += sizeof(struct context); - ret += last_grid_size; + ret += grid_size; return ret; } @@ -874,14 +862,14 @@ void gterm_context_save(uint64_t ptr) { memcpy32to64(ptr, (uint64_t)(uintptr_t)&context, sizeof(struct context)); ptr += sizeof(struct context); - memcpy32to64(ptr, (uint64_t)(uintptr_t)grid, last_grid_size); + memcpy32to64(ptr, (uint64_t)(uintptr_t)grid, grid_size); } void gterm_context_restore(uint64_t ptr) { memcpy32to64((uint64_t)(uintptr_t)&context, ptr, sizeof(struct context)); ptr += sizeof(struct context); - memcpy32to64((uint64_t)(uintptr_t)grid, ptr, last_grid_size); + memcpy32to64((uint64_t)(uintptr_t)grid, ptr, grid_size); for (size_t i = 0; i < (size_t)rows * cols; i++) { size_t x = i % cols; diff --git a/stage23/lib/gterm.h b/stage23/lib/gterm.h index f231440d..65a39282 100644 --- a/stage23/lib/gterm.h +++ b/stage23/lib/gterm.h @@ -9,6 +9,7 @@ extern struct fb_info fbinfo; bool gterm_init(size_t *_rows, size_t *_cols, size_t width, size_t height); +void gterm_deinit(void); void gterm_putchar(uint8_t c); void gterm_clear(bool move); diff --git a/stage23/lib/image.c b/stage23/lib/image.c index a325cc70..5e6f480b 100644 --- a/stage23/lib/image.c +++ b/stage23/lib/image.c @@ -21,13 +21,19 @@ void image_make_stretched(struct image *image, int new_x_size, int new_y_size) { image->y_size = new_y_size; } -int open_image(struct image *image, struct file_handle *file) { - image->file = file; - - if (!bmp_open_image(image, file)) - return 0; +struct image *image_open(struct file_handle *file) { + struct image *image = ext_mem_alloc(sizeof(struct image)); image->type = IMAGE_TILED; - return -1; + if (bmp_open_image(image, file)) + return image; + + pmm_free(image, sizeof(struct image)); + return NULL; +} + +void image_close(struct image *image) { + pmm_free(image->img, image->allocated_size); + pmm_free(image, sizeof(struct image)); } diff --git a/stage23/lib/image.h b/stage23/lib/image.h index 8c7a506c..54997fd0 100644 --- a/stage23/lib/image.h +++ b/stage23/lib/image.h @@ -5,7 +5,7 @@ #include struct image { - struct file_handle *file; + size_t allocated_size; size_t x_size; size_t y_size; int type; @@ -27,6 +27,7 @@ enum { void image_make_centered(struct image *image, int frame_x_size, int frame_y_size, uint32_t back_colour); void image_make_stretched(struct image *image, int new_x_size, int new_y_size); -int open_image(struct image *image, struct file_handle *file); +struct image *image_open(struct file_handle *file); +void image_close(struct image *image); #endif diff --git a/stage23/lib/term.c b/stage23/lib/term.c index 391e760f..fe114858 100644 --- a/stage23/lib/term.c +++ b/stage23/lib/term.c @@ -8,6 +8,15 @@ bool early_term = false; +void term_deinit(void) { + switch (term_backend) { + case VBE: + gterm_deinit(); + } + + term_backend = NOT_READY; +} + void term_vbe(size_t width, size_t height) { term_backend = NOT_READY; diff --git a/stage23/lib/term.s2.c b/stage23/lib/term.s2.c index eeb8df2e..34db61b0 100644 --- a/stage23/lib/term.s2.c +++ b/stage23/lib/term.s2.c @@ -129,10 +129,6 @@ void term_textmode(void) { } #endif -void term_deinit(void) { - term_backend = NOT_READY; -} - static uint64_t context_size(void) { uint64_t ret = 0; diff --git a/stage23/menu.c b/stage23/menu.c index e51f7a55..2acb0686 100644 --- a/stage23/menu.c +++ b/stage23/menu.c @@ -200,9 +200,7 @@ static char *config_entry_editor(const char *title, const char *orig_entry) { char *validation_enabled_config = config_get_value(NULL, 0, "EDITOR_VALIDATION"); if (!strcmp(validation_enabled_config, "no")) validation_enabled = false; - static char *buffer = NULL; - if (buffer == NULL) - buffer = ext_mem_alloc(EDITOR_MAX_BUFFER_SIZE); + char *buffer = ext_mem_alloc(EDITOR_MAX_BUFFER_SIZE); memcpy(buffer, orig_entry, entry_size); buffer[entry_size] = 0; @@ -439,6 +437,7 @@ refresh: return buffer; case GETCHAR_ESCAPE: disable_cursor(); + pmm_free(buffer, EDITOR_MAX_BUFFER_SIZE); return NULL; default: if (strlen(buffer) < EDITOR_MAX_BUFFER_SIZE - 1) { diff --git a/stage23/protos/linux.c b/stage23/protos/linux.c index 4ddcc2f8..62716bbf 100644 --- a/stage23/protos/linux.c +++ b/stage23/protos/linux.c @@ -403,6 +403,7 @@ void linux_load(char *config, char *cmdline) { fread(kernel, kernel_version, setup_header->kernel_version + 0x200, 128); print("linux: Kernel version: %s\n", kernel_version); } + pmm_free(kernel_version, 128); } setup_header->type_of_loader = 0xff; diff --git a/stage23/sys/idt.c b/stage23/sys/idt.c index b5768769..9b12b58d 100644 --- a/stage23/sys/idt.c +++ b/stage23/sys/idt.c @@ -16,7 +16,8 @@ static void dummy_isr(void *p) { } void init_flush_irqs(void) { - dummy_idt = ext_mem_alloc(256 * sizeof(struct idt_entry)); + size_t dummy_idt_size = 256 * sizeof(struct idt_entry); + dummy_idt = ext_mem_alloc(dummy_idt_size); for (size_t i = 0; i < 256; i++) { dummy_idt[i].offset_lo = (uint16_t)(uintptr_t)dummy_isr; @@ -30,6 +31,8 @@ void init_flush_irqs(void) { dummy_idt[i].offset_hi = (uint32_t)((uintptr_t)dummy_isr >> 32); #endif } + + pmm_free(dummy_idt, dummy_idt_size); } int irq_flush_type = IRQ_NO_FLUSH;