everywhere: Use pmm_free() in more places

This commit is contained in:
mintsuki 2021-10-21 02:37:05 +02:00
parent a8050bce47
commit 54e92b23ad
13 changed files with 77 additions and 66 deletions

View File

@ -469,6 +469,8 @@ void disk_create_index(void) {
find_unique_sectors(); find_unique_sectors();
find_part_handles(handles, handle_count); find_part_handles(handles, handle_count);
pmm_free(handles, handles_size);
} }
#endif #endif

View File

@ -61,7 +61,7 @@ struct edid_info_struct *get_edid_info(void) {
status = gBS->LocateHandle(ByProtocol, &gop_guid, NULL, &handles_size, handles); status = gBS->LocateHandle(ByProtocol, &gop_guid, NULL, &handles_size, handles);
if (status && status != EFI_BUFFER_TOO_SMALL) if (status && status != EFI_BUFFER_TOO_SMALL)
goto fail; goto fail_n;
handles = ext_mem_alloc(handles_size); handles = ext_mem_alloc(handles_size);
@ -88,10 +88,13 @@ struct edid_info_struct *get_edid_info(void) {
goto success; goto success;
fail: fail:
pmm_free(handles, handles_size);
fail_n:
printv("edid: Could not fetch EDID data.\n"); printv("edid: Could not fetch EDID data.\n");
return NULL; return NULL;
success: success:
pmm_free(handles, handles_size);
printv("edid: Success.\n"); printv("edid: Success.\n");
return buf; return buf;
} }

View File

@ -28,16 +28,16 @@ struct bmp_header {
uint32_t blue_mask; uint32_t blue_mask;
} __attribute__((packed)); } __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; struct bmp_header header;
fread(file, &header, 0, sizeof(struct bmp_header)); fread(file, &header, 0, sizeof(struct bmp_header));
if (memcmp(&header.bf_signature, "BM", 2) != 0) if (memcmp(&header.bf_signature, "BM", 2) != 0)
return -1; return false;
// We don't support bpp lower than 8 // We don't support bpp lower than 8
if (header.bi_bpp % 8 != 0) if (header.bi_bpp % 8 != 0)
return -1; return false;
image->img = ext_mem_alloc(header.bf_size); 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); fread(file, image->img, header.bf_offset, bf_size);
image->allocated_size = header.bf_size;
image->x_size = header.bi_width; image->x_size = header.bi_width;
image->y_size = header.bi_height; image->y_size = header.bi_height;
image->pitch = ALIGN_UP(header.bi_width * header.bi_bpp, 32) / 8; 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_width = header.bi_width;
image->img_height = header.bi_height; image->img_height = header.bi_height;
return 0; return true;
} }

View File

@ -5,6 +5,6 @@
#include <fs/file.h> #include <fs/file.h>
#include <lib/image.h> #include <lib/image.h>
int bmp_open_image(struct image *image, struct file_handle *file); bool bmp_open_image(struct image *image, struct file_handle *file);
#endif #endif

View File

@ -19,7 +19,6 @@
#define DEFAULT_FONT_WIDTH 8 #define DEFAULT_FONT_WIDTH 8
#define DEFAULT_FONT_HEIGHT 16 #define DEFAULT_FONT_HEIGHT 16
static size_t last_vga_font_bool = 0;
static size_t vga_font_width; static size_t vga_font_width;
static size_t vga_font_height; static size_t vga_font_height;
static size_t glyph_width = 8; static size_t glyph_width = 8;
@ -40,6 +39,7 @@ static uint16_t gterm_bpp;
extern symbol _binary_font_bin_start; extern symbol _binary_font_bin_start;
static uint8_t *vga_font_bits = NULL; static uint8_t *vga_font_bits = NULL;
static size_t vga_font_bool_size = 0;
static bool *vga_font_bool = NULL; static bool *vga_font_bool = NULL;
static uint32_t ansi_colours[8]; static uint32_t ansi_colours[8];
@ -48,7 +48,7 @@ static uint32_t default_fg, default_bg;
static struct image *background; 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 uint32_t *bg_canvas = NULL;
static size_t rows; static size_t rows;
@ -56,9 +56,9 @@ static size_t cols;
static size_t margin; static size_t margin;
static size_t margin_gradient; static size_t margin_gradient;
static size_t last_grid_size = 0; static size_t grid_size = 0;
static size_t last_queue_size = 0; static size_t queue_size = 0;
static size_t last_map_size = 0; static size_t map_size = 0;
struct gterm_char { struct gterm_char {
uint32_t c; 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) { if (background_path != NULL) {
struct file_handle *bg_file; struct file_handle *bg_file;
if ((bg_file = uri_open(background_path)) != NULL) { if ((bg_file = uri_open(background_path)) != NULL) {
background = ext_mem_alloc(sizeof(struct image)); background = image_open(bg_file);
if (open_image(background, bg_file)) {
background = NULL;
}
fclose(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; 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; 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); memcpy(vga_font_bits, (void *)_binary_font_bin_start, VGA_FONT_MAX);
@ -766,11 +761,8 @@ no_load_font:;
vga_font_width += font_spacing; vga_font_width += font_spacing;
size_t this_vga_font_bool = VGA_FONT_GLYPHS * vga_font_height * vga_font_width * sizeof(bool); vga_font_bool_size = 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(vga_font_bool_size);
vga_font_bool = ext_mem_alloc(this_vga_font_bool);
last_vga_font_bool = this_vga_font_bool;
}
for (size_t i = 0; i < VGA_FONT_GLYPHS; i++) { for (size_t i = 0; i < VGA_FONT_GLYPHS; i++) {
uint8_t *glyph = &vga_font_bits[i * vga_font_height]; 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_x = margin + ((gterm_width - margin * 2) % glyph_width) / 2;
offset_y = margin + ((gterm_height - margin * 2) % glyph_height) / 2; offset_y = margin + ((gterm_height - margin * 2) % glyph_height) / 2;
size_t new_grid_size = rows * cols * sizeof(struct gterm_char); grid_size = rows * cols * sizeof(struct gterm_char);
if (new_grid_size > last_grid_size) { grid = ext_mem_alloc(grid_size);
grid = ext_mem_alloc(new_grid_size);
last_grid_size = new_grid_size;
} else {
memset(grid, 0, new_grid_size);
}
size_t new_queue_size = rows * cols * sizeof(struct queue_item); queue_size = rows * cols * sizeof(struct queue_item);
if (new_queue_size > last_queue_size) { queue = ext_mem_alloc(queue_size);
queue = ext_mem_alloc(new_queue_size);
last_queue_size = new_queue_size;
}
queue_i = 0; queue_i = 0;
size_t new_map_size = rows * cols * sizeof(struct queue_item *); map_size = rows * cols * sizeof(struct queue_item *);
if (new_map_size > last_map_size) { map = ext_mem_alloc(map_size);
map = ext_mem_alloc(new_map_size);
last_map_size = new_map_size;
} else {
memset(map, 0, new_map_size);
}
size_t new_bg_canvas_size = gterm_width * gterm_height * sizeof(uint32_t); bg_canvas_size = gterm_width * gterm_height * sizeof(uint32_t);
if (new_bg_canvas_size > last_bg_canvas_size) { bg_canvas = ext_mem_alloc(bg_canvas_size);
bg_canvas = ext_mem_alloc(new_bg_canvas_size);
last_bg_canvas_size = new_bg_canvas_size;
}
gterm_generate_canvas(); gterm_generate_canvas();
gterm_clear(true); gterm_clear(true);
@ -861,11 +837,23 @@ no_load_font:;
return true; 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 gterm_context_size(void) {
uint64_t ret = 0; uint64_t ret = 0;
ret += sizeof(struct context); ret += sizeof(struct context);
ret += last_grid_size; ret += grid_size;
return ret; return ret;
} }
@ -874,14 +862,14 @@ void gterm_context_save(uint64_t ptr) {
memcpy32to64(ptr, (uint64_t)(uintptr_t)&context, sizeof(struct context)); memcpy32to64(ptr, (uint64_t)(uintptr_t)&context, sizeof(struct context));
ptr += 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) { void gterm_context_restore(uint64_t ptr) {
memcpy32to64((uint64_t)(uintptr_t)&context, ptr, sizeof(struct context)); memcpy32to64((uint64_t)(uintptr_t)&context, ptr, sizeof(struct 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++) { for (size_t i = 0; i < (size_t)rows * cols; i++) {
size_t x = i % cols; size_t x = i % cols;

View File

@ -10,6 +10,7 @@ extern struct fb_info fbinfo;
extern bool term_autoflush; extern bool term_autoflush;
bool gterm_init(size_t *_rows, size_t *_cols, size_t width, size_t height); 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_putchar(uint8_t c);
void gterm_clear(bool move); void gterm_clear(bool move);

View File

@ -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; image->y_size = new_y_size;
} }
int open_image(struct image *image, struct file_handle *file) { struct image *image_open(struct file_handle *file) {
image->file = file; struct image *image = ext_mem_alloc(sizeof(struct image));
if (!bmp_open_image(image, file))
return 0;
image->type = IMAGE_TILED; 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));
} }

View File

@ -5,7 +5,7 @@
#include <fs/file.h> #include <fs/file.h>
struct image { struct image {
struct file_handle *file; size_t allocated_size;
size_t x_size; size_t x_size;
size_t y_size; size_t y_size;
int type; 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_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); 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 #endif

View File

@ -8,6 +8,15 @@
bool early_term = false; 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) { void term_vbe(size_t width, size_t height) {
term_backend = NOT_READY; term_backend = NOT_READY;

View File

@ -129,10 +129,6 @@ void term_textmode(void) {
} }
#endif #endif
void term_deinit(void) {
term_backend = NOT_READY;
}
static uint64_t context_size(void) { static uint64_t context_size(void) {
uint64_t ret = 0; uint64_t ret = 0;

View File

@ -199,9 +199,7 @@ static char *config_entry_editor(const char *title, const char *orig_entry) {
char *validation_enabled_config = config_get_value(NULL, 0, "EDITOR_VALIDATION"); char *validation_enabled_config = config_get_value(NULL, 0, "EDITOR_VALIDATION");
if (!strcmp(validation_enabled_config, "no")) validation_enabled = false; if (!strcmp(validation_enabled_config, "no")) validation_enabled = false;
static char *buffer = NULL; char *buffer = ext_mem_alloc(EDITOR_MAX_BUFFER_SIZE);
if (buffer == NULL)
buffer = ext_mem_alloc(EDITOR_MAX_BUFFER_SIZE);
memcpy(buffer, orig_entry, entry_size); memcpy(buffer, orig_entry, entry_size);
buffer[entry_size] = 0; buffer[entry_size] = 0;
@ -438,6 +436,7 @@ refresh:
return buffer; return buffer;
case GETCHAR_ESCAPE: case GETCHAR_ESCAPE:
disable_cursor(); disable_cursor();
pmm_free(buffer, EDITOR_MAX_BUFFER_SIZE);
return NULL; return NULL;
default: default:
if (strlen(buffer) < EDITOR_MAX_BUFFER_SIZE - 1) { if (strlen(buffer) < EDITOR_MAX_BUFFER_SIZE - 1) {

View File

@ -403,6 +403,7 @@ void linux_load(char *config, char *cmdline) {
fread(kernel, kernel_version, setup_header->kernel_version + 0x200, 128); fread(kernel, kernel_version, setup_header->kernel_version + 0x200, 128);
print("linux: Kernel version: %s\n", kernel_version); print("linux: Kernel version: %s\n", kernel_version);
} }
pmm_free(kernel_version, 128);
} }
setup_header->type_of_loader = 0xff; setup_header->type_of_loader = 0xff;

View File

@ -16,7 +16,8 @@ static void dummy_isr(void *p) {
} }
void init_flush_irqs(void) { 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++) { for (size_t i = 0; i < 256; i++) {
dummy_idt[i].offset_lo = (uint16_t)(uintptr_t)dummy_isr; 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); dummy_idt[i].offset_hi = (uint32_t)((uintptr_t)dummy_isr >> 32);
#endif #endif
} }
pmm_free(dummy_idt, dummy_idt_size);
} }
int irq_flush_type = IRQ_NO_FLUSH; int irq_flush_type = IRQ_NO_FLUSH;