1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/kaleid/libbuf/buf.c

184 lines
5.4 KiB
C
Raw Normal View History

2019-03-24 22:51:16 +01:00
//----------------------------------------------------------------------------//
// 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/>. //
//----------------------------------------------------------------------------//
2019-04-06 07:53:58 +02:00
#include <extras/buf.h>
#include <extras/locks.h>
2019-03-24 22:51:16 +01:00
#include <extras/malloc.h>
Buffer_t *BStdOut, *BStdDbg;
2019-03-24 22:51:16 +01:00
// 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)
{
2019-03-25 17:33:51 +01:00
assert(buf && buf->initDone == INITOK);
2019-03-24 22:51:16 +01:00
ExAcquireLock(&buf->lock);
if (buf->flags & BF_FONCLOSE) {
BFlushBuf(buf);
}
2019-04-23 11:32:45 +02:00
if (buf->flags & BF_SALLOC && buf->buf) {
2019-04-06 07:53:58 +02:00
KalFreeMemory(buf->buf);
2019-03-24 22:51:16 +01:00
}
buf->buf = buf->rp = buf->wp = NULL;
buf->initDone = buf->flags = buf->state = buf->size = 0;
ExReleaseLock(&buf->lock);
2019-04-23 11:32:45 +02:00
if (buf->flags & BF_BALLOC) {
KalFreeMemory(buf);
}
2019-03-24 22:51:16 +01:00
return EOK;
}
//
2019-04-23 11:32:45 +02:00
// Creates an actual buffer
2019-03-24 22:51:16 +01:00
//
2019-04-23 11:32:45 +02:00
error_t BOpenPureBuf(Buffer_t **pbuf, int mode, size_t size)
2019-03-24 22:51:16 +01:00
{
2019-04-23 11:32:45 +02:00
return BOpenPureBufEx(pbuf, NULL, mode, size, NULL);
2019-03-24 22:51:16 +01:00
}
2019-04-23 11:32:45 +02:00
error_t BOpenPureBufEx(Buffer_t **pbuf, char *source, int mode, size_t size,
BFlusher_t flusher)
2019-03-24 22:51:16 +01:00
{
2019-04-23 11:32:45 +02:00
Buffer_t *buf;
2019-03-24 22:51:16 +01:00
2019-04-23 11:32:45 +02:00
assert(mode == BS_RDWR || mode == BS_RDONLY || mode == BS_WRONLY);
2019-04-23 11:32:45 +02:00
if (!pbuf) return EINVAL;
if (!*pbuf) {
buf = malloc(sizeof *buf);
if (!buf) return ENOMEM;
buf->flags |= BF_BALLOC;
}
else {
buf = *pbuf;
buf->flags = 0;
}
2019-03-24 22:51:16 +01:00
ExInitLock(&buf->lock, KLOCK_MUTEX);
ExAcquireLock(&buf->lock);
2019-04-23 11:32:45 +02:00
buf->size = size;
2019-03-24 22:51:16 +01:00
buf->state = mode;
if (source == NULL) {
2019-04-06 07:53:58 +02:00
KalAllocMemoryEx((void **)&buf->buf, buf->size, M_ZEROED, 0);
2019-04-23 11:32:45 +02:00
buf->flags |= BF_SALLOC;
2019-03-24 22:51:16 +01:00
} else {
2019-03-25 17:33:51 +01:00
buf->buf = (uchar *)source;
2019-03-24 22:51:16 +01:00
}
buf->wp = buf->rp = buf->buf;
buf->flusher = flusher;
buf->initDone = INITOK;
ExReleaseLock(&buf->lock);
2019-04-23 11:32:45 +02:00
// 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;
2019-03-24 22:51:16 +01:00
}
//
// 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;
2019-04-23 11:32:45 +02:00
if (buf->flags & (BF_EOF|BF_ERR)) {
return EENDF;
}
if (!buf->flusher) {
2019-03-24 22:51:16 +01:00
return EBADF;
}
ExAcquireLock(&buf->lock);
rc = buf->flusher(buf);
ExReleaseLock(&buf->lock);
return rc;
}