Skip to content

Commit dea199b

Browse files
authored
Memory allocation efficiencies and code style (#25)
1 parent b6180e3 commit dea199b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/watcher.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,21 @@ static void exec_later(void *data) {
5353
static void process_events(fsw_cevent const *const events, const unsigned int event_num, void *data) {
5454

5555
SEXP callback = (SEXP) data;
56+
watcher_cb *wcb = NULL;
5657

5758
if (callback != R_NilValue) {
5859

59-
watcher_cb *wcb = calloc(1, sizeof(watcher_cb));
60-
if (!wcb) return;
60+
wcb = malloc(sizeof(watcher_cb));
61+
if (wcb == NULL) goto fail;
6162

6263
wcb->event_num = event_num;
63-
wcb->paths = calloc(event_num, sizeof(char *));
64-
if (!wcb->paths) { watcher_unwind(wcb); return; }
6564
wcb->callback = callback;
65+
wcb->paths = calloc(event_num, sizeof(char *));
66+
if (wcb->paths == NULL) goto fail;
6667
for (unsigned int i = 0; i < event_num; i++) {
6768
size_t slen = strlen(events[i].path) + 1;
6869
wcb->paths[i] = malloc(sizeof(char) * slen);
69-
if (!wcb->paths[i]) { watcher_unwind(wcb); return; }
70+
if (wcb->paths[i] == NULL) goto fail;
7071
memcpy(wcb->paths[i], events[i].path, slen);
7172
}
7273
eln2(exec_later, wcb, 0, 0);
@@ -79,6 +80,11 @@ static void process_events(fsw_cevent const *const events, const unsigned int ev
7980

8081
}
8182

83+
return;
84+
85+
fail:
86+
watcher_unwind(wcb);
87+
8288
}
8389

8490
static void* watcher_thread(void *args) {

0 commit comments

Comments
 (0)