Skip to content

Commit

Permalink
Replace uses of xnew(T, 1) with xmalloc(sizeof(*t))
Browse files Browse the repository at this point in the history
For allocating single objects, this macro didn't serve any particular
purpose and only obscured things compared to the more familiar/direct
use of `xmalloc()`.

The array allocation cases have been left as-is, except that the macro
now expands to `xmallocarray(...)` and gains a proper `ALLOC_SIZE()`
annotation.
  • Loading branch information
craigbarnes committed Jan 26, 2025
1 parent c56c76c commit b5505b3
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void add_error_fmt (
int8_t idx[NR_ERRFMT_INDICES],
bool ignore
) {
ErrorFormat *f = xnew(ErrorFormat, 1);
ErrorFormat *f = xmalloc(sizeof(*f));
f->ignore = ignore;
f->re = *re; // Takes ownership (responsible for calling regfree(3))
memcpy(f->capture_index, idx, NR_ERRFMT_INDICES);
Expand Down
2 changes: 1 addition & 1 deletion src/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static mode_t get_umask(void)

EditorState *init_editor_state(EditorFlags flags)
{
EditorState *e = xnew(EditorState, 1);
EditorState *e = xmalloc(sizeof(*e));
*e = (EditorState) {
.status = EDITOR_INITIALIZING,
.flags = flags,
Expand Down
2 changes: 1 addition & 1 deletion src/file-history.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void file_history_append(FileHistory *history, unsigned long row, unsigned long
e = hashmap_remove(map, old_first->filename);
BUG_ON(e != old_first);
} else {
e = xnew(FileHistoryEntry, 1);
e = xmalloc(sizeof(*e));
}
e->filename = xstrdup(filename);
hashmap_insert(map, e->filename, e);
Expand Down
2 changes: 1 addition & 1 deletion src/file-option.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void add_file_options (
BUG_ON(!u.filetype);
}

FileOption *opt = xnew(FileOption, 1);
FileOption *opt = xmalloc(sizeof(*opt));
opt->u = u;
opt->type = type;
opt->strs = copy_string_array(strs, nstrs);
Expand Down
2 changes: 1 addition & 1 deletion src/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void history_append(History *history, const char *text)
e = hashmap_remove(map, old_first->text);
BUG_ON(e != old_first);
} else {
e = xnew(HistoryEntry, 1);
e = xmalloc(sizeof(*e));
}
e->text = xstrdup(text);
hashmap_insert(map, e->text, e);
Expand Down
2 changes: 1 addition & 1 deletion src/regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const InternedRegexp *regexp_intern(ErrorBuffer *ebuf, const char *pattern)
return ir;
}

ir = xnew(InternedRegexp, 1);
ir = xmalloc(sizeof(*ir));
int err = regcomp(&ir->re, pattern, DEFAULT_REGEX_FLAGS | REG_NEWLINE | REG_NOSUB);
if (unlikely(err)) {
regexp_error_msg(ebuf, &ir->re, pattern, err);
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static State *handle_heredoc (
.delim_len = len
};

HeredocState *s = xnew(HeredocState, 1);
HeredocState *s = xmalloc(sizeof(*s));
*s = (HeredocState) {
.state = merge_syntax(syn, &m, sm),
.delim = delim,
Expand Down
4 changes: 2 additions & 2 deletions src/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ static void tag_file_find_tags (
const StringView *name,
PointerArray *tags
) {
Tag *t = xnew(Tag, 1);
Tag *t = xmalloc(sizeof(*t));
size_t pos = 0;
while (next_tag(tf->buf, tf->size, &pos, name, true, t)) {
ptr_array_append(tags, t);
t = xnew(Tag, 1);
t = xmalloc(sizeof(*t));
}
free(t);

Expand Down
9 changes: 8 additions & 1 deletion src/util/xmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "macros.h"

#define XMEMDUP(ptr) xmemdup(ptr, sizeof(*ptr))
#define xnew(type, n) xmalloc(xmul(sizeof(type), (n)))
#define xnew(type, n) xmallocarray((n), sizeof(type))
#define xnew0(type, n) xcalloc((n), sizeof(type))
#define xrenew(mem, n) xreallocarray(mem, (n), sizeof(*mem))

Expand Down Expand Up @@ -40,6 +40,12 @@ static inline size_t xadd3(size_t a, size_t b, size_t c)
return xadd(a, xadd(b, c));
}

XMALLOC ALLOC_SIZE(1, 2)
static inline void *xmallocarray(size_t nmemb, size_t size)
{
return xmalloc(xmul(nmemb, size));
}

RETURNS_NONNULL WARN_UNUSED_RESULT ALLOC_SIZE(2, 3)
static inline void *xreallocarray(void *ptr, size_t nmemb, size_t size)
{
Expand All @@ -65,6 +71,7 @@ static inline char *xstrcut(const char *str, size_t size)
XSTRDUP
static inline char *xstrslice(const char *str, size_t pos, size_t end)
{
BUG_ON(pos > end);
return xstrcut(str + pos, end - pos);
}

Expand Down
2 changes: 1 addition & 1 deletion src/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ View *window_add_buffer(Window *window, Buffer *buffer)
// View::selection and View::select_mode
static_assert(SELECT_NONE == 0);

View *view = xnew(View, 1);
View *view = xmalloc(sizeof(*view));
*view = (View) {
.buffer = buffer,
.window = window,
Expand Down

0 comments on commit b5505b3

Please sign in to comment.