aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
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
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 '')
-rw-r--r--src/json.c16
-rw-r--r--src/json.h2
-rw-r--r--src/net.c24
-rw-r--r--src/net.h2
4 files changed, 9 insertions, 35 deletions
diff --git a/src/json.c b/src/json.c
index 3f22b39..10b34a9 100644
--- a/src/json.c
+++ b/src/json.c
@@ -17,21 +17,14 @@
#include <stdlib.h>
#include <string.h>
-char *json_to_string(struct json_object *obj)
+const char *json_to_string(struct json_object *obj)
{
const char *result = json_object_to_json_string(obj);
if (!result) {
json_errno("json_object_to_json_string");
return NULL;
}
-
- char *_result = strdup(result);
- if (!_result) {
- log_errno("strdup");
- return NULL;
- }
-
- return _result;
+ return result;
}
struct json_object *json_from_string(const char *src)
@@ -76,15 +69,11 @@ int json_send(struct json_object *obj, int fd)
struct buf *buf = NULL;
ret = buf_create_from_string(&buf, str);
- free((char *)str);
if (ret < 0)
return ret;
ret = net_send_buf(fd, buf);
buf_destroy(buf);
- if (ret < 0)
- return ret;
-
return ret;
}
@@ -103,6 +92,7 @@ struct json_object *json_recv(int fd)
goto destroy_buf;
destroy_buf:
+ free((void *)buf_get_data(buf));
buf_destroy(buf);
return result;
diff --git a/src/json.h b/src/json.h
index 77bfde4..6ac30cb 100644
--- a/src/json.h
+++ b/src/json.h
@@ -19,7 +19,7 @@
log_err("JSON: %s failed\n", fn); \
} while (0)
-char *json_to_string(struct json_object *);
+const char *json_to_string(struct json_object *);
struct json_object *json_from_string(const char *);
int json_clone(const struct json_object *, const char *key, struct json_object **value);
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:
diff --git a/src/net.h b/src/net.h
index e2fe61e..68ae311 100644
--- a/src/net.h
+++ b/src/net.h
@@ -26,7 +26,7 @@ int buf_create_from_string(struct buf **, const char *);
void buf_destroy(struct buf *);
uint32_t buf_get_size(const struct buf *);
-void *buf_get_data(const struct buf *);
+const void *buf_get_data(const struct buf *);
int net_send_buf(int fd, const struct buf *);
int net_recv_buf(int fd, struct buf **);