编写跨平台代码之memory alignment
编写网络包(存储在堆上)转换程序时,在hp-ux机器上运行时会遇到 si_code: 1 - BUS_ADRALN - Invalid address alignment. Please refer to the following link that helps in handling unaligned data 错误,经排查,是由于hp-ux 的cpu基于IA-64架构, 不支持内存随机存取,需要做字节序对齐操作。
当将内存数据类型进行强制转换后,随机读取的field1 地址很有可能不在一次可读取的地址上(如,地址为0X0001-0X0005), 此时便会抛出bus_adraln错误,导致宕机。
如:
struct SHead{
uint32_t field1;
uint32_t field2;
}; SHead *head = (SHead*)buf;
printf("%d\n", head->field1);
一种解决方案是多一次内存拷贝,如:
struct SHead{
uint32_t field1;
uint32_t field2;
}; SHead head;
memcpy(&head, buf, sizeof(SHead));
printf("%d\n", head.field1);
msdn上对内存字节对齐的介绍:
Many CPUs, such as those based on Alpha, IA-64, MIPS, and SuperH architectures, refuse to read misaligned data. When a program requests that one of these CPUs access data that is not aligned, the CPU enters an exception state and notifies the software that it cannot continue. On ARM, MIPS, and SH device platforms, for example, the operating system default is to give the application an exception notification when a misaligned access is requested.
Misaligned memory accesses can incur enormous performance losses on targets that do not support them in hardware.
Alignment is a property of a memory address, expressed as the numeric address modulo a power of 2. For example, the address 0x0001103F modulo 4 is 3; that address is said to be aligned to 4n+3, where 4 indicates the chosen power of 2. The alignment of an address depends on the chosen power of two. The same address modulo 8 is 7.
An address is said to be aligned to X if its alignment is Xn+0.
对内存对齐不是很熟悉的同学请参考:
http://msdn.microsoft.com/en-us/library/ms253949(VS.80).aspx
编写跨平台代码之memory alignment的更多相关文章
- 在 C# 中使用 Span<T> 和 Memory<T> 编写高性能代码
目录 在 C# 中使用 Span 和 Memory 编写高性能代码 .NET 中支持的内存类型 .NET Core 2.1 中新增的类型 访问连续内存: Span 和 Memory Span 介绍 C ...
- 如何编写跨平台的Java代码
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- “前.NET Core时代”如何实现跨平台代码重用 ——程序集重用
除了在源代码层面实现共享("前.NET Core时代"如何实现跨平台代码重用 --源文件重用)之外,我们还可以跨平台共享同一个程序集,这种独立于具体平台的"中性" ...
- 微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器
微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器 在 Build 2015 大会上,微软除了发布了 Microsoft Edge 浏览器和新的 Windows 10 预览 ...
- mvn编写主代码与测试代码
maven编写主代码与测试代码 3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目 ...
- maven编写主代码与测试代码
3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目主代码位于src/main/ja ...
- 通过Intel XDK编写跨平台app(二)
通过Intel XDK编写跨平台app(一) 通过Intel XDK编写跨平台app(二) 在这个系列的上一篇文章中,我们大致了解了Interl XDK的概况.在这一部分中,我们会详细地介绍如何通过这 ...
- 通过Intel XDK编写跨平台app(一)
Intel XDK 是一个新的跨平台手机应用开发工具.它努力把整个开发流程变的简单,尽可能把所有的平台都封装到一个包中,通过收集各种开发工具来使你的开发变的简单. 在这篇文章中,我将会向你介绍什么是I ...
- 用React Native编写跨平台APP
用React Native编写跨平台APP React Native 是一个编写iOS与Android平台实时.原生组件渲染的应用程序的框架.它基于React,Facebook的JavaScript的 ...
随机推荐
- [BZOJ 3530] [Sdoi2014] 数数 【AC自动机+DP】
题目链接:BZOJ - 3530 题目分析 明显是 AC自动机+DP,外加数位统计. WZY 神犇出的良心省选题,然而去年我太弱..比现在还要弱得多.. 其实现在做这道题,我自己也没想出完整解法.. ...
- BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理
Description Ever the maturing businessman, Farmer John realizes that he must manage his time effecti ...
- Android 观察系统中短信内容的变化(内容观察者)
//内容观察者(如果系统的短信发生了变化,比如刚获取一条短信,那么将触发onChange方法) ContentResolver contentResolver = getContentResolver ...
- spring初始化
* Created by litao on 15/12/29. */@Component("initTagDataProcessor")public class InitTagDa ...
- 17.1.1.2 Setting the Replication Slave Configuration
17.1.1.2 Setting the Replication Slave Configuration 在一个复制slave, 你必须创建一个唯一的server ID,如果这个没有做,slave设置 ...
- WordPress RokNewsPager插件‘thumb.php’多个安全漏洞
漏洞名称: WordPress RokNewsPager插件‘thumb.php’多个安全漏洞 CNNVD编号: CNNVD-201309-369 发布时间: 2013-09-24 更新时间: 201 ...
- js怎样生成json的数据
var row1 = {};row1.name = 'david';row1.age = '20'; //或者var row2 = {name: 'peter', age: '23'}; var da ...
- HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- Linux IIO子系统分析
最近稍微看了下LKML,发现里面有一个子系统叫做“IIO”.以前没有接触过,所以Google了下. IIO子系统全称是 Industrial I/O subsystem(工业 I/O 子系统) ...
- Redis 和 Memcached 的区别详解
Redis的作者Salvatore Sanfilippo曾经对这两种基于内存的数据存储系统进行过比较: Redis支持服务器端的数据操作:Redis相比Memcached来说,拥有更多的数据结构和并支 ...