summaryrefslogtreecommitdiffstats
path: root/akode/lib/bytebuffer.cpp
blob: 84b2914f9b16d650aa62519390154413faf77719 (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
/*  aKode: ByteBuffer

    Copyright (C) 2004 Allan Sandfeld Jensen <kde@carewolf.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/


#include <string.h>

#include "bytebuffer.h"

namespace aKode {

ByteBuffer::ByteBuffer(unsigned int len)
              : length(len)
              , readPos(0)
              , writePos(0)
              , flushed(false)
              , released(false)
              , closed(false)
{
    pthread_cond_init(&not_empty, 0);
    pthread_cond_init(&not_full, 0);
    pthread_mutex_init(&mutex, 0);
    buffer = new char[length];
}

ByteBuffer::~ByteBuffer() {
    delete[] buffer;
}

int ByteBuffer::write(char* buf, unsigned int len, bool blocking)
{
    pthread_mutex_lock(&mutex);
    if (released) len = 0;

    flushed = closed = false;
    while (space() < len) {
        if (blocking) {
            pthread_cond_wait(&not_full, &mutex);
            if (flushed || released) len = 0;
        }
        else
            len = space();
    }

    unsigned int base = len, rem = 0;
    if (writePos+len > length) {
        base = length-writePos;
        rem = len-base;
    }

    memcpy(buffer+writePos, buf, base);
    memcpy(buffer, buf+base, rem);

    writePos = (writePos+len) % length;

    pthread_cond_signal(&not_empty);
    pthread_mutex_unlock(&mutex);
    return len;
}

int ByteBuffer::read(char* buf, unsigned int len, bool blocking)
{
    pthread_mutex_lock(&mutex);
    if (released) len = 0;
    if (closed) blocking = false;

    while (content() < len) {
        if (blocking) {
            pthread_cond_wait(&not_empty, &mutex);
            if (released)
                len = 0;
            else if (closed)
                len = content();
        }
        else
            len = content();
    }

    unsigned int base = len, rem = 0;
    if (readPos+len > length) {
        base = length-readPos;
        rem = len-base;
    }

    memcpy(buf, buffer+readPos, base);
    memcpy(buf+base, buffer, rem);

    readPos = (readPos+len) % length;

    pthread_cond_signal(&not_full);
    pthread_mutex_unlock(&mutex);
    return len;
}

void ByteBuffer::close() {
    pthread_mutex_lock(&mutex);
    closed = true;
    pthread_cond_signal(&not_empty);
    pthread_mutex_unlock(&mutex);
}


bool ByteBuffer::eof() {
    return empty() && closed;
}

bool ByteBuffer::empty() {
    return (readPos == writePos);
}

bool ByteBuffer::full() {
    return (readPos == (writePos+1) % length);
}

unsigned int ByteBuffer::content() {
    unsigned int cn;
    if (readPos <= writePos)
        cn=writePos-readPos;
    else
        cn=writePos+length-readPos;
    return cn;
}

unsigned int ByteBuffer::space() {
    unsigned int sp = length - content() - 1;
    return sp;
}

void ByteBuffer::reset() {
    // We assume all processes have been released at this point
    readPos = writePos = 0;
    flushed = released = closed = false;
}

void ByteBuffer::flush() {
    pthread_mutex_lock(&mutex);
    readPos = writePos = 0;
    flushed = true;
    pthread_cond_signal(&not_full);
    pthread_mutex_unlock(&mutex);
}

void ByteBuffer::release() {
    pthread_mutex_lock(&mutex);
    released = true;
    pthread_cond_signal(&not_empty);
    pthread_cond_signal(&not_full);
    pthread_mutex_unlock(&mutex);
}

} // namespace