Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/20/24 01:28
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (7th_zorro), 793 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Memory allignment of parameters in Lite-C #135927
06/12/07 10:12
06/12/07 10:12
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
I am working on a networking plugin for gamestudio. To implement remote procedure calls with an arbitrary number and types of parameters, I have to know some things about lite-c internals.

I've done a small test which confirms that the parameters of a function are stored in a single, contiguous piece of memory, and that each parameter takes 4 bytes.

Here is my test code:
Code:

void testfunc(char x, int y)
{
char* chrPtr = &x;
int* intPtr = chrPtr + 4;

STRING* tmp = str_create("");
error(str_for_num(tmp, *intPtr)); // prints y
}


I'd like to know if this is guaranteed to be that way, or if you plan to make changes. Are there any exceptions where such code would not behave as expected?

On the receiving end, I have to call a function from a lite-c function pointer in C++, with the received parameters (the parameters are stored in an array of bytes). Calling Lite-C functions from C++ with a Lite-C function pointer works, but I don't know how to pass the parameters if they are stored in a char array and I don't know the exact function signature.

So, given an array of bytes that contains the parameter data (in C++), how could I call a Lite-C function from a function pointer and set the parameters to the data in array? Basicly I need some way to copy over the data in the array to the piece of memory where the parameters are stored. Where is that piece of memory?

Do you think this kind of coding is dangerous? Should I just let the users serialize their own parameters into a char array and always use that as the parameter for a remote procedure call? (Less convenient for the users)

Re: Memory allignment of parameters in Lite-C [Re: Excessus] #135928
06/12/07 10:35
06/12/07 10:35
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
That piece of memory is the stack.

In a programming language, parameters to a function are pushed on the stack before calling the function. All C functions are normally guaranteed to work this way, which is why you can call any C function from any library with an arbitrary number of parameters.

The number of bytes per parameter can vary due to compiler implementations. For speed reasons, C compilers (including lite-C) normally use 32 bit parameters even for char and short.

If you have a lite-C function

int foo(int a, int b, int c) { ... }

and pass the pointer of this function to a C++ program, you can then just call the pointer from C++

*fooptr(a,b,c)

and the parameters are passed correctly over to lite-C. It does not matter how many parameters the function has.

Re: Memory allignment of parameters in Lite-C [Re: jcl] #135929
06/12/07 10:51
06/12/07 10:51
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
Thank you jcl,

That was helpful but I need some more information, let me clarify:
I want the user of my plugin to be able to pass any function pointer (to a function with ANY signature) to C++, so I use a void* for this.

In C++, I want to call this function with parameter data that I received over the internet. This parameter data is stored as a char array. Now I want to call the function pointer and pass the parameter data *without knowing the function signature* (I only know the length in bytes of the parameter list). The parameter data has the correct length in bytes, so it just has to be copied to the stack at the location where the Lite-C function expects the first parameter.

I've tested this C++ function:
Code:

struct LargeStruct
{
int a;
int b;
int c;
int d;
};
typedef void (*LiteCFunc)(LargeStruct x);
DLLFUNC void tst(int x, void* funcPtr)
{
LiteCFunc myFunc = static_cast<LiteCFunc>(funcPtr);
LargeStruct x;
x.a = 0;
x.b = x;
x.c = 0;
x.d = 0;
(*myFunc)(x);
}


Called it from Lite-C:
Code:

void testfunc(char x, int y); // function pointer/prototype
void tst(int x, void* funcPtr); // DLL function prototype
tst(100, testfunc);


This results in the Lite-C function testfunc being called with the second parameter set to 100. However, there are 2 problems with this aproach:
-There is a limit to how large (in bytes) a parameter list can be.
-I always have to copy the maximum amount of bytes, and if the function has a shorter parameter list (like above) I will be writing to unknown memory.

Is there a cleaner way to copy the (arbitrary length) parameter data to the stack?

Re: Memory allignment of parameters in Lite-C [Re: Excessus] #135930
06/13/07 22:31
06/13/07 22:31
Joined: Aug 2000
Posts: 1,140
Baunatal, Germany
Tobias Offline

Moderator
Tobias  Offline

Moderator

Joined: Aug 2000
Posts: 1,140
Baunatal, Germany
Why dont you use a pointer

(*myFunc)(&x);

Re: Memory allignment of parameters in Lite-C [Re: Tobias] #135931
06/14/07 06:12
06/14/07 06:12
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Usually, calling a function with data of different size is done by giving the size as a function argument:

void foo(int size,char *data)
{
...
}

...
foo(sizeof(LargeStruct),&MyStruct);

Re: Memory allignment of parameters in Lite-C [Re: jcl] #135932
06/14/07 11:25
06/14/07 11:25
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
Yes that is possible, but I'm not the one writing the called functions, and I'd like to make this as easy as possible for the users of my plugin. I rather not require the user to do a lot of deserialization and casting (actually that's the whole point of implementing RPCs; convenience for the user). The user should be able to freely choose a parameter list, and my plugin should take care that the right values are passed.

The method with casting the parameters to a LargeStruct seems best. What I do is cast the RPC function pointer (passed to C++ as a void*) to a "void (*func) (LargeStruct)", regardless of what parameter list it has (parameter list size must be <= sizeof(LargeStruct)). I then cast the serialized parameter data to a LargeStruct and call the function pointer with this LargeStruct as parameter. Lite-C interprets the parameter data as the original parameter list (not as a LargeStruct), but since the data in the LargeStruct "fits" the parameter list, it will result in correct values for the parameters. I'm not so worried about the performance issues of copying a whole LargeStruct (could even use several structs of increasing size) nor about the fact that parameter list size will be limited to sizeof(LargeStruct).. I am however worried that since the LargeStruct is often larger than the parameter list, I am copying to memory I do not own. Could you tell me if this is a risk, or is the memory past the end of the parameter list safe to write to?

The best solution would be to be able to pass a char array by value, because then I can use a fitting size parameter list for every registered RPC function. I read here that this can be done by making the parameter a "const char []", but it doesn't seem to work.. It's hard to find information about this on the web, but maybe you have an idea as to why this doesn't work/how to make it work.

Thanks

Re: Memory allignment of parameters in Lite-C [Re: Excessus] #135933
06/14/07 14:28
06/14/07 14:28
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
why don't you copy the data onto the stack with assembler and then call the function without arguments?

Re: Memory allignment of parameters in Lite-C [Re: Joey] #135934
06/14/07 14:36
06/14/07 14:36
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
Hi Joey,

That's a really good idea! Unfortunately, I've never used assembler before. Where do you suggest I start learning assembler, and what topic should I study in particular to do this? Or is this trivial enough for you to be willing to write it for me/provide an example?

Re: Memory allignment of parameters in Lite-C [Re: Excessus] #135935
06/14/07 15:36
06/14/07 15:36
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
I'm not 100% sure that I fully understand what you intend to do, but when you call a function, the parameters are already on the stack. The assembler code for this is produced by the compiler before calling the function.

When you call something like

foo(MyStruct)

MyStruct is copied onto the stack and the function foo can access all members of it.

Re: Memory allignment of parameters in Lite-C [Re: jcl] #135936
06/14/07 16:06
06/14/07 16:06
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
yes, but if he has no idea about the arguments and the argument structure he can't call the function afaik. like that, he could get the arguments delivered by some kind of reinterpret_cast<char[]> (i'd suggest that) and then he could copy that char* to the stack and then furthermore call the function without any arguments (get the address of the function, call it in assembler). that's some kind of detour but i think most compilers would complain about calling an unknown function with a struct as arguments - or does it automatically get transformed into the single arguments then afterwards in the function? i don't think so.

assembler is hard to learn. instruction sets differ and you need loads of time. you could start writing assembler snippets for stuff you normally do directly in c(++), p.ex. additions, copying, searching strings. there's a good tutorial with the very basics in a wikibook. personally i've bought a reference for all instructions (x86, mmx, sse(1-3)) since when you've got the idea behind assembler it's still hard to remember the instructions.

joey.

Page 1 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