//----------------------------------------------------------------------------//
// OS on Kaleid //
// //
// Desc: Buffer library //
// //
// //
// Copyright © 2018-2021 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 . //
//----------------------------------------------------------------------------//
#include
#include
Buffer_t *BStdIn, *BStdOut, *BStdDbg;
//
// Creates an actual buffer
//
error_t BOpenPureBuf(Buffer_t **pbuf, int mode, size_t size)
{
if (*pbuf != NULL) {
assert(0);
return EINVAL;
}
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);
ExAcquireLock(&buf->lock);
buf->size = size;
buf->state = mode;
//buf->buf = NULL;
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;
ExReleaseLock(&buf->lock);
// Second allocation failed
if (buf->buf == NULL) {
BCloseBuf(buf);
return ENOMEM;
}
*pbuf = buf;
return EOK;
}
//
// Creates a terminal buffer of (nLines + pbCount*nLines) lines each
// of lineLen length (pb = playback buffer, for scrolling up)
//
error_t BOpenTermBuf(Buffer_t **pbuf, int mode,
int lineLen, int nLines, int pbCount)
{
if (*pbuf != NULL) {
assert(0);
return EINVAL;
}
return BOpenTermBufEx(pbuf, NULL, mode, lineLen, nLines, pbCount, NULL);
}
error_t BOpenTermBufEx(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_TERM;
}
return rc;
}