/*---------------------------------------------------------------------------
 * filename - coreleft.cas
 *
 * function(s)
 *    coreleft   - returns a measure of unused memory
 *--------------------------------------------------------------------------*/

/*
 *      C/C++ Run Time Library - Version 5.0
 *
 *      Copyright (c) 1987, 1992 by Borland International
 *      All Rights Reserved.
 *
 */


#pragma inline
#include <asmrules.h>
#include <alloc.h>

/*--------------------------------------------------------------------------*

Name            coreleft - returns a measure of unused memory

Usage           in the small data models :
                unsigned coreleft(void);

                in the large data models(except huge) :
                unsigned long coreleft(void);

Prototype in    alloc.h

Description     returns a measure of the unused memory.  It gives different
                values of measurement, depending on the memory model.

Return value    in the small data models:
                the amount of unused memory between the stack and
                the data segment, minus a buffer margin.

                in the large data models:
                the amount of unused memory between the heap and
                the stack.
*---------------------------------------------------------------------------*/


#if (LDATA)
#include <_fheap.h>

unsigned long coreleft(void)
{
    return(farcoreleft());
}
#else
#include <_heap.h>

unsigned coreleft(void)
{
asm mov ax, sp
asm sub ax, word ptr __brklvl

/* +32 allows for increased stack useage in malloc */
asm     sub     ax, (MARGIN + 32)
asm     jnc     room_left:
asm     xor     ax, ax
room_left:
asm     and     al, 0f0h
    return _AX;
}
#endif
