aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lfalloc/lf_allocX64.cpp
blob: 2a06e778ec808d6e1ba01d7816775539ff544346 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "lf_allocX64.h"
 
//////////////////////////////////////////////////////////////////////////
// hooks
#if defined(USE_INTELCC) || defined(_darwin_) || defined(_freebsd_) || defined(_STLPORT_VERSION)
#define OP_THROWNOTHING noexcept
#else
#define OP_THROWNOTHING
#endif

#ifndef _darwin_
#if !defined(YMAKE)
void* operator new(size_t size) {
    return LFAlloc(size);
}

void* operator new(size_t size, const std::nothrow_t&) OP_THROWNOTHING {
    return LFAlloc(size);
}

void operator delete(void* p)OP_THROWNOTHING {
    LFFree(p);
}

void operator delete(void* p, const std::nothrow_t&)OP_THROWNOTHING {
    LFFree(p);
}

void* operator new[](size_t size) {
    return LFAlloc(size);
}

void* operator new[](size_t size, const std::nothrow_t&) OP_THROWNOTHING {
    return LFAlloc(size);
}

void operator delete[](void* p) OP_THROWNOTHING {
    LFFree(p);
}

void operator delete[](void* p, const std::nothrow_t&) OP_THROWNOTHING {
    LFFree(p);
}
#endif

//#ifndef _MSC_VER

extern "C" void* malloc(size_t size) {
    return LFAlloc(size);
}

extern "C" void* valloc(size_t size) {
    return LFVAlloc(size);
}

extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
    return LFPosixMemalign(memptr, alignment, size);
}

extern "C" void* memalign(size_t alignment, size_t size) {
    void* ptr;
    int res = LFPosixMemalign(&ptr, alignment, size);
    return res ? nullptr : ptr;
}

extern "C" void* aligned_alloc(size_t alignment, size_t size) {
    return memalign(alignment, size);
}

#if !defined(_MSC_VER) && !defined(_freebsd_)
// Workaround for pthread_create bug in linux.
extern "C" void* __libc_memalign(size_t alignment, size_t size) {
    return memalign(alignment, size);
}
#endif

extern "C" void free(void* ptr) {
    LFFree(ptr);
}

extern "C" void* calloc(size_t n, size_t elem_size) {
    // Overflow check
    const size_t size = n * elem_size;
    if (elem_size != 0 && size / elem_size != n)
        return nullptr;

    void* result = LFAlloc(size);
    if (result != nullptr) {
        memset(result, 0, size);
    }
    return result;
}

extern "C" void cfree(void* ptr) {
    LFFree(ptr);
}

extern "C" void* realloc(void* old_ptr, size_t new_size) {
    if (old_ptr == nullptr) {
        void* result = LFAlloc(new_size);
        return result;
    }
    if (new_size == 0) {
        LFFree(old_ptr);
        return nullptr;
    }

    void* new_ptr = LFAlloc(new_size);
    if (new_ptr == nullptr) {
        return nullptr;
    }
    size_t old_size = LFGetSize(old_ptr);
    memcpy(new_ptr, old_ptr, ((old_size < new_size) ? old_size : new_size));
    LFFree(old_ptr);
    return new_ptr;
}

extern "C" size_t malloc_usable_size(void* ptr) {
    if (ptr == nullptr) {
        return 0;
    }
    return LFGetSize(ptr);
}

NMalloc::TMallocInfo NMalloc::MallocInfo() {
    NMalloc::TMallocInfo r;
#if defined(LFALLOC_DBG)
    r.Name = "lfalloc_dbg";
#elif defined(LFALLOC_YT)
    r.Name = "lfalloc_yt";
#else
    r.Name = "lfalloc";
#endif
    r.SetParam = &LFAlloc_SetParam;
    r.GetParam = &LFAlloc_GetParam;
    return r;
}
#else
NMalloc::TMallocInfo NMalloc::MallocInfo() {
    NMalloc::TMallocInfo r;
    r.Name = "system-darwin";
    return r;
}
#endif