Near and far library functions¶
The near and far functions of Open Watcom and Microsoft C are in every model, on DOS and on 16-bit Windows:
<string.h>has the far string and memory functions:_fmemcpy,_fmemmove,_fmemset,_fmemcmp,_fmemicmp,_fmemchr,_fmemccpy,_fstrlen,_fstrcpy,_fstrncpy,_fstrcat,_fstrncat,_fstrcmp,_fstrncmp,_fstricmp,_fstrnicmp,_fstrchr,_fstrrchr,_fstrstr,_fstrspn,_fstrcspn,_fstrpbrk,_fstrtok,_fstrdup,_fstrlwr,_fstrupr,_fstrrev,_fstrsetand_fstrnset. Their blocks stay within one segment.<malloc.h>(and<stdlib.h>) has the far heap,_fmalloc,_fcalloc,_frealloc,_ffreeand_fmsize, and the near heap,_nmalloc,_ncalloc,_nrealloc,_nfreeand_nmsize, and_msize. With near data (tiny, small, medium) the near heap is the heap ofmalloc, and the far heap takes segments of 8 KiB or more from DOS (orGlobalAlloc), so a small-model program can use most of memory in blocks of up to 64 KiB. With far data the far heap is the heap ofmalloc, and the near heap is in DGROUP: the local heap on Windows, and on DOS a 4 KiB array that_NEAR_HEAP_SIZE(bytes);in one source file makes larger.
You don't have to pick the function by hand. In the tiny, small and
medium models the headers add overloads of the standard names for far
pointers, so memcpy(farbuf, nearbuf, n) calls _fmemcpy, strlen(fp)
calls _fstrlen, and free, realloc and _msize of a far pointer use
the far heap. In the other models a near pointer converts to the default
one, and free, realloc and _msize of a __near pointer use the near
heap. Only allocation needs a choice, since malloc only sees a size:
_fmalloc or _nmalloc when the other kind is wanted. The overloads use
clang's overloadable in C and ordinary overloads in C++;
-D__I86_NO_OVERLOADS leaves them out. strtok has none, as it keeps
its place between calls: use _fstrtok.