VirtualDeleteOperator.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 
11 #include <iostream>
12 
14 {
15 public:
16  CallCounter();
17  void recordNew();
18  void recordDelete();
19  size_t news() const;
20  size_t deletes() const;
21 private:
22  size_t _news, _deletes;
23 };
24 
25 CallCounter::CallCounter() : _news(0), _deletes(0) {}
26 
29 size_t CallCounter::news() const {return _news;}
30 size_t CallCounter::deletes() const {return _deletes;}
31 
33 {
34 public:
35  void *operator new (size_t);
36  void operator delete (void *);
37  virtual ~BaseVirtual();
38  static void DeleteABase(BaseVirtual *aBase);
40 };
41 
43 
44 void *
45 BaseVirtual::operator new(size_t byteCount)
46 {
47  Calls.recordNew();
48  return ::operator new (byteCount);
49 }
50 
51 void
52 BaseVirtual::operator delete(void *address)
53 {
54  Calls.recordDelete();
55  ::operator delete (address);
56 }
57 
58 void
60 {
61  delete aBase;
62 }
63 
65 
66 class ChildVirtual : public BaseVirtual
67 {
68 public:
69  void *operator new (size_t);
70  void operator delete (void *);
71  virtual ~ChildVirtual();
73 };
74 
76 
77 void *
78 ChildVirtual::operator new(size_t byteCount)
79 {
80  Calls.recordNew();
81  return ::operator new (byteCount);
82 }
83 
84 void
85 ChildVirtual::operator delete(void *address)
86 {
87  Calls.recordDelete();
88  ::operator delete (address);
89 }
90 
92 
93 int
94 main(int, char *[])
95 {
96  assert (BaseVirtual::Calls.news() == 0);
97  assert (BaseVirtual::Calls.deletes() == 0);
98  assert (ChildVirtual::Calls.news() == 0);
99  assert (ChildVirtual::Calls.deletes() == 0);
100  BaseVirtual *aBase = new ChildVirtual;
101  assert (BaseVirtual::Calls.news() == 0);
102  assert (BaseVirtual::Calls.deletes() == 0);
103  assert (ChildVirtual::Calls.news() == 1);
104  assert (ChildVirtual::Calls.deletes() == 0);
106  assert (BaseVirtual::Calls.news() == 0);
107  assert (BaseVirtual::Calls.deletes() == 0);
108  assert (ChildVirtual::Calls.news() == 1);
109  assert (ChildVirtual::Calls.deletes() == 1);
110  // deleting nullptr works.
111  BaseVirtual::DeleteABase(nullptr);
112  assert (BaseVirtual::Calls.news() == 0);
113  assert (BaseVirtual::Calls.deletes() == 0);
114  assert (ChildVirtual::Calls.news() == 1);
115  assert (ChildVirtual::Calls.deletes() == 1);
116  return EXIT_SUCCESS;
117 }
118 
int main(int, char *[])
#define assert(EX)
Definition: assert.h:17
static CallCounter Calls
size_t news() const
size_t deletes() const
static CallCounter Calls
static void DeleteABase(BaseVirtual *aBase)
int const char size_t
Definition: stub_liblog.cc:84

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors