os-k/kaleid/libbuf/buf.c

184 lines
5.4 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Buffer library //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K 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 General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <extras/buf.h>
#include <extras/locks.h>
#include <extras/malloc.h>
Buffer_t *BStdOut, *BStdDbg;
// Straightforward functions
int BGetFlags(Buffer_t *buf) { return buf->flags; }
int BGetState(Buffer_t *buf) { return buf->state; }
int BGetLineLen(Buffer_t *buf) { return buf->lineLen; }
void BSetLineLen(Buffer_t *buf, int len) { buf->lineLen = len; }
void BLockBuf(Buffer_t *buf)
{
assert(buf && buf->initDone == INITOK);
ExAcquireLock(&buf->lock);
}
void BUnlockBuf(Buffer_t *buf) { ExReleaseLock(&buf->lock); }
bool BTrylockBuf(Buffer_t *buf) { return ExAttemptLock(&buf->lock); }
void BFlushOnClose(Buffer_t *buf) { buf->flags |= BF_FONCLOSE; }
//
// Closes a buffer, not flushing unless the proper flag is set
//
error_t BCloseBuf(Buffer_t *buf)
{
assert(buf && buf->initDone == INITOK);
ExAcquireLock(&buf->lock);
if (buf->flags & BF_FONCLOSE) {
BFlushBuf(buf);
}
if (buf->flags & BF_SALLOC && buf->buf) {
KalFreeMemory(buf->buf);
}
buf->buf = buf->rp = buf->wp = NULL;
buf->initDone = buf->flags = buf->state = buf->size = 0;
ExReleaseLock(&buf->lock);
if (buf->flags & BF_BALLOC) {
KalFreeMemory(buf);
}
return EOK;
}
//
// Creates an actual buffer
//
error_t BOpenPureBuf(Buffer_t **pbuf, int mode, size_t size)
{
return BOpenPureBufEx(pbuf, NULL, mode, size, NULL);
}
error_t BOpenPureBufEx(Buffer_t **pbuf, char *source, int mode, size_t size,
BFlusher_t flusher)
{
Buffer_t *buf;
assert(mode == BS_RDWR || mode == BS_RDONLY || mode == BS_WRONLY);
if (!pbuf) return EINVAL;
if (!*pbuf) {
buf = malloc(sizeof *buf);
if (!buf) return ENOMEM;
buf->flags |= BF_BALLOC;
}
else {
buf = *pbuf;
buf->flags = 0;
}
ExInitLock(&buf->lock, KLOCK_MUTEX);
ExAcquireLock(&buf->lock);
buf->size = size;
buf->state = mode;
if (source == NULL) {
KalAllocMemoryEx((void **)&buf->buf, buf->size, M_ZEROED, 0);
buf->flags |= BF_SALLOC;
} else {
buf->buf = (uchar *)source;
}
buf->wp = buf->rp = buf->buf;
buf->flusher = flusher;
buf->initDone = INITOK;
ExReleaseLock(&buf->lock);
// Second allocation failed
if (buf->buf == NULL) {
BCloseBuf(buf);
return ENOMEM;
}
*pbuf = buf;
return EOK;
}
//
// Creates a line buffer of (nLines + pbCount*nLines) lines each
// of lineLen length (pb = playback buffer, for scrolling up)
//
error_t BOpenLineBuf(Buffer_t **pbuf, int mode,
int lineLen, int nLines, int pbCount)
{
return BOpenLineBufEx(pbuf, NULL, mode, lineLen, nLines, pbCount, NULL);
}
error_t BOpenLineBufEx(Buffer_t **pbuf, char *source, int mode,
int lineLen, int nLines, int pbCount, BFlusher_t flusher)
{
error_t rc;
size_t size = lineLen * nLines * (pbCount + 1);
assert(lineLen > 0 && nLines > 0 && pbCount >= 0);
rc = BOpenPureBufEx(pbuf, source, mode, size, flusher);
if (!rc) {
(*pbuf)->lastLF = 0;
(*pbuf)->nLines = nLines;
(*pbuf)->lineLen = lineLen;
(*pbuf)->flags |= BF_LINE;
}
return rc;
}
//
// Flushes a buffer, returns EBADF when not possible
//
error_t BFlushBuf(Buffer_t *buf)
{
error_t rc;
assert(buf && buf->initDone == INITOK);
if (!buf) return EINVAL;
if (buf->flags & (BF_EOF|BF_ERR)) {
return EENDF;
}
if (!buf->flusher) {
return EBADF;
}
ExAcquireLock(&buf->lock);
rc = buf->flusher(buf);
ExReleaseLock(&buf->lock);
return rc;
}