2.1 static_assert 2.2 __same_type 2.3 (type *)0 2.4 offsetof 2.5 container_of 3、总结 Linux Version:6.6Author:DongeGithub:linux-api-insides 1、container_of函数介绍 container_of可以说是内核中使用最为频繁的一个函数了,简单来说,它的主要作用就是根据我们结构体中的已知的成员变量的地址,来寻求该结...
*/#definecontainer_of(ptr,type,member)({\void*__mptr=(void*)(ptr);\static_assert(__same_type(*(ptr),((type*)0)->member)||\__same_type(*(ptr),void),\"pointer type mismatch in container_of()");\((type*)(__mptr-offsetof(type,member)));}) 函数名称:container_of 文件位置:inc...
_Static_assert:C11中引入的关键字,用于判断表达式expr并打印错误信息msg。 在container_of函数中,主要用来断言判断 static_assert( __same_type(*(ptr), ((type *)0)->member) || __same_type(*(ptr), void) , "pointer type mismatch in container_of()" ); 2.2 __same_type /* Are two types/...
Linux Kernel 代码艺术——编译时断言【转】 转自:http://www.cnblogs.com/hazir/p/static_assert_macro.html 本系列文章主要写我在阅读Linux内核过程中,关注的比较难以理解但又设计巧妙的代码片段(不关注OS的各个模块的设计思想,此部分我准备写在“深入理解Linux Kernel” 系列文章中),一来通过内核代码复习一下C...
static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, int maxevents, struct timespec64 *timeout) { int res, eavail, timed_out = 0; u64 slack = 0; wait_queue_entry_t wait; ktime_t expires, *to = NULL; lockdep_assert_irqs_enabled(); if (timeout && (ti...
static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn){struct kobject *kobj = kn->parent->priv; if (kn->flags & KERNFS_LOCKDEP)lockdep_assert_held(kn);return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;} 对此,张银奎笃定:“对于这样的内存溢出问题,这肯定不是第一现场...
static void resched_task(struct task_struct *p){int cpu;assert_spin_locked(&task_rq(p)->lock);if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))return;//设置被被抢占标记set_tsk_thread_flag(p, TIF_NEED_RESCHED);cpu = task_cpu(p);if (cpu == smp_processor_id())return;/* ...
1#include<iostream>2#include<string>3#include<unistd.h>4#include<pthread.h>5#include<assert.h>6using namespace std;7void*thread_routine(void*args)8{9string name=static_cast<constchar*>(args);//安全的进行强制类型转换10while(1)11{12cout<<"这是新线程, name:"<<name<<endl;13sleep(1);...
#if __clang__ struct [[gnu::packed]] #else struct __attribute__((packed)) #endif SS { long long a; int b; long long c; }; static_assert(sizeof(SS) == 20);GCC上对于用 -D 定义的预处理符号,如果其值包含双引号 ",则需要使用转义符 \。比如:-DMY_NAME=\"Smith\"。 GCC编译宏_...
* These bits must not overlap. A static assert check should make sure of that. * Ifadding extra bits, make sure to decrease the bit index below as well. */#defineSPI_MODE_KERNEL_MASK (~(BIT(30) - 1))u32 mode;intirq;void*controller_state;void*controller_data;charmodalias[SPI_NAME_...