当前位置:网站首页>C language implementation of raii
C language implementation of raii
2022-07-24 19:21:00 【senseshield】
Although there are many disputes , But I still think C++ Medium RAII Idioms are a good thing , Also write C Code is the only thing I miss C++ characteristic . Here are some C Language implementation RAII Methods :
gcc
GCC Top available cleanup Extended implementation
#define RAII_VARIABLE(vartype,varname,initval,dtor) \
void _dtor_ ## varname (vartype * v) { dtor(*v); } \
vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
void example_usage() {
RAII_VARIABLE(FILE*, logfile, fopen("logfile.txt", "w+"), fclose);
fputs("hello logfile!", logfile);
}
Windows
Windows You can use SEH Of __try/__finally, Words Windows In fact, there are many things that are quite convenient .
standard C
For better performance and portability , You can also use setjmp/longjmp Mechanism , A simple package is as follows , Use TRY/FINALLY that will do
#define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0: while(1){
#define CATCH(x) break; case x:
#define FINALLY break; } default:
#define ETRY } }while(0)
#define THROW(x) longjmp(ex_buf__, x) The code above is switch Used in while, It uses Duff Device technology ( As shown below ). It's all because C Not allowed in language goto To switch Any of the statements case It's about .
Duff Device
C-FAQ The form given in is :
int n = (count + 7) / 8; /* count > 0 assumed */
switch (count % 8)
{
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
} This is in C It's legal in language ,switch The only function of a statement is ” Fall ” Start at the right place to start . In the above example, such a comparison can be performed 8 Secondary copy operations , At the same time, don't care count Can not be 8 Division and other problems . This operation is a special loop expansion mechanism (loop-unrolling mechanism).(switch Statement case It's actually a label , This is easy to understand ).
This technology is used in particularly low-level code ( Such as driving ) Or specific needs ( Such as C Language implementation RAII) You can use .
---------------------
author :Virbox Technology blogs
source :Virbox Technology blogs
original text :http://blog.virbox.com/2019/06/25/c-%E8%AF%AD%E8%A8%80%E5%AE%9E%E7%8E%B0-raii/
Copyright notice : This article is an original blog article , Please attach a link to the blog post !
边栏推荐
- SATA protocol OOB essay
- FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(下)
- Get module handle / base address
- Introduction to VIM
- Solve the problem of disconnection due to long idle time after SSH login
- profile环境切换
- MySQL version 5.7.9 SQL_ mode=only_ full_ group_ By question
- Crazy God redis notes 11
- 2022 Summer Games of Hangzhou electric power multiple schools 1012aalice and Bob (game theory)
- Day 9 (this keyword and experiment)
猜你喜欢

文献阅读:GoPose 3D Human Pose Estimation Using WiFi

Reading notes of XXL job source code

On July 31, 2022, the dama-cdga/cdgp data governance certification class was opened!

Easily learn pytoch transfer learning to realize surface defect inspection

LSTM and Gru of RNN_ Attention mechanism

day 1

This visual analysis library makes it easy for you to play with data science!

FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 2)

湖仓一体释放全量数据价值,SequoiaDB v5.2线上发布会重磅来袭

Feature extraction tool transformer Bert
随机推荐
Math
PostgreSQL Elementary / intermediate / advanced certification examination (7.16) passed the candidates' publicity
Nezha monitoring - server status monitoring, SSL certificate change expiration, Ping monitoring and scheduled task reminder
PCI express physical layer - electrical part
[in depth study of 4g/5g/6g topic -39]: urllc-10 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -3- how to distinguish urllc services? Detailed expl
MySQL1
Mysql数据库,去重,连接篇
[understanding of opportunity-48]: Guiguzi - Chapter 7 - collect information in advance, make predictions and implementation plans in advance
2022 Hangdian multi school second session 1009 shuangq (Mathematics)
Summary of articles in 2020
Channel state information (CSI) conjugate multiplication denoising method
Taokeeper environment setup
Zooinspector Download
OpenGL learning (III) glut two-dimensional image rendering
FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(下)
JS part
Easily learn pytoch transfer learning to realize surface defect inspection
Meshlab&PCL ISS关键点
Leetcode402 remove K digits
Wireshark simple filter rule