]> code.delx.au - pulseaudio/blob - src/packet.c
partial implementation of native protocol
[pulseaudio] / src / packet.c
1 #include <assert.h>
2 #include <stdlib.h>
3
4 #include "packet.h"
5
6 struct packet* packet_new(size_t length) {
7 struct packet *p;
8 assert(length);
9 p = malloc(sizeof(struct packet)+length);
10 assert(p);
11
12 p->ref = 1;
13 p->length = length;
14 p->data = (uint8_t*) (p+1);
15 p->type = PACKET_APPENDED;
16 return p;
17 }
18
19 struct packet* packet_dynamic(uint8_t* data, size_t length) {
20 struct packet *p;
21 assert(data && length);
22 p = malloc(sizeof(struct packet));
23 assert(p);
24
25 p->ref = 1;
26 p->length = length;
27 p->data = data;
28 p->type = PACKET_DYNAMIC;
29 }
30
31 struct packet* packet_ref(struct packet *p) {
32 assert(p && p->ref >= 1);
33 p->ref++;
34 return p;
35 }
36
37 void packet_unref(struct packet *p) {
38 assert(p && p->ref >= 1);
39 p->ref--;
40
41 if (p->ref == 0) {
42 if (p->type == PACKET_DYNAMIC)
43 free(p->data);
44 free(p);
45 }
46 }