FMemStats.cpp 2.85 KB
Newer Older
1
// ===================================================================================
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.  
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info". 
// "http://www.gnu.org/licenses".
15
// ===================================================================================
16
17
18
19
#include "FMemStats.h"

FMemStats FMemStats::controler;

20
#include <cstdio>
21

22
#ifdef ScalFMM_USE_MEM_STATS
23
    // Regular scalar new
BRAMAS Berenger's avatar
BRAMAS Berenger committed
24
    void* operator new(std::size_t n)
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    {
        using namespace std;

        for (;;) {
            void* allocated_memory = ::operator new(n, nothrow);
            if (allocated_memory != 0){
                return allocated_memory;
            }

            // Store the global new handler
            new_handler global_handler = set_new_handler(0);
            set_new_handler(global_handler);

            if (global_handler) {
                global_handler();
            } else {
                throw bad_alloc();
            }
        }
    }

    // Nothrow scalar new
47
    void* operator new(size_t n,const std::nothrow_t& nothrow_value) noexcept
48
49
50
51
52
53
54
55
56
57
58
59
    {
        //if (n == 0) n = 1;
        void* const allocated = malloc(n + 8);
        if(allocated){
            *(static_cast<size_t*>(allocated)) = n;
            FMemStats::controler.allocate(n);
            return static_cast<unsigned char*>(allocated) + 8;
        }
        return allocated;
    }

    // Regular array new
BRAMAS Berenger's avatar
BRAMAS Berenger committed
60
    void* operator new[](size_t n)
61
62
63
64
65
    {
        return ::operator new(n);
    }

    // Nothrow array new
66
    void* operator new[](size_t n,const std::nothrow_t& nothrow_value) noexcept
67
68
69
70
71
    {
        return ::operator new(n, std::nothrow);
    }

    // Regular scalar delete
BRAMAS Berenger's avatar
BRAMAS Berenger committed
72
    void operator delete(void* p) noexcept {
73
74
        if(p){
            FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
75
            free(static_cast<unsigned char*>(p) - 8);
76
77
78
79
        }
    }

    // Nothrow scalar delete
BRAMAS Berenger's avatar
BRAMAS Berenger committed
80
    void operator delete(void* p,const std::nothrow_t& nothrow_value) noexcept {
81
82
83
84
        ::operator delete(p);
    }

    // Regular array delete
BRAMAS Berenger's avatar
BRAMAS Berenger committed
85
    void operator delete[](void* p) noexcept
86
87
88
89
90
    {
        ::operator delete(p);
    }

    // Nothrow array delete
BRAMAS Berenger's avatar
BRAMAS Berenger committed
91
    void operator delete[](void* p,const std::nothrow_t& nothrow_value) noexcept
92
93
94
95
96
    {
        ::operator delete(p);
    }

#endif