home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / MODEL / ALLOC.C next >
C/C++ Source or Header  |  1996-04-19  |  828b  |  55 lines

  1. /*
  2.  *        メモリ管理
  3.  *
  4.  *        T.Kobayashi        1993.10.24
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #include "ml.h"
  11. #include "inlib.h"
  12.  
  13. #define    MALLOC_MAGIC    0x12345678
  14.  
  15. typedef    struct    {
  16.     int        magic ;
  17.     int        size ;
  18. } MemoryHeader ;
  19.  
  20. void    *MemoryAlloc( size )
  21. int        size ;
  22. {
  23.     MemoryHeader    *mem ;
  24.  
  25.     mem = malloc( size + sizeof( MemoryHeader ) );
  26.     if ( mem == NULL )
  27.         return NULL ;
  28.     mem->magic = MALLOC_MAGIC ;
  29.     mem->size = size ;
  30.  
  31. #if    0
  32.     printf( "malloc( %d ) : %08X\n", size, mem );
  33. #endif
  34.  
  35.     return (void*)( mem + 1 );
  36. }
  37.  
  38. void    MemoryFree( ptr )
  39. void    *ptr ;
  40. {
  41.     MemoryHeader    *mem ;
  42.  
  43.     mem = (MemoryHeader*)ptr - 1 ;
  44.  
  45. #if    0
  46.     printf( "free: %08X\n", mem );
  47. #endif
  48.     if ( mem->magic != MALLOC_MAGIC )
  49.     {
  50.         ExecError( "Memory Block Error(%X)!!!", mem );
  51.     }
  52.     mem->magic = 0 ;
  53.     free( mem );
  54. }
  55.