summaryrefslogtreecommitdiffstats
path: root/src/buffer.h
blob: 1cc596293b6b41493858c80f162c66a8939831b6 (plain)
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 2019-2023 OARC, Inc.
 * Copyright 2017-2018 Akamai Technologies
 * Copyright 2006-2016 Nominum, Inc.
 * All rights reserved.
 *
 * 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.
 */

#ifndef PERF_BUFFER_H
#define PERF_BUFFER_H 1

#include <stddef.h>

typedef struct perf_region {
    void*  base;
    size_t length;
} perf_region_t;

typedef struct perf_buffer {
    void*  base;
    size_t length, used, current, active;
} perf_buffer_t;

#include <assert.h>

#define perf_buffer_init(b, _base, _length) \
    {                                       \
        (b)->base    = _base;               \
        (b)->length  = _length;             \
        (b)->used    = 0;                   \
        (b)->current = 0;                   \
        (b)->active  = 0;                   \
    }

#define perf_buffer_add(b, n)                 \
    {                                         \
        assert((b)->used + n <= (b)->length); \
        (b)->used += (n);                     \
    }

#define perf_buffer_length(b) ((b)->length)

#define perf_buffer_availablelength(b) ((b)->length - (b)->used)

#define perf_buffer_base(b) ((b)->base)

#define perf_buffer_clear(b) \
    {                        \
        (b)->used    = 0;    \
        (b)->current = 0;    \
        (b)->active  = 0;    \
    }

#define perf_buffer_putmem(b, base, length)               \
    {                                                     \
        assert(perf_buffer_availablelength(b) >= length); \
        memcpy(perf_buffer_used(b), base, length);        \
        perf_buffer_add(b, length);                       \
    }

#define perf_buffer_putuint8(b, _val)                 \
    {                                                 \
        unsigned char* _cp;                           \
        uint8_t        _val2 = (_val);                \
        assert(perf_buffer_availablelength(b) >= 1U); \
        _cp = perf_buffer_used(b);                    \
        (b)->used += 1U;                              \
        _cp[0] = _val2;                               \
    }

#define perf_buffer_putuint16(b, _val)                \
    {                                                 \
        unsigned char* _cp;                           \
        uint16_t       _val2 = (_val);                \
        assert(perf_buffer_availablelength(b) >= 2U); \
        _cp = perf_buffer_used(b);                    \
        (b)->used += 2U;                              \
        _cp[0] = _val2 >> 8;                          \
        _cp[1] = _val2;                               \
    }

#define perf_buffer_putuint32(b, _val)                \
    {                                                 \
        unsigned char* _cp;                           \
        uint32_t       _val2 = (_val);                \
        assert(perf_buffer_availablelength(b) >= 4U); \
        _cp = perf_buffer_used(b);                    \
        (b)->used += 4U;                              \
        _cp[0] = _val2 >> 24;                         \
        _cp[1] = _val2 >> 16;                         \
        _cp[2] = _val2 >> 8;                          \
        _cp[3] = _val2;                               \
    }

#define perf_buffer_copyregion(b, r) perf_buffer_putmem(b, (r)->base, (r)->length)

#define perf_buffer_used(b) ((void*)((unsigned char*)(b)->base + (b)->used))
#define perf_buffer_usedlength(b) ((b)->used)
#define perf_buffer_usedregion(b, r) \
    {                                \
        (r)->base   = (b)->base;     \
        (r)->length = (b)->used;     \
    }

#endif