forked from gladish/rtMessage_original
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtBuffer.c
116 lines (101 loc) · 2.14 KB
/
rtBuffer.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Copyright [2017] [Comcast, Corp.]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "rtBuffer.h"
#include "rtEncoder.h"
#include <stdlib.h>
#include <string.h>
#define rtAtomic volatile int32_t
#define rtAtomicInc(ptr) (__sync_add_and_fetch(ptr, 1))
#define rtAtomicDec(ptr) (__sync_sub_and_fetch(ptr, 1))
struct _rtBuffer
{
uint8_t* data;
uint32_t len;
rtAtomic refcount;
};
rtError
rtBuffer_Create(rtBuffer* buff)
{
*buff = (rtBuffer) malloc(sizeof(struct _rtBuffer));
(*buff)->data = NULL;
(*buff)->len = 0;
(*buff)->refcount = 1;
return RT_OK;
}
rtError
rtBuffer_CreateFromBytes(rtBuffer* buff, uint8_t* b, int n)
{
rtError err = rtBuffer_Create(buff);
if (err != RT_OK)
return err;
(*buff)->data = (uint8_t *) malloc(sizeof(uint8_t) * n);
memcpy((*buff)->data, b, n);
return RT_OK;
}
rtError
rtBuffer_Destroy(rtBuffer buff)
{
if (buff)
{
if (buff->data)
free(buff->data);
free(buff);
}
return RT_OK;
}
rtError
rtBuffer_Retain(rtBuffer buff)
{
rtAtomicInc(&buff->refcount);
return RT_OK;
}
rtError
rtBuffer_Release(rtBuffer buff)
{
int32_t n = rtAtomicDec(&buff->refcount);
if (n == 0)
rtBuffer_Destroy(buff);
return RT_OK;
}
rtError
rtBuffer_WriteInt32(rtBuffer buff, int32_t n)
{
(void) buff;
(void) n;
return RT_OK;
}
rtError
rtBuffer_WriteString(rtBuffer buff, char const* s, int n)
{
(void) buff;
(void) s;
(void) n;
return RT_OK;
}
rtError
rtBuffer_ReadInt32(rtBuffer buff, int32_t* n)
{
(void) buff;
(void) n;
return RT_OK;
}
rtError
rtBuffer_ReadString(rtBuffer buff, char** s, int* n)
{
(void) buff;
(void) s;
(void) n;
return RT_OK;
}