cleanup属性:当变量离开它的作用域时,设置的cleanup_function函数将被调用。

cleanup (cleanup_function)

The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variablesit may not be applied to parameters or variables with static storage durationThe function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored.

If -fexceptions is enabled, then cleanup_function will be run during the stack unwinding that happens during the processing of the exception. Note that the cleanupattribute does not allow the exception to be caught, only to perform an action. It is undefined what happens if cleanup_function does not return normally.

#include <stdio.h>
#include <stdlib.h> #define local_type __attribute__ ((cleanup(my_free))) static void my_free(void* pmem)//the type of paramter must be a void pointer
{
printf("pmem=%p...\n", pmem);
printf("&pmem=%p...\n", &pmem);
void** ppmem = (void**) pmem;
printf("*ppmem=%p...\n", *ppmem);
free(*ppmem);
} #if 0
//warning: passing argument 1 of ‘my_free’ from incompatible pointer type
static void my_free(void** ppmem)
{
printf("ppmem=%p...\n", ppmem);
printf("*ppmem=%p...\n", *ppmem);
}
#endif int foo(void)
{
local_type int* p = (int*) malloc(sizeof(int));
printf("do something, p=%p\n", p); return ;
} int main(int argc, char** argv)
{ foo();
// when return, the memory block pointed by p is freed automatically
printf("11111111\n"); return ;
}

利用cleanup属性来实现智能指针:

 #include <stdlib.h>
#include <stdio.h> struct shared_ptr_s
{
// struct impl_t* inst;
int *use_cnt;
}; typedef struct shared_ptr_s shared_ptr_t; // unadorned type
#define shared_ptr struct shared_ptr_s __attribute__((cleanup(free_shared))) #define SHARED_PTR_GET_ADD_REF(sp_in, sp_name) ++(*sp_in.use_cnt); \
printf("add use_cnt = %d, sp_in=%p\n", *sp_in.use_cnt, sp_in); \
shared_ptr sp_name = sp_in; \
printf("&sp_name=%p, sp_name:%p\n", &sp_name, sp_name); void free_shared(struct shared_ptr_s* ptr)
{
printf("ptr:%p, usr_cnt:%p\n", ptr, ptr->use_cnt);
if(!ptr) return;
printf("del use_cnt = %d\n", *ptr->use_cnt - );
if( == --(*ptr->use_cnt)) {
//dtor(ptr->inst);
printf("freeing %p\n", (void *)ptr->use_cnt);
free(ptr->use_cnt);
}
//ptr->inst = 0;
ptr->use_cnt = ;
} void func(shared_ptr_t sp)
{
SHARED_PTR_GET_ADD_REF(sp, sp_loc);
printf("111111111\n");
return;
} int main(void)
{
shared_ptr_t sp = { // original type does not use __attribute__(cleanup)
//.inst = ctor(),
.use_cnt = malloc(sizeof(int))
};
printf("use_cnt content addr:%p, &use_cnt=%p, &sp=%p,sp=%p\n", sp.use_cnt, &sp.use_cnt, &sp,sp);
SHARED_PTR_GET_ADD_REF(sp, sp_loc); printf("222222222222\n");
func(sp_loc);
printf("333333333\n"); return ;
}

运行结果:

use_cnt content addr:0x9ee7008, &use_cnt=0xbfa11dac, &sp=0xbfa11dac,sp=0x9ee7008
add use_cnt = 1, sp_in=0x9ee7008
&sp_name=0xbfa11da8, sp_name:0x9ee7008
222222222222
add use_cnt = 2, sp_in=0x9ee7008
&sp_name=0xbfa11d74, sp_name:0x9ee7008
111111111
ptr:0xbfa11d74, usr_cnt:0x9ee7008
del use_cnt = 1
333333333
ptr:0xbfa11da8, usr_cnt:0x9ee7008
del use_cnt = 0
freeing 0x9ee7008

__attribute__系列之cleanup的更多相关文章

  1. __attribute__系列之介绍篇

    1.什么是__attribute__? __attribute__机制是GNU C的一大特色,它可以设置函数属性.变量属性和类型属性等.可以通过它们向编译器提供更多数据,帮助编译器执行优化等. 2._ ...

  2. __attribute__系列之aligned

    __attribute__的属性aligned,作用是为了设置字节对齐. aligned是对 变量和结构体进行 字节对齐的属性设置. 通过aligned属性设置(aligned(对齐字节数)),可以显 ...

  3. 如何在 Objective-C 的环境下实现 defer

    关注仓库,及时获得更新:https://github.com/draveness/iOS-Source-Code-Analyze Follow: https://github.com/Dravenes ...

  4. libextobjc 实现的 defer

    算法沉思录:分而治之(复用): 分而治之是指把大而复杂的问题分解成若干个简单的小问题,然后逐个解决.这种朴素的思想来源于人们生活与工作的经验,也完全适合于技术领域. 要崩溃的节奏: 要崩溃的节奏: V ...

  5. 黑魔法__attribute__((cleanup))

    原文地址:http://blog.sunnyxx.com/2014/09/15/objc-attribute-cleanup/ 编译器属性__attribute__用于向编译器描述特殊的标识.检查或优 ...

  6. [转]GCC系列: __attribute__((visibility("")))

    在 objc-api.h 里面有很多关于__attribute__ 的定义. 例如 #if !defined(OBJC_VISIBLE) # if TARGET_OS_WIN32 # if defin ...

  7. Objective-C 源码初探 __attribute__

    #import <Foundation/Foundation.h> //延迟执行,delayFunc函数即为延迟执行的函数 #define onExit\ __strong void (^ ...

  8. 《Entity Framework 6 Recipes》中文翻译系列 (11) -----第三章 查询之异步查询

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第三章 查询 前一章,我们展示了常见数据库场景的建模方式,本章将向你展示如何查询实体 ...

  9. Java多线程系列--“JUC线程池”03之 线程池原理(二)

    概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...

随机推荐

  1. [CF494C]Helping People

    题意:有$n$个穷人,每个穷人有$a_i$的钱,有一个富人决定做$q$次捐赠$(l_i,r_i,p_i)$,表示他有$p_i$的概率给$[l_i,r_i]$的人捐$1$的钱,捐赠的价值为捐赠后最富的人 ...

  2. [CF983D]Arkady and Rectangles

    题意:按顺序在坐标轴上画$n$个颜色为$1\cdots n$的矩形(数字大的颜色覆盖数字小的颜色),问最后能看到多少种颜色 先离散化,然后考虑扫描线+线段树 线段树每个节点用一个set存覆盖整个区间的 ...

  3. 【带修莫队】【权值分块】bzoj3196 Tyvj 1730 二逼平衡树

    这题用了三种算法写: 分块+二分:O(n*sqrt(n*log(n)) 函数式权值分块:O(n*sqrt(n)) 带修莫队+权值分块:O(n5/3) 结果……复杂度越高的实际上跑得越快……最后这个竟然 ...

  4. 【进制转换】CODEVS 1740 进制计算器

    #include<cstdio> #include<iostream> #include<string> using namespace std; string s ...

  5. STL之vector4

    描述 将一个n行m列矩阵元素输入存储后并输出. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { vector< vector<int> > ...

  6. cocos2dx 3.13 etc1 ClippingNode Bug 修正

    void TrianglesCommand::useMaterial() const { if(_stencl){ /*******[solyess] etc1 mask的支持************ ...

  7. Problem P: 素数求和

    #include<stdio.h> int main() { ; scanf("%d",&n); n>=&&n<=; ;i<= ...

  8. 十. 图形界面(GUI)设计5.布局设计

    在界面设计中,一个容器要放置许多组件,为了美观,为组件安排在容器中的位置,这就是布局设计.java.awt中定义了多种布局类,每种布局类对应一种布局的策略.常用的有以下布局类: FlowLayout, ...

  9. JQurey中getJSON方法错误回调方法

    1.使用try...catch实现 2.换$.ajax 3.JQuery 1.5+可以这样使用: $.getJSON("example.json", function() { al ...

  10. javascript快速入门21--DOM总结

    跨浏览器开发 市场上的浏览器种类多的不计其数,它们的解释引擎各不相同,期待所有浏览器都一致的支持JavaScript,CSS,DOM,那要等到不知什么时候,然而开发者不能干等着那天.历史上已经有不少方 ...