C Language Development Kit/Functions/FarMalloc

From OptiWiki

Jump to: navigation, search
Description This function allocates a block of memory from the heap to be used by the program.

This function is almost identical to the ANSI malloc function, with one important difference, the argument. This function uses a unsigned long type, where malloc uses size_t, defined as an int.

Syntax
void * far_malloc(unsigned long size);
Arguments
unsigned long size
The size of the memory block to be allocated. The size may exceed 64 kB. The maximum size that

can be allocated is determined by the size of the largest free block of memory on the heap.

Returns
A void pointer Void pointer to the start of the memory block.
NULL The requested memory block could not be allocated.
Remarks In case of the OPH100x, this function is identical to malloc and is implemented as a macro.

The OPH100x uses 32bit for an integer, where all the other terminals have 16 bit integers. Since the ANSI malloc function uses an int, it can only allocate 128KByte of memory on those terminals.

This function passes an unsigned long instead of an int, to overcome this problem.

Example
#include <stdio.h>
#include <stdlib.h>
#include "lib.h"
 
void main( void )
{
    static char data[20+1];
    FILE *fp;
 
    while( 1 )
    {
        printf("\f1: mem = %lu", coreleft());
        far_malloc( 70000L );
        while( getchar() == EOF )
            idle();
 
        printf("\f2: mem = %lu", coreleft());
        if( NULL != ( fp = fopen("TEST.DAT", "wb")))
        {
            fwrite( data, 1, 20, fp);
            fclose(fp);
        }
 
        while( getchar() == EOF )
            idle();
 
        printf("\f3: mem = %lu", coreleft());
        format();
        while( getchar() == EOF )
            idle();
 
        printf("\f4: mem = %lu", coreleft());
        while( getchar() == EOF )
            idle();
    }
}


Views
Personal tools