aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/net.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-07-18 01:59:39 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-07-18 09:48:17 +0200
commitb0963cf8e2b31ac6a60c314bad148cd93d8eaca1 (patch)
treea12e648fc0f072215741d2aaa805c207abb83c16 /src/net.c
parentprocess: preserve the exit code more (diff)
downloadcimple-b0963cf8e2b31ac6a60c314bad148cd93d8eaca1.tar.gz
cimple-b0963cf8e2b31ac6a60c314bad148cd93d8eaca1.zip
net: don't copy data in struct buf
Diffstat (limited to 'src/net.c')
-rw-r--r--src/net.c24
1 files changed, 4 insertions, 20 deletions
diff --git a/src/net.c b/src/net.c
index ac3cdde..cb85356 100644
--- a/src/net.c
+++ b/src/net.c
@@ -210,35 +210,22 @@ int net_recv(int fd, void *buf, size_t size)
struct buf {
uint32_t size;
- void *data;
+ const void *data;
};
int buf_create(struct buf **_buf, const void *data, uint32_t size)
{
- int ret = 0;
-
struct buf *buf = malloc(sizeof(struct buf));
if (!buf) {
log_errno("malloc");
return -1;
}
- buf->data = malloc(size);
- if (!buf->data) {
- log_errno("malloc");
- goto free;
- }
-
+ buf->data = data;
buf->size = size;
- memcpy(buf->data, data, size);
*_buf = buf;
- return ret;
-
-free:
- free(buf);
-
- return ret;
+ return 0;
}
int buf_create_from_string(struct buf **buf, const char *str)
@@ -248,7 +235,6 @@ int buf_create_from_string(struct buf **buf, const char *str)
void buf_destroy(struct buf *buf)
{
- free(buf->data);
free(buf);
}
@@ -257,7 +243,7 @@ uint32_t buf_get_size(const struct buf *buf)
return buf->size;
}
-void *buf_get_data(const struct buf *buf)
+const void *buf_get_data(const struct buf *buf)
{
return buf->data;
}
@@ -306,8 +292,6 @@ int net_recv_buf(int fd, struct buf **buf)
if (ret < 0)
goto free_data;
- free(data);
-
return ret;
free_data: