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的端口号码一样.以下为两种通信协议的端口列表 ...
随机推荐
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.2安装操作系统CentOS5.4
2.2. 安装操作系统CentOS5.4 两个虚拟机都安装,此步骤在创建虚拟机节点时: 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境所有链接: 1.资源 ...
- JAVA CAS原理、unsafe、AQS
concurrent包的实现 由于java的CAS同时具有 volatile 读和volatile写的内存语义,因此Java线程之间的通信现在有了下面四种方式: A线程写volatile变量,随后B线 ...
- sql server 安装后登录服务器
计算机名\数据库实例名 z*******f-PC\zzf
- selenium python (十)浏览器多窗口处理
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip'#在测试过程中有时候会遇到出现多个浏览器窗口的情况,这时候我们可以通过窗口 ...
- 类 .xml
pre{ line-height:1; color:#1e1e1e; background-color:#d2d2d2; font-size:16px;}.sysFunc{color:#627cf6; ...
- excel导入数据到sqlserver
1.读取excel数据到dataset public static System.Data.DataSet ExcelSqlConnection(string filepath, string tab ...
- 解读Cardinality Estimation<基数估计>算法(第一部分:基本概念)
基数计数(cardinality counting)是实际应用中一种常见的计算场景,在数据分析.网络监控及数据库优化等领域都有相关需求.精确的基数计数算法由于种种原因,在面对大数据场景时往往力不从心, ...
- SQL Server UDF用户自定义函数
UDF的定义 和存储过程很相似,用户自定义函数也是一组有序的T-SQL语句,UDF被预先优化和编译并且尅作为一个单元爱进行调用.UDF和存储过程的主要区别在于返回结果的方式. 使用UDF时可传入参数, ...
- CentOS下MySQL 5.7.9编译安装
MySQL 5.7 GA版本的发布,也就是说从现在开始5.7已经可以在生产环境中使用,有任何问题官方都将立刻修复. MySQL 5.7主要特性: 更好的性能:对于多核CPU.固态硬盘.锁有着更好的优化 ...
- beantool.map2Bean(map,obj)