Fern   www.davinpearson.com
New Zealanders making it harder to hate computers

    Main Menu     Research Projects      Photo Album      Curriculum Vitae   Greatest Artists  
  Email Address   Computer Games       Web Design          Java Tutorials         Other Links     
Debugging Macros String Class I Linked List System I Java for C Programmers Naming Convention
String Class II How I use m4 Strings III Symmetrical I/O Linked List System II
Run-Time Type Info Virtual Methods An Array System Science & Religion Submodes
Nested Packages Memory Leaks Garbage Collection Internet & Poverty What is Knowledge?
Limits of Evolution Emacs Additions Function Plotter


dmm.cc

#include "dmm.hh"

#ifdef DMM_ONLINE

struct Map_Entry
{
   bool             is_used;
   enum_debug_code  debug_code;
   int              how_many;
   Map_Entry()
   {
      is_used = false;
   }
   Map_Entry(enum_debug_code debug_code)
   {
      this->is_used    = true;
      this->debug_code = debug_code;
      this->how_many   = 1;
   }
};

class Map
{
private:
   static const int NUM_ENTRIES = 1000;
   ///
   /// first_free is the same as length...
   ///
   int       first_free;
   Map_Entry entries[NUM_ENTRIES];

public:
   Map()
   {
      first_free = 0;
   }
   void increment_key(enum_debug_code debug_code)
   {
      bool found = false;
      for (int i=0; i<NUM_ENTRIES; i++) {
         if (entries[i].is_used && entries[i].debug_code == debug_code) {
            entries[i].how_many++;
            found = true;
            break;
         }
      }

      if (!found) {
         ASSERT(first_free < NUM_ENTRIES);
         entries[first_free] = Map_Entry(debug_code);
         first_free++;
      }
   }

private:
#define DCODE_CASE(var) case var: return #var;

   const char* debug_decode(enum_debug_code code)
   {
      switch (code)
      {
         ///
         DCODE_CASE(DCODE_UNKNOWN);
         //DCODE_CASE(DCODE_REF_COUNT);

         DCODE_CASE(DCODE_BOOL);
         DCODE_CASE(DCODE_INT);
         DCODE_CASE(DCODE_XYI);
         DCODE_CASE(DCODE_XYQ);
         DCODE_CASE(DCODE_V3Q);

         DCODE_CASE(DCODE_INT_MANUAL);
         //DCODE_CASE(DCODE_PTR);
         ///
         DCODE_CASE(DCODE_QUICK);
         DCODE_CASE(DCODE_STRING);
         /// FROM: ../noallegro/*
         DCODE_CASE(DCODE_VECTOR_2D_CARTESIAN);
         /// FROM: ../nolist/list.hh
         DCODE_CASE(DCODE_LIST);
         DCODE_CASE(DCODE_NODE);
         /// FROM: ../../2004/noarray/array.hh
         DCODE_CASE(DCODE_ARRAY);

         /// FROM: ~/zallegro/2004/sprite-grabber/sprite-grabber.hh
         DCODE_CASE(DCODE_SPRITE_GRABBER_ACTION);

         /// FROM: ~/zallegro/2004/Tritus-II/*
         DCODE_CASE(DCODE_TRI_RANDOM);

         /// FOR TESTING PURPOSES:
         DCODE_CASE(DCODE_FOO);
      }
      return "<Error>";
      // char ch1 = (code >> 24) & 0xff;
      // char ch2 = (code >> 16) & 0xff;
      // char ch3 = (code >>  8) & 0xff;
      // char ch4 = (code >>  0) & 0xff;
      // std::cout << ch1 << ch2 << ch3 << ch4;
   }

public:
   void print()
   {
      std::cout << "**** BEGIN unfreed memory printout\n";
      for (int i=0; i<first_free; i++) {
         Map_Entry& me = entries[i];
         std::cout << '(';
         std::cout << debug_decode(me.debug_code);
         std::cout << ' ' << me.how_many << ")\n";

      }
      std::cout << "**** END unfreed memory printout\n";
   }
};

^L                                      // ALLOCATOR

class Debug_Allocator
{
public:
   bool is_used;
   int  address;
   Debug_Allocator()
   {
      this->is_used = false;
      this->address = 0;
   }
   Debug_Allocator(int address)
   {
      this->is_used = true;
      this->address = address;
   }
};

const  int      DEBUG_MEMORY_TABLE_ARRAY_SIZE = 1000 * 1000;
Debug_Allocator debug_memory_table_array[DEBUG_MEMORY_TABLE_ARRAY_SIZE];

void dmm_print_and_exit()
{
   std::cout << "**** BEGIN dmm_print_and_exit\n";

   Map m;

   int total_count   = 0;
   int unknown_count = 0;
   for (int i=0; i<DEBUG_MEMORY_TABLE_ARRAY_SIZE; i++) {
      //CHECKPOINT;
      if (debug_memory_table_array[i].is_used) {
         total_count++;

         int* addr = reinterpret_cast<int*>(debug_memory_table_array[i].address);
         int  code = *(addr + 1);
         enum_debug_code edc = static_cast<enum_debug_code>(code);

#undef  DCODE_CASE
#define DCODE_CASE(var) case var: m.increment_key(edc); found = true; break

         bool found = false;
         switch (edc) {
            DCODE_CASE(DCODE_UNKNOWN);
            //DCODE_CASE(DCODE_REF_COUNT);

            DCODE_CASE(DCODE_INT);
            DCODE_CASE(DCODE_BOOL);
            DCODE_CASE(DCODE_XYI);
            DCODE_CASE(DCODE_XYQ);
            DCODE_CASE(DCODE_V3Q);

            DCODE_CASE(DCODE_INT_MANUAL);
            //DCODE_CASE(DCODE_PTR);
            DCODE_CASE(DCODE_QUICK);
            DCODE_CASE(DCODE_STRING);
            /// FROM: ../noallegro/*
            DCODE_CASE(DCODE_VECTOR_2D_CARTESIAN);
            /// FROM: ../nolist2/list.hh
            DCODE_CASE(DCODE_LIST);
            DCODE_CASE(DCODE_NODE);
            /// FROM: ../../2004/noarray/array.hh
            DCODE_CASE(DCODE_ARRAY);

            /// FROM: ~/zallegro/2004/sprite-grabber/sprite-grabber.hh
            DCODE_CASE(DCODE_SPRITE_GRABBER_ACTION);

            /// FROM: ~/zallegro/2004/Tritus-II/*
            DCODE_CASE(DCODE_TRI_RANDOM);

            /// FOR TESTING PURPOSES:
            DCODE_CASE(DCODE_FOO);
         }

         if (!found) {
            unknown_count++;
         }
      }
   }

   int known_count = total_count - unknown_count;
   PRINT(unknown_count);
   PRINT(known_count);
   PRINT(total_count);
   PRINT(DEBUG_MEMORY_TABLE_ARRAY_SIZE);

   m.print();

   std::cout << "**** END dmm_print_and_exit\n";
   std::cout << "*** Called exit(EXIT_SUCCESS);\n";
   exit(EXIT_SUCCESS);
}

void* operator new (size_t size)
{
   //count++;
   ///
   /// YEHAR: cout works even before the main function is run!
   /// WARNING: careful with the ordering of linking... the last file in the list is executed first...
   ///
   //cout << "*** BEGIN Womble\n";

   void* address = ::malloc(size);

   if (address == null) {
      std::cout << "*** Bugger memory exhausted\n";
      exit(EXIT_FAILURE);
   }

   bool found = false;
   for (int i=0; i<DEBUG_MEMORY_TABLE_ARRAY_SIZE; i++) {
      if (!debug_memory_table_array[i].is_used) {
         debug_memory_table_array[i] = Debug_Allocator(reinterpret_cast<int>(address));
         found = true;
         break;
      }
   }
   if (!found) {
      std::cout <<  "debug_memory_table_array is full\n";
      exit(EXIT_FAILURE);
   }

   //std::cout << "*** Allocated address=" << reinterpret_cast<int>(address) << '\n';
   //cout << "*** END Womble\n";
   return address;
}

void operator delete (void* p)
{
   if (p == null) {
      return;
   }

   int address = reinterpret_cast<int>(p);
   bool found = false;
   for (int i=0; i<DEBUG_MEMORY_TABLE_ARRAY_SIZE; i++) {
      if (debug_memory_table_array[i].is_used &&
          (debug_memory_table_array[i].address == address)) {
         debug_memory_table_array[i].is_used = false;
         found = true;
         break;
      }
   }

   ///
   /// NOTE: About 4 operator deletes are called without ever calling operator new
   ///
   if (!found) {
      //std::cout << "*** Freed but not found address=" << address << '\n';
   }

   ::free(p);
}

#endif /* DMM_ONLINE */


Back

| Main Menu | Research Projects | Photo Album | Curriculum Vitae | Greatest Artists |
| Email Address | Computer Games | Web Design | Java Tutorials | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked List System II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter |

Please report any broken links to the webperson
Last modified: Wed Jun 13 18:47:31 NZST 2007
© Copyright 1999-2007 Davin Pearson.



Hosted by www.Geocities.ws

1