aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2023-05-29 21:40:42 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2023-05-29 21:42:30 +0300
commit59f9b2491899ddfae481c496bb0d947a9c1d72f2 (patch)
tree1ca4e82545234149c5ed19ae3b2629151924e045
parentde0dd03ef406606a56b22eb830eecc8430977c98 (diff)
downloadgran-59f9b2491899ddfae481c496bb0d947a9c1d72f2.tar.gz
gran-59f9b2491899ddfae481c496bb0d947a9c1d72f2.zip
speed up packet allocation by switching to malloc
+ Also allow reusing packets, though only with external buffers.
-rw-r--r--include/gran/packet.h4
-rw-r--r--src/packet.c17
2 files changed, 19 insertions, 2 deletions
diff --git a/include/gran/packet.h b/include/gran/packet.h
index 5c241be..25275f9 100644
--- a/include/gran/packet.h
+++ b/include/gran/packet.h
@@ -24,9 +24,13 @@ struct packet;
struct packet *create_packet(enum packet_type type, uintptr_t addr,
size_t size);
+
struct packet *create_packet_with(enum packet_type type, uintptr_t addr,
size_t size, void *data);
+struct packet *reuse_packet(struct packet *pkt, enum packet_type type,
+ uintptr_t addr, size_t size, void *data);
+
enum packet_type packet_type(struct packet *pkt);
enum packet_state packet_state(struct packet *pkt);
void packet_set_state(struct packet *pkt, enum packet_state state);
diff --git a/src/packet.c b/src/packet.c
index c5ece05..c778965 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -1,5 +1,6 @@
#include <gran/packet.h>
#include <stdlib.h>
+#include <assert.h>
struct packet {
enum packet_state state;
@@ -13,7 +14,7 @@ struct packet {
struct packet *create_packet(enum packet_type type, uintptr_t addr, size_t size)
{
- struct packet *pkt = calloc(1, sizeof(struct packet) + size);
+ struct packet *pkt = malloc(sizeof(struct packet) + size);
if (!pkt)
return NULL;
@@ -28,7 +29,7 @@ struct packet *create_packet(enum packet_type type, uintptr_t addr, size_t size)
struct packet *create_packet_with(enum packet_type type, uintptr_t addr,
size_t size, void *data)
{
- struct packet *pkt = calloc(1, sizeof(struct packet));
+ struct packet *pkt = malloc(sizeof(struct packet));
if (!pkt)
return NULL;
@@ -40,6 +41,18 @@ struct packet *create_packet_with(enum packet_type type, uintptr_t addr,
return pkt;
}
+struct packet *reuse_packet(struct packet *pkt, enum packet_type type,
+ uintptr_t addr, size_t size, void *data)
+{
+ assert(pkt->state == PACKET_DONE);
+ pkt->state = PACKET_INIT;
+ pkt->type = type;
+ pkt->addr = addr;
+ pkt->size = size;
+ pkt->data = data;
+ return pkt;
+}
+
enum packet_type packet_type(struct packet *pkt)
{
return pkt->type;