an implement of sizeof guaranteeing bit alignment

#define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )

The above macro simply aligns the size of n to the nearest greater-or-equal sizeof(int) boundary.

The basic algorithm for aligning value a to the nearest greater-or-equal arbitrary boundary b is to

  1. Divide a by b rounding up, and then
  2. Multiply the quotient by b again.

In the domain of unsigned (or just positive) values the first step is achieved by the following popular trick

q = (a + b - 1) / b
// where `/` is ordinary C-style integer division (rounding down)
// Now `q` is `a` divided by `b` rounded up

Combining this with the second step we get the following

aligned_a = (a + b - 1) / b * b

In aligned_a you get the desired aligned value.

Applying this algorithm to the problem at hand one would arrive at the following implementation of _INTSIZEOF macro

#define _INTSIZEOF(n)\
  ( (sizeof(n) + sizeof(int) - 1) / sizeof(int) * sizeof(int) )

This is already good enough.

However, if you know in advance that the alignment boundary is a power of 2, you can "optimize" the calculations by replacing the divide+multiply sequence with a simple bitwise operation

aligned_a = (a + b - 1) & ~(b - 1)

That is exactly what‘s done in the above original implementation of _INTSIZEOF macro.

This "optimization" might probably make sense with some compilers (although I would expect a modern compiler to be able to figure it out by itself). However, considering that the above _INTSIZEOF(n)macro is apparently intended to serve as a compile-time expression (it does not depend on any run-time values, barring VLA objects/types passed as n), there‘s not much point in optimizing it that way.

origin: c - an implement of sizeof guaranteeing bit alignment - Stack Overflow

material: 内存对齐#define _INTSIZEOF(n) ((sizeof(n)+sizeof(int)-1)&~(sizeof(int) - 1) )

an implement of sizeof guaranteeing bit alignment

时间: 2024-09-29 17:47:55

an implement of sizeof guaranteeing bit alignment的相关文章

linux内核-红黑树

//rbtree.h /*   Red Black Trees   (C) 1999  Andrea Arcangeli <[email protected]>     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 Foun

红茶一杯话Binder(传输机制篇_上)

红茶一杯话Binder (传输机制篇_上) 侯 亮 1 Binder是如何做到精确打击的? 我们先问一个问题,binder机制到底是如何从代理对象找到其对应的binder实体呢?难道它有某种制导装置吗?要回答这个问题,我们只能静下心来研究binder驱动的代码.在本系列文档的初始篇中,我们曾经介绍过ProcessState,这个结构是属于应用层次的东西,仅靠它当然无法完成精确打击.其实,在binder驱动层,还有个与之相对的结构,叫做binder_proc.为了说明问题,我修改了初始篇中的示意图

c malloc分配内存

php中的内存分配有用类似emalloc这样的函数,emalloc实际上是C语言中的malloc的一层封装,php启动后,会向OS申请一块内存,可以理解为内存池,以后的php分配内存都是在这块内存池中进行的,以至于efree,也不会向OS退回内存,而只是设置标志位,标识efree这块内存不再使用了,这样做的好处是,速度快,避免系统调用,因为频繁的从用户态和内核态之间的切换是很费CPU的. C语言的malloc函数的后面是glibc(内存管理系统) , 前段时间在看到php内存分配时,看到了ema

bullet HashMap 内存紧密的哈希表

last modified time:2014-11-9 14:07:00 bullet 是一款开源物理引擎,它提供了碰撞检测.重力模拟等功能,很多3D游戏.3D设计软件(如3D Mark)使用它作为物理引擎. 作为物理引擎,对速度的要求是非常苛刻的:bullet项目之所以能够发展到今天,很大程度取决于它在速度上优异的表现. 翻阅bullet的源码就能看到很多源码级别的优化,本文将介绍的HashMap就是一个典例. bullet项目首页:http://bulletphysics.org/ 注:b

union, enum, and struct, 以及结构填充和位字段实现。

Table 4-9 Compiler storage of data objects by byte alignment Type Bytes Alignment char, bool, _Bool 1 Located at any byte address. short, wchar_t 2 Located at any address that is evenly divisible by 2. float, int, long, pointer 4 Located at an addres

flutter手势

import 'package:flutter/material.dart'; import 'package:flutter_app/pages/dismissed_page.dart'; class GestureAppPage extends StatefulWidget { @override State<StatefulWidget> createState() { // TODO: implement createState return new _GestureAppPageSt

《python解释器源码剖析》第17章--python的内存管理与垃圾回收

17.0 序 内存管理,对于python这样的动态语言是至关重要的一部分,它在很大程度上决定了python的执行效率,因为在python的运行中会创建和销毁大量的对象,这些都设计内存的管理.同理python还提供了了内存的垃圾回收(GC,garbage collection),将开发者从繁琐的手动维护内存的工作中解放出来.这一章我们就来分析python的GC是如何实现的. 17.1 内存管理架构 在python中内存管理机制是分层次的,我们可以看成有四层,0 1 2 3.在最底层,也就是第0层是

Data alignment漫谈

特此声明:本人CSDN中的所有文章未经授权,不得转载,违者必究!一切被授权人.组织等的信息都会记录在本人CSDN授权文章的列表中. 以下讨论的概念性的东西应该都是适用于所有系统的,但是实际操作都是linux系统做的. 讨论基于单线程处理,目的是为了简化讨论,简化测试,但并不影响对理论的验证. 最后附上验证源码以及其解释. 背景之个人理解 本节内容都是个人的理解,如果有不正确的地方,欢迎讨论. 说内存对齐之前,先说说硬盘对数据的组织方式.我们知道,文件系统管理数据是按照块来管理的,假如说一个4k对

sizeof函数详解

原文链接http://blog.csdn.net/wzy198852/article/details/7246836 sizeof,一个其貌不扬的家伙,引无数菜鸟竟折腰,小虾我当初也没少犯迷糊,秉着“辛苦我一个,幸福千万人”的伟大思想,我决定将其尽可能详细的总结一下.但当我总结的时候才发现,这个问题既可以简单,又可以复杂,所以本文有的地方并不适合初学者,甚至都没有必要大作文章.但如果你想“知其然,更知其所以然”的话,那么这篇文章对你或许有所帮助.菜鸟我对C++的掌握尚未深入,其中不乏错误,欢迎各