Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using far function pointers

I realize that far is compiler specific, but my expectation is that the placement of the far specifier should make sense to those who really understand pointers.

So, I have two applications that share the processor's entire memory space.

App A needs to call function foo that exists in app B.

I know the memory location of function foo.

So this should work, in app A:

typedef int (* __far MYFP)(int input);

void somefunc(void)
{
   int returnvalue;
   MYFP foo;

   foo = (MYFP) 0xFFFFFA;

   returnvalue = foo(39);
}
  • Is the __far in the right spot in the typedef?
  • Do I need to add __far to the (MYFP) cast?
  • Some information suggests that the call to foo doesn't need to be dereferenced, what is your experience?
  • What else about this looks incorrect, or might I try to accomplish this?

  • Is there a better way to do this?

Edit:

This is on an embedded device (Freescale S12XEQ device) using Code Warrior. It's a 16 bit device with 24 bit memory space, so yes, it is segmented/banked.

-Adam

like image 517
Adam Davis Avatar asked Nov 20 '08 16:11

Adam Davis


People also ask

How do you use a far pointer?

Far pointer is a 32-bit pointer, can access information which is outside the computer memory in a given segment. To use this pointer, one must allocate his/her sector register to store data address in the segment and also another sector register must be stored within the most recent sector.

When should a far pointer be used?

A far pointer can refer to information outside the 64KB data segment. Typically, such pointers are used with farmalloc() and such, to manage a heap separate from where all the rest of the data lives. If you use a small-data, large-code model, you should explicitly make your function pointers far.

How do you declare a far pointer?

The __far keyword must appear in the declarator part of a pointer declaration, wherever a cv-qualifier can be used. For example, int * __far p; declares p to be a __far pointer to int .

What is the use of function pointers in C?

In C, we can use function pointers to avoid code redundancy. For example a simple qsort() function can be used to sort arrays in ascending order or descending or by any other order in case of array of structures. Not only this, with function pointers and void pointers, it is possible to use qsort for any data type.


1 Answers

The __far keyword, at least in the MS world, was used when creating binaries that used segmented memory. You need to understand 8086 memory system to understand this. The 8086 divided the memory into segments, each segment was 64K long, so each address in a segment needed 16bits. Addresses came in two forms: near and far. A near address was 16bits and was an offset into the current segment, one of CS,DS,ES,SS depending on the instruction, a far was 32bits and consisted of a segment and an offset. In the 8086, the absolute address was 16 * segment + offset, giving an addressable range of 1Mb.

If you're not using a segmented memory system, and it looks like you are not, then all addresses have the same number of bits so the near/far distinction is not necessary which means the keyword is probably ignored by the compiler.

like image 56
Skizz Avatar answered Sep 21 '22 11:09

Skizz