source: trunk/icedtea-web/tests/cpp-unit-tests/checked_allocations.cc

Last change on this file was 427, checked in by dmik, 11 years ago

icedtea-web: Import version 1.5.1 from vendor.

File size: 2.9 KB
Line 
1/* Copyright (C) 2012 Red Hat
2
3 This file is part of IcedTea.
4
5 IcedTea is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 IcedTea is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library. Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module. An independent module is a module which is not derived from
32 or based on this library. If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so. If you do not wish to do so, delete this
35 exception statement from your version. */
36
37// Overrides global 'new' operator with one that does error checking.
38
39#include <new>
40
41#include <UnitTest++.h>
42#include "checked_allocations.h"
43
44// We keep a set of allocations, that, for obvious reasons, does not itself use the 'new' operator.
45static AllocationSet* __allocations = NULL;
46
47// Override global definition of new and delete!
48void* operator new(size_t size) throw (std::bad_alloc) {
49 if (!__allocations) {
50 // This uses placement-new, which calls the constructor on a specific memory location
51 // This is needed because we cannot call 'new' in this context, nor can we rely on static-initialization
52 // for the set to occur before any call to 'new'!
53 void* memory = malloc(sizeof(AllocationSet));
54 __allocations = new (memory) AllocationSet();
55 }
56
57 void* mem = malloc(size);
58 if (mem == 0) {
59 throw std::bad_alloc(); // ANSI/ISO compliant behavior
60 }
61 __allocations->insert(mem);
62 return mem;
63}
64
65void operator delete(void* ptr) throw () {
66 if (__allocations->erase(ptr)) {
67 free(ptr);
68 } else {
69 printf(
70 "Attempt to free memory with operator 'delete' that was not allocated by 'new'!\n");
71 CHECK(false);
72 }
73}
74
75int cpp_unfreed_allocations() {
76 return __allocations->size();
77}
Note: See TracBrowser for help on using the repository browser.