Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Near heap realloc routines.
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. //#include "dll.h"        // needs to be first
  33. #include "variety.h"
  34. #include <stddef.h>
  35. #include <malloc.h>
  36. #include <string.h>
  37. #include "heap.h"
  38. #if defined(__DOS_EXT__)
  39.  #include "extender.h"
  40. #endif
  41.  
  42. #if defined(__SMALL_DATA__)
  43.  
  44. _WCRTLINK void *realloc( void *stg, size_t amount )
  45.     {
  46.         return( _nrealloc( stg, amount ) );
  47.     }
  48.  
  49. #endif
  50.  
  51. #pragma intrinsic(memcpy)
  52.  
  53. _WCRTLINK void _WCNEAR *_nrealloc( void _WCI86NEAR *stg, size_t req_size )
  54.     {
  55.         void _WCNEAR *p;
  56.         size_t     old_size;
  57.  
  58.         if( stg == NULL ) {
  59.             return( _nmalloc( req_size ) );
  60.         }
  61.         if( req_size == 0 ) {
  62.             _nfree( stg );
  63.             return( (void _WCNEAR *) NULL );
  64.         }
  65.         old_size = _nmsize( stg );
  66.         p = _nexpand( stg, req_size );  /* try to expand it in place */
  67.         if( p == NULL ) {               /* if couldn't be expanded in place */
  68.             #if defined(__DOS_EXT__)
  69.             if( _IsRational() ) {
  70.                 frlptr  flp, newflp;
  71.  
  72.                 flp = (frlptr) ((PTR)stg - TAG_SIZE);
  73.                 newflp = __ReAllocDPMIBlock( flp, req_size + TAG_SIZE );
  74.                 if( newflp ) {
  75.                     return( (void _WCNEAR *)((PTR)newflp + TAG_SIZE) );
  76.                 }
  77.             }
  78.             #endif
  79.             p = _nmalloc( req_size );   /* - allocate a new block */
  80.             if( p != NULL ) {           /* - if we got one */
  81.                 memcpy( p, stg, old_size );  /* copy it */
  82.                 _nfree( stg );                  /* and free old one */
  83.             } else {
  84.                 _nexpand( stg, old_size );      /* reset back to old size */
  85.             }
  86.         }
  87.         return( p );
  88.     }
  89.