in linux kernel driver is it possible to export a struct as a symbol? In other if there are 2 drivers i.e. Driver_A and Driver_B. And Driver_A has a struct test_a defined that is required to be exported so that Driver_B can directly reference struct test_a. Something like following
Driver_A has following structure defined
struct test_a {
int a,
long b,
int (*mem_write)(void *buf_priv, uint32_t size);
}
EXPORT_SYMBOL(test_a)
Driver_B
extern struct test_a test;
And method in Driver_B is able to access a,b and
Related
I'm writing a lua script for Nginx to communicate with a server which provides a protocol I must comply with. And the protocol accepts a struct with a struct member, I can't find a way to pack it in lua.
require "struct"
-- struct udp_packet_header {
-- uint32_t head_token;
-- uint64_t uuid;
-- uint64_t timestamp;
-- uint32_t order;
-- uint32_t trans_size;
-- uint32_t https_size;
-- uint16_t head_crc;
-- };
local udp_packet_header_def = '<ILLIIIH'
-- struct udp_packet {
-- struct udp_packet_header header;
-- char data[];
-- };
local udp_packet_struct_def = ?
As defined above, how can I pack the struct udp_packet with a struct member header?
I checked https://github.com/iryont/lua-struct but found nothing useful.
Is doing this even possible? If not, is there a better way to do this? Maybe write C code and call it from lua?
When I compile the following I get use of undeclared identifier 'rsdtHeader'
How can I do the following operation using typedef?
typedef struct
{
int length;
int x;
int y;
} SdtHeader_s;
typedef struct
{
SdtHeader_s rsdtHeader;
SdtHeader_s* rsdtEntry[(rsdtHeader.length - sizeof(rsdtHeader))/4];
} Rsdt_s;
i'm trying to study the linux kernel and reading the kernel code,
but i can't understand the structure they use for the page structure as shown below:
i mean,why they use union nested in the struct which nested in the union
(the code is simplified...)
struct page {
unsigned long flags;
struct address_space *mapping;
struct {
union {
pgoff_t index;
void *freelist;
};
union {
unsigned counters;
struct {
union {
atomic_t _mapcount;
struct {
unsigned inuse:16;
unsigned objects:15;
unsigned frozen:1;
};
};
atomic_t _count;
};
};
};
}
It is used to bring clarity into the code. It will be easier to read and understand if members are grouped.
Since you are not using the 'sub-structures' in any other data-structure, they are nested. Else, they would be declared separate and included as members, like below:
struct list_based{
pgoff_t index;
void *freelist;
};
struct page {
unsigned long flags;
struct address_space *mapping;
struct list_based lpage;
};
struct sector {
unsigned long sub sect;
struct list_based lsect;
};
I have this struct:
struct foo {
int a;
union {
struct {
int b;
struct bar
{
int c;
int d;
} *aBar;
} in;
} u;
};
How I need to declare a variable of type bar, in Visual C++ ?
When you declare an structure like this:
struct
{
int b;
} in;
You are actually creating an object with name in, having unnamed-data type. This data-type would be named internally by compiler, and depends on compiler. The style given above does not declare in to be a type, but a variable!
If you want to make it a type, use either of given approaches:
// Approach 1
struct in{...};
// Approach 2
typedef struct {..} in; // in is now a type, because of `typedef`
If you have compiler that supports C++0x, and specifically type decltype keyword, you can use it against the first style (which makes in a variable). Example:
decltype(in) in_var;
in_var.b = 10;
Thanks Ajay, I solved that way:
foo *k;
decltype(k->u.in.aBar) j;
j->c = 1;
j->d = 1;
I know if we have task_struct, surly we can get the contained sched_entity because it's one field in the task struct. But can we get the pointer to the task_struct given the shed_entity? Following is the sched_entity structure:
struct sched_entity {
struct load_weight load; /* for load-balancing */
struct rb_node run_node;
struct list_head group_node;
unsigned int on_rq;
u64 exec_start;
u64 sum_exec_runtime;
u64 vruntime;
u64 prev_sum_exec_runtime;
u64 nr_migrations;
#ifdef CONFIG_SCHEDSTATS
struct sched_statistics statistics;
#endif
#ifdef CONFIG_FAIR_GROUP_SCHED
struct sched_entity *parent;
/* rq on which this entity is (to be) queued: */
struct cfs_rq *cfs_rq;
/* rq "owned" by this entity/group: */
struct cfs_rq *my_q;
#endif
};
It seems that there is no place where I can get the task_struct. My final goal is to get the sched_entity of the task group_leader containing the task with this shed_entity :>
The Linux kernel code provides a standard way to take a pointer to an element contained within a structure, and get back a pointer to the containing structure: the container_of macro, which is used extensively throughout the kernel.
In this case, if you have a struct sched_entity *foo, you can get the enclosing task_struct with:
struct task_struct *task = container_of(foo, struct task_struct, se);
(Obviously this is only safe if you know for sure that the original struct sched_entity * pointer is pointing to a struct sched_entity which is inside a struct task_struct, so be careful...)