home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / ft-beta.zip / freetype / lib / ttmemory.c < prev    next >
C/C++ Source or Header  |  1997-10-06  |  6KB  |  256 lines

  1. /*******************************************************************
  2.  *
  3.  *  ttmemory.c                                               1.2
  4.  *
  5.  *    Memory management component (body).
  6.  *
  7.  *  Copyright 1996, 1997 by
  8.  *  David Turner, Robert Wilhelm, and Werner Lemberg.
  9.  *
  10.  *  This file is part of the FreeType project, and may only be used
  11.  *  modified and distributed under the terms of the FreeType project
  12.  *  license, LICENSE.TXT. By continuing to use, modify or distribute
  13.  *  this file you indicate that you have read the license and
  14.  *  understand and accept it fully.
  15.  *
  16.  *
  17.  *  Changes between 1.1 and 1.2 :
  18.  *
  19.  *  - the font pool is gone
  20.  *
  21.  *  - introduced the FREE macro and the Free function for
  22.  *    future use in destructors.
  23.  *
  24.  *  - Init_FontPool is now a macro to allow the compilation of
  25.  *    'legacy' applications (all four test programs have been updated)
  26.  *
  27.  ******************************************************************/
  28.  
  29. #include "tterror.h"
  30. #include "ttmemory.h"
  31. #include "ttengine.h"
  32. #include <stdlib.h>
  33.  
  34. #ifdef DEBUG_MEMORY
  35.  
  36. #include <stdio.h>
  37.  
  38.   #define MAX_TRACKED_BLOCKS  1024
  39.  
  40.   typedef struct _TMemRec
  41.   {
  42.     void*  base;
  43.     long   size;
  44.   } TMemRec;
  45.  
  46.   static TMemRec  pointers[MAX_TRACKED_BLOCKS+1];
  47.  
  48.   static int  num_alloc;
  49.   static int  num_free;
  50.   static int  fail_alloc;
  51.   static int  fail_free;
  52.  
  53. #endif
  54.  
  55. #ifndef TT_CONFIG_REENTRANT
  56.   long TTMemory_Allocated;
  57.   long TTMemory_MaxAllocated;
  58. #endif
  59.  
  60. /*******************************************************************
  61.  *
  62.  *  Function    :  TT_Alloc
  63.  *
  64.  *  Description :  Allocates memory from the heap buffer.
  65.  *
  66.  *  Input  :  Size      size of the memory to be allocated
  67.  *            P         pointer to a buffer pointer
  68.  *
  69.  *  Output :  Error code.  
  70.  *
  71.  *  NOTE : The newly allocated block should _always_ be zeroed
  72.  *         on return. Many parts of the engine rely on this to
  73.  *         work properly
  74.  *
  75.  ******************************************************************/
  76.  
  77.   TT_Error  TT_Alloc( long  Size, void**  P ) 
  78.   {
  79.     #ifdef DEBUG_MEMORY
  80.     Int i;
  81.     #endif
  82.  
  83.     if (Size)
  84.     {
  85.       *P = malloc( Size );
  86.       if ( !*P )
  87.         return TT_Err_Out_Of_Memory;
  88.  
  89.       #ifndef TT_CONFIG_REENTRANT
  90.       TTMemory_Allocated    += Size;
  91.       TTMemory_MaxAllocated += Size;
  92.       #endif
  93.  
  94.       #ifdef DEBUG_MEMORY
  95.  
  96.         num_alloc++;
  97.  
  98.         i = 0;
  99.         while ( i < MAX_TRACKED_BLOCKS && pointers[i].base != NULL )
  100.           i++;
  101.  
  102.         if (i >= MAX_TRACKED_BLOCKS)
  103.           fail_alloc++;
  104.         else
  105.         {
  106.           pointers[i].base = *P;
  107.           pointers[i].size = Size;
  108.         }
  109.   
  110.       #endif /* DEBUG_MEMORY */
  111.  
  112.       MEM_Set( *P, 0, Size );
  113.     }
  114.     else
  115.       *P = NULL;
  116.  
  117.     return TT_Err_Ok;
  118.   }
  119.  
  120.  
  121. /*******************************************************************
  122.  *
  123.  *  Function    :  TT_Free
  124.  *
  125.  *  Description :  Release a previously allocated block of memory
  126.  *
  127.  *  Input  :  P    pointer to memory block
  128.  *
  129.  *  Output :  always SUCCESS
  130.  *
  131.  *  Note : The pointer must _always_ be set to NULL by this function
  132.  *
  133.  ******************************************************************/
  134.  
  135.   TT_Error  TT_Free( void**  P )
  136.   {
  137.     #ifdef DEBUG_MEMORY
  138.       Int i;
  139.     #endif /* DEBUG_MEMORY */
  140.  
  141.     if ( !P || !*P )
  142.       return TT_Err_Ok;
  143.  
  144.     #ifdef DEBUG_MEMORY
  145.       num_free++;
  146.  
  147.       i = 0;
  148.       while ( i < MAX_TRACKED_BLOCKS && pointers[i].base != *P )
  149.         i++;
  150.  
  151.       if (i >= MAX_TRACKED_BLOCKS)
  152.         fail_free++;
  153.       else
  154.       {
  155.         #ifndef TT_CONFIG_REENTRANT
  156.         TTMemory_Allocated -= pointers[i].size;
  157.         #endif
  158.  
  159.         pointers[i].base = NULL;
  160.         pointers[i].size = 0;
  161.       }
  162.     #endif
  163.  
  164.     free( *P );
  165.  
  166.     *P = NULL;
  167.  
  168.     return TT_Err_Ok;
  169.   }
  170.  
  171.  
  172.   TT_Error  TTMemory_Init()
  173.   {
  174.  
  175. #ifdef DEBUG_MEMORY
  176.     Int i;
  177.     for ( i = 0; i < MAX_TRACKED_BLOCKS; i++ )
  178.     {
  179.       pointers[i].base = NULL;
  180.       pointers[i].size = 0;
  181.     }
  182.  
  183.     num_alloc = 0;
  184.     num_free  = 0;
  185.  
  186.     fail_alloc = 0;
  187.     fail_free  = 0;
  188. #endif
  189.  
  190.  
  191. #ifndef TT_CONFIG_REENTRANT
  192.     TTMemory_Allocated    = 0;
  193.     TTMemory_MaxAllocated = 0;
  194. #endif
  195.  
  196.     engine.memory_component = NULL;
  197.  
  198.     return TT_Err_Ok;
  199.   }
  200.  
  201.  
  202.   TT_Error  TTMemory_Done()
  203.   {
  204.  
  205. #ifdef DEBUG_MEMORY
  206.     Int i, num_leaked, tot_leaked;
  207.  
  208.     num_leaked = 0;
  209.     tot_leaked = 0;
  210.  
  211.     for ( i = 0; i < MAX_TRACKED_BLOCKS; i++ )
  212.     {
  213.       if (pointers[i].base)
  214.       {
  215.         num_leaked ++;
  216.         tot_leaked += pointers[i].size;
  217.       }
  218.     }
  219.  
  220.     fprintf( stderr, 
  221.              "%d memory allocations, of which %d failed\n",
  222.              num_alloc,
  223.              fail_alloc );
  224.  
  225.     fprintf( stderr,
  226.              "%d memory frees, of which %d failed\n",
  227.              num_free,
  228.              fail_free );
  229.                
  230.     if ( num_leaked > 0 )
  231.     {
  232.       fprintf( stderr, 
  233.                "There are %d leaked memory blocks, totalizing %d bytes\n",
  234.                num_leaked, tot_leaked );
  235.  
  236.       for ( i = 0; i < MAX_TRACKED_BLOCKS; i++ )
  237.       {
  238.         if (pointers[i].base)
  239.         {
  240.           fprintf( stderr, 
  241.                    "index: %4d ( base: $%08lx, size: %08ld )\n",
  242.                    i,
  243.                    (long)pointers[i].base,
  244.                    pointers[i].size );
  245.         }
  246.       }
  247.     }
  248.     else
  249.       fprintf( stderr, "No memory leaks !\n" );
  250. #endif
  251.  
  252.     return TT_Err_Ok;
  253.   }
  254.  
  255. /* End */
  256.