VirtualDeleteOperator.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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{
15public:
17 void recordNew();
18 void recordDelete();
19 size_t news() const;
20 size_t deletes() const;
21private:
22 size_t _news, _deletes;
23};
24
25CallCounter::CallCounter() : _news(0), _deletes(0) {}
26
29size_t CallCounter::news() const {return _news;}
30size_t CallCounter::deletes() const {return _deletes;}
31
33{
34public:
35 void *operator new (size_t);
36 void operator delete (void *);
37 virtual ~BaseVirtual();
38 static void DeleteABase(BaseVirtual *aBase);
40};
41
43
44void *
45BaseVirtual::operator new(size_t byteCount)
46{
47 Calls.recordNew();
48 return ::operator new (byteCount);
49}
50
51void
52BaseVirtual::operator delete(void *address)
53{
54 Calls.recordDelete();
55 ::operator delete (address);
56}
57
58void
60{
61 delete aBase;
62}
63
65
67{
68public:
69 void *operator new (size_t);
70 void operator delete (void *);
71 ~ChildVirtual() override;
73};
74
76
77void *
78ChildVirtual::operator new(size_t byteCount)
79{
80 Calls.recordNew();
81 return ::operator new (byteCount);
82}
83
84void
85ChildVirtual::operator delete(void *address)
86{
87 Calls.recordDelete();
88 ::operator delete (address);
89}
90
92
93int
94main(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.
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 void DeleteABase(BaseVirtual *aBase)
static CallCounter Calls
size_t news() const
size_t deletes() const
~ChildVirtual() override
static CallCounter Calls
int const char size_t
Definition: stub_liblog.cc:83

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors