Subject:
|
Re: How to make shared memory....not
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Fri, 28 Dec 2001 21:04:55 GMT
|
Viewed:
|
2124 times
|
| |
| |
Hi Steve,
> > BTW., this scheme can be extended to something like a shared library.
> ooohh. I like that idea. So I can write one program with all my functions
> and my other programs can just include a couple function calls.
Yes. Actually, I am working on a robot that finds a path trough a maze. After
solving the maze, a second program can run through the maze with the
informations from the first program. (I will do this with shared mem). Both
programs must have the code to walk. But memory is precious so I got the idea
to share the walking code between both programs.
Here is an example implementation. Program 1 allocates shared memory and
offers three functions for program2. Program 2 calls this functions and
displays the result. You must init the pointer to the value displayed by
program1.
-------- program 1 -------------------------------------------------------
#include <conio.h>
typedef int (*PFI)(); // PFI = pointer to func returning int
typedef struct {
int i, j, k; // just some ints for demonstration
PFI func[3]; // jump table (array of PFIs)
} shared;
int func1() { // some demo functions
return 123;
}
int func2() {
return 234;
}
int func3() {
return 345;
}
shared sh = { 0, 0, 0, { func1, func2, func3 } }; // init shared mem
int main()
{
cputw( (unsigned int)(&sh) ); // show address of shared mem
return 0;
}
------------- end of program 1 -------------------------------------
-----------------program 2 -----------------------------------------
#include <conio.h>
#include <unistd.h>
typedef int (*PFI)();
typedef struct {
int i, j, k;
PFI func[3];
} shared;
shared *psh;
#define func123 (*(psh->func[0])) // some handy defines,
#define func234 (*(psh->func[1])) // handle with care
#define func345 (*(psh->func[2])) //
int main()
{
int i;
int res;
psh = (shared *)0xafbe; // !! enter YOUR value here !!
for( i = 0; i < 3; i++ ) {
res = (*(psh->func[i]))(); // call function via pointer
lcd_int( res ); // and display result
sleep( 1 );
}
lcd_int( func123() ); sleep( 1 ); // or use defines to get
lcd_int( func234() ); sleep( 1 ); // better readable code
lcd_int( func345() ); sleep( 1 ); //
return 0;
}
------------------ end of program 2 ---------------------------------
Have a nice weekend,
Mike
|
|
Message is in Reply To:
| | Re: How to make shared memory....not
|
| (...) ooohh. I like that idea. So I can write one program with all my functions and my other programs can just include a couple function calls. Again, in my VB world, I don't mess with pointers. When you have a minute, can you type up a sample (...) (23 years ago, 27-Dec-01, to lugnet.robotics.rcx.legos)
|
11 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|