Commit 0fc5b801 authored by Moritz Lipp's avatar Moritz Lipp

Replace glib memory functions with try version

parent bafbedbc
......@@ -48,7 +48,10 @@ zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page)
return old;
}
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t));
zathura_bookmark_t* bookmark = g_try_malloc0(sizeof(zathura_bookmark_t));
if (bookmark == NULL) {
return NULL;
}
bookmark->id = g_strdup(id);
bookmark->page = page;
......
......@@ -400,7 +400,10 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu
}
}
girara_argument_t* arg = g_malloc0(sizeof(girara_argument_t));
girara_argument_t* arg = g_try_malloc0(sizeof(girara_argument_t));
if (arg == NULL) {
return false;
}
arg->n = FORWARD;
sc_search(session, arg, NULL, 0);
......
......@@ -134,7 +134,11 @@ guess_type_glib(const char* path)
g_free((void*)content_type);
content_type = NULL;
content = g_realloc(content, length + BUFSIZ);
content = g_try_realloc(content, length + BUFSIZ);
if (content == NULL) {
continue;
}
bytes_read = read(fd, content + length, BUFSIZ);
if (bytes_read == -1) {
break;
......
......@@ -326,13 +326,27 @@ plain_add_bookmark(zathura_database_t* db, const char* file,
return false;
}
char* bmx = g_try_malloc(G_ASCII_DTOSTR_BUF_SIZE);
if (bmx == NULL) {
return false;
}
char* bmy = g_try_malloc(G_ASCII_DTOSTR_BUF_SIZE);
if (bmy == NULL) {
g_free(bmx);
return false;
}
char* name = prepare_filename(file);
char* val_list[] = {
g_strdup_printf("%d", bookmark->page),
g_ascii_dtostr(g_malloc(G_ASCII_DTOSTR_BUF_SIZE), G_ASCII_DTOSTR_BUF_SIZE, bookmark->x),
g_ascii_dtostr(g_malloc(G_ASCII_DTOSTR_BUF_SIZE), G_ASCII_DTOSTR_BUF_SIZE, bookmark->y)
g_ascii_dtostr(bmx, G_ASCII_DTOSTR_BUF_SIZE, bookmark->x),
g_ascii_dtostr(bmy, G_ASCII_DTOSTR_BUF_SIZE, bookmark->y)
};
g_free(bmx);
g_free(bmy);
g_key_file_set_string_list(priv->bookmarks, name, bookmark->id, (const char**)val_list, LENGTH(val_list));
for (unsigned int i = 0; i < LENGTH(val_list); ++i) {
......@@ -397,7 +411,10 @@ plain_load_bookmarks(zathura_database_t* db, const char* file)
gsize num_vals = 0;
for (gsize i = 0; i < num_keys; i++) {
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t));
zathura_bookmark_t* bookmark = g_try_malloc0(sizeof(zathura_bookmark_t));
if (bookmark == NULL) {
continue;
}
bookmark->id = g_strdup(keys[i]);
char **val_list = g_key_file_get_string_list(priv->bookmarks, name, keys[i], &num_vals, NULL);
......@@ -439,7 +456,10 @@ get_jumplist_from_str(const char* str)
char* token = strtok_r(copy, " ", &saveptr);
while (token != NULL) {
zathura_jump_t* jump = g_malloc0(sizeof(zathura_jump_t));
zathura_jump_t* jump = g_try_malloc0(sizeof(zathura_jump_t));
if (jump == NULL) {
continue;
}
jump->page = strtoul(token, NULL, 0);
token = strtok_r(NULL, " ", &saveptr);
......
......@@ -390,7 +390,10 @@ sqlite_load_bookmarks(zathura_database_t* db, const char* file)
(girara_free_function_t) zathura_bookmark_free);
while (sqlite3_step(stmt) == SQLITE_ROW) {
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t));
zathura_bookmark_t* bookmark = g_try_malloc0(sizeof(zathura_bookmark_t));
if (bookmark == NULL) {
continue;
}
bookmark->id = g_strdup((const char*) sqlite3_column_text(stmt, 0));
bookmark->page = sqlite3_column_int(stmt, 1);
......@@ -524,7 +527,10 @@ sqlite_load_jumplist(zathura_database_t* db, const char* file)
int res = 0;
while ((res = sqlite3_step(stmt)) == SQLITE_ROW) {
zathura_jump_t* jump = g_malloc0(sizeof(zathura_jump_t));
zathura_jump_t* jump = g_try_malloc0(sizeof(zathura_jump_t));
if (jump == NULL) {
continue;
}
jump->page = sqlite3_column_int(stmt, 0);
jump->x = sqlite3_column_double(stmt, 1);
......
......@@ -289,13 +289,20 @@ handle_method_call(GDBusConnection* UNUSED(connection),
}
/* get rectangles */
girara_list_t** rectangles = g_malloc0(number_of_pages * sizeof(girara_list_t*));
girara_list_t** rectangles = g_try_malloc0(number_of_pages * sizeof(girara_list_t*));
if (rectangles == NULL) {
return;
}
rectangles[page] = girara_list_new2(g_free);
zathura_rectangle_t temp_rect;
while (g_variant_iter_loop(iter, "(dddd)", &temp_rect.x1, &temp_rect.x2,
&temp_rect.y1, &temp_rect.y2)) {
zathura_rectangle_t* rect = g_malloc0(sizeof(zathura_rectangle_t));
zathura_rectangle_t* rect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rect == NULL) {
continue;
}
*rect = temp_rect;
girara_list_append(rectangles[page], rect);
}
......@@ -314,7 +321,10 @@ handle_method_call(GDBusConnection* UNUSED(connection),
rectangles[temp_page] = girara_list_new2(g_free);
}
zathura_rectangle_t* rect = g_malloc0(sizeof(zathura_rectangle_t));
zathura_rectangle_t* rect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rect == NULL) {
continue;
}
*rect = temp_rect;
girara_list_append(rectangles[temp_page], rect);
}
......
......@@ -104,11 +104,19 @@ zathura_document_open(zathura_plugin_manager_t* plugin_manager, const char*
if (plugin == NULL) {
girara_error("unknown file type\n");
*error = ZATHURA_ERROR_UNKNOWN;
if (error != NULL) {
*error = ZATHURA_ERROR_UNKNOWN;
}
goto error_free;
}
document = g_malloc0(sizeof(zathura_document_t));
document = g_try_malloc0(sizeof(zathura_document_t));
if (document == NULL) {
if (error != NULL) {
*error = ZATHURA_ERROR_OUT_OF_MEMORY;
}
goto error_free;
}
document->file_path = real_path;
document->basename = g_path_get_basename(real_path);
......
......@@ -28,7 +28,10 @@ zathura_link_t*
zathura_link_new(zathura_link_type_t type, zathura_rectangle_t position,
zathura_link_target_t target)
{
zathura_link_t* link = g_malloc0(sizeof(zathura_link_t));
zathura_link_t* link = g_try_malloc0(sizeof(zathura_link_t));
if (link == NULL) {
return NULL;
}
link->type = type;
link->position = position;
......
......@@ -215,7 +215,10 @@ mark_add(zathura_t* zathura, int key)
GIRARA_LIST_FOREACH_END(zathura->global.marks, zathura_mark_t*, iter, mark);
/* add new mark */
zathura_mark_t* mark = g_malloc0(sizeof(zathura_mark_t));
zathura_mark_t* mark = g_try_malloc0(sizeof(zathura_mark_t));
if (mark == NULL) {
return NULL;
}
mark->key = key;
mark->page = page_id;
......
......@@ -31,7 +31,13 @@ zathura_page_new(zathura_document_t* document, unsigned int index, zathura_error
}
/* init page */
zathura_page_t* page = g_malloc0(sizeof(zathura_page_t));
zathura_page_t* page = g_try_malloc0(sizeof(zathura_page_t));
if (page == NULL) {
if (error != NULL) {
*error = ZATHURA_ERROR_OUT_OF_MEMORY;
}
goto error_ret;
}
page->index = index;
page->visible = false;
......@@ -45,7 +51,7 @@ zathura_page_new(zathura_document_t* document, unsigned int index, zathura_error
if (error != NULL) {
*error = ZATHURA_ERROR_NOT_IMPLEMENTED;
}
goto error_ret;
goto error_free;
}
zathura_error_t ret = functions->page_init(page);
......
......@@ -54,7 +54,10 @@ static void zathura_type_plugin_mapping_free(zathura_type_plugin_mapping_t* mapp
zathura_plugin_manager_t*
zathura_plugin_manager_new()
{
zathura_plugin_manager_t* plugin_manager = g_malloc0(sizeof(zathura_plugin_manager_t));
zathura_plugin_manager_t* plugin_manager = g_try_malloc0(sizeof(zathura_plugin_manager_t));
if (plugin_manager == NULL) {
return NULL;
}
plugin_manager->plugins = girara_list_new2((girara_free_function_t) zathura_plugin_free);
plugin_manager->path = girara_list_new2(g_free);
......@@ -159,7 +162,11 @@ zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
continue;
}
plugin = g_malloc0(sizeof(zathura_plugin_t));
plugin = g_try_malloc0(sizeof(zathura_plugin_t));
if (plugin == NULL) {
continue;
}
plugin->content_types = girara_list_new2(g_free);
plugin->handle = handle;
......@@ -294,7 +301,11 @@ plugin_mapping_new(zathura_plugin_manager_t* plugin_manager, const gchar* type,
}
GIRARA_LIST_FOREACH_END(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
zathura_type_plugin_mapping_t* mapping = g_malloc(sizeof(zathura_type_plugin_mapping_t));
zathura_type_plugin_mapping_t* mapping = g_try_malloc(sizeof(zathura_type_plugin_mapping_t));
if (mapping == NULL) {
return false;
}
mapping->type = g_strdup(type);
mapping->plugin = plugin;
girara_list_append(plugin_manager->type_plugin_mapping, mapping);
......
......@@ -117,14 +117,20 @@ zathura_renderer_init(ZathuraRenderer* renderer)
priv->requests = girara_list_new();
}
static void
static bool
page_cache_init(ZathuraRenderer* renderer, size_t cache_size)
{
private_t* priv = GET_PRIVATE(renderer);
priv->page_cache.size = cache_size;
priv->page_cache.cache = g_malloc(cache_size * sizeof(int));
priv->page_cache.cache = g_try_malloc(cache_size * sizeof(int));
if (priv->page_cache.cache == NULL) {
return false;
}
page_cache_invalidate_all(renderer);
return true;
}
ZathuraRenderer*
......@@ -134,7 +140,10 @@ zathura_renderer_new(size_t cache_size)
GObject* obj = g_object_new(ZATHURA_TYPE_RENDERER, NULL);
ZathuraRenderer* ret = ZATHURA_RENDERER(obj);
page_cache_init(ret, cache_size);
if (page_cache_init(ret, cache_size) == false) {
return NULL;
}
return ret;
}
......@@ -409,7 +418,11 @@ zathura_render_request(ZathuraRenderRequest* request, gint64 last_view_time)
if (unfinished_jobs == false) {
request_priv->last_view_time = last_view_time;
render_job_t* job = g_malloc0(sizeof(render_job_t));
render_job_t* job = g_try_malloc0(sizeof(render_job_t));
if (job == NULL) {
return;
}
job->request = g_object_ref(request);
job->aborted = false;
girara_list_append(request_priv->active_jobs, job);
......@@ -683,7 +696,11 @@ render(render_job_t* job, ZathuraRenderRequest* request, ZathuraRenderer* render
recolor(priv, page_width, page_height, surface);
}
emit_completed_signal_t* ecs = g_malloc(sizeof(emit_completed_signal_t));
emit_completed_signal_t* ecs = g_try_malloc(sizeof(emit_completed_signal_t));
if (ecs == NULL) {
return false;
}
ecs->job = job;
ecs->surface = cairo_surface_reference(surface);
......
......@@ -63,8 +63,12 @@ synctex_edit(zathura_t* zathura, zathura_page_t* page, int x, int y)
return;
}
char** argv = g_malloc0(sizeof(char*) * (zathura->synctex.editor != NULL ?
char** argv = g_try_malloc0(sizeof(char*) * (zathura->synctex.editor != NULL ?
6 : 4));
if (argv == NULL) {
return;
}
argv[0] = g_strdup("synctex");
argv[1] = g_strdup("edit");
argv[2] = g_strdup("-o");
......@@ -101,7 +105,11 @@ synctex_rectangles_from_position(const char* filename, const char* position,
return NULL;
}
char** argv = g_malloc0(sizeof(char*) * 6);
char** argv = g_try_malloc0(sizeof(char*) * 6);
if (argv == NULL) {
return NULL;
}
argv[0] = g_strdup("synctex");
argv[1] = g_strdup("view");
argv[2] = g_strdup("-i");
......@@ -180,31 +188,47 @@ synctex_rectangles_from_position(const char* filename, const char* position,
girara_list_append(hitlist, rectangle);
rectangle = NULL;
} else if (rectangle != NULL) {
synctex_page_rect_t* page_rect = g_malloc0(sizeof(synctex_page_rect_t));
synctex_page_rect_t* page_rect = g_try_malloc0(sizeof(synctex_page_rect_t));
if (page_rect == NULL) {
continue;
}
page_rect->page = current_page;
page_rect->rect = *rectangle;
girara_list_append(other_rects, page_rect);
}
g_free(rectangle);
rectangle = g_malloc0(sizeof(zathura_rectangle_t));
rectangle = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rectangle == NULL) {
continue;
}
}
break;
case SYNCTEX_PROP_H:
rectangle->x1 = scan_float(scanner);
if (rectangle != NULL) {
rectangle->x1 = scan_float(scanner);
}
break;
case SYNCTEX_PROP_V:
rectangle->y2 = scan_float(scanner);
if (rectangle != NULL) {
rectangle->y2 = scan_float(scanner);
}
break;
case SYNCTEX_PROP_WIDTH:
rectangle->x2 = rectangle->x1 + scan_float(scanner);
if (rectangle != NULL) {
rectangle->x2 = rectangle->x1 + scan_float(scanner);
}
break;
case SYNCTEX_PROP_HEIGHT:
rectangle->y1 = rectangle->y2 - scan_float(scanner);
if (rectangle != NULL) {
rectangle->y1 = rectangle->y2 - scan_float(scanner);
}
break;
}
break;
......@@ -218,11 +242,13 @@ synctex_rectangles_from_position(const char* filename, const char* position,
if (current_page == rpage) {
girara_list_append(hitlist, rectangle);
} else {
synctex_page_rect_t* page_rect = g_malloc0(sizeof(synctex_page_rect_t));
page_rect->page = current_page;
page_rect->rect = *rectangle;
girara_list_append(other_rects, page_rect);
g_free(rectangle);
synctex_page_rect_t* page_rect = g_try_malloc0(sizeof(synctex_page_rect_t));
if (page_rect != NULL) {
page_rect->page = current_page;
page_rect->rect = *rectangle;
girara_list_append(other_rects, page_rect);
g_free(rectangle);
}
}
}
......
......@@ -15,7 +15,10 @@ zathura_index_element_new(const char* title)
return NULL;
}
zathura_index_element_t* res = g_malloc0(sizeof(zathura_index_element_t));
zathura_index_element_t* res = g_try_malloc0(sizeof(zathura_index_element_t));
if (res == NULL) {
return NULL;
}
res->title = g_strdup(title);
......@@ -86,7 +89,10 @@ zathura_document_information_entry_new(zathura_document_information_type_t type,
}
zathura_document_information_entry_t* entry =
g_malloc0(sizeof(zathura_document_information_entry_t));
g_try_malloc0(sizeof(zathura_document_information_entry_t));
if (entry == NULL) {
return NULL;
}
entry->type = type;
entry->value = g_strdup(value);
......
......@@ -225,7 +225,11 @@ replace_substring(const char* string, const char* old, const char* new)
return NULL;
}
char* ret = g_malloc0(sizeof(char) * (i - count * old_len + count * new_len + 1));
char* ret = g_try_malloc0(sizeof(char) * (i - count * old_len + count * new_len + 1));
if (ret == NULL) {
return NULL;
}
i = 0;
/* replace */
......@@ -249,7 +253,10 @@ GdkAtom* get_selection(zathura_t* zathura)
char* value;
girara_setting_get(zathura->ui.session, "selection-clipboard", &value);
GdkAtom* selection = g_malloc(sizeof(GdkAtom));
GdkAtom* selection = g_try_malloc(sizeof(GdkAtom));
if (selection == NULL) {
return NULL;
}
if (strcmp(value, "primary") == 0) {
*selection = GDK_SELECTION_PRIMARY;
......
......@@ -53,7 +53,10 @@ static void zathura_jumplist_save(zathura_t* zathura);
zathura_t*
zathura_create(void)
{
zathura_t* zathura = g_malloc0(sizeof(zathura_t));
zathura_t* zathura = g_try_malloc0(sizeof(zathura_t));
if (zathura == NULL) {
return NULL;
}
/* global settings */
zathura->global.search_direction = FORWARD;
......@@ -821,7 +824,10 @@ document_open_idle(zathura_t* zathura, const char* path, const char* password,
return;
}
zathura_document_info_t* document_info = g_malloc0(sizeof(zathura_document_info_t));
zathura_document_info_t* document_info = g_try_malloc0(sizeof(zathura_document_info_t));
if (document_info == NULL) {
return;
}
document_info->zathura = zathura;
document_info->path = path;
......@@ -1245,7 +1251,11 @@ zathura_jumplist_append_jump(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->jumplist.list != NULL);
zathura_jump_t *jump = g_malloc(sizeof(zathura_jump_t));
zathura_jump_t *jump = g_try_malloc(sizeof(zathura_jump_t));
if (jump == NULL) {
return;
}
jump->page = 0;
jump->x = 0.0;
jump->y = 0.0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment