I am trying to understanding devm_kzalloc() function implementation. It is allocating more than the requested memory(sizeof(struct devres) + size) to manage resources.
struct devres is defined as follows, the second member is an incomplete array.
struct devres {
struct devres_node node;
/* -- 3 pointers */
unsigned long long data[]; /* guarantee ull alignment */
};
Following is the source to allocate memory.
size_t tot_size = sizeof(struct devres) + size;
struct devres *dr;
dr = kmalloc_track_caller(tot_size, gfp);
if (unlikely(!dr))
return NULL;
memset(dr, 0, tot_size);
INIT_LIST_HEAD(&dr->node.entry);
dr->node.release = release;
return dr;
I have following doubhts. . It is caliculating the tot_size, but in struct devres the array is imcomplete. . The devm_kzalloc() function(shown below) is returning dr->data as the starting of the requested memory. If we understand that array name contains the startig address of that array then we are allocating more than the requested memory. i.e size of unsigned long long + size.
void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
{
struct devres *dr;
/* use raw alloc_dr for kmalloc caller tracing */
dr = alloc_dr(devm_kzalloc_release, size, gfp);
if (unlikely(!dr))
return NULL;
set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
devres_add(dev, dr->data);
return dr->data;
}
Could you please help me to understand this.
Thanks
I have understood now.
Its flexible array member feature of C99 used in "struct devres" structure definition.