28

ngx_align 值对齐宏

 4 years ago
source link: http://www.cnblogs.com/shuqin/p/13832722.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ngx_align 值对齐宏

ngx_align 为nginx中的一个值对齐宏。主要在需要内存申请的地方使用,为了减少在不同的 cache line 中内存而生。

// d 为需要对齐的
// a 为对齐宽度,必须为 2 的幂
// 返回对齐值
#define ngx_align(d, a)     (((d) + (a - 1)) & ~(a - 1))

原理简单,利用 ~(a - 1) 的低位全为 0。在与 ~(a - 1)& 操作时,低位的1被丢弃,就得到了a倍数的值(对齐)。

如果使用原始值直接与 ~(a - 1)& 操作,那么得到的对齐值是会小于等于原始值的,这样会造成内存重叠,而期望的对齐值是一个大于等于原始值的,所以需要加上一个数来补上至对齐值这中间的差,这个数为 (a - 1) ,选择这个数的原因是 (a - 1) & ~(a - 1) 的结果为0:

mIJvAvF.png!mobile

Recommend

  • 14

    基于ngx_lua模块的waf开发实践 zhangsan

  • 7

    ngx_lua_waf适应多站点情况的研究 zhangsan

  • 16
    • blog.huoding.com 5 years ago
    • Cache

    关于OpenResty里的ngx.on_abort

    关于 OpenResty 里的 ngx.on_abort ,官方文档里是这样说明的: Registers a user Lua function as the callback which gets called automatically...

  • 17

    Example of Angular 4 Pagination Using ngx-pagination This tutorial help to implement Angular 4 pagination into HTML table using ngx-pagination.I will demonstrate how to implement HTML listing with pagination us...

  • 11

    让 Vim 的 Align 插件记住常用的对齐方式 本文来自依云's Blog,...

  • 19
    • www.digitalocean.com 3 years ago
    • Cache

    How To Use ngx-translate with Angular

    Tutorial How To Use ngx-translate with Angular Angular Introduction At some point,...

  • 4
    • nginx.org 3 years ago
    • Cache

    Module ngx_http_ssl_module

    Module ngx_http_ssl_module The ngx_http_ssl_module module provides the necessary support for HTTPS. This module is not built by default, it should be enabled with the --with-http_ssl_module configu...

  • 6
    • fann.im 2 years ago
    • Cache

    ngx_lua vs Go

    ngx_lua vs Go Nov 7, 2015 由于移动互联网的火爆,前后端分离的开发模式越来越流行:后端通过 API 提供数据,前端 native or web 做数据展示交互。那么谁能把吐数据这件事做的又快又好,谁就比...

  • 3
    • www.jansora.com 2 years ago
    • Cache

    Openresty 常用的 ngx 包和方法

    openresty 都有哪些 ngx 包或方法 OpenResty 是一个基于 Nginx 和 LuaJIT 的 Web 平台,提供了大量的模块和库以便开发高性能的 Web 应用。以下是一些常见的 OpenResty 与 Nginx 相关的 ngx 模块和方法: ngx_http_lua_mo...

  • 8

    Flutter 的 Align 对齐组件学习笔记 Align 对齐组件 alignment:子元素的位置 Alignment.topLeft:左上Alignment.topCenter:上Al...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK