FMemStats.cpp 2.82 KB
Newer Older
1
// See LICENCE file at project root
2
#include <Utils/FMemStats.h>
3 4 5

FMemStats FMemStats::controler;

6
#include <cstdio>
7
#include <cstdlib>
8

9
#ifdef SCALFMM_USE_MEM_STATS
10
    // Regular scalar new
11
    void* operator new(std::size_t n) {
12
        void* const allocated = std::malloc(n + sizeof(size_t));
13 14 15
        if(allocated){
            *(static_cast<size_t*>(allocated)) = n;
            FMemStats::controler.allocate(n);
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
            return static_cast<unsigned char*>(allocated) + sizeof(size_t);
        }
        throw std::bad_alloc();
        return allocated;
    }

    void* operator new[]( std::size_t n ) {
        void* const allocated = std::malloc(n + sizeof(size_t));
        if(allocated){
            *(static_cast<size_t*>(allocated)) = n;
            FMemStats::controler.allocate(n);
            return static_cast<unsigned char*>(allocated) + sizeof(size_t);
        }
        throw std::bad_alloc();
        return allocated;
    }

    void* operator new  ( std::size_t n, const std::nothrow_t& tag){
        void* const allocated = std::malloc(n + sizeof(size_t));
        if(allocated){
            *(static_cast<size_t*>(allocated)) = n;
            FMemStats::controler.allocate(n);
            return static_cast<unsigned char*>(allocated) + sizeof(size_t);
        }
        return allocated;
    }

    void* operator new[] ( std::size_t n, const std::nothrow_t& tag){
        void* const allocated = std::malloc(n + sizeof(size_t));
        if(allocated){
            *(static_cast<size_t*>(allocated)) = n;
            FMemStats::controler.allocate(n);
            return static_cast<unsigned char*>(allocated) + sizeof(size_t);
49 50 51 52 53
        }
        return allocated;
    }

    // Regular scalar delete
54
    void operator delete(void* p) noexcept{
55
        if(p){
56 57
            FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - sizeof(size_t))) );
            std::free(static_cast<unsigned char*>(p) - sizeof(size_t));
58 59 60
        }
    }

61 62
    void operator delete[](void* p) noexcept{
        if(p){
63 64
            FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - sizeof(size_t))) );
            std::free(static_cast<unsigned char*>(p) - sizeof(size_t));
65 66 67 68 69
        }
    }

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

    void operator delete[]( void* p, const std::nothrow_t& /*tag*/) {
76
        if(p){
77 78
            FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - sizeof(size_t))) );
            std::free(static_cast<unsigned char*>(p) - sizeof(size_t));
79
        }
80 81 82
    }

#endif