summaryrefslogtreecommitdiff
path: root/src/mbuf.c
blob: f2813c63a1aa93bceed671ca5e367c5d71353168 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2018 Richard Braun.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Upstream site with license notes :
 * http://git.sceen.net/rbraun/librbraun.git/
 */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "macros.h"
#include "mbuf.h"

/*
 * Message header.
 *
 * The size denotes the size of the data, without the header.
 */
struct mbuf_hdr {
    uint32_t size;
    char data[];
};

static int
mbuf_hdr_init(struct mbuf_hdr *hdr, size_t size)
{
    hdr->size = size;

    if (hdr->size != size) {
        return EMSGSIZE;
    }

    return 0;
}

static size_t
mbuf_hdr_total_size(const struct mbuf_hdr *hdr)
{
    return sizeof(*hdr) + hdr->size;
}

static size_t
mbuf_hdr_msg_size(const struct mbuf_hdr *hdr)
{
    return hdr->size;
}

void
mbuf_init(struct mbuf *mbuf, void *buf, size_t capacity)
{
    cbuf_init(&mbuf->cbuf, buf, capacity);
}

void
mbuf_clear(struct mbuf *mbuf)
{
    return cbuf_clear(&mbuf->cbuf);
}

static void
mbuf_clear_old_msgs(struct mbuf *mbuf, size_t total_size)
{
    struct mbuf_hdr hdr;
    size_t size;
    __unused int error;

    do {
        size = sizeof(hdr);
        error = cbuf_pop(&mbuf->cbuf, &hdr, &size);
        assert(!error);

        if (size == 0) {
            break;
        }

        size = mbuf_hdr_msg_size(&hdr);
        error = cbuf_pop(&mbuf->cbuf, NULL, &size);
        assert(!error && (size == mbuf_hdr_msg_size(&hdr)));
    } while (cbuf_avail_size(&mbuf->cbuf) < total_size);
}

int
mbuf_push(struct mbuf *mbuf, const void *buf, size_t size, bool erase)
{
    struct mbuf_hdr hdr;
    size_t total_size;
    int error;

    error = mbuf_hdr_init(&hdr, size);

    if (error) {
        return error;
    }

    total_size = mbuf_hdr_total_size(&hdr);

    if (total_size > cbuf_avail_size(&mbuf->cbuf)) {
        if (!erase || (total_size > cbuf_capacity(&mbuf->cbuf))) {
            return EMSGSIZE;
        }

        mbuf_clear_old_msgs(mbuf, total_size);
    }

    error = cbuf_push(&mbuf->cbuf, &hdr, sizeof(hdr), erase);
    assert(!error);
    error = cbuf_push(&mbuf->cbuf, buf, size, erase);
    assert(!error);

    return 0;
}

int
mbuf_pop(struct mbuf *mbuf, void *buf, size_t *sizep)
{
    struct mbuf_hdr hdr;
    size_t start, size;
    int error;

    start = cbuf_start(&mbuf->cbuf);

    size = sizeof(hdr);
    error = cbuf_read(&mbuf->cbuf, start, &hdr, &size);
    assert(!error);

    if (size == 0) {
        return EAGAIN;
    }

    assert(size == sizeof(hdr));
    size = mbuf_hdr_msg_size(&hdr);

    if (size > *sizep) {
        error = EMSGSIZE;
        goto out;
    }

    cbuf_set_start(&mbuf->cbuf, start + sizeof(hdr));
    error = cbuf_pop(&mbuf->cbuf, buf, &size);
    assert(!error && (size == mbuf_hdr_msg_size(&hdr)));

out:
    *sizep = size;
    return error;
}

size_t
mbuf_avail_size(const struct mbuf *mbuf)
{
    return cbuf_avail_size(&mbuf->cbuf);
}