Skip to content

Commit 988eef4

Browse files
committed
Moved memory functions to their own module, making explicit that they can fail
1 parent 946d870 commit 988eef4

File tree

21 files changed

+328
-336
lines changed

21 files changed

+328
-336
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
1414
include_directories(include)
1515
file(GLOB SOURCES src/*.c)
1616
file(GLOB TEST src/hashtable.c src/bst.c src/config.c src/list.c src/trie.c
17-
src/util.c src/iterator.c src/logging.c tests/*.c)
17+
src/util.c src/iterator.c src/logging.c src/memory.c tests/*.c)
1818

1919
set(AUTHOR "Andrea Giacomo Baldan")
2020
set(LICENSE "BSD2 license")

src/bst.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
*/
2828

2929
#include "bst.h"
30+
#include "memory.h"
3031

3132
#define MAX(a, b) a > b ? a : b
3233
#define HEIGHT(n) !n ? 0 : n->height
3334
#define BALANCE(n) !n ? 0 : (HEIGHT(n->left)) - (HEIGHT(n->right))
3435

3536
struct bst_node *bst_new(unsigned char key, const void *data) {
36-
struct bst_node *node = xmalloc(sizeof(*node));
37+
struct bst_node *node = try_alloc(sizeof(*node));
3738
node->key = key;
3839
node->height = 1;
3940
node->left = NULL;
@@ -187,7 +188,7 @@ struct bst_node *bst_delete(struct bst_node *node, unsigned char key) {
187188
node = NULL;
188189
} else
189190
*node = *tmp;
190-
xfree(tmp);
191+
free_memory(tmp);
191192
} else {
192193
struct bst_node *tmp = bst_min(node->right);
193194
node->key = tmp->key;

src/bst.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* BSD 2-Clause License
33
*
4-
* Copyright (c) 2018, 2019 Andrea Giacomo Baldan All rights reserved.
4+
* Copyright (c) 2020 Andrea Giacomo Baldan All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
77
* modification, are permitted provided that the following conditions are met:
@@ -29,8 +29,6 @@
2929
#ifndef BST_H
3030
#define BST_H
3131

32-
#include "util.h"
33-
3432
struct bst_node {
3533
unsigned char key;
3634
int height;

src/config.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <unistd.h>
3434
#endif
3535
#include "util.h"
36+
#include "memory.h"
3637
#include "config.h"
3738
#include "network.h"
3839
#include "logging.h"
@@ -117,25 +118,25 @@ char *memory_to_string(size_t memory) {
117118
translated_memory = memory;
118119
numlen = number_len(translated_memory);
119120
// +1 for 'b' +1 for nul terminating
120-
mstring = xmalloc(numlen + 1);
121+
mstring = try_alloc(numlen + 1);
121122
snprintf(mstring, numlen + 1, "%db", translated_memory);
122123
} else if (memory < 1048576) {
123124
translated_memory = memory / 1024;
124125
numlen = number_len(translated_memory);
125126
// +2 for 'Kb' +1 for nul terminating
126-
mstring = xmalloc(numlen + 2);
127+
mstring = try_alloc(numlen + 2);
127128
snprintf(mstring, numlen + 2, "%dKb", translated_memory);
128129
} else if (memory < 1073741824) {
129130
translated_memory = memory / (1024 * 1024);
130131
numlen = number_len(translated_memory);
131132
// +2 for 'Mb' +1 for nul terminating
132-
mstring = xmalloc(numlen + 2);
133+
mstring = try_alloc(numlen + 2);
133134
snprintf(mstring, numlen + 2, "%dMb", translated_memory);
134135
} else {
135136
translated_memory = memory / (1024 * 1024 * 1024);
136137
numlen = number_len(translated_memory);
137138
// +2 for 'Gb' +1 for nul terminating
138-
mstring = xmalloc(numlen + 2);
139+
mstring = try_alloc(numlen + 2);
139140
snprintf(mstring, numlen + 2, "%dGb", translated_memory);
140141
}
141142

@@ -155,25 +156,25 @@ char *time_to_string(size_t time) {
155156
translated_time = time;
156157
numlen = number_len(translated_time);
157158
// +1 for 's' +1 for nul terminating
158-
tstring = xmalloc(numlen + 1);
159+
tstring = try_alloc(numlen + 1);
159160
snprintf(tstring, numlen + 1, "%ds", translated_time);
160161
} else if (time < 60 * 60) {
161162
translated_time = time / 60;
162163
numlen = number_len(translated_time);
163164
// +1 for 'm' +1 for nul terminating
164-
tstring = xmalloc(numlen + 1);
165+
tstring = try_alloc(numlen + 1);
165166
snprintf(tstring, numlen + 1, "%dm", translated_time);
166167
} else if (time < 60 * 60 * 24) {
167168
translated_time = time / (60 * 60);
168169
numlen = number_len(translated_time);
169170
// +1 for 'h' +1 for nul terminating
170-
tstring = xmalloc(numlen + 1);
171+
tstring = try_alloc(numlen + 1);
171172
snprintf(tstring, numlen + 1, "%dh", translated_time);
172173
} else {
173174
translated_time = time / (60 * 60 * 24);
174175
numlen = number_len(translated_time);
175176
// +1 for 'd' +1 for nul terminating
176-
tstring = xmalloc(numlen + 1);
177+
tstring = try_alloc(numlen + 1);
177178
snprintf(tstring, numlen + 1, "%dd", translated_time);
178179
}
179180

@@ -399,8 +400,8 @@ void config_print(void) {
399400
const char *human_memory = memory_to_string(config.max_memory);
400401
log_info("Max memory: %s", human_memory);
401402
log_info("Event loop backend: %s", EVENTLOOP_BACKEND);
402-
xfree((char *) human_memory);
403-
xfree((char *) human_rsize);
403+
free_memory((char *) human_memory);
404+
free_memory((char *) human_rsize);
404405
}
405406
}
406407

@@ -437,9 +438,9 @@ bool config_read_passwd_file(const char *path, struct authentication **auth_map)
437438
while (*puname != '\n')
438439
password[i++] = *puname++;
439440

440-
struct authentication *auth = xmalloc(sizeof(*auth));
441-
auth->username = xstrdup(username);
442-
auth->salt = xstrdup(password);
441+
struct authentication *auth = try_alloc(sizeof(*auth));
442+
auth->username = try_strdup(username);
443+
auth->salt = try_strdup(password);
443444
HASH_ADD_STR(*auth_map, username, auth);
444445
}
445446

src/config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ struct authentication {
146146
struct authentication *auth, *dummy; \
147147
HASH_ITER(hh, (auth_map), auth, dummy) { \
148148
HASH_DEL((auth_map), auth); \
149-
xfree(auth->username); \
150-
xfree(auth->salt); \
151-
xfree(auth); \
149+
free_memory(auth->username); \
150+
free_memory(auth->salt); \
151+
free_memory(auth); \
152152
} \
153153
} while (0);
154154

src/ev.c

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#endif
3838
#include "ev.h"
3939
#include "util.h"
40+
#include "memory.h"
4041
#include "config.h"
4142

4243
#if defined(EPOLL)
@@ -103,20 +104,18 @@ static int epoll_del(int efd, int fd) {
103104
}
104105

105106
static int ev_api_init(struct ev_ctx *ctx, int events_nr) {
106-
struct epoll_api *e_api = xmalloc(sizeof(*e_api));
107-
if (!e_api)
108-
return -EV_OOM;
107+
struct epoll_api *e_api = try_alloc(sizeof(*e_api));
109108
e_api->fd = epoll_create1(0);
110-
e_api->events = xcalloc(events_nr, sizeof(struct epoll_event));
109+
e_api->events = try_calloc(events_nr, sizeof(struct epoll_event));
111110
ctx->api = e_api;
112111
ctx->maxfd = events_nr;
113112
return EV_OK;
114113
}
115114

116115
static void ev_api_destroy(struct ev_ctx *ctx) {
117116
close(((struct epoll_api *) ctx->api)->fd);
118-
xfree(((struct epoll_api *) ctx->api)->events);
119-
xfree(ctx->api);
117+
free_memory(((struct epoll_api *) ctx->api)->events);
118+
free_memory(ctx->api);
120119
}
121120

122121
static int ev_api_get_event_type(struct ev_ctx *ctx, int idx) {
@@ -200,20 +199,18 @@ struct poll_api {
200199
};
201200

202201
static int ev_api_init(struct ev_ctx *ctx, int events_nr) {
203-
struct poll_api *p_api = xmalloc(sizeof(*p_api));
204-
if (!p_api)
205-
return -EV_OOM;
202+
struct poll_api *p_api = try_alloc(sizeof(*p_api));
206203
p_api->nfds = 0;
207-
p_api->fds = xcalloc(events_nr, sizeof(struct pollfd));
204+
p_api->fds = try_calloc(events_nr, sizeof(struct pollfd));
208205
p_api->events_monitored = events_nr;
209206
ctx->api = p_api;
210207
ctx->maxfd = events_nr;
211208
return EV_OK;
212209
}
213210

214211
static void ev_api_destroy(struct ev_ctx *ctx) {
215-
xfree(((struct poll_api *) ctx->api)->fds);
216-
xfree(ctx->api);
212+
free_memory(((struct poll_api *) ctx->api)->fds);
213+
free_memory(ctx->api);
217214
}
218215

219216
static int ev_api_get_event_type(struct ev_ctx *ctx, int idx) {
@@ -249,7 +246,7 @@ static int ev_api_watch_fd(struct ev_ctx *ctx, int fd) {
249246
p_api->nfds++;
250247
if (p_api->nfds >= p_api->events_monitored) {
251248
p_api->events_monitored *= 2;
252-
p_api->fds = xrealloc(p_api->fds,
249+
p_api->fds = try_realloc(p_api->fds,
253250
p_api->events_monitored * sizeof(struct pollfd));
254251
}
255252
return EV_OK;
@@ -282,7 +279,7 @@ static int ev_api_register_event(struct ev_ctx *ctx, int fd, int mask) {
282279
p_api->nfds++;
283280
if (p_api->nfds >= p_api->events_monitored) {
284281
p_api->events_monitored *= 2;
285-
p_api->fds = xrealloc(p_api->fds,
282+
p_api->fds = try_realloc(p_api->fds,
286283
p_api->events_monitored * sizeof(struct pollfd));
287284
}
288285
return EV_OK;
@@ -337,9 +334,7 @@ static int ev_api_init(struct ev_ctx *ctx, int events_nr) {
337334
* 32 x 32 = 1024 as hard limit
338335
*/
339336
assert(events_nr <= 1024);
340-
struct select_api *s_api = xmalloc(sizeof(*s_api));
341-
if (!s_api)
342-
return -EV_OOM;
337+
struct select_api *s_api = try_alloc(sizeof(*s_api));
343338
FD_ZERO(&s_api->rfds);
344339
FD_ZERO(&s_api->wfds);
345340
ctx->api = s_api;
@@ -348,7 +343,7 @@ static int ev_api_init(struct ev_ctx *ctx, int events_nr) {
348343
}
349344

350345
static void ev_api_destroy(struct ev_ctx *ctx) {
351-
xfree(ctx->api);
346+
free_memory(ctx->api);
352347
}
353348

354349
static int ev_api_get_event_type(struct ev_ctx *ctx, int idx) {
@@ -462,20 +457,18 @@ struct kqueue_api {
462457
};
463458

464459
static int ev_api_init(struct ev_ctx *ctx, int events_nr) {
465-
struct kqueue_api *k_api = xmalloc(sizeof(*k_api));
466-
if (!k_api)
467-
return -EV_OOM;
460+
struct kqueue_api *k_api = try_alloc(sizeof(*k_api));
468461
k_api->fd = kqueue();
469-
k_api->events = xcalloc(events_nr, sizeof(struct kevent));
462+
k_api->events = try_calloc(events_nr, sizeof(struct kevent));
470463
ctx->api = k_api;
471464
ctx->maxfd = events_nr;
472465
return EV_OK;
473466
}
474467

475468
static void ev_api_destroy(struct ev_ctx *ctx) {
476469
close(((struct kqueue_api *) ctx->api)->fd);
477-
xfree(((struct kqueue_api *) ctx->api)->events);
478-
xfree(ctx->api);
470+
free_memory(((struct kqueue_api *) ctx->api)->events);
471+
free_memory(ctx->api);
479472
}
480473

481474
static int ev_api_get_event_type(struct ev_ctx *ctx, int idx) {
@@ -620,8 +613,8 @@ static void ev_add_monitored(struct ev_ctx *ctx, int fd, int mask,
620613
int i = ctx->maxevents;
621614
ctx->maxevents = fd;
622615
if (fd > ctx->events_nr) {
623-
ctx->events_monitored =
624-
xrealloc(ctx->events_monitored, (fd + 1) * sizeof(struct ev));
616+
ctx->events_monitored = try_realloc(ctx->events_monitored,
617+
(fd + 1) * sizeof(struct ev));
625618
for (; i < ctx->maxevents; ++i)
626619
ctx->events_monitored[i].mask = EV_NONE;
627620
}
@@ -650,7 +643,7 @@ int ev_init(struct ev_ctx *ctx, int events_nr) {
650643
ctx->fired_events = 0;
651644
ctx->maxevents = events_nr;
652645
ctx->events_nr = events_nr;
653-
ctx->events_monitored = xcalloc(events_nr, sizeof(struct ev));
646+
ctx->events_monitored = try_calloc(events_nr, sizeof(struct ev));
654647
return EV_OK;
655648
}
656649

@@ -660,7 +653,7 @@ void ev_destroy(struct ev_ctx *ctx) {
660653
ctx->events_monitored[i].mask != EV_NONE)
661654
ev_del_fd(ctx, ctx->events_monitored[i].fd);
662655
}
663-
xfree(ctx->events_monitored);
656+
free_memory(ctx->events_monitored);
664657
ev_api_destroy(ctx);
665658
}
666659

src/handlers.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
#include "mqtt.h"
3030
#include "config.h"
3131
#include "server.h"
32+
#include "memory.h"
3233
#include "logging.h"
33-
#include "sol_internal.h"
3434
#include "handlers.h"
35+
#include "sol_internal.h"
3536

3637
/* Prototype for a command handler */
3738
typedef int handler(struct io_event *);
@@ -384,9 +385,9 @@ static int connect_handler(struct io_event *e) {
384385
.publish = (struct mqtt_publish) {
385386
.pkt_id = 0, // placeholder
386387
.topiclen = tpc_len,
387-
.topic = (unsigned char *) xstrdup(will_topic),
388+
.topic = (unsigned char *) try_strdup(will_topic),
388389
.payloadlen = msg_len,
389-
.payload = (unsigned char *) xstrdup(will_message)
390+
.payload = (unsigned char *) try_strdup(will_message)
390391
}
391392
};
392393

@@ -397,9 +398,7 @@ static int connect_handler(struct io_event *e) {
397398
// We must store the retained message in the topic
398399
if (c->bits.will_retain == 1) {
399400
size_t publen = mqtt_size(&cc->session->lwt_msg, NULL);
400-
unsigned char *payload = xmalloc(publen);
401-
if (!payload)
402-
log_fatal("connect_handler failed: Out of memory")
401+
unsigned char *payload = try_alloc(publen);
403402
mqtt_pack(&cc->session->lwt_msg, payload);
404403
// We got a ready-to-be-sent bytestring in the retained message
405404
// field
@@ -447,13 +446,9 @@ static int disconnect_handler(struct io_event *e) {
447446

448447
static inline void add_wildcard(const char *topic, struct subscriber *s,
449448
bool wildcard) {
450-
struct subscription *subscription = xmalloc(sizeof(*subscription));
451-
if (!subscription)
452-
log_fatal("add_wildcard failed: Out of memory");
449+
struct subscription *subscription = try_alloc(sizeof(*subscription));
453450
subscription->subscriber = s;
454-
subscription->topic = xstrdup(topic);
455-
if (!subscription->topic)
456-
log_fatal("add_wildcard failed: Out of memory");
451+
subscription->topic = try_strdup(topic);
457452
subscription->multilevel = wildcard;
458453
INCREF(s, struct subscriber);
459454
server.wildcards = list_push(server.wildcards, subscription);
@@ -560,7 +555,7 @@ static int subscribe_handler(struct io_event *e) {
560555
// Retained message? Publish it
561556
// TODO move after SUBACK response
562557
if (t->retained_msg) {
563-
size_t len = xmalloc_size(t->retained_msg);
558+
size_t len = alloc_size(t->retained_msg);
564559
memcpy(c->wbuf + c->towrite, t->retained_msg, len);
565560
c->towrite += len;
566561
}
@@ -695,9 +690,7 @@ static int publish_handler(struct io_event *e) {
695690
pkt->publish = e->data.publish;
696691

697692
if (hdr->bits.retain == 1) {
698-
t->retained_msg = xmalloc(mqtt_size(&e->data, NULL));
699-
if (!t->retained_msg)
700-
log_fatal("publish_handler failed: Out of memory");
693+
t->retained_msg = try_alloc(mqtt_size(&e->data, NULL));
701694
mqtt_pack(&e->data, t->retained_msg);
702695
}
703696
#if THREADSNR > 0

0 commit comments

Comments
 (0)