• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thetic / cpputest / 12923813957

23 Jan 2025 06:48AM UTC coverage: 98.514% (-0.8%) from 99.279%
12923813957

push

github

thetic
Delete stuff

2 of 2 new or added lines in 1 file covered. (100.0%)

36 existing lines in 5 files now uncovered.

5436 of 5518 relevant lines covered (98.51%)

9343.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.57
/src/CppUTest/TestMemoryAllocator.cpp
1
/*
2
 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *     * Redistributions of source code must retain the above copyright
8
 *       notice, this list of conditions and the following disclaimer.
9
 *     * Redistributions in binary form must reproduce the above copyright
10
 *       notice, this list of conditions and the following disclaimer in the
11
 *       documentation and/or other materials provided with the distribution.
12
 *     * Neither the name of the <organization> nor the
13
 *       names of its contributors may be used to endorse or promote products
14
 *       derived from this software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ''AS IS'' AND ANY
17
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27

28
#include "CppUTest/TestHarness.h"
29
#include "CppUTest/TestMemoryAllocator.h"
30
#include "CppUTest/PlatformSpecificFunctions.h"
31

32
static char* checkedMalloc(size_t size)
221,821✔
33
{
34
    char* mem = (char*) PlatformSpecificMalloc(size);
221,821✔
35
    if (mem == NULLPTR)
221,821✔
36
    FAIL("malloc returned null pointer");
1✔
37
    return mem;
221,820✔
38
}
39

40
static TestMemoryAllocator* currentNewAllocator = NULLPTR;
41
static TestMemoryAllocator* currentNewArrayAllocator = NULLPTR;
42
static TestMemoryAllocator* currentMallocAllocator = NULLPTR;
43

44
void setCurrentNewAllocator(TestMemoryAllocator* allocator)
30✔
45
{
46
    currentNewAllocator = allocator;
30✔
47
}
30✔
48

49
TestMemoryAllocator* getCurrentNewAllocator()
45✔
50
{
51
    if (currentNewAllocator == NULLPTR) setCurrentNewAllocatorToDefault();
45✔
52
    return currentNewAllocator;
45✔
53
}
54

55
void setCurrentNewAllocatorToDefault()
3✔
56
{
57
    currentNewAllocator = defaultNewAllocator();
3✔
58
}
3✔
59

60
TestMemoryAllocator* defaultNewAllocator()
8✔
61
{
62
    static TestMemoryAllocator allocator("Standard New Allocator", "new", "delete");
8✔
63
    return &allocator;
8✔
64
}
65

66
void setCurrentNewArrayAllocator(TestMemoryAllocator* allocator)
29✔
67
{
68
    currentNewArrayAllocator = allocator;
29✔
69
}
29✔
70

71
TestMemoryAllocator* getCurrentNewArrayAllocator()
43✔
72
{
73
    if (currentNewArrayAllocator == NULLPTR) setCurrentNewArrayAllocatorToDefault();
43✔
74
    return currentNewArrayAllocator;
43✔
75
}
76

77
void setCurrentNewArrayAllocatorToDefault()
3✔
78
{
79
    currentNewArrayAllocator = defaultNewArrayAllocator();
3✔
80
}
3✔
81

82
TestMemoryAllocator* defaultNewArrayAllocator()
432,511✔
83
{
84
    static TestMemoryAllocator allocator("Standard New [] Allocator", "new []", "delete []");
432,511✔
85
    return &allocator;
432,511✔
86
}
87

88
void setCurrentMallocAllocator(TestMemoryAllocator* allocator)
29✔
89
{
90
    currentMallocAllocator = allocator;
29✔
91
}
29✔
92

93
TestMemoryAllocator* getCurrentMallocAllocator()
56✔
94
{
95
    if (currentMallocAllocator == NULLPTR) setCurrentMallocAllocatorToDefault();
56✔
96
    return currentMallocAllocator;
56✔
97
}
98

99
void setCurrentMallocAllocatorToDefault()
4✔
100
{
101
    currentMallocAllocator = defaultMallocAllocator();
4✔
102
}
4✔
103

104
TestMemoryAllocator* defaultMallocAllocator()
40✔
105
{
106
    static TestMemoryAllocator allocator("Standard Malloc Allocator", "malloc", "free");
40✔
107
    return &allocator;
40✔
108
}
109

110
/////////////////////////////////////////////
111

112
GlobalMemoryAllocatorStash::GlobalMemoryAllocatorStash()
18✔
113
    : originalMallocAllocator(NULLPTR), originalNewAllocator(NULLPTR), originalNewArrayAllocator(NULLPTR)
18✔
114
{
115
}
18✔
116

117
void GlobalMemoryAllocatorStash::save()
18✔
118
{
119
    originalMallocAllocator = getCurrentMallocAllocator();
18✔
120
    originalNewAllocator = getCurrentNewAllocator();
18✔
121
    originalNewArrayAllocator = getCurrentNewArrayAllocator();
18✔
122
}
18✔
123

124
void GlobalMemoryAllocatorStash::restore()
18✔
125
{
126
    if (originalMallocAllocator) setCurrentMallocAllocator(originalMallocAllocator);
18✔
127
    if (originalNewAllocator) setCurrentNewAllocator(originalNewAllocator);
18✔
128
    if (originalNewArrayAllocator) setCurrentNewArrayAllocator(originalNewArrayAllocator);
18✔
129
}
18✔
130

131
TestMemoryAllocator::TestMemoryAllocator(const char* name_str, const char* alloc_name_str, const char* free_name_str)
224✔
132
    : name_(name_str), alloc_name_(alloc_name_str), free_name_(free_name_str), hasBeenDestroyed_(false)
224✔
133
{
134
}
224✔
135

136
TestMemoryAllocator::~TestMemoryAllocator()
228✔
137
{
138
    hasBeenDestroyed_ = true;
224✔
139
}
228✔
140

UNCOV
141
bool TestMemoryAllocator::hasBeenDestroyed()
×
142
{
UNCOV
143
    return hasBeenDestroyed_;
×
144
}
145

UNCOV
146
bool TestMemoryAllocator::isOfEqualType(TestMemoryAllocator* allocator)
×
147
{
UNCOV
148
    return SimpleString::StrCmp(this->name(), allocator->name()) == 0;
×
149
}
150

151
char* TestMemoryAllocator::alloc_memory(size_t size, const char*, size_t)
221,821✔
152
{
153
    return checkedMalloc(size);
221,821✔
154
}
155

156
void TestMemoryAllocator::free_memory(char* memory, size_t, const char*, size_t)
221,510✔
157
{
158
    PlatformSpecificFree(memory);
221,510✔
159
}
221,510✔
160

161
const char* TestMemoryAllocator::name() const
4✔
162
{
163
    return name_;
4✔
164
}
165

166
const char* TestMemoryAllocator::alloc_name() const
5✔
167
{
168
    return alloc_name_;
5✔
169
}
170

171
const char* TestMemoryAllocator::free_name() const
5✔
172
{
173
    return free_name_;
5✔
174
}
175

176
TestMemoryAllocator* TestMemoryAllocator::actualAllocator()
1✔
177
{
178
    return this;
1✔
179
}
180

181
NullUnknownAllocator::~NullUnknownAllocator()
4✔
182
{
183
}
4✔
184

185
char* NullUnknownAllocator::alloc_memory(size_t /*size*/, const char*, size_t)
1✔
186
{
187
    return NULLPTR;
1✔
188
}
189

190
void NullUnknownAllocator::free_memory(char* /*memory*/, size_t, const char*, size_t)
1✔
191
{
192
}
1✔
193

194
NullUnknownAllocator::NullUnknownAllocator()
2✔
195
    : TestMemoryAllocator("Null Allocator", "unknown", "unknown")
2✔
196
{
197
}
2✔
198

199

UNCOV
200
TestMemoryAllocator* NullUnknownAllocator::defaultAllocator()
×
201
{
UNCOV
202
    static NullUnknownAllocator allocator;
×
UNCOV
203
    return &allocator;
×
204
}
205

206
class LocationToFailAllocNode
207
{
208
  public:
209
    int allocNumberToFail_;
210
    int actualAllocNumber_;
211
    const char* file_;
212
    size_t line_;
213
    LocationToFailAllocNode* next_;
214

215
    void failAtAllocNumber(int number, LocationToFailAllocNode* next)
216
    {
217
      init(next);
218
      allocNumberToFail_ = number;
219
    }
220

221
    void failNthAllocAt(int allocationNumber, const char* file, size_t line, LocationToFailAllocNode* next)
222
    {
223
      init(next);
224
      allocNumberToFail_ = allocationNumber;
225
      file_ = file;
226
      line_ = line;
227
    }
228

229
    bool shouldFail(int allocationNumber, const char* file, size_t line)
230
    {
231
      if (file_ && SimpleString::StrCmp(file, file_) == 0 && line == line_) {
232
        actualAllocNumber_++;
233
        return actualAllocNumber_ == allocNumberToFail_;
234
      }
235
      if (allocationNumber == allocNumberToFail_)
236
        return true;
237
      return false;
238
    }
239

240
  private:
241
    void init(LocationToFailAllocNode* next = NULLPTR)
242
    {
243
      allocNumberToFail_ = 0;
244
      actualAllocNumber_ = 0;
245
      file_ = NULLPTR;
246
      line_ = 0;
247
      next_ = next;
248
    }
249

250
};
251

252
struct MemoryAccountantAllocationNode
253
{
254
    size_t size_;
255
    size_t allocations_;
256
    size_t deallocations_;
257
    size_t maxAllocations_;
258
    size_t currentAllocations_;
259
    MemoryAccountantAllocationNode* next_;
260
};
261

262
MemoryAccountantAllocationNode* MemoryAccountant::createNewAccountantAllocationNode(size_t size, MemoryAccountantAllocationNode* next) const
36✔
263
{
264
    MemoryAccountantAllocationNode* node = (MemoryAccountantAllocationNode*) (void*) allocator_->alloc_memory(sizeof(MemoryAccountantAllocationNode), __FILE__, __LINE__);
36✔
265
    node->size_ = size;
36✔
266
    node->allocations_ = 0;
36✔
267
    node->deallocations_ = 0;
36✔
268
    node->maxAllocations_ = 0;
36✔
269
    node->currentAllocations_ = 0;
36✔
270
    node->next_ = next;
36✔
271
    return node;
36✔
272
}
273

274
void MemoryAccountant::destroyAccountantAllocationNode(MemoryAccountantAllocationNode* node) const
36✔
275
{
276
    allocator_->free_memory((char*) node, sizeof(*node), __FILE__, __LINE__);
36✔
277
}
36✔
278

279
MemoryAccountant::MemoryAccountant()
31✔
280
    : head_(NULLPTR), allocator_(defaultMallocAllocator()), useCacheSizes_(false)
31✔
281
{
282
}
31✔
283

284
MemoryAccountant::~MemoryAccountant()
31✔
285
{
286
    clear();
31✔
287
}
31✔
288

289
void MemoryAccountant::createCacheSizeNodes(size_t sizes[], size_t length)
4✔
290
{
291
    for (size_t i = 0; i < length; i++)
9✔
292
        findOrCreateNodeOfSize(sizes[i]);
5✔
293

294
    if (head_ == NULLPTR)
4✔
295
        head_ = createNewAccountantAllocationNode(0, NULLPTR);
1✔
296
    else {
297
        for (MemoryAccountantAllocationNode* lastNode = head_; lastNode; lastNode = lastNode->next_) {
5✔
298
            if (lastNode->next_ == NULLPTR) {
5✔
299
                lastNode->next_ = createNewAccountantAllocationNode(0, NULLPTR);
3✔
300
                break;
3✔
301
            }
302
        }
303
    }
304
}
4✔
305

306

307
void MemoryAccountant::useCacheSizes(size_t sizes[], size_t length)
5✔
308
{
309
    if (head_)
5✔
310
      FAIL("MemoryAccountant: Cannot set cache sizes as allocations already occured!");
1✔
311

312
    createCacheSizeNodes(sizes, length);
4✔
313
    useCacheSizes_ = true;
4✔
314
}
4✔
315

316
void MemoryAccountant::setAllocator(TestMemoryAllocator* allocator)
6✔
317
{
318
    allocator_ = allocator;
6✔
319
}
6✔
320

321
void MemoryAccountant::clear()
48✔
322
{
323
    MemoryAccountantAllocationNode* node = head_;
48✔
324
    MemoryAccountantAllocationNode* to_be_deleted = NULLPTR;
48✔
325
    while (node) {
84✔
326
        to_be_deleted = node;
36✔
327
        node = node->next_;
36✔
328
        destroyAccountantAllocationNode(to_be_deleted);
36✔
329
    }
330
    head_ = NULLPTR;
48✔
331
}
48✔
332

333
MemoryAccountantAllocationNode* MemoryAccountant::findNodeOfSize(size_t size) const
30✔
334
{
335
    if (useCacheSizes_) {
30✔
336
        for (MemoryAccountantAllocationNode* node = head_; node; node = node->next_) {
23✔
337
            if (((size > node->size_) && (node->next_ == NULLPTR))
23✔
338
                || ((size <= node->size_) &&
19✔
339
                    !((node->next_->size_ != 0) && (node->next_->size_ <= size))))
11✔
340
                return node;
15✔
341
        }
342
    }
343
    else
344
        for (MemoryAccountantAllocationNode* node = head_; node; node = node->next_)
31✔
345
            if (node->size_ == size)
27✔
346
                return node;
11✔
347
    return NULLPTR;
4✔
348
}
349

350
MemoryAccountantAllocationNode* MemoryAccountant::findOrCreateNodeOfSize(size_t size)
69✔
351
{
352
    if (useCacheSizes_)
69✔
353
      return findNodeOfSize(size);
15✔
354

355
    if (head_ && head_->size_ > size)
54✔
356
        head_ = createNewAccountantAllocationNode(size, head_);
6✔
357

358
    for (MemoryAccountantAllocationNode* node = head_; node; node = node->next_) {
100✔
359
        if (node->size_ == size)
85✔
360
            return node;
39✔
361
        if (node->next_ == NULLPTR || node->next_->size_ > size)
46✔
362
            node->next_ = createNewAccountantAllocationNode(size, node->next_);
11✔
363
    }
364
    head_ = createNewAccountantAllocationNode(size, head_);
15✔
365
    return head_;
15✔
366
}
367

368
void MemoryAccountant::alloc(size_t size)
36✔
369
{
370
    MemoryAccountantAllocationNode* node = findOrCreateNodeOfSize(size);
36✔
371
    node->allocations_++;
36✔
372
    node->currentAllocations_++;
36✔
373
    node->maxAllocations_ = (node->currentAllocations_ > node->maxAllocations_) ? node->currentAllocations_ : node->maxAllocations_;
36✔
374
}
36✔
375

376
void MemoryAccountant::dealloc(size_t size)
28✔
377
{
378
    MemoryAccountantAllocationNode* node = findOrCreateNodeOfSize(size);
28✔
379
    node->deallocations_++;
28✔
380
    if (node->currentAllocations_)
28✔
381
      node->currentAllocations_--;
14✔
382
}
28✔
383

384
size_t MemoryAccountant::totalAllocationsOfSize(size_t size) const
9✔
385
{
386
    MemoryAccountantAllocationNode* node = findNodeOfSize(size);
9✔
387
    if (node)
9✔
388
      return node->allocations_;
7✔
389
    return 0;
2✔
390
}
391

392
size_t MemoryAccountant::totalDeallocationsOfSize(size_t size) const
4✔
393
{
394
    MemoryAccountantAllocationNode* node = findNodeOfSize(size);
4✔
395
    if (node)
4✔
396
      return node->deallocations_;
3✔
397
    return 0;
1✔
398
}
399

400
size_t MemoryAccountant::maximumAllocationAtATimeOfSize(size_t size) const
2✔
401
{
402
    MemoryAccountantAllocationNode* node = findNodeOfSize(size);
2✔
403
    if (node)
2✔
404
      return node->maxAllocations_;
1✔
405
    return 0;
1✔
406
}
407

408
size_t MemoryAccountant::totalAllocations() const
6✔
409
{
410
    size_t theTotalAllocations = 0;
6✔
411

412
    for (MemoryAccountantAllocationNode* node = head_; node; node = node->next_)
22✔
413
        theTotalAllocations += node->allocations_;
16✔
414

415
    return theTotalAllocations;
6✔
416
}
417

418
size_t MemoryAccountant::totalDeallocations() const
4✔
419
{
420
    size_t theTotalDeallocations = 0;
4✔
421

422
    for (MemoryAccountantAllocationNode* node = head_; node; node = node->next_)
14✔
423
        theTotalDeallocations += node->deallocations_;
10✔
424

425
    return theTotalDeallocations;
4✔
426
}
427

428
SimpleString MemoryAccountant::reportNoAllocations() const
1✔
429
{
430
      return SimpleString("CppUTest Memory Accountant has not noticed any allocations or deallocations. Sorry\n");
1✔
431
}
432

433
SimpleString MemoryAccountant::reportTitle() const
7✔
434
{
435
      if (useCacheSizes_)
7✔
436
        return "CppUTest Memory Accountant report (with cache sizes):\n";
4✔
437
      return "CppUTest Memory Accountant report:\n";
3✔
438
}
439

440
SimpleString MemoryAccountant::reportHeader() const
7✔
441
{
442
    if (useCacheSizes_)
7✔
443
        return "Cache size          # allocations    # deallocations   max # allocations at one time\n";
4✔
444
    return "Allocation size     # allocations    # deallocations   max # allocations at one time\n";
3✔
445
}
446

447
#define MEMORY_ACCOUNTANT_ROW_FORMAT "%s               %5d            %5d             %5d\n"
448

449
SimpleString MemoryAccountant::reportFooter() const
7✔
450
{
451
    return SimpleString("   Thank you for your business\n");
7✔
452
}
453

454
SimpleString MemoryAccountant::stringSize(size_t size) const
15✔
455
{
456
    return (size == 0) ? StringFrom("other") : StringFromFormat("%5d", (int) size);
15✔
457
}
458

459
SimpleString MemoryAccountant::report() const
8✔
460
{
461
    if (head_ == NULLPTR)
8✔
462
      return reportNoAllocations();
1✔
463

464
    SimpleString accountantReport = reportTitle() + reportHeader();
7✔
465

466
    for (MemoryAccountantAllocationNode* node = head_; node; node = node->next_)
22✔
467
        accountantReport += StringFromFormat(MEMORY_ACCOUNTANT_ROW_FORMAT, stringSize(node->size_).asCharString(), (int) node->allocations_, (int) node->deallocations_, (int) node->maxAllocations_);
15✔
468

469
    return accountantReport + reportFooter();
7✔
470
}
7✔
471

472
AccountingTestMemoryAllocator::AccountingTestMemoryAllocator(MemoryAccountant& accountant, TestMemoryAllocator* origAllocator)
28✔
473
    : accountant_(accountant), originalAllocator_(origAllocator), head_(NULLPTR)
28✔
474
{
475
}
28✔
476

477
AccountingTestMemoryAllocator::~AccountingTestMemoryAllocator()
56✔
478
{
479
}
56✔
480

481
struct AccountingTestMemoryAllocatorMemoryNode
482
{
483
    char* memory_;
484
    size_t size_;
485
    AccountingTestMemoryAllocatorMemoryNode* next_;
486
};
487

488
void AccountingTestMemoryAllocator::addMemoryToMemoryTrackingToKeepTrackOfSize(char* memory, size_t size)
10✔
489
{
490
    AccountingTestMemoryAllocatorMemoryNode* node = (AccountingTestMemoryAllocatorMemoryNode*) (void*) originalAllocator_->alloc_memory(sizeof(AccountingTestMemoryAllocatorMemoryNode), __FILE__, __LINE__);
10✔
491
    node->memory_ = memory;
10✔
492
    node->size_ = size;
10✔
493
    node->next_ = head_;
10✔
494
    head_ = node;
10✔
495
}
10✔
496

497
size_t AccountingTestMemoryAllocator::removeNextNodeAndReturnSize(AccountingTestMemoryAllocatorMemoryNode* node)
4✔
498
{
499
    AccountingTestMemoryAllocatorMemoryNode* foundNode = node->next_;
4✔
500
    node->next_ = node->next_->next_;
4✔
501

502
    size_t size = foundNode->size_;
4✔
503
    originalAllocator_->free_memory((char*) foundNode, size, __FILE__, __LINE__);
4✔
504
    return size;
4✔
505
}
506

507
size_t AccountingTestMemoryAllocator::removeHeadAndReturnSize()
4✔
508
{
509
    AccountingTestMemoryAllocatorMemoryNode* foundNode = head_;
4✔
510
    head_ = head_->next_;
4✔
511

512
    size_t size = foundNode->size_;
4✔
513
    originalAllocator_->free_memory((char*) foundNode, size, __FILE__, __LINE__);
4✔
514
    return size;
4✔
515
}
516

517
size_t AccountingTestMemoryAllocator::removeMemoryFromTrackingAndReturnAllocatedSize(char* memory)
11✔
518
{
519
    if (head_ && head_->memory_ == memory)
11✔
520
        return removeHeadAndReturnSize();
4✔
521

522
    for (AccountingTestMemoryAllocatorMemoryNode* node = head_; node; node = node->next_) {
11✔
523
        if (node->next_ && node->next_->memory_ == memory)
8✔
524
            return removeNextNodeAndReturnSize(node);
4✔
525
    }
526

527
    return 0;
3✔
528
}
529

530
char* AccountingTestMemoryAllocator::alloc_memory(size_t size, const char* file, size_t line)
10✔
531
{
532
    accountant_.alloc(size);
10✔
533
    char* memory = originalAllocator_->alloc_memory(size, file, line);
10✔
534
    addMemoryToMemoryTrackingToKeepTrackOfSize(memory, size);
10✔
535
    return memory;
10✔
536
}
537

538
void AccountingTestMemoryAllocator::free_memory(char* memory, size_t, const char* file, size_t line)
11✔
539
{
540
    size_t size = removeMemoryFromTrackingAndReturnAllocatedSize(memory);
11✔
541
    accountant_.dealloc(size);
11✔
542
    originalAllocator_->free_memory(memory, size, file, line);
11✔
543
}
11✔
544

UNCOV
545
TestMemoryAllocator* AccountingTestMemoryAllocator::actualAllocator()
×
546
{
UNCOV
547
    return originalAllocator_->actualAllocator();
×
548
}
549

550
TestMemoryAllocator* AccountingTestMemoryAllocator::originalAllocator()
13✔
551
{
552
    return originalAllocator_;
13✔
553
}
554

555
const char* AccountingTestMemoryAllocator::alloc_name() const
1✔
556
{
557
    return originalAllocator_->alloc_name();
1✔
558
}
559

560
const char* AccountingTestMemoryAllocator::free_name() const
1✔
561
{
562
    return originalAllocator_->free_name();
1✔
563
}
564

565
GlobalMemoryAccountant::GlobalMemoryAccountant()
7✔
566
    : mallocAllocator_(NULLPTR), newAllocator_(NULLPTR), newArrayAllocator_(NULLPTR)
7✔
567
{
568
}
7✔
569

570
GlobalMemoryAccountant::~GlobalMemoryAccountant()
7✔
571
{
572
    restoreMemoryAllocators();
7✔
573
    delete mallocAllocator_;
7✔
574
    delete newAllocator_;
7✔
575
    delete newArrayAllocator_;
7✔
576
}
7✔
577

UNCOV
578
void GlobalMemoryAccountant::useCacheSizes(size_t sizes[], size_t length)
×
579
{
UNCOV
580
    accountant_.useCacheSizes(sizes, length);
×
UNCOV
581
}
×
582

583
void GlobalMemoryAccountant::start()
7✔
584
{
585
    if (mallocAllocator_ != NULLPTR)
7✔
586
      FAIL("Global allocator start called twice!");
1✔
587

588
    mallocAllocator_ = new AccountingTestMemoryAllocator(accountant_, getCurrentMallocAllocator());
6✔
589
    newAllocator_ = new AccountingTestMemoryAllocator(accountant_, getCurrentNewAllocator());
6✔
590
    newArrayAllocator_ = new AccountingTestMemoryAllocator(accountant_, getCurrentNewArrayAllocator());
6✔
591

592
    accountant_.setAllocator(getCurrentMallocAllocator());
6✔
593

594
    setCurrentMallocAllocator(mallocAllocator_);
6✔
595
    setCurrentNewAllocator(newAllocator_);
6✔
596
    setCurrentNewArrayAllocator(newArrayAllocator_);
6✔
597
}
6✔
598

599
void GlobalMemoryAccountant::restoreMemoryAllocators()
10✔
600
{
601
    if (getCurrentMallocAllocator() == mallocAllocator_)
10✔
602
        setCurrentMallocAllocator(mallocAllocator_->originalAllocator());
3✔
603

604
    if (getCurrentNewAllocator() == newAllocator_)
10✔
605
        setCurrentNewAllocator(newAllocator_->originalAllocator());
3✔
606

607
    if (getCurrentNewArrayAllocator() == newArrayAllocator_)
10✔
608
        setCurrentNewArrayAllocator(newArrayAllocator_->originalAllocator());
3✔
609
}
10✔
610

611
void GlobalMemoryAccountant::stop()
7✔
612
{
613
    if (mallocAllocator_ == NULLPTR)
7✔
614
      FAIL("GlobalMemoryAccount: Stop called without starting");
1✔
615

616
    if (getCurrentMallocAllocator() != mallocAllocator_)
6✔
617
        FAIL("GlobalMemoryAccountant: Malloc memory allocator has been changed while accounting for memory");
1✔
618

619
    if (getCurrentNewAllocator() != newAllocator_)
5✔
620
        FAIL("GlobalMemoryAccountant: New memory allocator has been changed while accounting for memory");
1✔
621

622
    if (getCurrentNewArrayAllocator() != newArrayAllocator_)
4✔
623
        FAIL("GlobalMemoryAccountant: New Array memory allocator has been changed while accounting for memory");
1✔
624

625
    restoreMemoryAllocators();
3✔
626
}
3✔
627

UNCOV
628
SimpleString GlobalMemoryAccountant::report()
×
629
{
UNCOV
630
    return accountant_.report();
×
631
}
632

633
TestMemoryAllocator* GlobalMemoryAccountant::getMallocAllocator()
1✔
634
{
635
    return mallocAllocator_;
1✔
636
}
637

638
TestMemoryAllocator* GlobalMemoryAccountant::getNewAllocator()
1✔
639
{
640
    return newAllocator_;
1✔
641
}
642

643
TestMemoryAllocator* GlobalMemoryAccountant::getNewArrayAllocator()
1✔
644
{
645
    return newArrayAllocator_;
1✔
646
}
647

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc