So I am trying to dynamically allocate a buffer on module initialization. The buffer needs to be in scope at all times as it stores data that user space programs interact with. So here is my code:
static char* file_data
#define MAX_SIZE 256
.
.
.
{
file_data = kzalloc(MAX_SIZE, GFP_KERNEL)
.
.
.
}
However when I do sizeof file_data it always returns 4. What am I doing wrong?
Edit: The buffer stores input from a user space program, but 4 characters is all that can be stored.
size_t read_file(char* __user buf, size_t count)
{
unsigned int len = 0;
len = copy_to_user(buf, file_data, count);
return count;
}
ssize_t write_file(char* __user buf, size_t count)
{
if(count >= MAX_SIZE)
return -EINVAL;
copy_from_user(file_data, buf,count)
return count;
}
file_datais a pointer. On a 32-bit platform, it’s size is 32 bits, or 4 bytes. What you want to know is the size of the data pointed to byfile_data. You can’t use thesizeofoperator for this becausesizeofis a compile time operation. You can’t use it on things allocated dynamically at run time.(Besides, you already know the size of the data pointed to by
file_data— it’sMAX_SIZE?)