2004-06-08 23:54:24 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#include "packet.h"
|
|
|
|
|
|
2004-06-20 01:12:13 +00:00
|
|
|
struct packet* packet_new(size_t length) {
|
2004-06-08 23:54:24 +00:00
|
|
|
struct packet *p;
|
|
|
|
|
assert(length);
|
|
|
|
|
p = malloc(sizeof(struct packet)+length);
|
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
|
|
p->ref = 1;
|
|
|
|
|
p->length = length;
|
2004-06-20 01:12:13 +00:00
|
|
|
p->data = (uint8_t*) (p+1);
|
|
|
|
|
p->type = PACKET_APPENDED;
|
2004-06-08 23:54:24 +00:00
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-20 01:12:13 +00:00
|
|
|
struct packet* packet_dynamic(uint8_t* data, size_t length) {
|
|
|
|
|
struct packet *p;
|
|
|
|
|
assert(data && length);
|
|
|
|
|
p = malloc(sizeof(struct packet));
|
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
|
|
p->ref = 1;
|
|
|
|
|
p->length = length;
|
|
|
|
|
p->data = data;
|
|
|
|
|
p->type = PACKET_DYNAMIC;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
struct packet* packet_ref(struct packet *p) {
|
|
|
|
|
assert(p && p->ref >= 1);
|
|
|
|
|
p->ref++;
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_unref(struct packet *p) {
|
|
|
|
|
assert(p && p->ref >= 1);
|
|
|
|
|
p->ref--;
|
|
|
|
|
|
2004-06-20 01:12:13 +00:00
|
|
|
if (p->ref == 0) {
|
|
|
|
|
if (p->type == PACKET_DYNAMIC)
|
|
|
|
|
free(p->data);
|
2004-06-08 23:54:24 +00:00
|
|
|
free(p);
|
2004-06-20 01:12:13 +00:00
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|