求助:proc_read() 函数参数start的意义

发布于 2022-10-15 08:43:09 字数 5208 浏览 15 评论 0

本帖最后由 Sherlockhlt 于 2011-04-24 11:06 编辑

在网上看到的:
How to be a proc read function
* ------------------------------
* Prototype:
* int f(char *buffer, char **start, off_t offset,
* int count, int *peof, void *dat)
*
* Assume that the buffer is "count" bytes in size.
*
* If you know you have supplied all the data you
* have, set *peof.
*
* You have three ways to return data:
* 0) Leave *start = NULL. (This is the default.)
* Put the data of the requested offset at that
* offset within the buffer. Return the number (n)
* of bytes there are from the beginning of the
* buffer up to the last byte of data. If the
* number of supplied bytes (= n - offset) is
* greater than zero and you didn't signal eof
* and the reader is prepared to take more data
* you will be called again with the requested
* offset advanced by the number of bytes
* absorbed. This interface is useful for files
* no larger than the buffer.
* 1) Set *start = an unsigned long value less than
* the buffer address but greater than zero.
* Put the data of the requested offset at the
* beginning of the buffer. Return the number of
* bytes of data placed there. If this number is
* greater than zero and you didn't signal eof
* and the reader is prepared to take more data
* you will be called again with the requested
* offset advanced by *start. This interface is
* useful when you have a large file consisting
* of a series of blocks which you want to count
* and return as wholes.
* (Hack by Paul.Russell@rustcorp.com.au)
* 2) Set *start = an address within the buffer.
* Put the data of the requested offset at *start.
* Return the number of bytes of data placed there.
* If this number is greater than zero and you
* didn't signal eof and the reader is prepared to
* take more data you will be called again with the
* requested offset advanced by the number of bytes
* absorbed.
*/

在示例代码里对于proc_read的用法是wheelz粘的注释中的第二种,这里的关键在于所谓
char **buffer_location
是个逻辑值,不是个物理上有意义的值,它的意义只对你自己有用,
内核不知道它是什么意思,
内核所做的事情只是在发现你还没*eof=1之前就反复调用你的xxx_proc_read,
每次调用,给你传下来:新的offset = 老的offset+你上次传上去的*buffer_location

举个例子,假设你有8K数据,打算以256Byte为单位往上传,它的代码大致如下

#define TOTAL_BLOCK_NR = 8*1024/256;
struct my_data_struct
{
        unsigned char[256];
};
struct my_data_struct my_buf[TOTAL_BLOCK_NR];
static ssize_t xxx_proc_read(char *buffer,
                                                         char **buffer_location,
                                                         off_t offset, int buffer_length, int *eof,
                                                         void *data)
{
        if (offset >= TOTAL_BLOCK_NR) {
                *eof = 1;
                return 0;
        }

        int i = offset;
        ssize_t cur_size = 0;
        for (i=offset;i<TOTAL_BLOCK_NR;i++)
        {
                if (cur_size+256>buffer_length)
                {
                        break;
                }
                memcpy(buffer, my_buf, 256);
                buffer += 256;
                                cur_size+=256;
        }

//我这一次传了多少组?告诉内核,让内核帮我记着,下次通过@offset告诉我
        *buffer_location = ( i - offset);         
               return (i-offset)*256; //我这一次实际传了多少字节
       
}

在上面的例子中,buffer_location是char**,那么*buffer_location 就应该是一个char*指针,怎么可以直接把整数赋值给*buffer_location 呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文