Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8348d1a0b78ccc05da0ef385b4d219d2dc4ca023 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007, 2017 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

/*
 * Utility module that implements an abstract output queue.
 */

#include <tcf/config.h>
#include <assert.h>
#include <string.h>
#include <tcf/framework/outputbuf.h>
#include <tcf/framework/myalloc.h>
#include <tcf/framework/trace.h>
#include <tcf/framework/errors.h>

#define MAX_POOL_SIZE 32

#define link2buf(A) ((OutputBuffer *)((char *)(A) - offsetof(OutputBuffer, link)))

static LINK pool = TCF_LIST_INIT(pool);
static int pool_size = 0;

void output_queue_ini(OutputQueue * q) {
    list_init(&q->queue);
}

OutputBuffer * output_queue_alloc_obuf(void) {
    OutputBuffer * bf;
    if (list_is_empty(&pool)) {
        bf = (OutputBuffer *)loc_alloc_zero(sizeof(OutputBuffer));
    }
    else {
        bf = link2buf(pool.next);
        list_remove(&bf->link);
        pool_size--;
        bf->queue = NULL;
        bf->buf_pos = 0;
        bf->buf_len = 0;
    }
    return bf;
}

void output_queue_free_obuf(OutputBuffer * bf) {
    if (pool_size < MAX_POOL_SIZE) {
        bf->queue = NULL;
        list_add_last(&bf->link, &pool);
        pool_size++;
    }
    else {
        loc_free(bf);
    }
}

void output_queue_add_obuf(OutputQueue * q, OutputBuffer * bf) {
    if (q->queue.next != q->queue.prev) {
        /* Append data to the last pending buffer */
        OutputBuffer * bp = link2buf(q->queue.prev);
        size_t gap = sizeof(bp->buf) - bp->buf_len;
        assert(bp->buf_pos == 0);
        if (gap >= bf->buf_len) {
            memcpy(bp->buf + bp->buf_len, bf->buf, bf->buf_len);
            bp->buf_len += bf->buf_len;
            output_queue_free_obuf(bf);
            return;
        }
    }
    bf->queue = q;
    bf->buf_pos = 0;
    list_add_last(&bf->link, &q->queue);
    if (q->queue.next == &bf->link) {
        q->post_io_request(bf);
    }
}

void output_queue_add(OutputQueue * q, const void * buf, size_t size) {
    if (q->error) return;
    if (q->queue.next != q->queue.prev) {
        /* Append data to the last pending buffer */
        OutputBuffer * bf = link2buf(q->queue.prev);
        size_t gap = sizeof(bf->buf) - bf->buf_len;
        assert(bf->buf_pos == 0);
        if (gap > 0) {
            size_t len = size;
            if (len > gap) len = gap;
            memcpy(bf->buf + bf->buf_len, buf, len);
            bf->buf_len += len;
            buf = (const char *)buf + len;
            size -= len;
        }
    }
    while (size > 0) {
        size_t len = size;
        OutputBuffer * bf = output_queue_alloc_obuf();
        if (len > sizeof(bf->buf)) len = sizeof(bf->buf);
        bf->queue = q;
        bf->buf_pos = 0;
        bf->buf_len = len;
        memcpy(bf->buf, buf, len);
        list_add_last(&bf->link, &q->queue);
        if (q->queue.next == &bf->link) {
            q->post_io_request(bf);
        }
        buf = (const char *)buf + len;
        size -= len;
    }
}

void output_queue_done(OutputQueue * q, int error, int size) {
    OutputBuffer * bf = link2buf(q->queue.next);

    assert(q->error == 0);
    if (error) {
        q->error = error;
        trace(LOG_PROTOCOL, "Can't write() on output queue %#" PRIxPTR ": %s", (uintptr_t)q, errno_to_str(q->error));
        output_queue_clear(q);
    }
    else {
        bf->buf_pos += size;
        if (bf->buf_pos < bf->buf_len) {
            /* Nothing */
        }
        else {
            list_remove(&bf->link);
            output_queue_free_obuf(bf);
        }
    }
    if (!list_is_empty(&q->queue)) {
        bf = link2buf(q->queue.next);
        q->post_io_request(bf);
    }
}

void output_queue_clear(OutputQueue * q) {
    while (!list_is_empty(&q->queue)) {
        OutputBuffer * bf = link2buf(q->queue.next);
        list_remove(&bf->link);
        output_queue_free_obuf(bf);
    }
}

Back to the top