Skip to content

Commit

Permalink
Use OUT_OF_MEMORY macro everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
rgburke committed Sep 25, 2016
1 parent 30050de commit c4508ec
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 150 deletions.
36 changes: 12 additions & 24 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ Status bf_read_file(Buffer *buffer, const FileInfo *file_info)

/* Attempt to allocate necessary memory before loading into gap buffer */
if (!gb_preallocate(buffer->data, new_size)) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - ",
"File is too large to load into memory");
return OUT_OF_MEMORY("File is too large to load into memory");
}

Status status = STATUS_SUCCESS;
Expand All @@ -278,8 +277,7 @@ Status bf_read_file(Buffer *buffer, const FileInfo *file_info)

/* Add text to gap buffer and advance internal point */
if (!gb_add(buffer->data, buf, read)) {
status = st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to populate buffer");
status = OUT_OF_MEMORY("Unable to populate buffer");
break;
}
} while (read == FILE_BUF_SIZE);
Expand Down Expand Up @@ -336,8 +334,7 @@ Status bf_write_file(Buffer *buffer, const char *file_path)
char *tmp_file_path = malloc(tmp_file_path_len);

if (tmp_file_path == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to create temporary file path");
return OUT_OF_MEMORY("Unable to create temporary file path");
}

snprintf(tmp_file_path, tmp_file_path_len, "%sXXXXXX", file_path);
Expand Down Expand Up @@ -1316,8 +1313,7 @@ Status bf_add_new_mark(Buffer *buffer, BufferPos *pos,
Mark *mark = bp_new_mark(pos, prop);

if (mark == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable allocate mark" );
return OUT_OF_MEMORY("Unable allocate mark" );
}

Status status = bf_add_mark(buffer, mark);
Expand All @@ -1341,8 +1337,7 @@ static Status bf_add_mark(Buffer *buffer, Mark *mark)
if (hashmap_get(buffer->marks, addr) != NULL) {
return st_get_error(ERR_DUPLICATE_MARK, "Mark already tracked");
} else if (!hashmap_set(buffer->marks, addr, mark)) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to save mark" );
return OUT_OF_MEMORY("Unable to save mark" );
}

return STATUS_SUCCESS;
Expand Down Expand Up @@ -1389,8 +1384,7 @@ static Status bf_update_marks(Buffer *buffer, const BufferPos *change_pos,
const char **mark_refs = hashmap_get_keys(buffer->marks);

if (mark_refs == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to allocate mark list");
return OUT_OF_MEMORY("Unable to allocate mark list");
}

size_t mark_num = hashmap_size(buffer->marks);
Expand Down Expand Up @@ -1493,8 +1487,7 @@ static Status bf_auto_indent(Buffer *buffer, int advance_cursor)
char *indent = malloc(indent_length + new_line_len);

if (indent == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to insert character");
return OUT_OF_MEMORY("Unable to insert character");
}

gb_get_range(buffer->data, line_start_offset,
Expand Down Expand Up @@ -1586,8 +1579,7 @@ Status bf_insert_string(Buffer *buffer, const char *string,
size_t lines_before = gb_lines(pos->data);

if (!gb_insert(buffer->data, string, string_length)) {
status = st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to insert text");
status = OUT_OF_MEMORY("Unable to insert text");
goto cleanup;
}

Expand Down Expand Up @@ -1683,8 +1675,7 @@ Status bf_delete(Buffer *buffer, size_t byte_num)
deleted_str = malloc(byte_num);

if (deleted_str == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out of memory - "
"Unable to save deletion");
return OUT_OF_MEMORY("Unable to save deletion");
}

gb_get_range(buffer->data, pos->offset, deleted_str, byte_num);
Expand All @@ -1694,8 +1685,7 @@ Status bf_delete(Buffer *buffer, size_t byte_num)
gb_set_point(buffer->data, pos->offset);

if (!gb_delete(buffer->data, byte_num)) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out of memory - "
"Unable to delete character");
return OUT_OF_MEMORY("Unable to delete character");
}

size_t lines_after = gb_lines(buffer->data);
Expand Down Expand Up @@ -1799,8 +1789,7 @@ Status bf_copy_selected_text(Buffer *buffer, TextSelection *text_selection)
text_selection->str = malloc(range_size + 1);

if (text_selection->str == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to copy selected text");
return OUT_OF_MEMORY("Unable to copy selected text");
}

size_t copied = gb_get_range(buffer->data, range.start.offset,
Expand Down Expand Up @@ -1896,8 +1885,7 @@ static Status bf_convert_fileformat(TextSelection *in_ts, TextSelection *out_ts,
}

if (out_ts->str == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to convert fileformat of text");
return OUT_OF_MEMORY("Unable to convert fileformat of text");
}

out_ts->str_len = strlen(out_ts->str);
Expand Down
43 changes: 14 additions & 29 deletions command.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,8 +1124,7 @@ static Status cm_save_file_prompt(Session *sess, char **file_path_ptr)
char *file_path = pr_get_prompt_content(sess->prompt);

if (file_path == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
} else if (*file_path == '\0') {
Status status = st_get_error(ERR_INVALID_FILE_PATH,
"Invalid file path \"%s\"", file_path);
Expand All @@ -1137,9 +1136,7 @@ static Status cm_save_file_prompt(Session *sess, char **file_path_ptr)
free(file_path);

if (processed_path == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY,
"Out of memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
}

*file_path_ptr = processed_path;
Expand Down Expand Up @@ -1197,8 +1194,7 @@ static Status cm_prepare_search(Session *sess, const BufferPos *start_pos,
char *pattern = pr_get_prompt_content(sess->prompt);

if (pattern == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
} else if (*pattern == '\0') {
free(pattern);
return STATUS_SUCCESS;
Expand All @@ -1222,8 +1218,7 @@ static Status cm_prepare_search(Session *sess, const BufferPos *start_pos,
free(pattern);

if (processed_pattern == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
} else {
pattern = processed_pattern;
}
Expand Down Expand Up @@ -1415,8 +1410,7 @@ static Status cm_prepare_replace(Session *sess, char **rep_text_ptr,
char *rep_text = pr_get_prompt_content(sess->prompt);

if (rep_text == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
}

*rep_length = strlen(rep_text);
Expand All @@ -1438,8 +1432,7 @@ static Status cm_prepare_replace(Session *sess, char **rep_text_ptr,
free(rep_text);

if (processed_rep_text == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
}

*rep_text_ptr = processed_rep_text;
Expand Down Expand Up @@ -1523,8 +1516,7 @@ static Status cm_buffer_replace(const CommandArgs *cmd_args)
}

if (response == QR_ERROR) {
status = st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
status = OUT_OF_MEMORY("Unable to process input");
break;
} else if (response == QR_CANCEL) {
break;
Expand Down Expand Up @@ -1607,8 +1599,7 @@ static Status cm_buffer_goto_line(const CommandArgs *cmd_args)
char *input = pr_get_prompt_content(sess->prompt);

if (input == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out of memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
}

Status status = se_add_lineno_to_history(sess, input);
Expand Down Expand Up @@ -1656,8 +1647,7 @@ static Status cm_session_open_file(const CommandArgs *cmd_args)
char *input = pr_get_prompt_content(sess->prompt);

if (input == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
} else if (*input == '\0') {
status = st_get_error(ERR_INVALID_FILE_PATH,
"Invalid file path \"%s\"", input);
Expand Down Expand Up @@ -1796,8 +1786,7 @@ static Status cm_session_close_buffer(const CommandArgs *cmd_args)
QR_YES | QR_NO, QR_YES);

if (response == QR_ERROR) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
} else if (response == QR_CANCEL) {
return STATUS_SUCCESS;
} else if (response == QR_YES) {
Expand Down Expand Up @@ -1840,8 +1829,7 @@ static Status cm_session_run_command(const CommandArgs *cmd_args)
char *input = pr_get_prompt_content(sess->prompt);

if (input == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
}

Status status = se_add_cmd_to_history(sess, input);
Expand Down Expand Up @@ -1901,8 +1889,7 @@ static Status cm_session_change_buffer(const CommandArgs *cmd_args)
char *input = pr_get_prompt_content(sess->prompt);

if (input == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to process input");
return OUT_OF_MEMORY("Unable to process input");
}

Status status = se_add_buffer_to_history(sess, input);
Expand Down Expand Up @@ -2200,8 +2187,7 @@ static Status cm_session_map(const CommandArgs *cmd_args)
free(map_to);

if (key_mapping == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to create key mapping");
return OUT_OF_MEMORY("Unable to create key mapping");
}

KeyMap *key_map = &sess->key_map;
Expand All @@ -2216,8 +2202,7 @@ static Status cm_session_map(const CommandArgs *cmd_args)
}

if (!rt_insert(map, key_mapping->key, key_len, key_mapping)) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to create key mapping");
return OUT_OF_MEMORY("Unable to create key mapping");
}

key_map->active_op_modes[OM_USER] = 1;
Expand Down
9 changes: 3 additions & 6 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ Status cf_init_session_config(Session *sess)
}

if (!cf_populate_config(NULL, config, CL_SESSION)) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to load config");
return OUT_OF_MEMORY("Unable to load config");
}

se_add_error(sess, cf_load_config_if_exists(sess, CFG_SYSTEM_DIR,
Expand Down Expand Up @@ -257,8 +256,7 @@ void cf_load_config_def(Session *sess, ConfigType cf_type,
char *file_name = malloc(file_path_length);

if (file_name == NULL) {
se_add_error(sess, st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - ",
"Unable to load config file"));
se_add_error(sess, OUT_OF_MEMORY("Unable to load config file"));
return;
}

Expand Down Expand Up @@ -317,8 +315,7 @@ static Status cf_path_append(const char *path, const char *append,
char *res = concat(path, append);

if (res == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out of memory - "
"Unable to construct config path");
return OUT_OF_MEMORY("Unable to construct config path");
}

*result = res;
Expand Down
10 changes: 3 additions & 7 deletions config_parse_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,7 @@ static void cp_process_syntax_block(Session *sess,
wed_def = (WedSyntaxDefinition *)ws_new(sess);

if (wed_def == NULL) {
se_add_error(sess, st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to allocate SyntaxDefinition"));
se_add_error(sess, OUT_OF_MEMORY("Unable to allocate SyntaxDefinition"));

goto cleanup;
}
Expand All @@ -940,8 +939,7 @@ static void cp_process_syntax_block(Session *sess,
SyntaxManager *sm = &sess->sm;

if (!hashmap_set(sm->syn_defs, SVAL(name), wed_def)) {
se_add_error(sess, st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to save SyntaxDefinition"));
se_add_error(sess, OUT_OF_MEMORY("Unable to save SyntaxDefinition"));
goto cleanup;
}

Expand Down Expand Up @@ -1045,9 +1043,7 @@ static void cp_process_theme_block(Session *sess, StatementBlockNode *stmb_node)
Theme *theme = th_get_default_theme();

if (theme == NULL) {
se_add_error(sess, st_get_error(ERR_OUT_OF_MEMORY,
"Out Of Memory - ",
"Unable to create Theme"));
se_add_error(sess, OUT_OF_MEMORY("Unable to create theme"));
return;
}

Expand Down
13 changes: 3 additions & 10 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ Status fi_init(FileInfo *file_info, const char *path)
file_info->rel_path = fi_process_path(path);

if (file_info->rel_path == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY,
"Out of memory - Unable to determine"
" fileinfo for file %s",
path);
return OUT_OF_MEMORY("Unable to allocate file path");
}

file_info->file_name = basename(file_info->rel_path);
Expand All @@ -67,10 +64,7 @@ Status fi_init(FileInfo *file_info, const char *path)

if (file_info->abs_path == NULL) {
free(file_info->rel_path);
return st_get_error(ERR_OUT_OF_MEMORY,
"Out of memory - Unable to determine"
" fileinfo for file %s",
path);
return OUT_OF_MEMORY("Unable to allocate file path");
}

/* Get the canonicalized absolute pathname.
Expand Down Expand Up @@ -119,8 +113,7 @@ Status fi_init_stdin(FileInfo *file_info, const char *path)
file_info->abs_path = strdup(path);

if (file_info->abs_path == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out of memory - "
"Unable copy file path");
return OUT_OF_MEMORY("Unable copy file path");
}
}

Expand Down
9 changes: 3 additions & 6 deletions file_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ Status ft_init(FileType **file_type_ptr, const char *name,
FileType *file_type = malloc(sizeof(FileType));

if (file_type == NULL) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to allocate filetype definition");
return OUT_OF_MEMORY("Unable to allocate filetype definition");
}

memset(file_type, 0, sizeof(FileType));
Expand Down Expand Up @@ -63,16 +62,14 @@ Status ft_init(FileType **file_type_ptr, const char *name,
file_type->name = strdup(name);

if (file_type->name == NULL) {
status = st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to allocate filetype definition");
status = OUT_OF_MEMORY("Unable to allocate filetype definition");
goto cleanup;
}

file_type->display_name = strdup(display_name);

if (file_type->display_name == NULL) {
status = st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to allocate filetype definition");
status = OUT_OF_MEMORY("Unable to allocate filetype definition");
goto cleanup;
}

Expand Down
3 changes: 1 addition & 2 deletions input.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ static Status ip_add_keystr_input(InputBuffer *input_buffer, size_t pos,
gb_set_point(buffer, pos);

if (!gb_add(buffer, keystr, keystr_len)) {
return st_get_error(ERR_OUT_OF_MEMORY, "Out Of Memory - "
"Unable to save input");
return OUT_OF_MEMORY("Unable to save input");
}

gb_set_point(buffer, 0);
Expand Down
Loading

0 comments on commit c4508ec

Please sign in to comment.