Change malloc and related functions to glib ones.

* malloc  -> g_malloc
* realloc -> g_realloc
* free    -> g_free
* strdup  -> g_strdup
* strndup -> g_strndup
This commit is contained in:
John Chen 2017-01-30 11:31:35 +08:00
parent 7037afb2bc
commit 17e2929ee7
8 changed files with 76 additions and 79 deletions

View File

@ -5,7 +5,6 @@
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dunst.h"
#include "notification.h"
@ -132,7 +131,7 @@ static void on_notify(GDBusConnection * connection,
gchar *icon = NULL;
gchar *summary = NULL;
gchar *body = NULL;
Actions *actions = malloc(sizeof(Actions));
Actions *actions = g_malloc(sizeof(Actions));
if(actions == NULL) {
die("Unable to allocate memory", EXIT_FAILURE);
}
@ -309,13 +308,13 @@ static void on_notify(GDBusConnection * connection,
n->progress = (progress < 0 || progress > 100) ? 0 : progress + 1;
n->urgency = urgency;
n->category = category;
n->dbus_client = strdup(sender);
n->dbus_client = g_strdup(sender);
if (actions->count > 0) {
n->actions = actions;
} else {
n->actions = NULL;
g_strfreev(actions->actions);
free(actions);
g_free(actions);
}
for (int i = 0; i < ColLast; i++) {
@ -434,7 +433,7 @@ static void on_name_lost(GDBusConnection * connection,
static RawImage * get_raw_image_from_data_hint(GVariant *icon_data)
{
RawImage *image = malloc(sizeof(RawImage));
RawImage *image = g_malloc(sizeof(RawImage));
GVariant *data_variant;
gsize expected_len;
@ -456,7 +455,7 @@ static RawImage * get_raw_image_from_data_hint(GVariant *icon_data)
" but got a " "length of %" G_GSIZE_FORMAT,
expected_len,
g_variant_get_size (data_variant));
free(image);
g_free(image);
return NULL;
}

View File

@ -12,7 +12,6 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "dbus.h"
@ -328,9 +327,9 @@ int dunst_main(int argc, char *argv[])
if (settings.startup_notification) {
notification *n = notification_create();
n->appname = strdup("dunst");
n->summary = strdup("startup");
n->body = strdup("dunst is up and running");
n->appname = g_strdup("dunst");
n->summary = g_strdup("startup");
n->body = g_strdup("dunst is up and running");
n->progress = 0;
n->timeout = 10;
n->markup = MARKUP_NO;

View File

@ -79,11 +79,11 @@ char *extract_urls(const char *to_match)
start = m.rm_so + (p - to_match);
finish = m.rm_eo + (p - to_match);
char *match = strndup(to_match + start, finish - start);
char *match = g_strndup(to_match + start, finish - start);
urls = string_append(urls, match, "\n");
free(match);
g_free(match);
p += m.rm_eo;
}
@ -162,7 +162,7 @@ void invoke_action(const char *action)
*/
void dispatch_menu_result(const char *input)
{
char *in = strdup(input);
char *in = g_strdup(input);
g_strstrip(in);
switch (in[0]) {
case '#':
@ -177,12 +177,12 @@ void dispatch_menu_result(const char *input)
char *maybe_url = extract_urls(in);
if (maybe_url) {
open_browser(maybe_url);
free(maybe_url);
g_free(maybe_url);
break;
}
}
}
free(in);
g_free(in);
}
/*
@ -215,12 +215,12 @@ void context_menu(void)
int parent_io[2];
if (pipe(child_io) != 0) {
PERR("pipe()", errno);
free(dmenu_input);
g_free(dmenu_input);
return;
}
if (pipe(parent_io) != 0) {
PERR("pipe()", errno);
free(dmenu_input);
g_free(dmenu_input);
return;
}
int pid = fork();
@ -250,7 +250,7 @@ void context_menu(void)
size_t len = read(parent_io[0], buf, 1023);
if (len == 0) {
free(dmenu_input);
g_free(dmenu_input);
return;
}
@ -262,6 +262,6 @@ void context_menu(void)
dispatch_menu_result(buf);
free(dmenu_input);
g_free(dmenu_input);
}
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -168,12 +168,12 @@ int notification_is_duplicate(const notification *a, const notification *b)
void notification_free(notification * n)
{
assert(n != NULL);
free(n->appname);
free(n->summary);
free(n->body);
free(n->icon);
free(n->msg);
free(n->dbus_client);
g_free(n->appname);
g_free(n->summary);
g_free(n->body);
g_free(n->icon);
g_free(n->msg);
g_free(n->dbus_client);
g_free(n->category);
if (n->text_to_render)
@ -184,16 +184,16 @@ void notification_free(notification * n)
if (n->actions) {
g_strfreev(n->actions->actions);
free(n->actions->dmenu_str);
g_free(n->actions->dmenu_str);
}
if (n->raw_icon) {
if (n->raw_icon->data)
free(n->raw_icon->data);
free(n->raw_icon);
g_free(n->raw_icon->data);
g_free(n->raw_icon);
}
free(n);
g_free(n);
}
/*
@ -246,10 +246,10 @@ char *notification_replace_format(const char *needle, const char *replacement,
char* ret;
if (markup_mode == MARKUP_NO) {
tmp = strdup(replacement);
tmp = g_strdup(replacement);
tmp = notification_quote_markup(tmp);
} else {
tmp = strdup(replacement);
tmp = g_strdup(replacement);
if (settings.ignore_newline) {
tmp = string_replace_all("<br>", " ", tmp);
tmp = string_replace_all("<br/>", " ", tmp);
@ -273,7 +273,7 @@ char *notification_replace_format(const char *needle, const char *replacement,
}
ret = string_replace_all(needle, tmp, haystack);
free(tmp);
g_free(tmp);
return ret;
}
@ -286,7 +286,7 @@ char *notification_extract_markup_urls(char **str_ptr) {
while ((start = strstr(str, "<a href")) != NULL) {
end = strstr(start, ">");
if (end != NULL) {
replace_buf = strndup(start, end - start + 1);
replace_buf = g_strndup(start, end - start + 1);
url = extract_urls(replace_buf);
if (url != NULL) {
str = string_replace(replace_buf, "[", str);
@ -297,18 +297,18 @@ char *notification_extract_markup_urls(char **str_ptr) {
} else {
char *tmp = urls;
urls = g_strconcat(tmp, "\n", index_buf, " ", url, NULL);
free(tmp);
g_free(tmp);
}
index_buf[0] = ' ';
str = string_replace("</a>", index_buf, str);
free(index_buf);
free(url);
g_free(index_buf);
g_free(url);
} else {
str = string_replace(replace_buf, "", str);
str = string_replace("</a>", "", str);
}
free(replace_buf);
g_free(replace_buf);
} else {
break;
}
@ -323,7 +323,7 @@ char *notification_extract_markup_urls(char **str_ptr) {
*/
notification *notification_create(void)
{
notification *n = malloc(sizeof(notification));
notification *n = g_malloc(sizeof(notification));
if(n == NULL) die("Unable to allocate memory", EXIT_FAILURE);
memset(n, 0, sizeof(notification));
return n;
@ -398,12 +398,12 @@ int notification_init(notification * n, int id)
n->msg = g_strchomp(n->msg);
if (n->icon != NULL && strlen(n->icon) <= 0) {
free(n->icon);
g_free(n->icon);
n->icon = NULL;
}
if (n->raw_icon == NULL && n->icon == NULL) {
n->icon = strdup(settings.icons[n->urgency]);
n->icon = g_strdup(settings.icons[n->urgency]);
}
if (id == 0) {
@ -432,8 +432,8 @@ int notification_init(notification * n, int id)
/* notifications that differ only in progress hints should be expected equal,
* but we want the latest message, with the latest hint value
*/
free(orig->msg);
orig->msg = strdup(n->msg);
g_free(orig->msg);
orig->msg = g_strdup(n->msg);
notification_free(n);
wake_up();
return orig->id;
@ -447,8 +447,8 @@ int notification_init(notification * n, int id)
/* notifications that differ only in progress hints should be expected equal,
* but we want the latest message, with the latest hint value
*/
free(orig->msg);
orig->msg = strdup(n->msg);
g_free(orig->msg);
orig->msg = g_strdup(n->msg);
/* If the progress differs this was probably intended to replace the notification
* but notify-send was used. So don't increment dup_count in this case
*/
@ -506,7 +506,7 @@ int notification_init(notification * n, int id)
if (tmp_urls != NULL) {
if (n->urls != NULL) {
n->urls = string_append(n->urls, tmp_urls, "\n");
free(tmp_urls);
g_free(tmp_urls);
} else {
n->urls = tmp_urls;
}
@ -522,12 +522,12 @@ int notification_init(notification * n, int id)
char *act_str = g_strdup_printf("#%s [%s]", human_readable, n->appname);
if (act_str) {
n->actions->dmenu_str = string_append(n->actions->dmenu_str, act_str, "\n");
free(act_str);
g_free(act_str);
}
}
}
free(tmp);
g_free(tmp);
if (settings.print_notifications)
notification_print(n);
@ -590,7 +590,7 @@ int notification_close(notification * n, int reason)
void notification_update_text_to_render(notification *n)
{
if (n->text_to_render) {
free(n->text_to_render);
g_free(n->text_to_render);
n->text_to_render = NULL;
}
@ -638,7 +638,7 @@ void notification_update_text_to_render(notification *n)
new_buf = g_strdup_printf("%s (%ds old)", buf, seconds);
}
free(buf);
g_free(buf);
buf = new_buf;
}

View File

@ -48,7 +48,7 @@ section_t *new_section(char *name)
}
section_count++;
sections = realloc(sections, sizeof(section_t) * section_count);
sections = g_realloc(sections, sizeof(section_t) * section_count);
if(sections == NULL) die("Unable to allocate memory.\n", 1);
sections[section_count - 1].name = g_strdup(name);
sections[section_count - 1].entries = NULL;
@ -60,13 +60,13 @@ void free_ini(void)
{
for (int i = 0; i < section_count; i++) {
for (int j = 0; j < sections[i].entry_count; j++) {
free(sections[i].entries[j].key);
free(sections[i].entries[j].value);
g_free(sections[i].entries[j].key);
g_free(sections[i].entries[j].value);
}
free(sections[i].entries);
free(sections[i].name);
g_free(sections[i].entries);
g_free(sections[i].name);
}
free(sections);
g_free(sections);
section_count = 0;
sections = NULL;
}
@ -90,7 +90,7 @@ void add_entry(char *section_name, char *key, char *value)
s->entry_count++;
int len = s->entry_count;
s->entries = realloc(s->entries, sizeof(entry_t) * len);
s->entries = g_realloc(s->entries, sizeof(entry_t) * len);
s->entries[s->entry_count - 1].key = g_strdup(key);
s->entries[s->entry_count - 1].value = clean_value(value);
}
@ -234,7 +234,7 @@ int load_ini_file(FILE * fp)
*end = '\0';
if (current_section)
free(current_section);
g_free(current_section);
current_section = (g_strdup(start + 1));
new_section(current_section);
continue;
@ -282,7 +282,7 @@ int load_ini_file(FILE * fp)
}
free(line);
if (current_section)
free(current_section);
g_free(current_section);
return 0;
}
@ -308,7 +308,7 @@ int cmdline_find_option(char *key)
/* look for first key */
for (int i = 0; i < cmdline_argc; i++) {
if (strcmp(key1, cmdline_argv[i]) == 0) {
free(key1);
g_free(key1);
return i;
}
}
@ -317,13 +317,13 @@ int cmdline_find_option(char *key)
if (key2) {
for (int i = 0; i < cmdline_argc; i++) {
if (strcmp(key2, cmdline_argv[i]) == 0) {
free(key1);
g_free(key1);
return i;
}
}
}
free(key1);
g_free(key1);
return -1;
}
@ -466,16 +466,16 @@ void cmdline_usage_append(char *key, char *type, char *description)
if (!usage_str) {
usage_str =
g_strdup_printf("%-40s - %s\n", key_type, description);
free(key_type);
g_free(key_type);
return;
}
char *tmp;
tmp =
g_strdup_printf("%s%-40s - %s\n", usage_str, key_type, description);
free(key_type);
g_free(key_type);
free(usage_str);
g_free(usage_str);
usage_str = tmp;
}

View File

@ -4,7 +4,6 @@
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef STATIC_CONFIG
#include <basedir.h>
@ -66,7 +65,7 @@ static int ini_get_urgency(char *section, char *key, int def)
urg);
}
if (urg)
free(urg);
g_free(urg);
return ret;
}
@ -114,7 +113,7 @@ void load_settings(char *cmdline_config_path)
);
settings.markup = parse_markup_mode(c);
free(c);
g_free(c);
} else if (ini_is_set("global", "allow_markup")) {
bool allow_markup = option_get_bool(
"global",
@ -180,7 +179,7 @@ void load_settings(char *cmdline_config_path)
if (strlen(c) > 0) {
parse_follow_mode(c);
free(c);
g_free(c);
}
}
@ -243,7 +242,7 @@ void load_settings(char *cmdline_config_path)
else
fprintf(stderr,
"Warning: unknown alignment\n");
free(c);
g_free(c);
}
}
@ -319,7 +318,7 @@ void load_settings(char *cmdline_config_path)
settings.sep_color = CUSTOM;
settings.sep_custom_color_str = g_strdup(c);
}
free(c);
g_free(c);
}
}
@ -375,7 +374,7 @@ void load_settings(char *cmdline_config_path)
else
fprintf(stderr,
"Warning: unknown icon position: %s\n", c);
free(c);
g_free(c);
}
}
@ -576,7 +575,7 @@ void load_settings(char *cmdline_config_path)
if (strlen(c) > 0) {
r->markup = parse_markup_mode(c);
free(c);
g_free(c);
}
}

View File

@ -26,7 +26,7 @@ char *string_replace_at(char *buf, int pos, int len, const char *repl)
if (repl_len <= len) {
tmp = buf;
} else {
tmp = malloc(size);
tmp = g_malloc(size);
}
memcpy(tmp, buf, pos);
@ -34,7 +34,7 @@ char *string_replace_at(char *buf, int pos, int len, const char *repl)
memmove(tmp + pos + repl_len, buf + pos + len, buf_len - (pos + len) + 1);
if(tmp != buf) {
free(buf);
g_free(buf);
}
return tmp;
@ -84,7 +84,7 @@ char *string_append(char *a, const char *b, const char *sep)
new = g_strconcat(a, b, NULL);
else
new = g_strconcat(a, sep, b, NULL);
free(a);
g_free(a);
return new;

View File

@ -336,14 +336,14 @@ static GdkPixbuf *get_pixbuf_from_path(char *icon_path)
end = strchr(start, ':');
if (end == NULL) end = strchr(settings.icon_folders, '\0'); /* end = end of string */
current_folder = strndup(start, end - start);
current_folder = g_strndup(start, end - start);
/* try svg */
maybe_icon_path = g_strconcat(current_folder, "/", icon_path, ".svg", NULL);
if (!does_file_exist(maybe_icon_path)) {
/* fallback to png */
maybe_icon_path = g_strconcat(current_folder, "/", icon_path, ".png", NULL);
}
free(current_folder);
g_free(current_folder);
pixbuf = get_pixbuf_from_file(maybe_icon_path);
g_free(maybe_icon_path);
@ -384,7 +384,7 @@ static GdkPixbuf *get_pixbuf_from_raw_image(const RawImage *raw_image)
static colored_layout *r_init_shared(cairo_t *c, notification *n)
{
colored_layout *cl = malloc(sizeof(colored_layout));
colored_layout *cl = g_malloc(sizeof(colored_layout));
if(cl == NULL) {
die("Unable to allocate memory", EXIT_FAILURE);
}
@ -1418,7 +1418,7 @@ void x_shortcut_init(keyboard_shortcut * ks)
ks->is_valid = true;
}
free(str_begin);
g_free(str_begin);
}
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */