misc/libfreetype/src/base/ftdbgmem.c
author sheepluva
Thu, 21 Jul 2011 16:30:42 +0200
changeset 5434 99078dea3a8f
parent 5172 88f2e05288ba
permissions -rw-r--r--
fix wrong location of "Type"-label in map-selection
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     1
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     3
/*  ftdbgmem.c                                                             */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     4
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
/*    Memory debugger (body).                                              */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     7
/*  Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2009 by                  */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     8
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
/*  This file is part of the FreeType project, and may only be used,       */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    11
/*  modified, and distributed under the terms of the FreeType project      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    12
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
/*  this file you indicate that you have read the license and              */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
/*  understand and accept it fully.                                        */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    15
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    16
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    17
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
#include <ft2build.h>
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
#include FT_CONFIG_CONFIG_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
#include FT_INTERNAL_DEBUG_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
#include FT_INTERNAL_MEMORY_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
#include FT_SYSTEM_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
#include FT_ERRORS_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
#include FT_TYPES_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
#ifdef FT_DEBUG_MEMORY
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
#define  KEEPALIVE /* `Keep alive' means that freed blocks aren't released
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    31
                    * to the heap.  This is useful to detect double-frees
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    32
                    * or weird heap corruption, but it uses large amounts of
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
                    * memory, however.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
                    */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
#include FT_CONFIG_STANDARD_LIBRARY_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    37
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    38
  FT_BASE_DEF( const char* )  _ft_debug_file   = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
  FT_BASE_DEF( long )         _ft_debug_lineno = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
  extern void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
  FT_DumpMemory( FT_Memory  memory );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
  typedef struct FT_MemSourceRec_*  FT_MemSource;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
  typedef struct FT_MemNodeRec_*    FT_MemNode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
  typedef struct FT_MemTableRec_*   FT_MemTable;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
#define FT_MEM_VAL( addr )  ((FT_PtrDist)(FT_Pointer)( addr ))
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    51
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    52
  /*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
   *  This structure holds statistics for a single allocation/release
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
   *  site.  This is useful to know where memory operations happen the
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
   *  most.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    56
   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    57
  typedef struct  FT_MemSourceRec_
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
    const char*   file_name;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
    long          line_no;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
    FT_Long       cur_blocks;   /* current number of allocated blocks */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
    FT_Long       max_blocks;   /* max. number of allocated blocks    */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
    FT_Long       all_blocks;   /* total number of blocks allocated   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
    FT_Long       cur_size;     /* current cumulative allocated size */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
    FT_Long       max_size;     /* maximum cumulative allocated size */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
    FT_Long       all_size;     /* total cumulative allocated size   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
    FT_Long       cur_max;      /* current maximum allocated size */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    71
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    72
    FT_UInt32     hash;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
    FT_MemSource  link;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
  } FT_MemSourceRec;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
  /*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    79
   *  We don't need a resizable array for the memory sources, because
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    80
   *  their number is pretty limited within FreeType.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    81
   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    82
#define FT_MEM_SOURCE_BUCKETS  128
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    83
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    84
  /*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    85
   *  This structure holds information related to a single allocated
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    86
   *  memory block.  If KEEPALIVE is defined, blocks that are freed by
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    87
   *  FreeType are never released to the system.  Instead, their `size'
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    88
   *  field is set to -size.  This is mainly useful to detect double frees,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    89
   *  at the price of large memory footprint during execution.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    90
   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    91
  typedef struct  FT_MemNodeRec_
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    92
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    93
    FT_Byte*      address;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    94
    FT_Long       size;     /* < 0 if the block was freed */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    95
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    96
    FT_MemSource  source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    97
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    98
#ifdef KEEPALIVE
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    99
    const char*   free_file_name;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   100
    FT_Long       free_line_no;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   101
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   102
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   103
    FT_MemNode    link;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   104
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   105
  } FT_MemNodeRec;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   106
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   107
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   108
  /*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   109
   *  The global structure, containing compound statistics and all hash
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   110
   *  tables.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   111
   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   112
  typedef struct  FT_MemTableRec_
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   113
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   114
    FT_ULong         size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   115
    FT_ULong         nodes;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   116
    FT_MemNode*      buckets;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   117
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   118
    FT_ULong         alloc_total;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   119
    FT_ULong         alloc_current;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   120
    FT_ULong         alloc_max;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   121
    FT_ULong         alloc_count;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   122
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   123
    FT_Bool          bound_total;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   124
    FT_ULong         alloc_total_max;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   125
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   126
    FT_Bool          bound_count;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   127
    FT_ULong         alloc_count_max;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   128
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   129
    FT_MemSource     sources[FT_MEM_SOURCE_BUCKETS];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   130
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   131
    FT_Bool          keep_alive;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   132
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   133
    FT_Memory        memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   134
    FT_Pointer       memory_user;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   135
    FT_Alloc_Func    alloc;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   136
    FT_Free_Func     free;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   137
    FT_Realloc_Func  realloc;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   138
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   139
  } FT_MemTableRec;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   140
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   141
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   142
#define FT_MEM_SIZE_MIN  7
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   143
#define FT_MEM_SIZE_MAX  13845163
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   144
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   145
#define FT_FILENAME( x )  ((x) ? (x) : "unknown file")
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   146
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   147
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   148
  /*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   149
   *  Prime numbers are ugly to handle.  It would be better to implement
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   150
   *  L-Hashing, which is 10% faster and doesn't require divisions.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   151
   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   152
  static const FT_UInt  ft_mem_primes[] =
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   153
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   154
    7,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   155
    11,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   156
    19,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   157
    37,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   158
    73,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   159
    109,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   160
    163,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   161
    251,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   162
    367,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   163
    557,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   164
    823,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   165
    1237,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   166
    1861,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   167
    2777,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   168
    4177,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   169
    6247,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   170
    9371,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   171
    14057,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   172
    21089,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   173
    31627,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   174
    47431,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   175
    71143,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   176
    106721,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   177
    160073,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   178
    240101,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   179
    360163,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   180
    540217,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   181
    810343,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   182
    1215497,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   183
    1823231,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   184
    2734867,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   185
    4102283,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   186
    6153409,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   187
    9230113,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   188
    13845163,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   189
  };
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   190
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   191
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   192
  static FT_ULong
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   193
  ft_mem_closest_prime( FT_ULong  num )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   194
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   195
    FT_UInt  i;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   196
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   197
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   198
    for ( i = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   199
          i < sizeof ( ft_mem_primes ) / sizeof ( ft_mem_primes[0] ); i++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   200
      if ( ft_mem_primes[i] > num )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   201
        return ft_mem_primes[i];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   202
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   203
    return FT_MEM_SIZE_MAX;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   204
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   205
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   206
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   207
  extern void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   208
  ft_mem_debug_panic( const char*  fmt,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   209
                      ... )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   210
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   211
    va_list  ap;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   212
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   213
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   214
    printf( "FreeType.Debug: " );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   215
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   216
    va_start( ap, fmt );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   217
    vprintf( fmt, ap );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   218
    va_end( ap );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   219
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   220
    printf( "\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   221
    exit( EXIT_FAILURE );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   222
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   223
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   224
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   225
  static FT_Pointer
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   226
  ft_mem_table_alloc( FT_MemTable  table,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   227
                      FT_Long      size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   228
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   229
    FT_Memory   memory = table->memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   230
    FT_Pointer  block;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   231
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   232
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   233
    memory->user = table->memory_user;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   234
    block = table->alloc( memory, size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   235
    memory->user = table;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   236
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   237
    return block;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   238
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   239
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   240
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   241
  static void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   242
  ft_mem_table_free( FT_MemTable  table,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   243
                     FT_Pointer   block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   244
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   245
    FT_Memory  memory = table->memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   246
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   247
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   248
    memory->user = table->memory_user;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   249
    table->free( memory, block );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   250
    memory->user = table;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   251
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   252
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   253
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   254
  static void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   255
  ft_mem_table_resize( FT_MemTable  table )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   256
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   257
    FT_ULong  new_size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   258
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   259
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   260
    new_size = ft_mem_closest_prime( table->nodes );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   261
    if ( new_size != table->size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   262
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   263
      FT_MemNode*  new_buckets;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   264
      FT_ULong     i;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   265
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   266
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   267
      new_buckets = (FT_MemNode *)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   268
                      ft_mem_table_alloc( table,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   269
                                          new_size * sizeof ( FT_MemNode ) );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   270
      if ( new_buckets == NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   271
        return;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   272
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   273
      FT_ARRAY_ZERO( new_buckets, new_size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   274
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   275
      for ( i = 0; i < table->size; i++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   276
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   277
        FT_MemNode  node, next, *pnode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   278
        FT_PtrDist  hash;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   279
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   280
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   281
        node = table->buckets[i];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   282
        while ( node )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   283
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   284
          next  = node->link;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   285
          hash  = FT_MEM_VAL( node->address ) % new_size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   286
          pnode = new_buckets + hash;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   287
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   288
          node->link = pnode[0];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   289
          pnode[0]   = node;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   290
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   291
          node = next;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   292
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   293
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   294
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   295
      if ( table->buckets )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   296
        ft_mem_table_free( table, table->buckets );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   297
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   298
      table->buckets = new_buckets;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   299
      table->size    = new_size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   300
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   301
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   302
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   303
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   304
  static FT_MemTable
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   305
  ft_mem_table_new( FT_Memory  memory )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   306
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   307
    FT_MemTable  table;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   308
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   309
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   310
    table = (FT_MemTable)memory->alloc( memory, sizeof ( *table ) );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   311
    if ( table == NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   312
      goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   313
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   314
    FT_ZERO( table );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   315
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   316
    table->size  = FT_MEM_SIZE_MIN;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   317
    table->nodes = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   318
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   319
    table->memory = memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   320
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   321
    table->memory_user = memory->user;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   322
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   323
    table->alloc   = memory->alloc;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   324
    table->realloc = memory->realloc;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   325
    table->free    = memory->free;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   326
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   327
    table->buckets = (FT_MemNode *)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   328
                       memory->alloc( memory,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   329
                                      table->size * sizeof ( FT_MemNode ) );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   330
    if ( table->buckets )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   331
      FT_ARRAY_ZERO( table->buckets, table->size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   332
    else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   333
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   334
      memory->free( memory, table );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   335
      table = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   336
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   337
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   338
  Exit:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   339
    return table;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   340
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   341
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   342
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   343
  static void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   344
  ft_mem_table_destroy( FT_MemTable  table )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   345
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   346
    FT_ULong  i;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   347
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   348
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   349
    FT_DumpMemory( table->memory );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   350
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   351
    if ( table )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   352
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   353
      FT_Long   leak_count = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   354
      FT_ULong  leaks      = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   355
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   356
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   357
      /* remove all blocks from the table, revealing leaked ones */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   358
      for ( i = 0; i < table->size; i++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   359
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   360
        FT_MemNode  *pnode = table->buckets + i, next, node = *pnode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   361
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   362
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   363
        while ( node )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   364
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   365
          next       = node->link;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   366
          node->link = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   367
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   368
          if ( node->size > 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   369
          {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   370
            printf(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   371
              "leaked memory block at address %p, size %8ld in (%s:%ld)\n",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   372
              node->address, node->size,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   373
              FT_FILENAME( node->source->file_name ),
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   374
              node->source->line_no );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   375
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   376
            leak_count++;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   377
            leaks += node->size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   378
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   379
            ft_mem_table_free( table, node->address );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   380
          }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   381
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   382
          node->address = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   383
          node->size    = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   384
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   385
          ft_mem_table_free( table, node );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   386
          node = next;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   387
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   388
        table->buckets[i] = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   389
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   390
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   391
      ft_mem_table_free( table, table->buckets );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   392
      table->buckets = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   393
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   394
      table->size  = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   395
      table->nodes = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   396
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   397
      /* remove all sources */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   398
      for ( i = 0; i < FT_MEM_SOURCE_BUCKETS; i++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   399
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   400
        FT_MemSource  source, next;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   401
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   402
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   403
        for ( source = table->sources[i]; source != NULL; source = next )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   404
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   405
          next = source->link;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   406
          ft_mem_table_free( table, source );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   407
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   408
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   409
        table->sources[i] = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   410
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   411
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   412
      printf(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   413
        "FreeType: total memory allocations = %ld\n", table->alloc_total );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   414
      printf(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   415
        "FreeType: maximum memory footprint = %ld\n", table->alloc_max );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   416
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   417
      ft_mem_table_free( table, table );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   418
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   419
      if ( leak_count > 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   420
        ft_mem_debug_panic(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   421
          "FreeType: %ld bytes of memory leaked in %ld blocks\n",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   422
          leaks, leak_count );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   423
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   424
      printf( "FreeType: no memory leaks detected\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   425
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   426
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   427
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   428
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   429
  static FT_MemNode*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   430
  ft_mem_table_get_nodep( FT_MemTable  table,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   431
                          FT_Byte*     address )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   432
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   433
    FT_PtrDist   hash;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   434
    FT_MemNode  *pnode, node;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   435
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   436
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   437
    hash  = FT_MEM_VAL( address );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   438
    pnode = table->buckets + ( hash % table->size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   439
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   440
    for (;;)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   441
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   442
      node = pnode[0];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   443
      if ( !node )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   444
        break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   445
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   446
      if ( node->address == address )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   447
        break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   448
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   449
      pnode = &node->link;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   450
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   451
    return pnode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   452
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   453
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   454
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   455
  static FT_MemSource
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   456
  ft_mem_table_get_source( FT_MemTable  table )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   457
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   458
    FT_UInt32     hash;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   459
    FT_MemSource  node, *pnode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   460
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   461
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   462
    /* cast to FT_PtrDist first since void* can be larger */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   463
    /* than FT_UInt32 and GCC 4.1.1 emits a warning       */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   464
    hash  = (FT_UInt32)(FT_PtrDist)(void*)_ft_debug_file +
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   465
              (FT_UInt32)( 5 * _ft_debug_lineno );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   466
    pnode = &table->sources[hash % FT_MEM_SOURCE_BUCKETS];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   467
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   468
    for ( ;; )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   469
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   470
      node = *pnode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   471
      if ( node == NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   472
        break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   473
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   474
      if ( node->file_name == _ft_debug_file &&
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   475
           node->line_no   == _ft_debug_lineno   )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   476
        goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   477
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   478
      pnode = &node->link;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   479
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   480
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   481
    node = (FT_MemSource)ft_mem_table_alloc( table, sizeof ( *node ) );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   482
    if ( node == NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   483
      ft_mem_debug_panic(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   484
        "not enough memory to perform memory debugging\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   485
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   486
    node->file_name = _ft_debug_file;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   487
    node->line_no   = _ft_debug_lineno;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   488
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   489
    node->cur_blocks = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   490
    node->max_blocks = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   491
    node->all_blocks = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   492
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   493
    node->cur_size   = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   494
    node->max_size   = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   495
    node->all_size   = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   496
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   497
    node->cur_max    = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   498
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   499
    node->link = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   500
    node->hash = hash;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   501
    *pnode     = node;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   502
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   503
  Exit:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   504
    return node;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   505
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   506
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   507
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   508
  static void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   509
  ft_mem_table_set( FT_MemTable  table,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   510
                    FT_Byte*     address,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   511
                    FT_ULong     size,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   512
                    FT_Long      delta )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   513
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   514
    FT_MemNode  *pnode, node;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   515
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   516
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   517
    if ( table )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   518
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   519
      FT_MemSource  source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   520
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   521
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   522
      pnode = ft_mem_table_get_nodep( table, address );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   523
      node  = *pnode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   524
      if ( node )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   525
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   526
        if ( node->size < 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   527
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   528
          /* This block was already freed.  Our memory is now completely */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   529
          /* corrupted!                                                  */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   530
          /* This can only happen in keep-alive mode.                    */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   531
          ft_mem_debug_panic(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   532
            "memory heap corrupted (allocating freed block)" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   533
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   534
        else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   535
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   536
          /* This block was already allocated.  This means that our memory */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   537
          /* is also corrupted!                                            */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   538
          ft_mem_debug_panic(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   539
            "memory heap corrupted (re-allocating allocated block at"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   540
            " %p, of size %ld)\n"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   541
            "org=%s:%d new=%s:%d\n",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   542
            node->address, node->size,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   543
            FT_FILENAME( node->source->file_name ), node->source->line_no,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   544
            FT_FILENAME( _ft_debug_file ), _ft_debug_lineno );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   545
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   546
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   547
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   548
      /* we need to create a new node in this table */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   549
      node = (FT_MemNode)ft_mem_table_alloc( table, sizeof ( *node ) );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   550
      if ( node == NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   551
        ft_mem_debug_panic( "not enough memory to run memory tests" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   552
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   553
      node->address = address;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   554
      node->size    = size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   555
      node->source  = source = ft_mem_table_get_source( table );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   556
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   557
      if ( delta == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   558
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   559
        /* this is an allocation */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   560
        source->all_blocks++;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   561
        source->cur_blocks++;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   562
        if ( source->cur_blocks > source->max_blocks )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   563
          source->max_blocks = source->cur_blocks;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   564
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   565
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   566
      if ( size > (FT_ULong)source->cur_max )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   567
        source->cur_max = size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   568
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   569
      if ( delta != 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   570
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   571
        /* we are growing or shrinking a reallocated block */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   572
        source->cur_size     += delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   573
        table->alloc_current += delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   574
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   575
      else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   576
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   577
        /* we are allocating a new block */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   578
        source->cur_size     += size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   579
        table->alloc_current += size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   580
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   581
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   582
      source->all_size += size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   583
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   584
      if ( source->cur_size > source->max_size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   585
        source->max_size = source->cur_size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   586
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   587
      node->free_file_name = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   588
      node->free_line_no   = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   589
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   590
      node->link = pnode[0];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   591
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   592
      pnode[0] = node;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   593
      table->nodes++;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   594
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   595
      table->alloc_total += size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   596
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   597
      if ( table->alloc_current > table->alloc_max )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   598
        table->alloc_max = table->alloc_current;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   599
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   600
      if ( table->nodes * 3 < table->size  ||
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   601
           table->size  * 3 < table->nodes )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   602
        ft_mem_table_resize( table );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   603
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   604
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   605
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   606
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   607
  static void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   608
  ft_mem_table_remove( FT_MemTable  table,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   609
                       FT_Byte*     address,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   610
                       FT_Long      delta )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   611
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   612
    if ( table )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   613
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   614
      FT_MemNode  *pnode, node;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   615
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   616
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   617
      pnode = ft_mem_table_get_nodep( table, address );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   618
      node  = *pnode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   619
      if ( node )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   620
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   621
        FT_MemSource  source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   622
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   623
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   624
        if ( node->size < 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   625
          ft_mem_debug_panic(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   626
            "freeing memory block at %p more than once at (%s:%ld)\n"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   627
            "block allocated at (%s:%ld) and released at (%s:%ld)",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   628
            address,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   629
            FT_FILENAME( _ft_debug_file ), _ft_debug_lineno,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   630
            FT_FILENAME( node->source->file_name ), node->source->line_no,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   631
            FT_FILENAME( node->free_file_name ), node->free_line_no );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   632
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   633
        /* scramble the node's content for additional safety */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   634
        FT_MEM_SET( address, 0xF3, node->size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   635
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   636
        if ( delta == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   637
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   638
          source = node->source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   639
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   640
          source->cur_blocks--;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   641
          source->cur_size -= node->size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   642
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   643
          table->alloc_current -= node->size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   644
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   645
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   646
        if ( table->keep_alive )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   647
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   648
          /* we simply invert the node's size to indicate that the node */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   649
          /* was freed.                                                 */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   650
          node->size           = -node->size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   651
          node->free_file_name = _ft_debug_file;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   652
          node->free_line_no   = _ft_debug_lineno;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   653
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   654
        else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   655
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   656
          table->nodes--;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   657
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   658
          *pnode = node->link;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   659
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   660
          node->size   = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   661
          node->source = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   662
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   663
          ft_mem_table_free( table, node );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   664
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   665
          if ( table->nodes * 3 < table->size  ||
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   666
               table->size  * 3 < table->nodes )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   667
            ft_mem_table_resize( table );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   668
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   669
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   670
      else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   671
        ft_mem_debug_panic(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   672
          "trying to free unknown block at %p in (%s:%ld)\n",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   673
          address,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   674
          FT_FILENAME( _ft_debug_file ), _ft_debug_lineno );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   675
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   676
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   677
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   678
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   679
  extern FT_Pointer
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   680
  ft_mem_debug_alloc( FT_Memory  memory,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   681
                      FT_Long    size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   682
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   683
    FT_MemTable  table = (FT_MemTable)memory->user;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   684
    FT_Byte*     block;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   685
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   686
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   687
    if ( size <= 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   688
      ft_mem_debug_panic( "negative block size allocation (%ld)", size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   689
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   690
    /* return NULL if the maximum number of allocations was reached */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   691
    if ( table->bound_count                           &&
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   692
         table->alloc_count >= table->alloc_count_max )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   693
      return NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   694
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   695
    /* return NULL if this allocation would overflow the maximum heap size */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   696
    if ( table->bound_total                                             &&
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   697
         table->alloc_total_max - table->alloc_current > (FT_ULong)size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   698
      return NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   699
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   700
    block = (FT_Byte *)ft_mem_table_alloc( table, size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   701
    if ( block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   702
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   703
      ft_mem_table_set( table, block, (FT_ULong)size, 0 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   704
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   705
      table->alloc_count++;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   706
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   707
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   708
    _ft_debug_file   = "<unknown>";
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   709
    _ft_debug_lineno = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   710
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   711
    return (FT_Pointer)block;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   712
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   713
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   714
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   715
  extern void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   716
  ft_mem_debug_free( FT_Memory   memory,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   717
                     FT_Pointer  block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   718
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   719
    FT_MemTable  table = (FT_MemTable)memory->user;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   720
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   721
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   722
    if ( block == NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   723
      ft_mem_debug_panic( "trying to free NULL in (%s:%ld)",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   724
                          FT_FILENAME( _ft_debug_file ),
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   725
                          _ft_debug_lineno );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   726
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   727
    ft_mem_table_remove( table, (FT_Byte*)block, 0 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   728
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   729
    if ( !table->keep_alive )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   730
      ft_mem_table_free( table, block );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   731
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   732
    table->alloc_count--;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   733
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   734
    _ft_debug_file   = "<unknown>";
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   735
    _ft_debug_lineno = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   736
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   737
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   738
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   739
  extern FT_Pointer
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   740
  ft_mem_debug_realloc( FT_Memory   memory,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   741
                        FT_Long     cur_size,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   742
                        FT_Long     new_size,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   743
                        FT_Pointer  block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   744
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   745
    FT_MemTable  table = (FT_MemTable)memory->user;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   746
    FT_MemNode   node, *pnode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   747
    FT_Pointer   new_block;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   748
    FT_Long      delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   749
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   750
    const char*  file_name = FT_FILENAME( _ft_debug_file );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   751
    FT_Long      line_no   = _ft_debug_lineno;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   752
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   753
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   754
    /* unlikely, but possible */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   755
    if ( new_size == cur_size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   756
      return block;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   757
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   758
    /* the following is valid according to ANSI C */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   759
#if 0
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   760
    if ( block == NULL || cur_size == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   761
      ft_mem_debug_panic( "trying to reallocate NULL in (%s:%ld)",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   762
                          file_name, line_no );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   763
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   764
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   765
    /* while the following is allowed in ANSI C also, we abort since */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   766
    /* such case should be handled by FreeType.                      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   767
    if ( new_size <= 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   768
      ft_mem_debug_panic(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   769
        "trying to reallocate %p to size 0 (current is %ld) in (%s:%ld)",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   770
        block, cur_size, file_name, line_no );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   771
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   772
    /* check `cur_size' value */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   773
    pnode = ft_mem_table_get_nodep( table, (FT_Byte*)block );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   774
    node  = *pnode;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   775
    if ( !node )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   776
      ft_mem_debug_panic(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   777
        "trying to reallocate unknown block at %p in (%s:%ld)",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   778
        block, file_name, line_no );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   779
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   780
    if ( node->size <= 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   781
      ft_mem_debug_panic(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   782
        "trying to reallocate freed block at %p in (%s:%ld)",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   783
        block, file_name, line_no );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   784
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   785
    if ( node->size != cur_size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   786
      ft_mem_debug_panic( "invalid ft_realloc request for %p. cur_size is "
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   787
                          "%ld instead of %ld in (%s:%ld)",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   788
                          block, cur_size, node->size, file_name, line_no );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   789
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   790
    /* return NULL if the maximum number of allocations was reached */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   791
    if ( table->bound_count                           &&
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   792
         table->alloc_count >= table->alloc_count_max )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   793
      return NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   794
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   795
    delta = (FT_Long)( new_size - cur_size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   796
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   797
    /* return NULL if this allocation would overflow the maximum heap size */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   798
    if ( delta > 0                                                       &&
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   799
         table->bound_total                                              &&
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   800
         table->alloc_current + (FT_ULong)delta > table->alloc_total_max )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   801
      return NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   802
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   803
    new_block = (FT_Byte *)ft_mem_table_alloc( table, new_size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   804
    if ( new_block == NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   805
      return NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   806
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   807
    ft_mem_table_set( table, (FT_Byte*)new_block, new_size, delta );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   808
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   809
    ft_memcpy( new_block, block, cur_size < new_size ? cur_size : new_size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   810
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   811
    ft_mem_table_remove( table, (FT_Byte*)block, delta );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   812
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   813
    _ft_debug_file   = "<unknown>";
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   814
    _ft_debug_lineno = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   815
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   816
    if ( !table->keep_alive )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   817
      ft_mem_table_free( table, block );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   818
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   819
    return new_block;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   820
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   821
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   822
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   823
  extern FT_Int
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   824
  ft_mem_debug_init( FT_Memory  memory )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   825
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   826
    FT_MemTable  table;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   827
    FT_Int       result = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   828
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   829
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   830
    if ( getenv( "FT2_DEBUG_MEMORY" ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   831
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   832
      table = ft_mem_table_new( memory );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   833
      if ( table )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   834
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   835
        const char*  p;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   836
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   837
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   838
        memory->user    = table;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   839
        memory->alloc   = ft_mem_debug_alloc;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   840
        memory->realloc = ft_mem_debug_realloc;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   841
        memory->free    = ft_mem_debug_free;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   842
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   843
        p = getenv( "FT2_ALLOC_TOTAL_MAX" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   844
        if ( p != NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   845
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   846
          FT_Long   total_max = ft_atol( p );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   847
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   848
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   849
          if ( total_max > 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   850
          {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   851
            table->bound_total     = 1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   852
            table->alloc_total_max = (FT_ULong)total_max;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   853
          }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   854
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   855
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   856
        p = getenv( "FT2_ALLOC_COUNT_MAX" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   857
        if ( p != NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   858
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   859
          FT_Long  total_count = ft_atol( p );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   860
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   861
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   862
          if ( total_count > 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   863
          {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   864
            table->bound_count     = 1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   865
            table->alloc_count_max = (FT_ULong)total_count;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   866
          }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   867
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   868
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   869
        p = getenv( "FT2_KEEP_ALIVE" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   870
        if ( p != NULL )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   871
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   872
          FT_Long  keep_alive = ft_atol( p );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   873
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   874
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   875
          if ( keep_alive > 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   876
            table->keep_alive = 1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   877
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   878
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   879
        result = 1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   880
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   881
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   882
    return result;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   883
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   884
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   885
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   886
  extern void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   887
  ft_mem_debug_done( FT_Memory  memory )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   888
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   889
    FT_MemTable  table = (FT_MemTable)memory->user;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   890
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   891
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   892
    if ( table )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   893
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   894
      memory->free    = table->free;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   895
      memory->realloc = table->realloc;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   896
      memory->alloc   = table->alloc;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   897
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   898
      ft_mem_table_destroy( table );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   899
      memory->user = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   900
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   901
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   902
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   903
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   904
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   905
  static int
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   906
  ft_mem_source_compare( const void*  p1,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   907
                         const void*  p2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   908
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   909
    FT_MemSource  s1 = *(FT_MemSource*)p1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   910
    FT_MemSource  s2 = *(FT_MemSource*)p2;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   911
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   912
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   913
    if ( s2->max_size > s1->max_size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   914
      return 1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   915
    else if ( s2->max_size < s1->max_size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   916
      return -1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   917
    else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   918
      return 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   919
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   920
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   921
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   922
  extern void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   923
  FT_DumpMemory( FT_Memory  memory )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   924
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   925
    FT_MemTable  table = (FT_MemTable)memory->user;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   926
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   927
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   928
    if ( table )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   929
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   930
      FT_MemSource*  bucket = table->sources;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   931
      FT_MemSource*  limit  = bucket + FT_MEM_SOURCE_BUCKETS;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   932
      FT_MemSource*  sources;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   933
      FT_UInt        nn, count;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   934
      const char*    fmt;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   935
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   936
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   937
      count = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   938
      for ( ; bucket < limit; bucket++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   939
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   940
        FT_MemSource  source = *bucket;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   941
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   942
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   943
        for ( ; source; source = source->link )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   944
          count++;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   945
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   946
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   947
      sources = (FT_MemSource*)ft_mem_table_alloc(
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   948
                                 table, sizeof ( *sources ) * count );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   949
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   950
      count = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   951
      for ( bucket = table->sources; bucket < limit; bucket++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   952
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   953
        FT_MemSource  source = *bucket;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   954
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   955
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   956
        for ( ; source; source = source->link )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   957
          sources[count++] = source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   958
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   959
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   960
      ft_qsort( sources, count, sizeof ( *sources ), ft_mem_source_compare );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   961
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   962
      printf( "FreeType Memory Dump: "
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   963
              "current=%ld max=%ld total=%ld count=%ld\n",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   964
              table->alloc_current, table->alloc_max,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   965
              table->alloc_total, table->alloc_count );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   966
      printf( " block  block    sizes    sizes    sizes   source\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   967
      printf( " count   high      sum  highsum      max   location\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   968
      printf( "-------------------------------------------------\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   969
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   970
      fmt = "%6ld %6ld %8ld %8ld %8ld %s:%d\n";
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   971
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   972
      for ( nn = 0; nn < count; nn++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   973
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   974
        FT_MemSource  source = sources[nn];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   975
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   976
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   977
        printf( fmt,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   978
                source->cur_blocks, source->max_blocks,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   979
                source->cur_size, source->max_size, source->cur_max,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   980
                FT_FILENAME( source->file_name ),
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   981
                source->line_no );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   982
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   983
      printf( "------------------------------------------------\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   984
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   985
      ft_mem_table_free( table, sources );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   986
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   987
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   988
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   989
#else  /* !FT_DEBUG_MEMORY */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   990
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   991
  /* ANSI C doesn't like empty source files */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   992
  typedef int  _debug_mem_dummy;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   993
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   994
#endif /* !FT_DEBUG_MEMORY */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   995
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   996
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   997
/* END */