aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/vespamalloc/malloc/common.cpp
blob: 4d97aa13b05e6df7d4725a4bc2da11fc63241f0b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "common.h"
#include <vespamalloc/util/callstack.h>
#include <pthread.h>

namespace vespamalloc {

std::atomic<uint32_t> Mutex::_threadCount(0);
bool     Mutex::_stopRecursion = true;

void Mutex::lock()
{
    if (_use) {
        pthread_mutex_lock(&_mutex);
    }
}
void Mutex::unlock()
{
    if (_use) {
        pthread_mutex_unlock(&_mutex);
    }
}

void Mutex::quit()
{
    if (_use) {
        _use = false;
        pthread_mutex_destroy(&_mutex);
    }
}

void Mutex::init() {
    if (!_use && ! _stopRecursion) {
        pthread_mutex_init(&_mutex, nullptr);
        _use = true;
    }
}

Guard::Guard(Mutex & m) :
     _mutex(&m)
{
    MallocRecurseOnSuspend(false);
    _mutex->lock();
    MallocRecurseOnSuspend(true);
}

FILE *  _G_logFile = stderr;
size_t  _G_bigBlockLimit = 0x80000000;

void
logStackTrace() {
    StackEntry st[32];
    size_t count = StackEntry::fillStack(st, NELEMS(st));
    st[4].info(_G_logFile);
    fprintf(_G_logFile, "\n");
    for(size_t i=1; (i < count) && (i < NELEMS(st)); i++) {
        const auto & s = st[i];
        if (s.valid()) {
            s.info(_G_logFile);
            fprintf(_G_logFile, " from ");
        }
    }
    fprintf(_G_logFile, "\n");
}

void
logBigBlock(const void *ptr, size_t exact, size_t adjusted, size_t gross)
{
    size_t sz(exact);
    if (std::max(std::max(sz, adjusted), gross) > _G_bigBlockLimit) {
        fprintf(_G_logFile, "validating %p(%ld, %ld, %ld) ", ptr, sz, adjusted, gross);
        logStackTrace();
    }
}

}

extern "C" void MallocRecurseOnSuspend(bool recurse)
{
    (void) recurse;
}