Re: equivalent to far in linux/unix

From: Andre Majorel (amajorel_at_teezer.fr)
Date: 08/28/04


Date: Sat, 28 Aug 2004 14:07:04 +0000 (UTC)

On 2004-08-27, Andrew Falanga <falandr@hp.com> wrote:

> In any event, I was wondering if anyone here could tell me what the far
> type would translate too in linux? For example, how would I read a line
> like this?:
>
> void far *LinkAddress;
>
> What the heck is going on there? I thought void was a type. How can
> you have two types for the same variable? Would that simply be
> something like?:
>
> int *LinkAddress;

Yes. To port DOS C code to a flat memory model environment such
as i386 Linux, you can just remove the "far" and "huge"
qualifiers, and use malloc() instead of farmalloc() and halloc()
and free() instead of farfree() and hfree().

  #define far
  #define farmalloc malloc
  #define farfree free
  #define huge
  #define halloc malloc
  #define hfree free

The "far" and "huge" qualifiers are an artefact of the segmented
architecture of the 8086. On the 8086, pointers can be 16-bit
long or 32-bit long. A "far" or "huge" qualifier states that
this is a 32-bit pointer. This is necessary when you have more
the 64 kB of code or data.

Conversely, "near" states that this is a 16-bit pointer.

In the absence of a qualifier, pointers use the default model,
which can be near, far or huge, as specified by the relevant
compiler flags.

> It sounds as though far is typedef'd as some sort of 32 bit int.
> Perhaps, I could be totally wrong too.

"far" means that the pointer is made of a 16-bit segment number
plus a 16-bit offset. The total size is indeed 32-bit although
the addressable space is only 20-bit because the 8086 calculates
the effective address like this : segment << 4 + offset.

A "near" pointer is just a 16-bit offset. The segment number is
implied and comes from the relevant segment register (CS for
code, DS for data, etc.) The advantage of near pointers is that
they're small and fast. The drawback is that you're limited to
64 kB of code and 64 kB of data.

"huge" is the same thing as "far" except that the object can be
larger than 64 kB.

-- 
André Majorel <URL:http://www.teaser.fr/~amajorel/>
"See daddy ? All the keys are in alphabetical order now."


Relevant Pages

  • Re: User implementable standard library functions
    ... and, if from mmap, it is aligned to a page boundary. ... good enough for if running on linux, just have to detect and use. ... on windows, I use malloc. ... pointer to an integer or vice versa is implementation-defined. ...
    (comp.lang.c)
  • 1 global variable cross multiply files
    ... I'm writing a Win32 library which I will port to linux soon. ... This library consist of a global variable which is a pointer. ... exe + more dlls or binary + multiply shared objects then each file will ... need a portable solution so one problem is one too many. ...
    (comp.unix.programmer)
  • Re: [PATCH] 2.4.22pre10: {,un}likely_p() macros for pointers
    ... Linux will absolutely not work in this case. ... > when testing pointer or floating-point values. ... You can freely cast between long and any pointer type ... padding only occurs at the end ...
    (Linux-Kernel)
  • Re: 1 global variable cross multiply files
    ... > This library consist of a global variable which is a pointer. ... > offers API functions that will make it possible for the user manipulate with ... I didn't try with linux but I ... > need a portable solution so one problem is one too many. ...
    (comp.unix.programmer)
  • Re: Confusion about undefined behaviour
    ... pointer, correct code consists of following that definition. ... implementation of C being used to compile the kernel. ... what C implementations the Linux kernel can use is determined by ... anything useful with the result of dereferencing the null pointer. ...
    (comp.lang.c)