All pastes #229584 Raw Edit

Unnamed

public text v1 · immutable
#229584 ·published 2006-10-30 20:05 UTC
rendered paste body
#include <dos.h>
#include <errno.h>

int __doalloc( unsigned size );
#pragma aux __doalloc = \
    "mov ah, 0x48"      \
    "int 0x21"          \
    "jnc finish"        \
    "sbb ax, ax"        \
    "finish:"           \
    parm [bx]           \
    value [ax]          \
    modify [ax bx];

unsigned _dos_allocmem( unsigned size, unsigned *seg )
{
    register int retval = __doalloc( size );

    if( retval < 0 ) {
        errno = ENOMEM;
        return( ( _doserrno = -retval ) );
    }

    *seg = retval;
    return( 0 );
}


@@ end of dallcmem.c
#include <dos.h>
#include <malloc.h>

static unsigned __malloc_ds = 0;

void *malloc( size_t bytes )
{
    unsigned segm;

    if( __malloc_ds == 0 ) {
        char *p;
        __malloc_ds = FP_SEG( ( char far * )p );
        /* DS will be the segment of a char * cast to a char far * */
    }

    if( _dos_allocmem( bytes, &segm ) != 0 ) return( NULL );

    segm = ( segm - __malloc_ds ) * 16;
    if( segm > ( 0xFFFF - segm ) ) { /* Can't be a near pointer */
        free( ( void * )segm );

        return( NULL );
    }

    return( ( void * )segm );
}

void free( void *buf )
{
    _dos_freemem( ( ( unsigned )buf / 16 ) + __malloc_ds );
}