Project

Profile

Help

Download (1.1 KB) Statistics
| Branch: | Revision:

he / src / main / c / Saxon.C.API / SaxoncNewDebug.h @ 55b80284

1 55b80284 O'Neil Delpratt
#ifndef MY_NEW5
2
#define MY_NEW5
3
4
#include <algorithm>
5
#include <cstdlib>
6
#include <iostream>
7
#include <new>
8
#include <vector>
9
10
std::vector<void*> myAlloc;
11
12
extern void* newImpl(std::size_t sz,char const* file, int line){
13
    static int counter{};
14
    void* ptr= std::malloc(sz);
15
    std::cerr << "Saxon/C Mem test: "<<file << ": " << line << " " <<  ptr << std::endl;
16
    myAlloc.push_back(ptr);
17
    return ptr;
18
}
19
20
void* operator new(std::size_t sz,char const* file, int line){
21
    return newImpl(sz,file,line);
22
}
23
24
void* operator new [](std::size_t sz,char const* file, int line){
25
    return newImpl(sz,file,line);
26
}
27
28
void operator delete(void* ptr) noexcept{
29
    auto ind= std::distance(myAlloc.begin(),std::find(myAlloc.begin(),myAlloc.end(),ptr));
30
    myAlloc[ind]= nullptr;
31
    std::free(ptr);
32
}
33
34
#define new new(__FILE__, __LINE__)
35
36
void dummyFunction(){
37
    int* dummy= new int;
38
}
39
40
void getInfo(){
41
42
    std::cout << std::endl;
43
44
    std::cout << "Allocation: " << std::endl;
45
    for (auto i: myAlloc){
46
        if (i != nullptr ) std::cout << " " << i << std::endl;
47
    }
48
49
    std::cout << std::endl;
50
51
}
52
53
54
#endif // MY_NEW5