English 中文(简体)
kmalloc: 只分配 4 字节
原标题:kmalloc: only allocating 4 bytes
  • 时间:2012-05-23 18:30:34
  •  标签:
  • linux
  • kernel

所以,我正试图动态地分配模块初始化的缓冲。缓冲需要始终处于范围,因为它存储了用户空间程序互动的数据。所以我的代码是:

static char* file_data
#define MAX_SIZE 256
.
.
.
{
   file_data = kzalloc(MAX_SIZE, GFP_KERNEL)

.
.
.
}

然而当我做文件 size of file_ data 时,它总是返回 4。 我做错什么了?

编辑 : 缓冲存储输入来自用户空间程序, 但四个字符是所有可以存储的字符 。

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_data is a pointer. 在32位平台上,它的大小为32位位元或4位字节。您想知道的是 file_data 所点出的数据的大小 。 您不能为此使用 size 运算符, 因为 size is a concollation time operation. 您不能在运行时动态分配到的东西上使用它 。

(此外,您已经知道 file_data -- it s MAX_SIZE 指点的数据大小? )

问题回答

char* file_data 是字符的指针。 显然, 你在一个32位的系统中重新输入, 所以任何指针都是 4 字节。 编译者( 处理 < code> size ) 并不知道或在乎您为 < code> file_ data 重新分配多少内存来指向, 它只是知道您正在询问指针的大小( 您是哪个, 是否有意) 。 如果您想要它指向的内存的大小, 您必须自己跟踪 。





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...