-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts_send.c
88 lines (78 loc) · 2 KB
/
ts_send.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
struct ring_fifo{
int head;
int tail;
int size;
int length;
void **data;
};
struct ts_node{
int delta_time;
int size;
int head;
void *data;
};
#define ts_malloc malloc
#define ts_free free
#define ts_warn(fmt, args...) do{printf("[warn] Line:%d Func:%s File:%s\n", __LINE__, __FUNCTION__, __FILE__);printf(fmt,##args);}while(0)
#define ts_error(fmt, args...) do{printf("[Error] Line:%d Func:%s File:%s\n", __LINE__, __FUNCTION__, __FILE__);printf(fmt,##args);}while(0)
#define ts_info(fmt, args...) do{printf("[Info] Line:%d Func:%s File:%s\n", __LINE__, __FUNCTION__, __FILE__);printf(fmt,##args);}while(0)
#define ts_trace(fmt, args...) do{printf("[Trace] Line:%d Func:%s File:%s\n", __LINE__, __FUNCTION__, __FILE__);printf(fmt,##args);}while(0)
#define ts_debug(fmt, args...) do{printf("[Debug] Line:%d Func:%s File:%s\n", __LINE__, __FUNCTION__, __FILE__)}while(0)
struct ring_fifo *ring_fifo_resize(struct ring_fifo *r){
assert(r);
int length = r->length>0?r->length * 2:2;
void *data = ts_malloc(length);
if(!data){
ts_warn("malloc failed\n");
return NULL;
}
int i;
int j = 0;
for(i = 0; i < r->size; i++){
j = (r->head + i)%r->length;
data[i] = r->data[j];
}
r->tail = j;
r->length = length;
return r;
}
struct ring_fifo_new(){
struct ring_fifo_new *r = ts_malloc(sizeof(*r));
if(!r){
ts_warn("malloc failed\n");
return NULL;
}
memset(r, 0, sizeof(*r));
return ring_fifo_resize(r);
}
void ring_fifo_free(struct ring_fifo *r){
assert(r);
ts_free(r);
}
bool ring_fifo_push(struct ring_fifo *r, void *node){
assert(r);
if(r->size == r->length){
if(!ring_fifo_resize(r)){
ts_warn("ring fifo resize failed, length:%d\n", r->length);
return false;
}
}
r->tail = r->tail%r->length;
r->data[r->tail] = node;
r->tail++;
r->size++;
return true;
}
void *ring_fifo_pop(struct ring_fifo *r){
assert(r);
if(r->size == 0){
return NULL;
}
r->head++;
r->size--;
r->head = r->head%r->length;
void *data = r->data[r->head];
return data;
}