Commit 223addb1 authored by Andreas Schmidt's avatar Andreas Schmidt

Packet copies are now shallow copies by default

parent fff6c3e9
Pipeline #2904 passed with stages
in 1 minute and 26 seconds
...@@ -16,6 +16,8 @@ add_subdirectory(util) ...@@ -16,6 +16,8 @@ add_subdirectory(util)
add_executable(sender sender.c) add_executable(sender sender.c)
add_executable(receiver receiver.c) add_executable(receiver receiver.c)
add_executable(refcount refcount.c)
target_link_libraries(sender LINK_PUBLIC PRRT UTIL ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(sender LINK_PUBLIC PRRT UTIL ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(receiver LINK_PUBLIC PRRT UTIL ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(receiver LINK_PUBLIC PRRT UTIL ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(refcount LINK_PUBLIC PRRT UTIL ${CMAKE_THREAD_LIBS_INIT})
...@@ -92,6 +92,11 @@ int PrrtPacket_print(PrrtPacket *packet_ptr) { ...@@ -92,6 +92,11 @@ int PrrtPacket_print(PrrtPacket *packet_ptr) {
} }
PrrtPacket *PrrtPacket_copy(PrrtPacket *original) { PrrtPacket *PrrtPacket_copy(PrrtPacket *original) {
__sync_fetch_and_add(&original->refcount, 1);
return original;
}
PrrtPacket *PrrtPacket_deepcopy(PrrtPacket *original) {
PrrtPacket *newPacket = calloc(1, sizeof(PrrtPacket)); PrrtPacket *newPacket = calloc(1, sizeof(PrrtPacket));
check_mem(newPacket); check_mem(newPacket);
void *payload = calloc(1, original->payloadLength); void *payload = calloc(1, original->payloadLength);
...@@ -105,6 +110,8 @@ PrrtPacket *PrrtPacket_copy(PrrtPacket *original) { ...@@ -105,6 +110,8 @@ PrrtPacket *PrrtPacket_copy(PrrtPacket *original) {
newPacket->sequenceNumber = original->sequenceNumber; newPacket->sequenceNumber = original->sequenceNumber;
newPacket->type_priority = original->type_priority; newPacket->type_priority = original->type_priority;
newPacket->refcount = 1;
return newPacket; return newPacket;
error: error:
...@@ -122,6 +129,7 @@ create_header(uint8_t priority, prrtSequenceNumber_t seqno, prrtPacketLength_t s ...@@ -122,6 +129,7 @@ create_header(uint8_t priority, prrtSequenceNumber_t seqno, prrtPacketLength_t s
packet->index = index; packet->index = index;
packet->sequenceNumber = seqno; packet->sequenceNumber = seqno;
packet->payloadLength = size; packet->payloadLength = size;
packet->refcount = 1;
return packet; return packet;
...@@ -214,6 +222,10 @@ void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) { ...@@ -214,6 +222,10 @@ void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
} }
bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket) { bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket) {
// targetPacket is uninitialized, so we need to set the reference count
targetPacket->refcount = 1;
prrtPacketLength_t payload_len = (prrtPacketLength_t) (srcBufferSize - PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH); prrtPacketLength_t payload_len = (prrtPacketLength_t) (srcBufferSize - PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH);
targetPacket->type_priority = *(uint8_t *) srcBuffer; targetPacket->type_priority = *(uint8_t *) srcBuffer;
srcBuffer += 1; srcBuffer += 1;
...@@ -295,11 +307,17 @@ void *decode_data_header(void *dstBuffer, const void *srcBuffer) { ...@@ -295,11 +307,17 @@ void *decode_data_header(void *dstBuffer, const void *srcBuffer) {
} }
int PrrtPacket_destroy(PrrtPacket *packet) { int PrrtPacket_destroy(PrrtPacket *packet) {
if (packet->payload != NULL) {
free(packet->payload); prrtRefcount_t refcount = __sync_fetch_and_sub(&packet->refcount, 1);
if (refcount > 1) {
return 0;
} else {
if (packet->payload != NULL) {
free(packet->payload);
}
free(packet);
return 0;
} }
free(packet);
return 0;
} }
PrrtPacket *PrrtPacket_create_data_packet(uint8_t priority, const void *payloadPointer, PrrtPacket *PrrtPacket_create_data_packet(uint8_t priority, const void *payloadPointer,
......
...@@ -36,6 +36,8 @@ typedef atomic_uint_fast32_t prrtAtomicDeliveryRate_t; ...@@ -36,6 +36,8 @@ typedef atomic_uint_fast32_t prrtAtomicDeliveryRate_t;
typedef uint32_t prrtByteCount_t; typedef uint32_t prrtByteCount_t;
typedef uint8_t prrtPacketType_t; typedef uint8_t prrtPacketType_t;
typedef uint16_t prrtRefcount_t;
typedef struct prrtIncompleteBlock { typedef struct prrtIncompleteBlock {
prrtSequenceNumber_t sequenceNumberBase; prrtSequenceNumber_t sequenceNumberBase;
uint16_t repairCycleIndex; uint16_t repairCycleIndex;
...@@ -58,6 +60,8 @@ typedef struct prrtPacket { ...@@ -58,6 +60,8 @@ typedef struct prrtPacket {
prrtTimedelta_t rtt; prrtTimedelta_t rtt;
struct sockaddr_in sender_addr; struct sockaddr_in sender_addr;
prrtRefcount_t refcount;
} PrrtPacket; } PrrtPacket;
#define PRRT_PACKET_GENERAL_HEADER_SIZE 8 #define PRRT_PACKET_GENERAL_HEADER_SIZE 8
#define PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH 4 #define PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH 4
...@@ -124,6 +128,8 @@ int PrrtPacket_print(PrrtPacket *packet_ptr); ...@@ -124,6 +128,8 @@ int PrrtPacket_print(PrrtPacket *packet_ptr);
PrrtPacket *PrrtPacket_copy(PrrtPacket *original); PrrtPacket *PrrtPacket_copy(PrrtPacket *original);
PrrtPacket *PrrtPacket_deepcopy(PrrtPacket *original);
PrrtPacket *PrrtPacket_create_data_packet(uint8_t priority, const void *payloadPointer, PrrtPacket *PrrtPacket_create_data_packet(uint8_t priority, const void *payloadPointer,
prrtPacketLength_t dataLength, prrtSequenceNumber_t sequenceNumber, prrtPacketLength_t dataLength, prrtSequenceNumber_t sequenceNumber,
prrtTimedelta_t targetDelay); prrtTimedelta_t targetDelay);
......
#include "proto/types/packet.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int prio = 0;
const int seqnum = 1337;
const int timedelta = 0;
int main() {
const char* payload = "just some example payload";
struct prrtPacket *a = PrrtPacket_create_data_packet(prio, payload, strlen(payload), seqnum, timedelta);
printf("refcount(a): %u\n\n", a->refcount);
struct prrtPacket *b = PrrtPacket_copy(a);
printf("refcount(a): %u\nrefcount(b): %u\n\n", a->refcount, b->refcount);
PrrtPacket_destroy(a);
printf("refcount(b): %u\n", b->refcount);
PrrtPacket_destroy(b);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment