FMemStats.cpp 2.34 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
#include <cstdlib>
22

23
#ifdef ScalFMM_USE_MEM_STATS
24
    // Regular scalar new
25 26
    void* operator new(std::size_t n) {
        void* const allocated = std::malloc(n + 8);
27 28 29 30 31 32 33 34 35
        if(allocated){
            *(static_cast<size_t*>(allocated)) = n;
            FMemStats::controler.allocate(n);
            return static_cast<unsigned char*>(allocated) + 8;
        }
        return allocated;
    }

    // Regular scalar delete
36
    void operator delete(void* p) noexcept{
37 38
        if(p){
            FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
39
            std::free(static_cast<unsigned char*>(p) - 8);
40 41 42
        }
    }

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    void operator delete[](void* p) noexcept{
        if(p){
            FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
            std::free(static_cast<unsigned char*>(p) - 8);
        }
    }

    void operator delete  ( void* p, const std::nothrow_t& /*tag*/) {
        if(p){
            FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
            std::free(static_cast<unsigned char*>(p) - 8);
        }
    }

    void operator delete[]( void* p, const std::nothrow_t& /*tag*/) {
58 59 60 61
        if(p){
            FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
            std::free(static_cast<unsigned char*>(p) - 8);
        }
62 63 64
    }

#endif