Gamestudio Links
Zorro Links
Newest Posts
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (VoroneTZ), 1,276 guests, and 14 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19058 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 2 1 2
Re: Memory allignment of parameters in Lite-C [Re: Joey] #135937
06/14/07 18:30
06/14/07 18:30
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline OP
Expert
Excessus  Offline OP
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Quote:

but if he has no idea about the arguments and the argument structure



Yes this is pretty much how it is. Although "I" and "my code" knows what kind of arguments are passed, the C++ compiler doesn't. The parameter data is stored in a piece of memory on the heap pointed to by a char*. The function pointer is stored as a void*.

Example of how my solution works:
- The user function to be called has the signature "void functionName (int, int)".
- The user function is passed to C++ as a void*. At this point the original type information is lost for the C++ compiler so I cast it to a "void (*) (LargeStruct)".
- I have a char* to a char array of 8 bytes containing the data for the two int parameters. They are in a char array because they where serialized and sent over the internet.
- I copy the parameter data pointed to by the char* to an instance of LargeStruct.
- I call the function pointed to by the "void (*) (LargeStruct)" with the instance of LargeStruct as parameter.
- Lite-C knows the original function signature and interprets the passed data as two ints. The int can be used like normal in the function.
The problem: sizeof(LargeStruct) > 2 * sizeof(int), so when I called the function from C++ (which thinks the function has a LargeStruct as parameter), more than 8 bytes where allocated. I'm afraid that the Lite-C compiler only frees 2 * sizeof(int) when the function returns. Is this the case?

The parameter data is in a char array, but I cannot just pass this char* because the Lite-C function (user function) expects some arbitrary parameter list and not a char*. I don't want to require the user to read his parameters from a char array. What the trick with LargeStruct does is create a type that is large enough to contain the parameter data while also allowing pass-by-value (unlike an array for example). The thing here is that C++ interprets the function as a function taking a LargeStruct as parameter, and Lite-C interprets it as a function taking whatever parameters the user has specified.

As I sayd the trick (or should I say hack?) with the LargeStruct seems to work, but I'm afraid that because Lite-C expects less parameter data than sizeof LargeStruct, it will not free all memory. I don't know enough about Lite-C internals and general compiler architecture to tell if this is a valid concern or if the memory will be freed anyway.

I hope it is understandable now. And I hope it is possible with the LargeStruct trick. Otherwise I'll have to start learning assembler.

One assembler related question: do the instruction sets among all computers that run gamestudio differ, or do all intel and amd cpus understand the same assembler?

Re: Memory allignment of parameters in Lite-C [Re: Excessus] #135938
06/15/07 06:28
06/15/07 06:28
Joined: Jul 2000
Posts: 27,987
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,987
Frankfurt
Don't worry, there is no memory allocated. The stack is just a continuous memory area that is reserved for passing parameters. Setting the stack pointer back to its correct position is the task of the calling function when using a __cdecl function declaration (which is the default).

Re: Memory allignment of parameters in Lite-C [Re: jcl] #135939
06/15/07 07:32
06/15/07 07:32
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline OP
Expert
Excessus  Offline OP
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Quote:

Setting the stack pointer back to its correct position is the task of the calling function when using a __cdecl function declaration



Thanks! That's exactly the information I needed.

Page 2 of 2 1 2

Moderated by  old_bill, Tobias 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1