关于opaque

Swift5 17.不透明类型Opaque Types, Memory Safety 目录Opaque Types Opaque Types 阅读详情

有这么一行代码:

typedef struct __opaque yaffs_DIR;
   

而该头文件中并没有任何关于struct __opaque的定义,搜寻了一下系统里的其他头文件,也没有找到。google了一下才找到一篇介绍这种用法的文章Programming Tools - Opaque Pointers。

opaque直译的意思是不透明的,C语言中允许通过typedef申明一个抽象的结构体类型,如上例所示,你无需定义struct __opaque的具体实现,就能在其他函数的声明中使用该数据类型的指针。注意,只能是指针,如果是void foo(yaffs_DIR dir),系统就会提示error: dir has incomplete type。

在库文件中,opaque类型的实现如下面的代码所示:

typedef struct
   
{
   
    __u32 magic;
   
    yaffs_dirent de;            /* directory entry being used by this dsc */
   
    char name[NAME_MAX+1];      /* name of directory being searched */
   
    yaffs_Object *dirObj;       /* ptr to directory being searched */
   
    yaffs_Object *nextReturn;   /* obj to be returned by next readddir*/
   
    int offset;
   
    struct list_head others;    
   
} yaffsfs_DirectorySearchContext;
   

   
     
   
yaffs_DIR *dir = NULL;
   
yaffsfs_DirectorySearchContext *dsc = NULL;
   
...
   
dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
   
dir = (yaffs_DIR *)dsc;
   

代码中为抽象类型yaffs_DIR的指针分配了一个具体类型yaffsfs_DirectorySearchContext的空间。而这一层对使用者是不可见的,也许这是opaque这个名字的由来,这种做法可以提高库文件升级过程中对外接口的稳定性。

最后提一点,opaque并不是一个关键词,你可以任意使用其他名字,只是用opaque更能明确地表示这是一个希望对用户隐藏内部结构的数据类型。


Programming Tools - Opaque Pointers

One of the most powerful concepts when writing software is abstraction - hiding the details of a system behind a simpler interface. This article shows you how to use a language feature of C (and C++) to provide a very powerful form of abstraction for use in libraries: opaque pointers.

C/C++ has an interesting language feature. If you declare a typedef of a structure pointer, you don't need to provide that structure's definition. For example:

typedef struct _hidden_struct *handle;

This has declared a new type, handle, which is a pointer to a struct _hidden_struct. What's interesting about this? Mainly the fact that you can now use this handle type without ever having a definition of the structure it's a pointer to. Why is this powerful? That's what this article will show you (some people might already see why). Please note that the typedef above (and those used throught this article) aren't strictly needed to hide the structure's internal definition - you can just keep using the struct keyword as part of the name. However, the typedefs are used to make an abstraction away from the fact that there's a structure at all, instead of turning it into a "handle."

Sample problem

To really see the power of opaque pointers, we'll need a problem to solve. Let's suppose we want to make an image library that loads and saves bitmaps. We know that as time goes on this library will need to be able to do more things (new image types, basic transforms) and we want to preserve both compile time and runtime compatibility for this library. This means that if we have an older application, it should work with a new shared library and that if that older application is rebuilt against the new library and headers, it should still build without errors.

What about C++?

Before this goes much further, I need to address the waving hands of all the C++ fans out there, who might be thinking: "C++ already lets me do all of this behind a nice class interface with inheritance and other nice C++ language features". And this is pretty much true in the case of compile-time compatibility. But, because C++ doesn't let you separate the public and private definitions of a class, the way the class is declared changes its runtime behavior (class size, vtable offsets, etc.) - you can't provide runtime compatibility without a lot of tender care (and sometimes special compilers). So, this is still useful to C++ people, even if the slant is more towards those of us using C.

Okay, back to the library. When people design libraries like this, they'll often declare a structure that will get filled in by the library and an API for manipulating this structure. For example:

typedef struct

{

void *ptr;

int size;

int bytes_per_pixel;

} bitmap_t;

int bitmap_load_file( char *filename, bitmap_t *bitmap );

int bitmap_save_file( char *filename, bitmap_t *bitmap );

So, to use this library, you would declare a bitmap_t variable and invoke bitmap_load_file() with a filename and a pointer to the bitmap_t that was declared. For example:

bitmap_t bitmap;

int ret;

ret = bitmap_load_file( "sample.bmp", &bitmap );

Then the user of the library could simply access the pointer inside the structure and, along with the size and bytes_per_pixel members, go to work on the image. However, because the contents of bitmap_t are known publicly, if the library is updated with new entries in that structure, then the size of the structure will have been changed. And applications that use an updated shared library will probably crash or Other Bad Things when they call into the new library with a structure of a smaller (different) size.

Alternatively, the API could be defined to take a structure size:

int bitmap_load_file( char *filename, bitmap_t *bitmap, int size );

int bitmap_save_file( char *filename, bitmap_t *bitmap, int size );

and would be used like this:

ret = bitmap_load_file( "sample.bmp", &bitmap, sizeof( bitmap_t ) );

Which effectively tags the version of the library by using the size of the structure. However, this makes for a terrible support nightmare inside the library where the structure size has to be checked and different code paths are taken based on this structure size. This leads to a lot of code bloat.

We could also try to avoid the structure size issue by padding the structure with some "reserved" space:

typedef struct

{

void *ptr;

int size;

int bytes_per_pixel;

char reserved[64];

} bitmap_t;

But this is only a temporary fix. What if we didn't choose a value that's large enough? Then we're back to the case where a new library causes problems. If we're liberal with our reserved space, then we waste memory. (Since you're reading this on a QNX web page, I'm guessing that wasting memory doesn't sit well with you either.)

Another problem common to all of these approaches is what can occur if the layout of the structure changes. Say a new version of the library is built with a structure definition that looks like this:

typedef struct

{

int version;

void *ptr;

int size;

int bytes_per_pixel;

} bitmap_t;

Then the compiled apps will also get "confused", since what was previously the structure member ptr is now version, and so on. The position of a structure member within a structure is important. Seems pretty much impossible to meet our goals, huh? Fear not, loyal readers, the situation isn't that dire!

Hiding the structure's internals

The common thread to all the situations above was that the compiled application was aware of the size of the structure and the location in the structure of the structure members. So we need to hide the internals of the structure and provide access functions to get the important data out of the structure.

Let's try out a new public interface:

typedef struct _internal_bitmap * bitmap_t;

int bitmap_alloc( bitmap_t *bitmap );

int bitmap_free( bitmap_t *bitmap );

int bitmap_load_file( bitmap_t bitmap, char *filename );

int bitmap_save_file( bitmap_t bitmap, char *filename );

int bitmap_get_ptr( bitmap_t bitmap, void **ptr );

int bitmap_get_size( bitmap_t bitmap, int *size );

int bitmap_get_bpp( bitmap_t bitmap, int *bpp );

And now we can maintain a private interface that applications never get to see or use, only the library:

struct _internal_bitmap

{

void *ptr;

int size;

int bytes_per_pixel;

}

Did you notice the opaque pointer? Also, notice we've added "access functions" to get the interesting data from the new bitmap "handle"? It's pretty obvious now that we're passing in a bitmap_t (a structure pointer) as a handle to the library, but the alloc and free functions are a little confusing for people.

When we declare a bitmap_t, we're really just declaring a pointer to a structure, so we need to provide some memory for that pointer to point at. Here are the guts of the bitmap_alloc() function:

int bitmap_alloc( bitmap_t *bitmap )

{

struct _internal_bitmap *handle;

handle = ( struct _internal_bitmap * )malloc( sizeof( *handle ) );

if( handle == NULL )

{

return -1;

}

memset( handle, 0, sizeof( *handle ) );

*bitmap = handle;

return 0;

}

Since a bitmap_t is just a struct pointer, we allocate the proper sized struct (which we can do - this code is part of the library and it knows how big the structure is). Once we've verified that the malloc() didn't fail, we assign the newly allocated structure to the bitmap_t pointer. So when the application calls this function, it will get back the proper sized structure to pass into the rest of the library functions.

Here's an example of an "access function" that uses the allocated bitmap handle:

int bitmap_get_ptr( bitmap_t bitmap, void **ptr )

{

if( ptr == NULL ){  return -1; }

*ptr = bitmap->ptr;

return 0;

}

Since the library knows the definition of the _internal_bitmap structure, it can directly access its members. If you tried to access the internals of the bitmap_t handle in application code, the compiler would return an error, because it has no idea how the structure is organized or what any of its members are named.

For the last bit of code, I'll write a function that loads a bitmap, sets all the pixels to 255, and writes the bitmap back:

int turn_bitmap_white( char *filename )                                              {

int ret, i;

bitmap_t bitmap;

unsigned char *ptr;

int size;

ret = bitmap_alloc( &bitmap );

if( ret )

return ret;

ret = bitmap_load_file( bitmap, filename );

if( ret )

return ret;

ret = bitmap_get_ptr( bitmap, (void **)&ptr );

ret |= bitmap_get_size( bitmap, &size );

if( ret )

return ret;

for( i=0; i<size; i++ )

{

ptr[i] = 255;

}

ret = bitmap_save_file( bitmap, filename );

if( ret )

return ret;

bitmap_free( &bitmap );

return 0;

}

Problem solved

So, we've solved our problem. If we change the structure layout, we'll be okay, since the application code can't access the internals of the structure directly and must use "access functions" to get at the internal data.

If we change the size of the structure, we'll be okay, since the library itself allocates the memory for the structure and knows the proper size of the structure to allocate for the given version of the library. You can now replace the library (in shared library form) without having to rebuild any applications. And you can rebuild applications against the library without change. Now, this does assume that you haven't changed the API to your library - opaque pointers are a powerful tool, but they can't perform magic.

Lastly, the use of opaque pointers enforces good programming practice by providing a defined interface (abstraction) between the application and the library. This is usually a good method even when doing your own projects, since it lets you easily separate functionality for future projects!

 

AI训练师团队管理运营思路 工具运营AI训练师主要是支撑运营,协助算法和运营提高托管机器人的解决能力,以协助降低业务场景熔断与优化场景识别为核心运营目标,通过算法聚类、标注分析等方式,结合业务知识,提供精准、清晰的标注数据和解决方案,确保训练效果满足需求。过程中需要与算法、运营一块参与模型搭建和数据验收,并且负责核心指标和数据日常的跟踪维护。④针对知识库已有意图进行梳理和分析,反馈运营评估缺失或合并意图优化方向。③对语料数据进行挖掘、清洗,解除缠绕、冗余等问题导致的未识别。①线上case分析,及时发现识别问题导致的托管熔断问题。 阅读详情

相关推荐

U盘拒绝访问的根源与权限修复全指南

U盘‘拒绝访问’是Windows权限机制的典型表现,本质源于用户SID与NTFS安全描述符(Security Descriptor)匹配失败,而非硬件故障或文件系统损坏。其原理涉及UAC控制、ACL继承中断、所有权归属缺失及组策略压制等多层机制。技术价值在于无需格式化即可恢复访问,保障数据零丢失;典型应用场景包括跨电脑使用U盘、Win11家庭版无gpedit.msc、OEM预装软件干扰等。本文聚焦U盘权限修复核心路径,结合icacls命令重置继承、takeown夺回所有权、注册表绕过组策略限制等实操方法,覆

weixin_31742871的博客 333

C++ (opaque) handle

C++ (opaque) handlehandleopaque handle參考連結 handle Handle是一個指標或索引,被用來存取某項資源。範例可以參考What is a handle in C++? - jmucchiello。 opaque handle Opaque handle則是一個在定義時看不到它的內容的一種handle。詳見Opaque Pointer。 在TensorRT...

keineahnung2345的博客 836

3分钟!教会你用 DeepSeek/Claude3.7+Cursor 快速入门AI编程(喂饭级教程)

3分钟!教会你用 DeepSeek/Claude3.7+Cursor 快速入门AI编程(喂饭级教程)

yizhen91y的博客 4727

Diem Move Prover 性能实验室:opaque 与 non-opaque 函数规格的验证耗时对比实践

本文以 `language/move-prover/lab/data/opaque/README.md` 记载的实验为主体,深入剖析 Diem Move Prover 中 `opaque` 规格(pragma)对验证性能的影响。通过默认 opaque、仅忽略内部函数 opaque、全局忽略 opaque 三种配置,量化"函数内联展开 vs 仅使用前置/后置条件"给求解器带来的负载差异,并完整给出基

gitblog_00454的博客 648

海康VisionMaster使用学习笔记7-Group模块

光标需放在[]中选择循环索引,循环索引用于实现依次检测10个匹配框的顶点,若不订阅,模块会循环检测同一个匹配框。可以看到绿色是它的边缘点.为方便查看顶点,将边缘点设置为不可见。循环使能打开,开始值设为0,循环结束值设为快速匹配的匹配个数。回到上一层,点击Group工具的设置图标,圆拟合的点设置为Group工具输出的点集。图像输入源选择Group的图像输入源。区域选择Group的快速匹配的匹配框。添加订阅到Group内部的顶点检测。对匹配框区域进行顶点检测。用于定位匹配框中的顶点。添加快速匹配的匹配框。

风凌的博客 4181

GPU架构-SIMT

本文深度剖析了GPU的SIMT架构,从硬件实现层面解析其与SIMD的本质差异。SIMT通过将32个线程捆绑为warp执行,在标量编程模型下实现数据并行。文章对比了SIMD和SIMT的执行模型、分支处理机制及存储层次,指出SIMT是对SIMD的虚拟化抽象。重点分析了Volta架构引入的独立线程调度技术,以及访存合并对性能的关键影响。通过定量数据展示了各代GPU架构的寄存器文件规模和线程调度机制,为开发者优化GPU程序提供了硬件层面的理论基础。

技术引领业务创新 976

什么是OPAQUE协议

它的目标是安全地管理密码验证过程,使用户无需向服务器透露密码本身,也能实现安全的认证。这对保护用户密码免受泄露攻击特别有效,即使服务器遭到入侵,攻击者也无法直接获得用户密码。

航哥的女人最优秀 1150

5、Visual Studio Code:强大的编码与文件管理功能

本文详细介绍了 Visual Studio Code 的强大编码与文件管理功能,涵盖代码导航、引用查找、符号重命名、实时代码分析、IntelliCode 提示、文件和文件夹操作等内容。通过功能对比、流程图和实际开发案例,展示了 VS Code 如何提升开发效率和代码质量,适用于各类编程语言和项目开发场景。

water的专栏 134

为什么设置了view的Opaque性能会提高(GPU)

Opaque该属性为BOOL值,UIView的默认值是YES,但UIButton等子类的默认值都是NO。opaque表示当前UIView是否不透明,不过搞笑的是事实上它却决定不了当前UIView是不是不透明,比如你将opaque设为NO,该UIView照样是可见的(上文说过,是否可见是由alpha或hidden属性决定的),照理说为NO就表示透明,那就应该是不可见的呀? 显示器中

iOS开发之路 3642

手把手教你学simulink实例--基于Simulink的电动汽车动力总成控制策略验证与标定仿真平台

电动汽车的动力总成(Powertrain)是车辆的核心系统,包括动力电池、电机控制器、逆变器和驱动电机等关键部件。动力总成的控制策略直接影响车辆的动力性能、能耗效率和驾驶体验。为了验证和优化这些控制策略,基于MATLAB/Simulink的仿真平台成为一种高效且经济的解决方案。本项目旨在构建一个完整的基于Simulink的电动汽车动力总成控制策略验证与标定仿真平台,涵盖控制策略建模、性能评估以及参数标定等功能模块,并通过仿真验证系统的有效性。

MHD0815的博客 300

使用DrissionPage实现携程酒店信息智能爬取

本文介绍了一个基于DrissionPage技术的携程酒店信息爬虫系统,通过监听API数据包实现高效采集。系统可自动获取酒店名称、价格、评分、位置等15个维度的数据,支持多页滚动加载和智能去重。相比传统爬虫,该方法直接解析API返回的JSON数据,避免了复杂的HTML解析和加密参数分析,提高了开发效率和稳定性。文章详细阐述了13个关键实现步骤,并强调数据采集应严格遵守法律法规,仅用于技术学习和市场研究。该系统可为旅游决策、价格监测等提供数据支持,但需注意合理使用频率和隐私保护。

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 392

iOS开发-Opaque、Alpha、Hidden区别

opaque表示当前的UIView的不透明度,设置是否之后对于UIView的显示并没有什么影响,官方文档的意思简单点说:opaque默认为YES,如果alpha小于1,那么应该设置opaque设置为NO,但是如果alpha为1,opaque设置为NO,产生的后果是不可预料的~ This property provides a hint to the drawing system as to how...

chenzimin_blog 1039

YOLOv10精度上不去?QAT量化感知训练全流程,INT8推理mAP只掉0.8%

普通PTQ量化虽简单,但无法应对YOLOv10的小目标特征和激活分布波动,导致精度暴跌。QAT通过“模拟量化误差+联合优化权重与量化参数”,让INT8模型精度接近FP16,同时速度提升1.8倍,完美适配实时检测场景(如安防监控、自动驾驶)。该流程已在工业级项目中验证,针对YOLOv10的优化策略(敏感层保护、小目标加权、分阶段训练)可直接迁移至其他YOLO系列(v8/v9),量化后精度损失均可控制在1%以内。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 916

ios开发之View属性hidden, opaque, alpha, opacity的区别

一、alpha 液晶显示器是由一个个的像素点组成的,每个像素点都可以显示一个由RGBA颜色空间组成的一种色值。其中的A就表示透明度alpha,UIView中alpha是一个浮点值,取值范围0~1.0,表示从完全透明到完全不透明。 1、alpha值只会影响自己,不会影响subView的背景颜色。 2、alpha设为空之后,UIView不会从响应者链中移除,仍然可以收到event。

qq_21649645的专栏 9562

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

OpenGL ES 【Opaque 属性在 CAEAGLLayer 中的配置详解】

为什么在设置 CAEAGLLayer 时,要将其 opaque 为 YES。另比较阐述 opaque(不透明度)与 alpha、hidden 的区别。 剖析公式 Result = Source + Destination * (1 - SourceAlpha)

CoderZYWang 1715

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

view 的 opaque 属性

给绘图系统提供一个性能优化的开关, 如果设置为 YES,绘图系统把这个视图当做一个不透明的来对待,这样就会提高性能, 如果设置为 NO,绘图系统就会把它和其他的普通view一样来对待,不去做优化操作. 所以说应该把 view 的 opaque 设置为 YES.

不夜城的Caster 686

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

隐私计算之MPC(安全多方计算)工具OPAQUE

OPAQUE是一个基于密码学的隐私计算工具,它使用安全多方计算(MPC)协议来实现保护用户隐私的应用程序。MPC是一种协议,允许多个参与方在不暴露私密数据的情况下进行计算。在OPAQUE中,参与方之间可以安全地进行计算,而无需将原始数据传输给其他参与方或中心化的服务器。OPAQUE的核心思想是使用零知识证明(Zero-Knowledge Proofs)和伪随机函数(Pseudo-Random Functions)来确保计算的安全性和隐私性。

runqu的博客 1573

Opaque

要注意,首先尽量将view设置为Opaque,如果view是不透明的,那么应该将其opaque属性设置为YES。   为什么要这样做呢?其实是有一定的原因的,一起来看一下。这样设置ios可以让系统以最优的方式来绘制view。opaque属性可以在Interface Builder或代码中设置。   苹果的官方文档对opaque属性有如下解释:This property provides a

Nero的专栏 1510

iOS:Alpha、Hidden 与 Opaque 区别

UIView中的这三个属性用的比较多,尤其是 Alpha 和 Opaque 之间有的时候不是很好分别: Alpha(不透明度) alpha 是不透明度,属性为浮点类型的值,取值范围从 0 到 1.0,表示从完全透明到完全不透明,其特性有当前 UIView 的 alpha 值会被其所有 subview 继承。alpha 值会影响到 UIView 跟其所有 subview,alpha ...

GSNICE 2707

oracle opaque_transform,hint OPAQUE_TRANSFORM产生的原因

在AWR报告中经常看到一个hint /*+ OPAQUE_TRANSFORM */,感到莫名其妙。原因是什么引起的呢?如果在数据库B的AWR中看到这种hint,原在AWR报告中经常看到一个hint /*+ OPAQUE_TRANSFORM */,感到莫名其妙。原因是什么引起的呢?如果在数据库B的AWR中看到这种hint,原因是其他数据库通过DB link访问数据库B,且用的是insert into...

weixin_31907307的博客 346

Opaque SQL 项目教程

Opaque SQL 项目教程 1. 项目的目录结构及介绍 Opaque SQL 项目的目录结构如下: opaque-sql/ ├── build/ ├── data/ ├── docker/ ├── opaque-sql-docs/ ├── project/ ├── scripts/ ├── src/ │ ├── clang-format │ ├── gitignore │ ├── ...

gitblog_00493的博客 769

ios开发之View属性hidden, opaque, alpha的区别

1. @property(nonatomic) CGFloat alpha; The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque.This v

CaryaLiu的专栏 9472

JSON Web Token(JWT)对比Opaque Token

JWT Opaque

逍遥云中鸟 3247

UIView 中 hidden、alpha、clear color 与 opaque 的区别

透明度与图层混合相关,影响到图片绘制的效率。 hidden 此属性为 BOOL 值,用来表示 UIView 是否隐藏。关于隐藏大家都知道就是让 UIView 不显示而已,但是需要注意的是: 当前 UIView 的所有 subview 也会被隐藏,忽略 subview 的 hidden属性。UIView 中的 subview 就相当于 UIView 的死忠小弟,老大干什么我们就跟...

diaoju3333的博客 269

Opaque Layers保护用户隐私推理

Opaque Layers技术通过可信执行环境与加密计算结合,实现AI推理中的端到端数据隐私保护,兼顾安全性与效率,适用于医疗、金融等敏感场景。

weixin_32687875的博客 1059

iphone UIView的opaque属性详解

该属性用与决定该消息接收者(UIView instance)是否让其视图不透明。 其用处在于:给绘图系统提供一个性能优化开关。如果该值为YES, 那么绘图在绘制该视图的时候把整个视图当作不透明对待。这样,绘图系统在执行绘图过程中会优化一些操作并提升系统性能;如果是设置为NO, 绘图系统将其和其他内容平等对待,不去做优化操作。为了性能方面的考量,默认被置为YES(意味着‘优化’)。 另一

森... 2660
上一篇: xfs服务
下一篇: 关于wireshark插件开发.
proing
博客等级 码龄19年 2092粉丝 94原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值