Mailbox and Mail
#ifndef __MAILBOX_H__
#define __MAILBOX_H__ #include <stdint.h>
#include <stdlib.h>
#include <string.h> typedef struct
{
// uint32_t Capacity;
uint8_t * Memory;
uint32_t MailSize;
uint32_t MailCount;
uint32_t ReadIndex;
uint32_t ReadCount;
} MAILBOX; // Creates a new mailbox and Init it.
MAILBOX * MBX_Create( uint32_t MailSize, uint32_t MailCount ); // Deletes a specified mailbox.
void MBX_Delete( MAILBOX * Mailbox ); // Clears all messages in a specified mailbox.
void MBX_Clear( MAILBOX * Mailbox ); // Init a new mailbox.
void MBX_Init( MAILBOX * Mailbox, uint32_t MailSize, uint32_t MailCount,
void * Memory ); // Stores a new message of a predefined size in a mailbox.
uint32_t MBX_Write( MAILBOX * Mailbox, void * Mail ); /*
* Stores a new message of a predefined size into a mailbox in front of all
* other messages, if the mailbox is able to accept one more message.
* The new message will be retrieved first.
*/
uint32_t MBX_WriteFront( MAILBOX * Mailbox, void * Mail ); /* Retrieves a new message of a predefined size from a mailbox.
* Mail : Pointer to the memory area that the message should be stored at.
* Make sure that it points to a valid memory area and that there is sufficient
* space for an entiremessage. The message size (in bytes) was defined
* when the mailbox was created.
*/
uint32_t MBX_Read( MAILBOX * Mailbox, void * Mail ); // Returns the number of mail currently available in a specified mailbox.
uint32_t MBX_GetCount( MAILBOX * Mailbox ); #endif /* __MAILBOX_H__ */
#include "mailbox.h"
#include "macro_misc.h"
#include "cmsis_os.h" // Creates a new mailbox and Init it.
MAILBOX * MBX_Create( uint32_t MailSize, uint32_t MailCount )
{
uint32_t Size = ALIGN_UP( sizeof(MAILBOX), ) + MailSize * MailCount;
MAILBOX * Mailbox = (MAILBOX *) osMalloc( Size, osWaitForever );
if ( Mailbox == )
return Mailbox; uint8_t * Memory = //
(uint8_t *) ( ( (uint32_t) ( Mailbox ) ) + ALIGN_UP( sizeof(MAILBOX), ) ); MBX_Init( Mailbox, MailSize, MailCount, Memory ); return Mailbox;
} // Deletes a specified mailbox.
void MBX_Delete( MAILBOX * Mailbox )
{
osFree( Mailbox );
} // Clears all messages in a specified mailbox.
void MBX_Clear( MAILBOX * Mailbox )
{
Mailbox->ReadCount = ;
} // Returns the number of mail currently available in a specified mailbox.
uint32_t MBX_GetCount( MAILBOX * Mailbox )
{
return Mailbox->ReadCount;
} // Init a new mailbox.
void MBX_Init( MAILBOX * Mailbox, uint32_t MailSize, uint32_t MailCount,
void * Memory )
{
// Mailbox->Capacity = MailCount * MailSize;
Mailbox->MailSize = MailSize;
Mailbox->MailCount = MailCount;
Mailbox->Memory = Memory;
Mailbox->ReadIndex = ;
Mailbox->ReadCount = ;
} /* Stores a new message of a predefined size in a mailbox.
*
* 0: Message could not be stored (mailbox is full).
* 1: Success; message stored.
*/
uint32_t MBX_Write( MAILBOX * Mailbox, void * Mail )
{
if ( Mailbox->ReadCount == Mailbox->MailCount )
return ; uint32_t Value = osDisableInterrupt( ); uint32_t IndexToWrite = ( Mailbox->ReadIndex + Mailbox->ReadCount );
if ( IndexToWrite == Mailbox->MailCount )
IndexToWrite = ; uint32_t MemoryIndexToWrite = IndexToWrite * Mailbox->MailSize;
memcpy( &Mailbox->Memory[ MemoryIndexToWrite ], Mail, Mailbox->MailSize );
Mailbox->ReadCount++; osRestoreInterrupt( Value ); return ;
} /*
* Stores a new message of a predefined size into a mailbox in front of all
* other messages, if the mailbox is able to accept one more message.
* The new message will be retrieved first.
*
* 0: Message could not be stored (mailbox is full).
* 1: Success; message stored.
*/
uint32_t MBX_WriteFront( MAILBOX * Mailbox, void * Mail )
{
if ( Mailbox->ReadCount == Mailbox->MailCount )
return ; if ( Mailbox->ReadCount == )
return MBX_Write( Mailbox, Mail ); uint32_t Value = osDisableInterrupt( ); uint32_t IndexToWrite; if ( Mailbox->ReadIndex )
IndexToWrite = Mailbox->ReadIndex - ;
else
IndexToWrite = Mailbox->MailCount - ; uint32_t MemoryIndexToWrite = IndexToWrite * Mailbox->MailSize;
memcpy( &Mailbox->Memory[ MemoryIndexToWrite ], Mail, Mailbox->MailSize ); Mailbox->ReadIndex = IndexToWrite;
Mailbox->ReadCount++; osRestoreInterrupt( Value ); return ;
} /* Retrieves a new message of a predefined size from a mailbox.
* Mail : Pointer to the memory area that the message should be stored at.
* Make sure that it points to a valid memory area and that there is sufficient
* space for an entiremessage. The message size (in bytes) was defined
* when the mailbox was created.
*
* 0: Message could not be retrieved (mailbox is empty)
* 1: Success; message retrieved.
*/
uint32_t MBX_Read( MAILBOX * Mailbox, void * Mail )
{
if ( Mailbox->ReadCount == )
return ; uint32_t Value = osDisableInterrupt( ); uint32_t MemoryIndexToRead = Mailbox->ReadIndex * Mailbox->MailSize;
memcpy( Mail, &Mailbox->Memory[ MemoryIndexToRead ], Mailbox->MailSize ); Mailbox->ReadIndex++;
if ( Mailbox->ReadIndex == Mailbox->MailCount )
Mailbox->ReadIndex = ;
Mailbox->ReadCount--; osRestoreInterrupt( Value ); return ;
}
Mailbox and Mail的更多相关文章
- DNS报文格式(RFC1035)
一.域名和资源记录的定义 1.Name space definitions 2.资源记录定义(RR definitions) 2.1 格式 后面分析报文的时候详细解释. ...
- DNS 中的协议字段详细定义
DNS中的协议字段定义 Table of Contents 1 概述 2 DNS Classes 3 DNS OpCodes 4 DNS RCODEs 5 DNS Label Types 6 DNS资 ...
- TCP/UDP端口列表
http://zh.wikipedia.org/wiki/TCP/UDP%E7%AB%AF%E5%8F%A3%E5%88%97%E8%A1%A8 TCP/UDP端口列表 本条目可通过翻译外语维 ...
- 【转载】Powershell获取世纪互联Office365所有用户最后一次登录时间
#$Mails=get-mailbox -ResultSize 10 $Mails=get-mailbox -ResultSize Unlimited $Mails | Measure-Object ...
- conky 配置变量表
转自conky 配置变量表 项目主页:http://conky.sourceforge.net/ 文档说明:http://conky.sourceforge.net/docs.html Variabl ...
- 《Linux命令行与shell脚本编程大全》 第十五章 学习笔记
第十五章:控制脚本 处理信号 重温Linux信号 信号 名称 描述 1 HUP 挂起 2 INT 中断 3 QUIT 结束运行 9 KILL 无条件终止 11 SEGV 段错误 15 TERM 尽可能 ...
- 计算机网络——DNS协议的学习与实现
1. 主要内容 不说废话,直接进入正题.先说说本文本文的主要内容,好让你决定是否看下去: 介绍DNS是干什么的: 介绍DNS是如何工作的: 介绍DNS请求与响应的消息格式: 编程实现一个简单的DNS服 ...
- 邮件服务器 postfix
背景介绍 邮件服务器普遍需要一个主机名来使得mail from 以"账号@主机名"方式显示.由于外网上垃圾邮件太多,现在已不使用ip发邮件,很多网络供应商都会对来源不明的邮件进行限 ...
- TCP/UDP 常用端口列表
计算机之间依照互联网传输层TCP/IP协议不同的协议通信,都有不同的对应端口.所以,利用短信(datagram)的UDP,所采用的端口号码不一定和采用TCP的端口号码一样.以下为两种通信协议的端口列表 ...
随机推荐
- MySQL与Oracle 差异比较之四条件循环语句
循环语句 编号 类别 ORACLE MYSQL 注释 1 IF语句使用不同 IF iv_weekly_day = 'MON' THEN ii_weekly_day := 'MON';ELS ...
- 嵌入式 hi3518x平台h264+g711a封装mp4代码demo
先看代码吧,有代码有真相,具体代码的demo(下载demo的朋友请勿在网上上传我的demo,谢谢)下载连接为: http://download.csdn.net/detail/skdkjxy/8071 ...
- Android 者开发如何选择测试机列表
Android 系统已经分化成多种不同的定制版本,制造厂商的不同手机使用的硬件千差万别.差异化带来良好的用户体验的同时,也给开发者带来的适配的问题.于是每个开发团队都需要面临选择测试机列表的问题.我基 ...
- Win7桌面快捷方式全变成某个软件的图标,然后所有快捷方式都只打开这个图标的软件
电脑真是用到老学老好,之前没有遇到的情况,今天终于碰上了. 由于电脑桌面搜狗浏览器图标总不显示,于是选择快捷方式的打开方式为搜狗浏览器,结果,尼玛呀,全部快捷图标都变成搜狗的. 上网找了一下,双击就搞 ...
- 【2013微软面试题】输出节点数为n的二叉树的所有形态
转自:http://blog.csdn.net/monsterxd/article/details/8449005 /* * 题意,求节点数为n的二叉树的所有形态,先要想个方式来唯一标示一棵二叉树 ...
- Android中设置全屏的方法
在实际的应用程序开发中,我们有时需要把 Activity 设置成全屏显示,一般情况下,可以通过两种方式来设置全屏显示效果.其一,通过在代码中可以设置,其二,通过manifest配置文件来设置全屏. 其 ...
- Linux 的 screen用法
screen可以将任务挂起,即将任务放在后台,一般5个任务左右. 1.新建screen会话:直接输入screen命令或者screen -S [会话名称] 2.退出会话:按下组合键Ctrl+a并松开,此 ...
- [POJ] #1008# Maya Calendar : 字符处理/同余问题
一. 题目 Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74085 Accepted: 2 ...
- 服务器多块磁盘 ,同时磁盘类型混合(SSD+STAT+SAS)
服务器多块磁盘 ,同时磁盘类型混合(SSD+STAT+SAS)
- UVa 1617 Laptop (贪心)
题意:有n个长度为1的线段,确定它们的起点,使得第i个线段在[ri,di]之间,输出空隙数目的最小值. 析:很明显的贪心题,贪心策略是这样的,先把所有的区间排序,原则是按右端点进行排序,如果相等再按左 ...