#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA643_A31E_X31J
#ifdef MEDIA618_A31E
#include "MEM1CLCP.C"
#endif

#ifdef MEDIA692_X31J
#define F9012_MEM1_08_Available         MEM1AVAIL
#define F9011_MEM1_07_Free              MEM1FREE
#define F9010_MEM1_06_Allocate          MEM1ALLOC
#define F9014_MEM1_Initialize           MEM1INIT
#endif

extern long AvailMem();
extern void FreeMem();
extern void* AllocMem();


unsigned char* F9010_MEM1_06_Allocate(P3103_l_ByteCount, P3104_i_Requirements)
#ifdef MEDIA618_A31E
register long P3103_l_ByteCount;
#endif
#ifdef MEDIA692_X31J
unsigned long P3103_l_ByteCount;
#endif
int16_t P3104_i_Requirements;
{
#ifdef MEDIA618_A31E
        register long L2984_l_;
#endif
        register long* L2985_pl_;


        ASM_STUB_PREFIX
#ifdef MEDIA618_A31E
        L2984_l_ = (P3104_i_Requirements & (MASK0x0040_MEMREQ | MASK0x0010_MEMREQ | MASK0x0008_MEMREQ)) ? MEMF_CHIP : 0;
        P3103_l_ByteCount += 4;
        if ((L2985_pl_ = (long*)AllocMem(P3103_l_ByteCount, L2984_l_)) == NULL) {
#endif
#ifdef MEDIA692_X31J
        if (M007_GET(P3104_i_Requirements, MASK0x0010_MEMREQ) || (P3103_l_ByteCount >= 16777215)) {
#endif
                ASM_STUB_SUFFIX
                return NULL;
        } else {
#ifdef MEDIA618_A31E
                *L2985_pl_++ = P3103_l_ByteCount; /* Store memory block size in first long */
#endif
#ifdef MEDIA692_X31J
                asm { /* May be replaced with a function call */
                                move.l  P3103_l_ByteCount,-(sp)
                                dc.w    0xFF48          /* DOS CALL MALLOC */
                                addq.l  #4,sp
                                movea.l d0,a4           /* L2985_pl_ */
                                andi.l  #0xFF000000,d0
                                beq.s   @Tloc_294
                                movea.w #0,a4           /* L2985_pl_ */
                        @Tloc_294:
                }
#endif
                ASM_STUB_SUFFIX
                return (unsigned char*)L2985_pl_;
        }
}

void F9011_MEM1_07_Free(P3105_pl_)
long* P3105_pl_;
{
#ifdef MEDIA618_A31E
        register long L2986_l_;
        register long* L2987_pl_;
#endif


        ASM_STUB_PREFIX
#ifdef MEDIA618_A31E
        L2987_pl_ = P3105_pl_;
        if (L2987_pl_ != NULL) {
                L2987_pl_--;
                L2986_l_ = *L2987_pl_; /* Get memory block size from first long */
                FreeMem(L2987_pl_, L2986_l_);
        }
#endif
#ifdef MEDIA692_X31J
        asm {
                move.l  P3105_pl_(a6),-(sp)
                dc.w    0xFF49  /* DOS CALL MFREE */
                addq.l  #4,sp
        }
#endif
        ASM_STUB_SUFFIX
}

long F9012_MEM1_08_Available(P3106_i_)
int16_t P3106_i_;
{
        register long L2988_l_;
#ifdef MEDIA618_A31E
        register long L2989_l_;
#endif


        ASM_STUB_PREFIX
#ifdef MEDIA618_A31E
        L2989_l_ = (P3106_i_ & 0x58) ? MEMF_CHIP : 0;
        L2988_l_ = AvailMem(L2989_l_);
        if (L2989_l_ == MEMF_CHIP) {
                L2988_l_ -= 16384;
                if (L2988_l_ < 0) {
                        L2988_l_ = 0;
                }
        }
#endif
#ifdef MEDIA692_X31J
        if (P3106_i_ & 0x10) {
                ASM_STUB_SUFFIX
                return 0;
        }
        asm {
                move.l  #0xFFFFFF,-(sp)
                dc.w    0xFF48  /* DOS CALL MALLOC */
                addq.l  #4,sp
                andi.l  #0xFFFFFF,d0
                move.l  d0,d7
        }
        L2988_l_ -= 8000;
        if (L2988_l_ < 0) {
                L2988_l_ = 0;
        }
#endif
        ASM_STUB_SUFFIX
        return L2988_l_;
}

void F9013_MEM1_09_EmptyFunction(
)
{
        ASM_STUB_PREFIX
        ASM_STUB_SUFFIX
}

FTL_LIBRARY* F9014_MEM1_Initialize(P3107_l_, P3108_ps_)
long P3107_l_;
MEM1_FTL_LIBRARY* P3108_ps_;
{
#ifdef MEDIA618_A31E
        F1065_SetExecBase();
#endif
#ifdef MEDIA692_X31J
        P3108_ps_->_w_0x36 = 2;
#endif
        return (FTL_LIBRARY*)P3108_ps_;
}

void F9015_MEM1_03_Expunge(
)
{
        ASM_STUB_PREFIX
        ASM_STUB_SUFFIX
}
#endif
