⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buf.c

📁 Bycore是一个嵌入式操作系统内核。Bycore包括内存管理、任务管理、中断管理、任务互斥、同步与通信管理等功能。Bycore全部由C语言完成
💻 C
字号:
/** *  buf.c - A shared buffer and related calls. * *  Copyright (C) 2008  ZhangHu *  All rights reserved. *  E-MAIL: anmnmnly@gmail.com * *  This program 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 *  (at your option) any later version. * *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. */#include "include/types.h"#include "include/mem.h"#include "include/core.h"#include "include/buf.h"/** * bufInit - Creation a shared buffer size of 'BufSize' * @BufSize: The size of shared buffer. * @return: The start address of this shared buffer. * * @notes: */buf_t *bufInit(uword_t BufSize) {    buf_t *pbuf;    char_t *pdata;    if((pbuf = (buf_t*)kmalloc(sizeof(buf_t))) == NULL) {        return NULL;    }    if((pdata = (char_t *)kmalloc(BufSize *sizeof(char_t))) == NULL) {        kfree(pbuf); /* it is necessary to free buf_t space                        while failure to apply for space,                        and return NULL*/        return NULL;    }    /* initialize shared buffer */    pbuf->size = BufSize;    pbuf->used = 0;    pbuf->start = pdata;    pbuf->end = &pdata[BufSize - 1];    pbuf->inpos = pdata;    pbuf->outpos = pdata;    /* setting all position of buffer to zero */    do {        *pbuf->inpos = 0;        pbuf->inpos++;    } while(pbuf->inpos != pbuf->end + 1);    pbuf->inpos = pdata;    return pbuf;}/** * bufSend - Write a byte of data to the shared buffer. * @pBuf: Poiter to the shared buffer. * @ch: A data which will write to shared buffer. * @err: It stores reasons about this operation. * @return: TRUE if sucessfully, otherwise FALSE. * * @notes: */word_t bufSend(buf_t *pBuf, char_t ch, uword_t *err) {    if(pBuf == NULL) {        *err = BUF_NOT_EXIST;        return FALSE;    }    /* Shared buffer is full */    if(pBuf->size <= pBuf->used) {        *err = BUF_FULL;        return FALSE;    }    *pBuf->inpos = ch;    pBuf->used++;    if(pBuf->inpos++ == pBuf->end) {        pBuf->inpos = pBuf->start;    }    *err = BUF_OK;    return TRUE;}/** * bufSendStr - Write bytes of data to shared buffer. * @pBuf: Poiter to the shared buffer. * @Buf: Pointer to a buffer where data will be write. * @err: It stores reasons about this operation. * @return: How many data are written into. * * @notes: It just called bufSend to complete its job. */uword_t bufSendStr(buf_t *pBuf, char_t *Buf, uword_t *err) {    uword_t num = 0;    char_t *p = Buf;    while(*p != '\0') {        if(bufSend(pBuf, *p, err) == TRUE) {            num++;            p++;        } else {            break;        }    }    return num;}/** * bufRev - Read a byte from a shared buffer. * @pBuf: Poiter to the shared buffer. * @err: It stores reasons about this operation. * @return: A byte of data if successful, otherwise return 0. * * @notes: */char_t bufRev(buf_t *pBuf, uword_t *err) {    char_t redata;    if(pBuf == NULL) {        *err = BUF_NOT_EXIST;        return 0;    }    /* shared buffer is empty */    if(pBuf->used == 0) {        *err = BUF_EMPTY;        return 0;    }    redata = *pBuf->outpos;    pBuf->used--;    if(pBuf->outpos++ == pBuf->end) {        pBuf->outpos = pBuf->start;    }    *err = BUF_OK;    return redata;}/** * bufRevStr - Read bytes of data from the shared buffer. * @pBuf: Poiter to the shared buffer. * @Buf: Pointer to the buffer that take back data. * @err: It stores reasons about this operation. * @return: How many bytes are written. * * @notes: */uword_t bufRevStr(buf_t *pBuf, char_t *Buf, uword_t *err) {    uword_t num = 0;    char_t *p = Buf;    while(1) {        *p = bufRev(pBuf, err);        if(*err == BUF_OK) {            num++;            p++;        } else {            *p = '\0';            *err = BUF_OK;            return num;        }    }}/** * bufClear - Clean the shared buffer. * @pBuf: Poiter to the shared buffer. * @return: * * @notes: */void bufClear(buf_t *pBuf) {    pBuf->used = 0;    pBuf->outpos = pBuf->start;    pBuf->inpos = pBuf->start;    do {        *pBuf->inpos = 0;        pBuf->inpos++;    } while(pBuf->inpos != pBuf->end + 1);    pBuf->inpos = pBuf->start;}/** * bufGetUsed - Get current used space of the shared buffer. * @pBuf: Poiter to the shared buffer. * @return: Used space. * * @notes: */word_t bufGetUsed(buf_t *pBuf) {    return pBuf->used;}/** * bufDel - Delete a shared buffer. * @pBuf: Poiter to the shared buffer. * @return: * * @notes: It is only free its memory space. */void bufDel(buf_t *pBuf) {    kfree(pBuf->start);    kfree(pBuf);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -