gpt4 book ai didi

c - 替代 container_of()

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:38 28 4
gpt4 key购买 nike

我是一个尝试编写串行驱动程序(基于 PCI)的新手,我不想使用 container_of() 因为缺乏向下兼容性。我可能会编译模块的内核版本将是 < 2.6.x,所以我想让它与大多数新旧版本兼容。

我想访问串行卡驱动程序的结构成员。该结构是一个包含原子变量的自定义结构,例如 - use_count 及其相关操作 - atomic_inc(&serial_card->use_count )。我不想使用 container_of() 函数来访问它们,该函数会给我包含结构container_of() 函数是否有任何替代方法。如果我没记错的话,Allesandro Roubini 撰写的文本 LINux 设备驱动程序在第 174 页上描述了一种方法 |第 6 章:高级字符驱动程序操作。但我仍然对如何分配类似 struct scull_dev *dev = &scull_s_device 的内容感到困惑。如果结构本身包含类型为 struct pc_device *dev 的变量,则上述语句会填充一个类似的变量并分配给 dev,

在我的例子中,我声明了一个结构和相关的函数,如下所示

struct serial_card
{
unsigned int id; // to identify the each card
//atomic_t use_count; // variable used to check whether the device is already opened or not
wait_queue_head_t rx_queue[64]; // queue in which the process are stored
unsigned int data_ready[64]; // queue in which the process is ready
unsigned int rx_chan; // used by interrupt handler
unsigned int base, len; // holds physical base address , holds the total area ( for each card )
unsigned int *base; // holds virtual address
/*struct cdev cdev; // kernel uses this structure to represent the EACH char device
not using the new method to represent char devices in kernel instead using the old method of register_chrdev();*/

struct pci_dev *device; // pci_dev structure for EACH device.
//struct semaphore sem; //Semaphore needed to handle the co-ordination of processes,use incase need arises
};

static struct serial_card *serial_cards; // pointer to array of structures [ depending on number of cards ],NO_OF_CARDS #defined in header file


static int serialcard_open(struct inode *inode,struct file *filep)
{

//getting the structure details of type struct serialcard,using the pointer inode->i_cdev and field type cdev

//struct serial_card *serial_cards = container_of(inode->i_cdev, struct serial_card, cdev);

// read the current value of use_count

static int Device_Open = 0;
if ( Device_Open ) //Device_Open is static varibale used here for checking the no of times a device is opened
{
printk("cPCIserial: Open attempt rejected\n");
return -EBUSY;
}
Device_Open++;


// using the card so increment use_count
//atomic_inc(&serial_cards->use_count);
//filep->private_data = serial_cards;

return 0;
}

第174-175页的完整描述如下

Single-Open Devices

The brute-force way to provide access control is to permit a device to be opened by only one process at a time (single openness). This technique is best avoided because it inhibits user ingenuity. A user might want to run different processes on the same device, one reading status information while the other is writing data. In some cases, users can get a lot done by running a few simple programs through a shell script, as long as they can access the device concurrently. In other words, implementing a singleopen behavior amounts to creating policy, which may get in the way of what your users want to do. Allowing only a single process to open a device has undesirable properties, but it is also the easiest access control to implement for a device driver, so it’s shown here. The source code is extracted from a device called scullsingle.

scullsingle设备维护了一个atomic_t变量,叫做scull_s_available;那变量被初始化为值一,表明该设备确实可用。open 调用递减并测试 scull_s_available 并拒绝访问,如果有人否则已经打开了设备:

static atomic_t scull_s_available = ATOMIC_INIT(1);
static int scull_s_open(struct inode *inode, struct file *filp)
{
struct scull_dev *dev = &scull_s_device; /* device information */
if (! atomic_dec_and_test (&scull_s_available)) {
atomic_inc(&scull_s_available);
return -EBUSY; /* already open */
}

/* then, everything else is copied from the bare scull device */
if ( (filp->f_flags & O_ACCMODE) = = O_WRONLY) {
scull_trim(dev);
filp->private_data = dev;
return 0; /* success */
}

另一方面,release 调用将设备标记为不再忙碌:

static int scull_s_release(struct inode *inode, struct file *filp)
{
atomic_inc(&scull_s_available); /* release the device */
return 0;
}

通常,我们建议您将打开标志 scull_s_available 放在设备结构(此处为Scull_Dev) 因为,从概念上讲,它属于设备。这然而,scull 驱动程序使用独立变量来保存标志,因此它可以使用相同的设备结构和方法作为裸 scull 设备并最小化代码重复。

请让我知道任何替代方案

感谢和问候

最佳答案

也许我没有捕获要点,但是“cotainer_of”不是一个函数,而是一个宏。如果您有移植问题,如果系统头文件没有实现它,您可以安全地自己定义它。这是一个基本的实现:

#ifndef container_of
#define container_of(ptr, type, member) \
((type *) \
( ((char *)(ptr)) \
- ((char *)(&((type*)0)->member)) ))

#endif

或者这是来自最近 linux 头文件的更准确的实现:

#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr);
(type *)( (char *)__mptr - offsetof(type,member) );})

关于c - 替代 container_of(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5469255/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com