www.mikrocontroller.net

malloc.c

malloc.c
/*******************************************************************************
*
* malloc.c 
*
 * COPYRIGHT(C) 1999(2000-2002) RENESAS TECHNOLOGY CORPORATION
 * AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
 *
*
*  $Id: malloc.c,v 1.6 2003/06/11 08:26:14 muranaka Exp $
*
*  NC30 Version 0.00.00
*
*     : Version 0.00.00
*        
*    
*
*******************************************************************************/


#if defined(NC30)    /* NCxx */
  /* dummy */
#elif defined(NC77)
  #error "NC77 not supported"
#elif defined(NC79)
  #error "NC79 not supported"
#else
  #error "NC30, NC77, NC79 not defined"
#endif        /* NCxx */




#include <stdlib.h>
#include <string.h>

#ifndef NULL
#define NULL 0
#endif

char _FAR *_mbase;                      /*                   */
char _FAR *_mnext;                      /*                   */

unsigned long  _msize;                  /*                   */

struct _MEMT {
        struct _MEMT _FAR *top;         /*                   */
        unsigned long      size;        /*                   */
};

struct _MEMT  _pool = { NULL, NULL };  /*                */
struct _MEMT  _memt = { NULL, NULL };  /*                 */

#define _MEMTSIZE sizeof( struct _MEMT )

char _FAR *getmem( unsigned int );
int  rlsmem( char _FAR *cp, unsigned nbytes );

/*******************************************************************************
*
* calloc 
*
 * COPYRIGHT(C) 1999(2000-2002) RENESAS TECHNOLOGY CORPORATION
 * AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
 *
*
*        calloc --               
*
*        #include <stdlib.h>
*           p = calloc( n, size );
*
*           char _FAR *p;                       
*           unsigned n;             
*           unsigned size;                      
*
*                                     
*                     
*
*                                
*                                
*
*******************************************************************************/
void _FAR *calloc( size_t n, size_t size )  /*                   */
{
    char _FAR *cp;                      /*                   */

    if ( cp = malloc( n * size ) )      /*                   */
        memset( cp, ( int )'\0', n * size );
                                        /*                   */
    return ( cp );                      /*                   */
}


/*******************************************************************************
*
* free 
*
 * COPYRIGHT(C) 1999(2000-2002) RENESAS TECHNOLOGY CORPORATION
 * AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
 *
*
*        free --          
*
*        #include <stdlib.h>
*           ret = free( cp );
*
*           int  ret;             
*           void _FAR *cp;                          
*
*        ret ==  0                  
*               == -1           "cp"                 
*
*             malloc calloc                       
*                char *                malloc calloc   
*                             
*
*******************************************************************************/
void free( void _FAR *cp )  /*                   */
{
    struct _MEMT _FAR *p;               /*                   */

    if( NULL == cp )
  return;
    p = ( struct _MEMT _FAR * )cp - 1;   /*                  */
#if 0
    return ( rlsmem( ( char _FAR * )p, p->size * _MEMTSIZE ) );
#else
    rlsmem( ( char _FAR * )p, p->size * _MEMTSIZE );
#endif
                                        /*                   */
}


/*******************************************************************************
*
* malloc -- Version 3.10.00
*
 * COPYRIGHT(C) 1999(2000-2002) RENESAS TECHNOLOGY CORPORATION
 * AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
 *
*
*        malloc --           
*
*        #include <stdlib.h>
*           p = malloc( nbytes );
*
*           char _FAR *p;                       
*           unsigned nbytes;                      
*
*                                     
*                     
*
*                                 
*
*******************************************************************************/
void _FAR *malloc( size_t nbytes ) /*                   */
{
    unsigned nunits;                    /*                   */
    struct _MEMT _FAR *p;               /*                   */

    if ( nbytes == 0 )
  return ( NULL );     /*   "nbytes"           */
    nunits = ( nbytes + _MEMTSIZE * 2 - 1 ) / _MEMTSIZE;
                                        /*                   */
    if ( ( p = ( struct _MEMT _FAR * )getmem( nunits * _MEMTSIZE ) ) == NULL )
        return ( NULL );                /*                   */
    p->size = nunits;                   /*                   */
    return ( ( char _FAR * )( p + 1 ) );     /*                   */
}


/*******************************************************************************
*
* realloc 
*
 * COPYRIGHT(C) 1999(2000-2002) RENESAS TECHNOLOGY CORPORATION
 * AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
 *
*
*        realloc --                
*
*        #include <stdlib.h>
*           np = realloc( cp, nbytes );
*
*           char _FAR *np;                          
*           char _FAR *cp;                          
*           unsigned nbytes;                       
*
*                                      
*                                        
*                     
*
*          malloc calloc                     
*             "cp"                    "nbytes" 
*                         
*
*******************************************************************************/
void _FAR *realloc( void _FAR *cp, size_t nbytes )/*                   */
{
    char _FAR *np;                      /*                   */
    unsigned size;                      /*                   */
    struct _MEMT _FAR *p;               /*                   */

    if ( ( np = malloc( nbytes ) ) != NULL ) {
                                        /*                   */
        if ( cp != NULL ){
            p = ( struct _MEMT _FAR * )cp - 1;   /*                */
            size = p->size * _MEMTSIZE - _MEMTSIZE;
                                        /*                   */
           if ( size > nbytes )         /*                   */
                size = nbytes;          /*                   */
            memcpy( np, cp, size );     /*                   */
            free( cp );                 /*                   */
        }
    }
    return( np );                       /*                   */
}


/*******************************************************************************
*
* getmem 
*
 * COPYRIGHT(C) 1999(2000-2002) RENESAS TECHNOLOGY CORPORATION
 * AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
 *
*
*        getmem --           
*
*        p = getmem( nbytes );
*
*           char _FAR *p;                         
*           unsigned nbytes;             
*
*                                     
*                     
*
*                                    nbytes 
*                                          
*                                  
*
*******************************************************************************/
char _FAR *getmem( unsigned nbytes )  /*                   */
{

    unsigned long lbytes;               /*                   */
    unsigned long lunits;               /*                   */
    struct _MEMT _FAR *p;               /*                   */
    struct _MEMT _FAR *q;               /*                   */

    lbytes = ( unsigned long )(nbytes);
    if ( lbytes <= 0L )
  return ( NULL );    /*                   */
    lunits = ( lbytes + _MEMTSIZE - 1 ) / _MEMTSIZE;
                                        /*                   */
    q = &_memt;                         /*                   */
     for ( p = q->top; p != NULL; q = p, p = p->top ) {
                                        /*                   */
        if ( lunits <= p->size ) {      /*                   */
            if ( p->size != lunits ) {  /*                   */
                p->size -= lunits;      /*                   */
                p += p->size;           /*                   */
            }
            else q->top = p->top;       /*                   */
            _memt.size -= lunits;       /*                   */
            return ( ( char _FAR * )p );  /*                  */
        }
    }
    if ( lunits * _MEMTSIZE >_msize )  /*                   */
        return ( NULL );                /*                   */
    p = ( struct _MEMT _FAR * )_mnext;  /*                   */
    _mnext += lbytes;                   /*                   */
    _msize -= lbytes;                   /*                   */
    if ( _pool.size == NULL ) {         /*                   */
        _pool.top = p;                  /*                   */
        _pool.size = lunits;            /*                   */
    }
    else {                              /*                   */
        q = _pool.top + _pool.size;     /*                   */
        if ( p == q )                   /*                   */
            _pool.size += lunits;       /*                   */
    }
    return ( ( char _FAR * )p );         /*                  */
}


/*******************************************************************************
*
* rlsmem 
*
 * COPYRIGHT(C) 1999(2000-2002) RENESAS TECHNOLOGY CORPORATION
 * AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
 *
*
*        rlsmem --          
*
*        ret = rlsmem( cp, nbytes );
*
*           int      ret;         
*           char     _FAR *cp;                        
*           unsigned nbytes;                
*
*        ret ==  0                  
*               == -1           "cp"                 
*
*                         
*                                     
*                                           
*                                          
*                                            
*                                            
*
*******************************************************************************/
int  rlsmem( char _FAR *cp, unsigned nbytes )   /*        */
{
    long lbytes;                        /*                   */
    long lunits;                        /*                   */
    struct _MEMT _FAR *p;     /*                   */
    struct _MEMT _FAR *pu;     /*                   */
    struct _MEMT _FAR *q;     /*                   */
    struct _MEMT _FAR *qu;     /*                   */
    struct _MEMT _FAR *n;     /*                   */
    struct _MEMT _FAR *nu;     /*                   */

    lbytes = nbytes;
    if ( lbytes <= 0 ) return ( -1 );   /*                   */
    n = ( struct _MEMT _FAR * )cp;      /*                   */
    lunits = ( lbytes + _MEMTSIZE - 1 ) / _MEMTSIZE;
                                        /*                   */
    nu = n + lunits;                    /*                   */
    _memt.size += lunits;               /*                   */
    q = &_memt;                         /*                   */
    for ( p = q->top; p != NULL; q = p, qu = pu, p = p->top ) {
                                        /*                   */
        pu = p + p->size;               /*                   */
        if ( p  > nu ) {                /*                   */
            n->top = p;                 /*                   */
            n->size = lunits;           /*                   */
            q->top = n;                 /*                   */
            return ( 0 );               /*                   */
        }
        if ( p == nu ) {                /*                   */
            n->top = p->top;            /*                   */
            n->size = lunits + p->size; /*                   */
            q->top = n;                 /*                   */
            return ( 0 );               /*                   */
        }
        if ( n < pu ) {                 /*                   */
            _memt.size -= lunits;       /*                   */
            return ( -1 );              /*                   */
        }
        if ( n == pu ) {                /*                   */
            if ( p->top != NULL ) {     /*                   */
                if ( nu > p->top ) {    /*                   */
                    _memt.size -= lunits;
                                        /*                   */
                    return ( -1 );      /*                   */
                }
            }
            p->size += lunits;          /*                   */
            if ( p->top != NULL ) {     /*                   */
                if ( nu == p->top ) {   /*                   */
                    p->size += nu->size;
                                        /*                   */
                    p->top = nu->top;   /*                   */
                }
            }
            return ( 0 );               /*                   */
        }
    }
    q->top = n;                         /*           "_MEMT"     */
    n->top = NULL;                      /*                   */
    n->size = lunits;                   /*                   */
    return ( 0 );                       /*                   */
}

/*******************************************************************************
*
* malloc.c 
*
 * COPYRIGHT(C) 1999(2000-2002) RENESAS TECHNOLOGY CORPORATION
 * AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
 *
*
*******************************************************************************/

webmaster@mikrocontroller.netImpressumWerbung auf Mikrocontroller.net