Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

mem.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the OPIE Project
00003                              
00004                =.            Copyright (c)  2002 Andy Qua <andy.qua@blueyonder.co.uk>
00005              .=l.                                Dan Williams <drw@handhelds.org>
00006            .>+-=
00007  _;:,     .>    :=|.         This file is free software; you can
00008 .> <`_,   >  .   <=          redistribute it and/or modify it under
00009 :`=1 )Y*s>-.--   :           the terms of the GNU General Public
00010 .="- .-=="i,     .._         License as published by the Free Software
00011  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00012      ._= =}       :          or (at your option) any later version.
00013     .%`+i>       _;_.
00014     .i_,=:_.      -<s.       This file is distributed in the hope that
00015      +  .  -:.       =       it will be useful, but WITHOUT ANY WARRANTY;
00016     : ..    .:,     . . .    without even the implied warranty of
00017     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00018   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU General
00019 ..}^=.=       =       ;      Public License for more details.
00020 ++=   -.     .`     .:
00021  :     =  ...= . :.=-        You should have received a copy of the GNU
00022  -.   .:....=;==+<;          General Public License along with this file;
00023   -_. . .   )=.  =           see the file COPYING. If not, write to the
00024     --        :-=`           Free Software Foundation, Inc.,
00025                              59 Temple Place - Suite 330,
00026                              Boston, MA 02111-1307, USA.
00027 
00028 */
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 #include <qvaluelist.h>
00035 
00036 #define __MEMFILE_C
00037 #include "global.h"
00038 
00039 #ifdef _DEBUG
00040 
00041 void __cdecl *operator new( unsigned int size, const char *file, int line )
00042 {
00043       void *ptr = (void *)malloc(size);
00044       AddTrack((long)ptr, size, file, line);
00045       return(ptr);
00046 }
00047 
00048 void operator delete(void *p)
00049 {
00050       RemoveTrack((long)p);
00051       free(p);
00052 }
00053 
00054 #endif
00055 
00056 
00057 typedef struct {
00058       long      address;
00059       long      size;
00060       char      file[64];
00061       long      line;
00062 } ALLOC_INFO;
00063 
00064 typedef QValueList<ALLOC_INFO*> AllocList;
00065 
00066 AllocList allocList;
00067 
00068 
00069 
00070 void AddTrack(long addr,  long asize,  const char *fname, long lnum)
00071 {
00072       ALLOC_INFO *info;
00073 
00074 
00075       info = (ALLOC_INFO *)malloc(sizeof( ALLOC_INFO ));
00076       info->address = addr;
00077       strncpy(info->file, fname, 63);
00078       info->line = lnum;
00079       info->size = asize;
00080       allocList.insert(allocList.begin(), info);
00081 };
00082 
00083 void RemoveTrack(long addr)
00084 {
00085       AllocList::Iterator i;
00086 
00087       bool found = false;
00088       for(i = allocList.begin(); i != allocList.end(); i++)
00089       {
00090               if((*i)->address == addr)
00091               {
00092                       allocList.remove((*i));
00093                       found = true;
00094                       break;
00095               }
00096       }
00097 }
00098 
00099 void DumpUnfreed()
00100 {
00101       AllocList::Iterator i;
00102       long totalSize = 0;
00103       char buf[1024];
00104       // Debug output, okay to leave untranslated
00105       for(i = allocList.begin(); i != allocList.end(); i++) {
00106               sprintf(buf, "%-15s:  LINE %ld,  ADDRESS %ld %ld unfreed",
00107                       (*i)->file, (*i)->line, (*i)->address, (*i)->size);
00108               totalSize += (*i)->size;
00109       }
00110       sprintf(buf, "-----------------------------------------------------------\n");
00111       sprintf(buf, "Total Unfreed: %ld bytes\n", totalSize);
00112 };

Generated on Sat Nov 5 16:17:47 2005 for OPIE by  doxygen 1.4.2