commit 36608c00944e3674d86eb897e70dd9aed78d5fd5
parent b6114dd1dd6a8dd56613ecb5c6cf8ab725f84fe8
Author: lumidify <nobody@lumidify.org>
Date: Sat, 20 Feb 2021 14:36:24 +0100
Improve memory wrappers
Diffstat:
11 files changed, 118 insertions(+), 73 deletions(-)
diff --git a/box.c b/box.c
@@ -89,7 +89,7 @@ ltk_box_draw(ltk_box *box, ltk_rect clip) {
static ltk_box *
ltk_box_create(ltk_window *window, const char *id, ltk_orientation orient) {
- ltk_box *box = ltk_malloc(sizeof(ltk_box), "ltk_box_create");
+ ltk_box *box = ltk_malloc(sizeof(ltk_box));
ltk_fill_widget_defaults(&box->widget, id, window, <k_box_draw,
NULL, <k_box_destroy, 0, LTK_BOX);
@@ -223,7 +223,7 @@ ltk_box_add(ltk_window *window, ltk_widget *widget, ltk_box *box, unsigned short
}
if (box->num_widgets >= box->num_alloc) {
size_t new_size = box->num_alloc > 0 ? box->num_alloc * 2 : 4;
- ltk_widget **new = ltk_realloc(box->widgets, new_size * sizeof(ltk_widget *), "ltk_box_add");
+ ltk_widget **new = ltk_realloc(box->widgets, new_size * sizeof(ltk_widget *));
box->num_alloc = new_size;
box->widgets = new;
}
diff --git a/button.c b/button.c
@@ -237,16 +237,14 @@ ltk_button_mouse_release(ltk_button *button, XEvent event) {
static ltk_button *
ltk_button_create(ltk_window *window, const char *id, const char *text) {
char *text_copy;
- ltk_button *button = ltk_malloc(sizeof(ltk_button), "ltk_button_create");
+ ltk_button *button = ltk_malloc(sizeof(ltk_button));
ltk_fill_widget_defaults(&button->widget, id, window,
<k_button_draw, <k_button_change_state, <k_button_destroy, 1, LTK_BUTTON);
button->widget.mouse_release = <k_button_mouse_release;
button->widget.resize = <k_button_resize;
uint16_t font_size = window->theme.font_size;
- text_copy = strdup(text);
- if (!text_copy)
- ltk_fatal_errno("Unable to allocate button text.\n");
+ text_copy = ltk_strdup(text);
button->tl = ltk_text_line_create(window->xwindow, font_size, text_copy, -1);
int text_w, text_h;
ltk_text_line_get_size(button->tl, &text_w, &text_h);
diff --git a/draw.c b/draw.c
@@ -81,7 +81,7 @@ ltk_draw_draw(ltk_draw *draw) {
static ltk_draw *
ltk_draw_create(ltk_window *window, const char *id, int w, int h, const char *color) {
- ltk_draw *draw = ltk_malloc(sizeof(ltk_draw), ltk_draw_create);
+ ltk_draw *draw = ltk_malloc(sizeof(ltk_draw));
ltk_fill_widget_defaults(&draw->widget, id, window,
<k_draw_draw, NULL, <k_draw_destroy, 1, LTK_DRAW);
diff --git a/grid.c b/grid.c
@@ -110,7 +110,7 @@ ltk_grid_draw(ltk_grid *grid, ltk_rect clip) {
static ltk_grid *
ltk_grid_create(ltk_window *window, const char *id, int rows, int columns) {
- ltk_grid *grid = ltk_malloc(sizeof(ltk_grid), "ltk_grid_create");
+ ltk_grid *grid = ltk_malloc(sizeof(ltk_grid));
ltk_fill_widget_defaults(&grid->widget, id, window, <k_grid_draw,
NULL, <k_grid_destroy, 0, LTK_GRID);
@@ -123,14 +123,14 @@ ltk_grid_create(ltk_window *window, const char *id, int rows, int columns) {
grid->rows = rows;
grid->columns = columns;
- grid->widget_grid = ltk_malloc(rows * columns * sizeof(ltk_widget), "ltk_grid_create");
- grid->row_heights = ltk_malloc(rows * sizeof(int), "ltk_grid_create");
- grid->column_widths = ltk_malloc(rows * sizeof(int), "ltk_grid_create");
- grid->row_weights = ltk_malloc(rows * sizeof(int), "ltk_grid_create");
- grid->column_weights = ltk_malloc(columns * sizeof(int), "ltk_grid_create");
+ grid->widget_grid = ltk_malloc(rows * columns * sizeof(ltk_widget));
+ grid->row_heights = ltk_malloc(rows * sizeof(int));
+ grid->column_widths = ltk_malloc(rows * sizeof(int));
+ grid->row_weights = ltk_malloc(rows * sizeof(int));
+ grid->column_weights = ltk_malloc(columns * sizeof(int));
/* Positions have one extra for the end */
- grid->row_pos = ltk_malloc((rows + 1) * sizeof(int), "ltk_grid_create");
- grid->column_pos = ltk_malloc((columns + 1) * sizeof(int), "ltk_grid_create");
+ grid->row_pos = ltk_malloc((rows + 1) * sizeof(int));
+ grid->column_pos = ltk_malloc((columns + 1) * sizeof(int));
/* FIXME: wow, that's horrible, this should just use memset */
int i;
for (i = 0; i < rows; i++) {
diff --git a/label.c b/label.c
@@ -95,14 +95,12 @@ ltk_label_draw(ltk_label *label, ltk_rect clip) {
static ltk_label *
ltk_label_create(ltk_window *window, const char *id, const char *text) {
char *text_copy;
- ltk_label *label = ltk_malloc(sizeof(ltk_label), "ltk_label_create");
+ ltk_label *label = ltk_malloc(sizeof(ltk_label));
ltk_fill_widget_defaults(&label->widget, id, window,
<k_label_draw, NULL, <k_label_destroy, 1, LTK_LABEL);
uint16_t font_size = window->theme.font_size;
- text_copy = strdup(text);
- if (!text_copy)
- ltk_fatal_errno("Unable to allocate label text.\n");
+ text_copy = ltk_strdup(text);
label->tl = ltk_text_line_create(window->xwindow, font_size, text_copy, -1);
int text_w, text_h;
ltk_text_line_get_size(label->tl, &text_w, &text_h);
diff --git a/ltkd.c b/ltkd.c
@@ -325,7 +325,7 @@ get_sock_path(char *basedir, Window id) {
len = strlen(basedir);
/* FIXME: MAKE SURE THIS IS ACTUALLY BIG ENOUGH! */
- path = ltk_malloc(len + 20, "get_sock_path");
+ path = ltk_malloc(len + 20);
/* FIXME: also check for less than 0 */
if (snprintf(path, len + 20, "%s/%d.sock", basedir, id) >= len + 20)
ltk_fatal("Tell lumidify to fix his code.\n");
@@ -514,11 +514,11 @@ ltk_create_xcolor(ltk_window *window, const char *hex, XColor *col) {
void
ltk_queue_event(ltk_window *window, ltk_event_type type, const char *id, const char *data) {
/* FIXME: make it nicer and safer */
- struct ltk_event_queue *new = ltk_malloc(sizeof(struct ltk_event_queue), "ltk_queue_event");
+ struct ltk_event_queue *new = ltk_malloc(sizeof(struct ltk_event_queue));
new->event_type = type;
int id_len = strlen(id);
int data_len = strlen(data);
- new->data = ltk_malloc(id_len + data_len + 3, "ltk_queue_event");
+ new->data = ltk_malloc(id_len + data_len + 3);
strcpy(new->data, id);
new->data[id_len] = ' ';
strcpy(new->data + id_len + 1, data);
@@ -585,7 +585,7 @@ ltk_create_window(const char *title, int x, int y, unsigned int w, unsigned int
char *theme_path;
XWindowAttributes attrs;
- ltk_window *window = ltk_malloc(sizeof(ltk_window), "ltk_create_window");
+ ltk_window *window = ltk_malloc(sizeof(ltk_window));
window->dpy = XOpenDisplay(NULL);
window->screen = DefaultScreen(window->dpy);
@@ -675,9 +675,7 @@ ltk_window_ini_handler(ltk_window *window, const char *prop, const char *value)
ltk_color_create(window->dpy, window->screen,
window->cm, value, &window->theme.fg);
} else if (strcmp(prop, "font") == 0) {
- window->theme.font = strdup(value);
- if (!window->theme.font)
- ltk_fatal_errno("Unable to allocate copy of font name.\n");
+ window->theme.font = ltk_strdup(value);
} else if (strcmp(prop, "font_size") == 0) {
window->theme.font_size = atoi(value);
}
@@ -703,7 +701,7 @@ static void
ltk_window_setup_theme_defaults(ltk_window *window) {
window->theme.border_width = 0;
window->theme.font_size = 15;
- window->theme.font = "Liberation Mono";
+ window->theme.font = ltk_strdup("Liberation Mono");
ltk_color_create(window->dpy, window->screen,
window->cm, "#000000", &window->theme.bg);
ltk_color_create(window->dpy, window->screen,
@@ -734,9 +732,7 @@ ltk_fill_widget_defaults(ltk_widget *widget, const char *id, ltk_window *window,
void (*destroy) (void *, int), unsigned int needs_redraw,
ltk_widget_type type) {
if (id) {
- widget->id = strdup(id);
- if (!widget->id)
- ltk_fatal_errno("Unable to allocate copy of widget ID.\n");
+ widget->id = ltk_strdup(id);
} else {
widget->id = NULL;
}
@@ -932,9 +928,7 @@ ltk_set_widget(ltk_widget *widget, const char *id) {
khint_t k;
/* apparently, khash requires the string to stay accessible */
/* FIXME: How is this freed? */
- char *tmp = strdup(id);
- if (!tmp)
- ltk_fatal_errno("Unable to add widget to hash table.\n");
+ char *tmp = ltk_strdup(id);
k = kh_put(widget, widget_hash, tmp, &ret);
kh_value(widget_hash, k) = widget;
}
@@ -957,7 +951,7 @@ push_token(struct token_list *tl, char *token) {
if (tl->num_tokens >= tl->num_alloc) {
new_size = (tl->num_alloc * 2) > (tl->num_tokens + 1) ?
(tl->num_alloc * 2) : (tl->num_tokens + 1);
- char **new = ltk_realloc(tl->tokens, new_size * sizeof(char *), "push_token");
+ char **new = ltk_realloc(tl->tokens, new_size * sizeof(char *));
if (!new) return -1;
tl->tokens = new;
tl->num_alloc = new_size;
diff --git a/memory.c b/memory.c
@@ -26,46 +26,84 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdarg.h>
+#include <string.h>
#include "color.h"
#include "ltk.h"
+char *
+ltk_strdup_impl(const char *s) {
+ char *str = strdup(s);
+ if (!str)
+ ltk_fatal("Out of memory.\n");
+ return str;
+}
+
+char *
+ltk_strdup_debug(const char *s, const char *caller, const char *file, int line) {
+ char *str = strdup(s);
+ fprintf(stderr, "DEBUG: strdup %p to %p in %s (%s:%d)\n",
+ s, str, caller, file, line);
+ if (!str)
+ ltk_fatal("Out of memory.\n");
+ return str;
+}
+
void *
-ltk_malloc(size_t size, const char *caller) {
+ltk_malloc_impl(size_t size) {
void *ptr = malloc(size);
if (!ptr)
- ltk_fatal("%s: out of memory.\n", caller);
- #ifdef DEBUG
- fprintf(stderr, "DEBUG: malloc: address %p, %zu bytes.\n", ptr, size);
- #endif
+ ltk_fatal("Out of memory.\n");
+ return ptr;
+}
+
+void *
+ltk_malloc_debug(size_t size, const char *caller, const char *file, int line) {
+ void *ptr = malloc(size);
+ fprintf(stderr, "DEBUG: malloc %p, %zu bytes in %s (%s:%d)\n",
+ ptr, size, caller, file, line);
+ if (!ptr)
+ ltk_fatal("Out of memory.\n");
+ return ptr;
+}
+
+void *
+ltk_calloc_impl(size_t nmemb, size_t size) {
+ void *ptr = calloc(nmemb, size);
+ if (!ptr)
+ ltk_fatal("Out of memory.\n");
return ptr;
}
void *
-ltk_calloc(size_t nmemb, size_t size, const char *caller) {
+ltk_calloc_debug(size_t nmemb, size_t size, const char *caller, const char *file, int line) {
void *ptr = calloc(nmemb, size);
+ fprintf(stderr, "DEBUG: calloc %p, %zu bytes in %s (%s:%d)\n",
+ ptr, size, caller, file, line);
if (!ptr)
- ltk_fatal("%s: out of memory.\n", caller);
- #ifdef DEBUG
- fprintf(stderr, "DEBUG: calloc: address %p, %zu bytes.\n", ptr, size);
- #endif
+ ltk_fatal("Out of memory.\n");
return ptr;
}
void *
-ltk_realloc(void *ptr, size_t size, const char *caller) {
+ltk_realloc_impl(void *ptr, size_t size) {
+ void *new_ptr = realloc(ptr, size);
+ if (!new_ptr)
+ ltk_fatal("Out of memory.\n");
+ return new_ptr;
+}
+
+void *
+ltk_realloc_debug(void *ptr, size_t size, const char *caller, const char *file, int line) {
void *new_ptr = realloc(ptr, size);
+ fprintf(stderr, "DEBUG: realloc %p to %p, %zu bytes in %s (%s:%d)\n",
+ ptr, new_ptr, size, caller, file, line);
if (!new_ptr)
- ltk_fatal("%s: out of memory.\n", caller);
- #ifdef DEBUG
- fprintf(stderr, "DEBUG: realloc: old %p, new %p, %zu bytes.\n", ptr, new_ptr, size);
- #endif
+ ltk_fatal("Out of memory.\n");
return new_ptr;
}
void
-ltk_free(void *ptr) {
- #ifdef DEBUG
- fprintf(stderr, "DEBUG: free: address %p\n", ptr);
- #endif
+ltk_free_debug(void *ptr, const char *caller, const char *file, int line) {
+ fprintf(stderr, "DEBUG: free %p in %s (%s:%d)\n", ptr, caller, file, line);
free(ptr);
}
diff --git a/memory.h b/memory.h
@@ -26,9 +26,31 @@
/* FIXME: Move ltk_warn, etc. to util.* */
-void *ltk_malloc(size_t size, const char *caller);
-void *ltk_calloc(size_t nmemb, size_t size, const char *caller);
-void *ltk_realloc(void *ptr, size_t size, const char *caller);
-void ltk_free(void *ptr);
+/* Requires: <stdlib.h> */
+
+#ifdef DEBUG
+ #define ltk_strdup(s) ltk_strdup_debug(s, __func__, __FILE__, __LINE__)
+ #define ltk_malloc(size) ltk_malloc_debug(size, __func__, __FILE__, __LINE__)
+ #define ltk_calloc(nmemb, size) ltk_calloc_debug(nmemb, size, __func__, __FILE__, __LINE__)
+ #define ltk_realloc(ptr, size) ltk_realloc_debug(ptr, size, __func__, __FILE__, __LINE__)
+ #define ltk_free(ptr) ltk_free_debug(ptr, __func__, __FILE__, __LINE__)
+#else
+ #define ltk_strdup(s) ltk_strdup_impl(s)
+ #define ltk_malloc(size) ltk_malloc_impl(size);
+ #define ltk_calloc(nmemb, size) ltk_calloc_impl(nmemb, size);
+ #define ltk_realloc(ptr, size) ltk_realloc_impl(ptr, size);
+ #define ltk_free(ptr) free(ptr);
+#endif
+
+char *ltk_strdup_impl(const char *s);
+void *ltk_malloc_impl(size_t size);
+void *ltk_calloc_impl(size_t nmemb, size_t size);
+void *ltk_realloc_impl(void *ptr, size_t size);
+
+char *ltk_strdup_debug(const char *s, const char *caller, const char *file, int line);
+void *ltk_malloc_debug(size_t size, const char *caller, const char *file, int line);
+void *ltk_calloc_debug(size_t nmemb, size_t size, const char *caller, const char *file, int line);
+void *ltk_realloc_debug(void *ptr, size_t size, const char *caller, const char *file, int line);
+void ltk_free_debug(void *ptr, const char *caller, const char *file, int line);
#endif /* _LTK_MEMORY_H_ */
diff --git a/scrollbar.c b/scrollbar.c
@@ -222,7 +222,7 @@ ltk_scrollbar_motion_notify(ltk_scrollbar *sc, XEvent event) {
ltk_scrollbar *
ltk_scrollbar_create(ltk_window *window, ltk_orientation orient, void (*callback)(void *), void *data) {
- ltk_scrollbar *sc = ltk_malloc(sizeof(ltk_scrollbar), "ltk_scrollbar_create");
+ ltk_scrollbar *sc = ltk_malloc(sizeof(ltk_scrollbar));
ltk_fill_widget_defaults(sc, NULL, window, <k_scrollbar_draw,
NULL, <k_scrollbar_destroy, 1, LTK_UNKNOWN);
sc->last_mouse_x = sc->last_mouse_y = 0;
diff --git a/text_pango.c b/text_pango.c
@@ -40,9 +40,7 @@ void
ltk_init_text(const char *default_font, Display *dpy, int screen, Colormap cm) {
tm.fontmap = pango_xft_get_font_map(dpy, screen);
tm.context = pango_font_map_create_context(tm.fontmap);
- tm.default_font = strdup(default_font);
- if (!tm.default_font)
- ltk_fatal_errno("Unable to allocate copy of font path.\n");
+ tm.default_font = ltk_strdup(default_font);
tm.dpy = dpy;
tm.screen = screen;
tm.cm = cm;
@@ -50,7 +48,6 @@ ltk_init_text(const char *default_font, Display *dpy, int screen, Colormap cm) {
void
ltk_cleanup_text(void) {
- /* FIXME: strdup, etc. wrapper */
if (tm.default_font) ltk_free(tm.default_font);
/* FIXME: destroy fontmap and context */
}
@@ -64,7 +61,7 @@ LtkTextLine *
ltk_text_line_create(Window window, uint16_t font_size, char *text, int width) {
if (!tm.context)
ltk_err("ltk_text_line_create (pango): text not initialized yet");
- LtkTextLine *line = ltk_malloc(sizeof(LtkTextLine), "ltk_text_line_create");
+ LtkTextLine *line = ltk_malloc(sizeof(LtkTextLine));
line->text = text;
line->font_size = font_size;
line->layout = pango_layout_new(tm.context);
diff --git a/text_stb.c b/text_stb.c
@@ -201,7 +201,7 @@ void
ltk_init_text(const char *default_font, Display *dpy, int screen, Colormap cm) {
tm.fonts_bufsize = 1;
tm.glyph_cache = kh_init(glyphcache);
- tm.fonts = ltk_malloc(sizeof(LtkFont *), "ltk_init_text");
+ tm.fonts = ltk_malloc(sizeof(LtkFont *));
ltk_load_default_font(default_font);
tm.dpy = dpy;
tm.screen = screen;
@@ -224,7 +224,7 @@ ltk_cleanup_text(void) {
static LtkGlyphInfo *
ltk_create_glyph_info(LtkFont *font, int id, float scale) {
- LtkGlyphInfo *glyph = ltk_malloc(sizeof(LtkGlyphInfo), "ltk_create_glyph_info");
+ LtkGlyphInfo *glyph = ltk_malloc(sizeof(LtkGlyphInfo));
glyph->id = id;
glyph->refs = 0;
@@ -320,7 +320,7 @@ ltk_load_default_font(char *name) {
static LtkFont *
ltk_create_font(char *path, uint16_t id, int index) {
unsigned long len;
- LtkFont *font = ltk_malloc(sizeof(LtkFont), "ltk_create_font");
+ LtkFont *font = ltk_malloc(sizeof(LtkFont));
char *contents = ltk_read_file(path, &len);
if (!contents)
ltk_fatal_errno("Unable to read font file %s\n", path);
@@ -330,9 +330,7 @@ ltk_create_font(char *path, uint16_t id, int index) {
font->id = id;
font->refs = 0;
font->index = index;
- font->path = strdup(path);
- if (!font->path)
- ltk_fatal_errno("Unable to allocate copy of font path.\n");
+ font->path = ltk_strdup(path);
return font;
}
@@ -347,7 +345,7 @@ static LtkFont *
ltk_load_font(char *path, int index) {
LtkFont *font = ltk_create_font(path, tm.font_id_cur++, index);
if (tm.num_fonts == tm.fonts_bufsize) {
- LtkFont *new = ltk_realloc(tm.fonts, tm.fonts_bufsize * 2 * sizeof(LtkFont *), "ltk_load_font");
+ LtkFont *new = ltk_realloc(tm.fonts, tm.fonts_bufsize * 2 * sizeof(LtkFont *));
tm.fonts = new;
tm.fonts_bufsize *= 2;
}
@@ -471,7 +469,7 @@ ltk_unref_glyphs(ltk_glyph *glyphs, int num_glyphs) {
static XImage *
ltk_create_ximage(int w, int h, int depth, XColor bg) {
XImage *img = XCreateImage(tm.dpy, CopyFromParent, depth, ZPixmap, 0, NULL, w, h, 32, 0);
- img->data = ltk_calloc(img->bytes_per_line, img->height, "ltk_create_ximage");
+ img->data = ltk_calloc(img->bytes_per_line, img->height);
XInitImage(img);
int b;
@@ -569,12 +567,12 @@ ltk_text_line_create_glyphs(LtkTextLine *tl) {
LtkTextLine *
ltk_text_line_create(Window window, uint16_t font_size, char *text, int width) {
- LtkTextLine *line = ltk_malloc(sizeof(LtkTextLine), "ltk_text_line_create");
+ LtkTextLine *line = ltk_malloc(sizeof(LtkTextLine));
line->window = window;
line->img = NULL;
line->text = text;
line->glyph_len = u8_strlen(text);
- line->glyphs = ltk_malloc(line->glyph_len * sizeof(LtkGlyph), "ltk_text_line_create");
+ line->glyphs = ltk_malloc(line->glyph_len * sizeof(LtkGlyph));
line->font_size = font_size;
ltk_text_line_create_glyphs(line);
return line;