Translation of C struct to D

Iain Buclaw ibuclaw at ubuntu.com
Tue Nov 15 15:53:20 PST 2011


On 14 November 2011 20:54, Michael Kremser <mkspamx-usenet at yahoo.de> wrote:
> Hi!
>
> I currently try to create a simple linux driver (for test purposes) just
> like in [1] in D based on [2]. The main difficulty is to call
> register_chrdev. It expects an argument "fops" of type file_operations which
> can look like that in C:
>
> static struct file_operations fops = {
>        .read = device_read,
>        .write = device_write,
>        .open = device_open,
>        .release = device_release
> };
>
> device_read and so on are functions. Originally, file_operations has much
> more fields and it's possible to omit unused fields.
>
> What I tried so far: I declared "register_chrdev" as follows:
>
> extern (C):
> int register_chrdev (uint major, char *name, file_operations *fops);
>
> My try for the translation of file_operations looks like that:
>
> struct file_operations {
>        int function() open;
>        int function() release;
> };
>
> In init_module() I have the following code:
>
> char[] DEVICE_NAME = "dtestdev";
> file_operations fops;
> fops.open = &device_open;
> fops.release = &device_close;
> Major = register_chrdev(0, std.string.toStringz(DEVICE_NAME), &fops);
>
> The code compiles with some warnings, and if I try to insert it into the
> kernel, I get some messages in the kernel log.
>
> WARNING: "_D3std6string9toStringzFAaZPa" [...../hello.ko] undefined!
> WARNING: "_Dmodule_ref" [...../hello.ko] undefined!
> WARNING: "_D15TypeInfo_Struct6__vtblZ" [...../hello.ko] undefined!
> WARNING: "register_chrdev" [...../hello.ko] undefined!
> WARNING: "_D3std6string12__ModuleInfoZ" [...../hello.ko] undefined!
>
> Nov 14 21:12:39 eeepc1104 kernel: [17127.068990] hello: Unknown symbol
> _D3std6string12__ModuleInfoZ (err 0)
> Nov 14 21:12:39 eeepc1104 kernel: [17127.069613] hello: Unknown symbol
> register_chrdev (err 0)
> Nov 14 21:12:39 eeepc1104 kernel: [17127.070462] hello: Unknown symbol
> _D15TypeInfo_Struct6__vtblZ (err 0)
> Nov 14 21:12:39 eeepc1104 kernel: [17127.071359] hello: Unknown symbol
> _Dmodule_ref (err 0)
> Nov 14 21:12:39 eeepc1104 kernel: [17127.072352] hello: Unknown symbol
> _D3std6string9toStringzFAaZPa (err 0)
>
> Any help greatly appreciated.
>
> Greetings
>
> Michael
>
> [1] http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html#AEN680
> [2]
> http://iainbuclaw.wordpress.com/2010/05/22/writing-a-linux-kernel-module-in-d/
>

If you are going to write a kernel module, do not use or rely any part
of the D, or C standard library. Do not attempt to do anything that
would generate hidden calls to the D runtime library, or would depend
on there being one.  Otherwise you will find yourself hitting a total
system freeze when loading the module into the kernel. :~)


Rather than using toStringz, you should be able to get by with
declaring the register_chrdev function as follows.

extern(C)
int register_chrdev (uint major, in char *name, file_operations *fops)



Regards
-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';


More information about the Digitalmars-d mailing list