【VxWorks5,Golang编程基础教程

本文档介绍了如何在VxWorks5操作系统中使用Golang语言实现环形缓冲库,包括创建、删除、清空、读取和写入缓冲区的函数,以及相关宏定义。示例代码展示了如何分配内存、初始化和管理环形缓冲区。
摘要由CSDN通过智能技术生成

This library also supplies two macros, RNG_ELEM_PUT and RNG_ELEM_GET,
for putting and getting single bytes from a ring buffer. They are defined
in rngLib.h.
.CS
int RNG_ELEM_GET (ringId, pch, fromP)
int RNG_ELEM_PUT (ringId, ch, toP)
.CE
Both macros require a temporary variable or , which
should be declared as `register int’ for maximum efficiency. RNG_ELEM_GET
returns 1 if there was a character available in the buffer; it returns 0
otherwise. RNG_ELEM_PUT returns 1 if there was room in the buffer; it returns
0 otherwise. These are somewhat faster than rngBufPut() and rngBufGet(),
which can put and get multi-byte buffers.

INCLUDE FILES: rngLib.h
*/

/* LINTLIBRARY */

#include “vxWorks.h”
#include “memLib.h”
#include “rngLib.h”
#include “stdlib.h”
#include “string.h”

/*******************************************************************************
*

  • rngCreate - create an empty ring buffer
  • This routine creates a ring buffer of size , and initializes
  • it. Memory for the buffer is allocated from the system memory partition.
  • RETURNS
  • The ID of the ring buffer, or NULL if memory cannot be allocated.
    */

RING_ID rngCreate
(
int nbytes /* number of bytes in ring buffer */
)
{
char *buffer;
RING_ID ringId = (RING_ID) malloc (sizeof (RING));

if (ringId == NULL)
return (NULL);

/* bump number of bytes requested because ring buffer algorithm

  • always leaves at least one empty byte in buffer */

buffer = (char *) malloc ((unsigned) ++nbytes);

if (buffer == NULL)
{
free ((char *)ringId);
return (NULL);
}

ringId->bufSize = nbytes;
ringId->buf = buffer;

rngFlush (ringId);

return (ringId);
}
/*******************************************************************************
*

  • rngDelete - delete a ring buffer
  • This routine deletes a specified ring buffer.
  • Any data currently in the buffer will be lost.
  • RETURNS: N/A
    */

void rngDelete
(
FAST RING_ID ringId /* ring buffer to delete /
)
{
free (ringId->buf);
free ((char )ringId);
}
/
*****************************************************************************
*

  • rngFlush - make a ring buffer empty
  • This routine initializes a specified ring buffer to be empty.
  • Any data currently in the buffer will be lost.
  • RETURNS: N/A
    */

void rngFlush
(
FAST RING_ID ringId /* ring buff

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值