Passing Structs to function like in C

D.Rex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 13 08:47:51 PDT 2016


On Friday, 12 August 2016 at 15:23:17 UTC, Cauterite wrote:
> On Friday, 12 August 2016 at 15:21:22 UTC, D.Rex wrote:
>> extern unsigned long free_page_tables(struct task_struct * 
>> tsk);
>
> extern(C) ulong free_page_tables(task_struct* tsk);
>
> void main() {
>     task_struct tsk =  …… ;
>     free_page_tables(&tsk);
> };
>
> That should be what you're after?

Thanks for the answer!  I really appreciate it :).

I'm not sure if the context of what I wrote is entirely correct,  
I came across this particular thing (passing structs as 
parameters) to methods when I was taking a look at some of the 
linux source code and stumbled upon an instance of this, and 
realised I had never seen its usage before.

The method was in a the mm.h header file 'include/linux/mm.h, 
line 111 (in Linux Kernel v0.99, and probably in later versions 
as well) and links to memory.c (mm/memory.c) where it has the 
same thing (struct task_struct * tsk) - task_struct being already 
defined in include/linux/sched.h.

I suppose declaring the method in mm.h would be pointless in D 
equivalent anyway since (as far as I am aware) D doesn't have 
seperate Header and Source files, and the method could simply be 
declared in memory.d in a D context, and instead of using an 
import for mm.h for using free_page_tables(), one could simply 
import memory.d and it would work much the same, please correct 
me if I am wrong.

So going out on a limb here ( and again please correct me if I am 
mistaken ), given what you have shown above if all code were 
translated to D, it would look something like:

/* memory.d file */
module memory;
import include.linux.sched;    /* contains task_struct definition 
*/

void free_page_tables(task_struct* tsk) {
         /* do something with &tsk */
}

And to use the method from somewhere else
/* use method.d */

module usemethod;
import include.linux.sched;
import mm.memory;

void some method() {
         free_page_tables(*pointer to task_struct*);
}

I hope my explanation is not rambling nonsense.

Cheers.




More information about the Digitalmars-d-learn mailing list