File: Util.C

package info (click to toggle)
waili 19990723-19
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,288 kB
  • ctags: 940
  • sloc: cpp: 9,964; ansic: 273; makefile: 205; sh: 74
file content (181 lines) | stat: -rw-r--r-- 4,228 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
//	Utility Routines
//
//  $Id: Util.C,v 4.0.2.2.2.1 1999/07/20 16:15:52 geert Exp $
//
//  Copyright (C) 1996-1999 Department of Computer Science, K.U.Leuven, Belgium
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "waili/Types.h"
#include "waili/Util.h"

#include "exception"

    //  Program Failure

static char ERRSTR[512];

void Die(const char *fmt, ...)
{
    ERRSTR[0]=0;
    
    va_list args;
  
    va_start(args, fmt);
    vsnprintf(ERRSTR+strlen(ERRSTR), 512, fmt, args);
    va_end(args);
    fprintf(stderr,ERRSTR);
    //exception e;//=new exception();
    //e->what=ERRSTR;
    char *e=ERRSTR;
    throw e;
    
}


// ----------------------------------------------------------------------------

#ifdef TRACK_MEMORY

    //  Memory Tracking

#define MAX_HASH	(31)
#define MEMTRACK_MAGIC	(0x4d74724d)	// MtrM

struct MemChunk {
    u32 Magic;
    struct MemChunk *Next;
    size_t Size;
    bool IsArray;
    // sizeof(struct MemChunk) == 16
    u_char Address[0];
};

static struct MemChunk *MemChunks[MAX_HASH] = { 0, };
static size_t AllocatedMemory = 0;


static void AddMemChunk(struct MemChunk *chunk)
{
    u_int hash = (u_int)chunk % MAX_HASH;

    chunk->Magic = MEMTRACK_MAGIC;
    chunk->Next = MemChunks[hash];
    MemChunks[hash] = chunk;
    AllocatedMemory += chunk->Size;
}

static void RemMemChunk(struct MemChunk *chunk)
{
    u_int hash = (u_int)chunk % MAX_HASH;
    struct MemChunk **p = &MemChunks[hash];
    const char *msg;

    switch (chunk->Magic) {
    	case MEMTRACK_MAGIC:
	    for (p = &MemChunks[hash]; *p; p = &(*p)->Next)
		if (*p == chunk) {
		    *p = chunk->Next;
		    chunk->Magic = ~MEMTRACK_MAGIC;
		    AllocatedMemory -= chunk->Size;
		    return;
		}
	    msg = "Not on list";
	    break;
	case ~MEMTRACK_MAGIC:
	    msg = "Freed twice";
	    break;
	default:
	    msg = "No magic";
	    break;
    }
    fprintf(stderr, "Warning: trying to delete unallocated block of size %d at"
		    " %p (%s)\n", chunk->Size, chunk->Address, msg);
}


static void *TrackedMalloc(size_t size, bool is_array)
{
    struct MemChunk *chunk;

    if (!(chunk = (struct MemChunk *)malloc(sizeof(struct MemChunk)+size)))
	Die("Fatal Error: Can't allocate %d bytes\n", size);
    else {
	chunk->Size = size;
	chunk->IsArray = is_array;
	AddMemChunk(chunk);
	return(chunk->Address);
    }
}

static void TrackedFree(void *p, bool is_array)
{
    struct MemChunk *chunk;

    if (p) {
	chunk = (struct MemChunk *)(p-sizeof(struct MemChunk));
	if (chunk->IsArray != is_array)
	    fprintf(stderr, "Warning: using delete%s on a block allocated with"
			    " new%s (%d bytes at %p)\n",
			    is_array ? " []" : "", is_array ? "" : " []",
			    chunk->Size, chunk->Address);
	RemMemChunk(chunk);
	free(chunk);
    }
}

void *operator new(size_t size)
{
    return(TrackedMalloc(size, 0));
}

void *operator new[](size_t size)
{
    return(TrackedMalloc(size, 1));
}

void operator delete(void *p)
{
    TrackedFree(p, 0);
}

void operator delete[](void *p)
{
    TrackedFree(p, 1);
}


void DumpMemoryStatus(void)
{
    struct MemChunk *chunk;

    if (AllocatedMemory) {
	fprintf(stderr, "%d bytes allocated:\n", AllocatedMemory);
	for (u_int i = 0; i < MAX_HASH; i++)
	    for (chunk = MemChunks[i]; chunk; chunk = chunk->Next)
		fprintf(stderr, "    %p: %d bytes%s\n", chunk->Address,
			chunk->Size, chunk->IsArray ? " []" : "");
    } else
	fputs("No memory allocated\n", stderr);
}

#endif /* TRACK_MEMORY */