common: Add additional memory utilities (xzalloc() etc.)

This commit is contained in:
John Lindgren 2022-09-16 18:41:02 -04:00
parent b89f7bfc0d
commit cb40cdc36c
35 changed files with 193 additions and 167 deletions

View file

@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
#include <wlr/util/log.h>
#include "common/mem.h"
#include "config/keybind.h"
#include "config/rcxml.h"
@ -27,7 +28,7 @@ parse_modifier(const char *symname)
struct keybind *
keybind_create(const char *keybind)
{
struct keybind *k = calloc(1, sizeof(struct keybind));
struct keybind *k = xzalloc(sizeof(struct keybind));
xkb_keysym_t keysyms[MAX_KEYSYMS];
gchar **symnames = g_strsplit(keybind, "-", -1);
for (int i = 0; symnames[i]; i++) {
@ -60,7 +61,7 @@ keybind_create(const char *keybind)
return NULL;
}
wl_list_insert(rc.keybinds.prev, &k->link);
k->keysyms = malloc(k->keysyms_len * sizeof(xkb_keysym_t));
k->keysyms = xmalloc(k->keysyms_len * sizeof(xkb_keysym_t));
memcpy(k->keysyms, keysyms, k->keysyms_len * sizeof(xkb_keysym_t));
wl_list_init(&k->actions);
return k;

View file

@ -2,8 +2,10 @@
#include <string.h>
#include <strings.h>
#include "common/mem.h"
#include "config/libinput.h"
#include "config/rcxml.h"
static void
libinput_category_init(struct libinput_category *l)
{
@ -36,10 +38,7 @@ get_device_type(const char *s)
struct libinput_category *
libinput_category_create(void)
{
struct libinput_category *l = calloc(1, sizeof(struct libinput_category));
if (!l) {
return NULL;
}
struct libinput_category *l = xzalloc(sizeof(struct libinput_category));
libinput_category_init(l);
wl_list_insert(&rc.libinput_categories, &l->link);
return l;

View file

@ -5,6 +5,7 @@
#include <strings.h>
#include <unistd.h>
#include <wlr/util/log.h>
#include "common/mem.h"
#include "config/mousebind.h"
#include "config/rcxml.h"
@ -108,7 +109,7 @@ mousebind_create(const char *context)
wlr_log(WLR_ERROR, "mousebind context not specified");
return NULL;
}
struct mousebind *m = calloc(1, sizeof(struct mousebind));
struct mousebind *m = xzalloc(sizeof(struct mousebind));
m->context = context_from_str(context);
if (m->context != LAB_SSD_NONE) {
wl_list_insert(rc.mousebinds.prev, &m->link);

View file

@ -14,9 +14,9 @@
#include <wayland-server-core.h>
#include <wlr/util/log.h>
#include "action.h"
#include "common/mem.h"
#include "common/nodename.h"
#include "common/string-helpers.h"
#include "common/zfree.h"
#include "config/keybind.h"
#include "config/libinput.h"
#include "config/mousebind.h"
@ -194,7 +194,7 @@ fill_libinput_category(char *nodename, char *content)
|| !strcmp(content, "default")) {
current_libinput_category->type = get_device_type(content);
} else {
current_libinput_category->name = strdup(content);
current_libinput_category->name = xstrdup(content);
}
} else if (!strcasecmp(nodename, "naturalScroll")) {
current_libinput_category->natural_scroll =
@ -241,7 +241,7 @@ set_font_attr(struct font *font, const char *nodename, const char *content)
{
if (!strcmp(nodename, "name")) {
zfree(font->name);
font->name = strdup(content);
font->name = xstrdup(content);
} else if (!strcmp(nodename, "size")) {
font->size = atoi(content);
} else if (!strcmp(nodename, "slant")) {
@ -361,7 +361,7 @@ entry(xmlNode *node, char *nodename, char *content)
} else if (!strcasecmp(nodename, "adaptiveSync.core")) {
rc.adaptive_sync = get_bool(content);
} else if (!strcmp(nodename, "name.theme")) {
rc.theme_name = strdup(content);
rc.theme_name = xstrdup(content);
} else if (!strcmp(nodename, "cornerradius.theme")) {
rc.corner_radius = atoi(content);
} else if (!strcmp(nodename, "name.font.theme")) {
@ -401,8 +401,8 @@ entry(xmlNode *node, char *nodename, char *content)
} else if (!strcasecmp(nodename, "cycleViewOutlines.core")) {
rc.cycle_preview_outlines = get_bool(content);
} else if (!strcasecmp(nodename, "name.names.desktops")) {
struct workspace *workspace = calloc(1, sizeof(struct workspace));
workspace->name = strdup(content);
struct workspace *workspace = xzalloc(sizeof(struct workspace));
workspace->name = xstrdup(content);
wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link);
} else if (!strcasecmp(nodename, "popupTime.desktops")) {
rc.workspace_config.popuptime = atoi(content);
@ -671,13 +671,13 @@ post_processing(void)
merge_mouse_bindings();
if (!rc.font_activewindow.name) {
rc.font_activewindow.name = strdup("sans");
rc.font_activewindow.name = xstrdup("sans");
}
if (!rc.font_menuitem.name) {
rc.font_menuitem.name = strdup("sans");
rc.font_menuitem.name = xstrdup("sans");
}
if (!rc.font_osd.name) {
rc.font_osd.name = strdup("sans");
rc.font_osd.name = xstrdup("sans");
}
if (!wl_list_length(&rc.libinput_categories)) {
/* So we still allow tap to click by default */
@ -685,8 +685,8 @@ post_processing(void)
l->type = DEFAULT_DEVICE;
}
if (!wl_list_length(&rc.workspace_config.workspaces)) {
struct workspace *workspace = calloc(1, sizeof(struct workspace));
workspace->name = strdup("Default");
struct workspace *workspace = xzalloc(sizeof(struct workspace));
workspace->name = xstrdup("Default");
wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link);
}
if (rc.workspace_config.popuptime == INT_MIN) {

View file

@ -8,6 +8,7 @@
#include <sys/stat.h>
#include <wlr/util/log.h>
#include "common/buf.h"
#include "common/mem.h"
#include "common/spawn.h"
#include "common/string-helpers.h"
@ -78,10 +79,7 @@ build_path(const char *dir, const char *filename)
return NULL;
}
int len = strlen(dir) + strlen(filename) + 2;
char *buffer = calloc(1, len);
if (!buffer) {
return NULL;
}
char *buffer = xzalloc(len);
strcat(buffer, dir);
strcat(buffer, "/");
strcat(buffer, filename);
@ -103,25 +101,17 @@ update_activation_env(const char *env_keys)
const char *dbus = "dbus-update-activation-environment ";
const char *systemd = "systemctl --user import-environment ";
cmd = calloc(1, strlen(dbus) + strlen(env_keys) + 1);
if (!cmd) {
wlr_log(WLR_ERROR, "Failed to allocate memory for dbus env update");
} else {
strcat(cmd, dbus);
strcat(cmd, env_keys);
spawn_async_no_shell(cmd);
free(cmd);
}
cmd = xzalloc(strlen(dbus) + strlen(env_keys) + 1);
strcat(cmd, dbus);
strcat(cmd, env_keys);
spawn_async_no_shell(cmd);
free(cmd);
cmd = calloc(1, strlen(systemd) + strlen(env_keys) + 1);
if (!cmd) {
wlr_log(WLR_ERROR, "Failed to allocate memory for systemd env update");
} else {
strcat(cmd, systemd);
strcat(cmd, env_keys);
spawn_async_no_shell(cmd);
free(cmd);
}
cmd = xzalloc(strlen(systemd) + strlen(env_keys) + 1);
strcat(cmd, systemd);
strcat(cmd, env_keys);
spawn_async_no_shell(cmd);
free(cmd);
}
void
@ -158,11 +148,7 @@ session_autostart_init(const char *dir)
}
wlr_log(WLR_INFO, "run autostart file %s", autostart);
int len = strlen(autostart) + 4;
char *cmd = calloc(1, len);
if (!cmd) {
wlr_log(WLR_ERROR, "Failed to allocate memory for autostart command");
goto out;
}
char *cmd = xzalloc(len);
strcat(cmd, "sh ");
strcat(cmd, autostart);
spawn_async_no_shell(cmd);