aboutsummaryrefslogtreecommitdiffstats
path: root/include/osmocom/core
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2019-03-18 18:38:47 +0100
committerHarald Welte <laforge@gnumonks.org>2019-04-10 22:42:32 +0000
commit179f35702ece13f4ab7fd1b331bef664834d8473 (patch)
tree9dad4da8eb773040ea8a2dc096190118f5d2abd6 /include/osmocom/core
parentd08e9866a5c3da6edda574bbe6d277047d10fded (diff)
Add _c versions of functions that otherwise return static buffers
We have a habit of returning static buffers from some functions, particularly when generating some kind of string values. This is convenient in terms of memory management, but it comes at the expense of not being thread-safe, and not allowing for two calls of the related function within one printf() statement. Let's introduce _c suffix versions of those functions where the caller passes in a talloc context from which the output buffer shall be allocated. Change-Id: I8481c19b68ff67cfa22abb93c405ebcfcb0ab19b
Diffstat (limited to 'include/osmocom/core')
-rw-r--r--include/osmocom/core/msgb.h26
-rw-r--r--include/osmocom/core/socket.h1
-rw-r--r--include/osmocom/core/utils.h6
3 files changed, 33 insertions, 0 deletions
diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h
index 0c51ce2..e05d37f 100644
--- a/include/osmocom/core/msgb.h
+++ b/include/osmocom/core/msgb.h
@@ -60,6 +60,7 @@ struct msgb {
unsigned char _data[0]; /*!< optional immediate data array */
};
+extern struct msgb *msgb_alloc_c(const void *ctx, uint16_t size, const char *name);
extern struct msgb *msgb_alloc(uint16_t size, const char *name);
extern void msgb_free(struct msgb *m);
extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
@@ -68,9 +69,11 @@ extern void msgb_reset(struct msgb *m);
uint16_t msgb_length(const struct msgb *msg);
extern const char *msgb_hexdump(const struct msgb *msg);
char *msgb_hexdump_buf(char *buf, size_t buf_len, const struct msgb *msg);
+char *msgb_hexdump_c(const void *ctx, const struct msgb *msg);
extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
int old_size, int new_size);
extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
+extern struct msgb *msgb_copy_c(const void *ctx, const struct msgb *msg, const char *name);
static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure));
/*! Free all msgbs from a queue built with msgb_enqueue().
@@ -501,6 +504,29 @@ static inline int msgb_l3trim(struct msgb *msg, int l3len)
return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
}
+/*! Allocate message buffer with specified headroom from specified talloc context.
+ * \param[in] ctx talloc context from which to allocate
+ * \param[in] size size in bytes, including headroom
+ * \param[in] headroom headroom in bytes
+ * \param[in] name human-readable name
+ * \returns allocated message buffer with specified headroom
+ *
+ * This function is a convenience wrapper around \ref msgb_alloc
+ * followed by \ref msgb_reserve in order to create a new \ref msgb with
+ * user-specified amount of headroom.
+ */
+static inline struct msgb *msgb_alloc_headroom_c(const void *ctx, int size, int headroom,
+ const char *name)
+{
+ osmo_static_assert(size > headroom, headroom_bigger);
+
+ struct msgb *msg = msgb_alloc_c(ctx, size, name);
+ if (msg)
+ msgb_reserve(msg, headroom);
+ return msg;
+}
+
+
/*! Allocate message buffer with specified headroom
* \param[in] size size in bytes, including headroom
* \param[in] headroom headroom in bytes
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 4f6ed72..37b1eae 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -68,6 +68,7 @@ int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto,
char *osmo_sock_get_name(const void *ctx, int fd);
const char *osmo_sock_get_name2(int fd);
+char *osmo_sock_get_name2_c(const void *ctx, int fd);
int osmo_sock_get_name_buf(char *str, size_t str_len, int fd);
int osmo_sock_get_ip_and_port(int fd, char *ip, size_t ip_len, char *port, size_t port_len, bool local);
int osmo_sock_get_local_ip(int fd, char *host, size_t len);
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 6a2b7d5..51e43ee 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -56,7 +56,9 @@ int osmo_hexparse(const char *str, uint8_t *b, int max_len);
char *osmo_ubit_dump_buf(char *buf, size_t buf_len, const uint8_t *bits, unsigned int len);
char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
char *osmo_hexdump(const unsigned char *buf, int len);
+char *osmo_hexdump_c(const void *ctx, const unsigned char *buf, int len);
char *osmo_hexdump_nospc(const unsigned char *buf, int len);
+char *osmo_hexdump_nospc_c(const void *ctx, const unsigned char *buf, int len);
const char *osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned char *buf, int len, const char *delim,
bool delim_after_last);
@@ -73,9 +75,11 @@ void osmo_str2upper(char *out, const char *in)
size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src);
const char *osmo_str_tolower(const char *src);
+char *osmo_str_tolower_c(const void *ctx, const char *src);
size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src);
const char *osmo_str_toupper(const char *src);
+char *osmo_str_toupper_c(const void *ctx, const char *src);
#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
do { \
@@ -139,8 +143,10 @@ bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
const char *osmo_escape_str(const char *str, int len);
char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
+char *osmo_escape_str_c(const void *ctx, const char *str, int in_len);
const char *osmo_quote_str(const char *str, int in_len);
char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
+char *osmo_quote_str_c(const void *ctx, const char *str, int in_len);
uint32_t osmo_isqrt32(uint32_t x);