File: Zone.h

package info (click to toggle)
sbnc 1.2-26
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,216 kB
  • sloc: cpp: 17,556; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 284
file content (393 lines) | stat: -rw-r--r-- 8,613 bytes parent folder | download | duplicates (3)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*******************************************************************************
 * shroudBNC - an object-oriented framework for IRC                            *
 * Copyright (C) 2005-2007 Gunnar Beutner                                      *
 *                                                                             *
 * 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. *
 *******************************************************************************/

template<typename Type, int HunkSize>
void ZoneLeakCheck(void *Zone);

/**
 * CZoneInformation
 *
 * Provides information about a zone.
 */
struct CZoneInformation {
public:
	virtual unsigned int GetCount(void) const = 0;
	virtual const char *GetTypeName(void) const = 0;
	virtual size_t GetTypeSize(void) const = 0;
	virtual void PerformLeakCheck(void) const = 0;
};

/**
 * hunkobject_t
 *
 * A single hunk object.
 */
template<typename Type>
struct hunkobject_s {
	bool Valid; /**< specifies whether this object is in use */
	char Data[sizeof(Type)]; /**< the object's data */
};

/**
 * CZone<Type>
 *
 * A memory zone used for efficiently storing similar objects.
 */
template<typename Type, int HunkSize>
class SBNCAPI CZone : public CZoneInformation {

	template<typename HunkType, int HunkObjectSize>
	struct hunk_s {
		bool Full;
		hunk_s<HunkType, HunkObjectSize> *NextHunk;
		hunkobject_s<HunkType> HunkObjects[HunkObjectSize]; /**< the objects in this hunk */
	};

	hunk_s<Type, HunkSize> *m_FirstHunk;

	unsigned int m_DeletedItems;
	unsigned int m_Count;
	bool m_Registered;

	/**
	 * AddHunk
	 *
	 * Creates a new hunk of memory in this zone.
	 */
	hunk_s<Type, HunkSize> *AddHunk(void) {
		hunk_s<Type, HunkSize> *NewHunk;

		NewHunk = (hunk_s<Type, HunkSize> *)malloc(sizeof(hunk_s<Type, HunkSize>));

		if (NewHunk == NULL) {
			return NULL;
		}

		mmark(NewHunk);

		NewHunk->NextHunk = m_FirstHunk;
		m_FirstHunk = NewHunk;

		NewHunk->Full = false;

		for (unsigned int i = 0; i < HunkSize; i++) {
			NewHunk->HunkObjects[i].Valid = false;
		}

		return NewHunk;
	}

	/**
	 * Optimize
	 *
	 * Optimizes the zone by removing unused hunks.
	 */
	void Optimize(void) {
		bool UnusedHunk;
		hunk_s<Type, HunkSize> *PreviousHunk = m_FirstHunk;
		hunk_s<Type, HunkSize> *Hunk = m_FirstHunk->NextHunk;

		while (Hunk != NULL) {
			UnusedHunk = true;

			if (!Hunk->Full) {
				for (unsigned int h = 0; h < HunkSize; h++) {
					hunkobject_s<Type> *HunkObject = &(Hunk->HunkObjects[h]);

					if (HunkObject->Valid) {
						UnusedHunk = false;

						break;
					}
				}
			} else {
				UnusedHunk = false;
			}

			if (UnusedHunk) {
				PreviousHunk->NextHunk = Hunk->NextHunk;

				free(Hunk);

				Hunk = PreviousHunk->NextHunk;
			} else {
				PreviousHunk = Hunk;

				Hunk = Hunk->NextHunk;
			}
		}
	}

public:
	/**
	 * CZone
	 *
	 * Constructs a new memory zone which can be used for
	 * allocating fixed-sized memory blocks.
	 */
	CZone(void) {
		m_Count = 0;
		m_DeletedItems = 0;
		m_FirstHunk = NULL;
	}

	/**
	 * ~CZone
	 *
	 * Destructs a memory zone.
	 */
	virtual ~CZone(void) {
		hunk_s<Type, HunkSize> *Hunk;

		if (m_FirstHunk != NULL) {
			Hunk = m_FirstHunk->NextHunk;

			while (Hunk != NULL) {
				hunk_s<Type, HunkSize> *NextHunk = Hunk->NextHunk;

				free(Hunk);

				Hunk = NextHunk;
			}
		}
	}

	/**
	 * Register
	 *
	 * Registers a zone.
	 */
	bool Register(void) {
		return RegisterZone(this);
	}

	/**
	 * Allocate
	 *
	 * Allocates a new object from the memory zone.
	 */
	Type *Allocate(void) {
#ifdef _DEBUG
		return (Type *)malloc(sizeof(Type));
#endif

		hunk_s<Type, HunkSize> *Hunk;

		if (!m_Registered) {
			m_Registered = Register();
		}

		Hunk = m_FirstHunk;

		while (Hunk != NULL) {
			if (!Hunk->Full) {
				for (unsigned int h = 0; h < HunkSize; h++) {
					hunkobject_s<Type> *HunkObject = &(Hunk->HunkObjects[h]);

					if (!HunkObject->Valid) {
						HunkObject->Valid = true;

						m_Count++;

						return (Type *)HunkObject->Data;
					}
				}

				Hunk->Full = true;
			}

			Hunk = Hunk->NextHunk;
		}

		Hunk = AddHunk();

		if (Hunk == NULL) {
			return NULL;
		}

		m_Count++;

		Hunk->HunkObjects[0].Valid = true;

		return (Type *)Hunk->HunkObjects[0].Data;
	}

	/**
	 * Delete
	 *
	 * Marks an object as unused.
	 */
	void Delete(Type *Object) {
#ifdef _DEBUG
		free(Object);

		return;
#endif

		hunk_s<Type, HunkSize> *Hunk = m_FirstHunk;
		hunkobject_s<Type> *HunkObject = NULL;
		hunk_s<Type, HunkSize> *OwningHunk;

		HunkObject = (hunkobject_s<Type> *)((char *)Object - ((char *)HunkObject->Data - (char *)HunkObject));

		if (!HunkObject->Valid) {
			safe_printf("Double free for zone object %p", Object);
		} else {
			m_Count--;

			OwningHunk = NULL;

			while (Hunk != NULL) {
				if (HunkObject >= Hunk->HunkObjects && HunkObject < &(Hunk->HunkObjects[HunkSize])) {
					OwningHunk = Hunk;

					break;
				}

				Hunk = Hunk->NextHunk;
			}

			if (OwningHunk != NULL) {
				OwningHunk->Full = false;
			} else {
				safe_printf("CZone::Delete(): Couldn't find hunk for an object.\n");
			}
		}

		HunkObject->Valid = false;

		m_DeletedItems++;

		if ((m_DeletedItems % 10) == 0) {
			Optimize();
		}
	}

	/**
	 * PerformLeakCheck
	 *
	 * Checks the zone for leaked objects. This function should be called
	 * when there _should_ be no more active objects in this zone.
	 */
	void PerformLeakCheck(void) const {
#if defined(_DEBUG) && defined(SBNC)
		int Count = GetCount();

		if (Count > 0) {
			safe_printf("Leaked %d zone objects of type \"%s\" (size %d).\n", Count, GetTypeName(), GetTypeSize());
		}
#endif
	}

	/**
	 * GetCount
	 *
	 * Returns the number of active objects in the memory zone.
	 */
	unsigned int GetCount(void) const {
		return m_Count;
	}

	/**
	 * GetTypeName
	 *
	 * Returns the typename of the objects which can be allocated
	 * using the zone object.
	 */
	const char *GetTypeName(void) const {
		return typeid(Type).name();
	}

	/**
	 * GetTypeSize
	 *
	 * Returns the size of the objects which can be allocated
	 * using the zone object.
	 */
	size_t GetTypeSize(void) const {
		return sizeof(Type);
	}
};

/**
 * ZoneLeakCheck<>
 *
 * Performs a leakcheck on a zone object.
 */
template<typename Type, int HunkSize>
void ZoneLeakCheck(void *Zone) {
	CZone<Type, HunkSize> *ZoneObject = (CZone<Type, HunkSize> *)Zone;

	ZoneObject->LeakCheck();
}

/**
 * CZoneObject<Type>
 *
 * A class which uses zones for allocation of its objects.
 */
template<typename InheritedClass, int HunkSize = 512>
class CZoneObject {
	static CZone<InheritedClass, HunkSize> m_Zone; /**< the zone for objects of this class */
public:
	/**
	 * operator new
	 *
	 * Overrides the operator "new" for classes which inherit from this class.
	 *
	 * @param Size the requested size of the new memory block
	 */
	void *operator new(size_t Size) throw() {
		assert(Size <= sizeof(InheritedClass));

		return m_Zone.Allocate();
	}

	void *operator new(size_t Size, CMemoryManager *Manager) throw() {
		assert(Size <= sizeof(InheritedClass));

		if (!Manager->MemoryAddBytes(Size)) {
			return NULL;
		}

		Manager->MemoryRemoveBytes(Size);

		return m_Zone.Allocate();
	}

	/**
	 * operator delete
	 *
	 * Overrides the operator "delete" for classes which inherit from this class.
	 *
	 * @param Object the object whics is to be deleted
	 */
	void operator delete(void *Object) {
		m_Zone.Delete((InheritedClass *)Object);
	}

	const CZone<InheritedClass, HunkSize> *GetZone(void) {
		return &m_Zone;
	}
};

template<typename InheritedClass, int HunkSize>
CZone<InheritedClass, HunkSize> CZoneObject<InheritedClass, HunkSize>::m_Zone;

#define unew new (GETUSER())