aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/tests/stacktrace/stacktrace.cpp
blob: b28a9653d2771b99984b29d4ba3d245e5c4eb208 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <cstdlib>
#include <cstdio>
#include <pthread.h>
#include <dlfcn.h>
#include <cassert>
#include <malloc.h>

void * run(void * arg)
{
    (void) arg;
    char * a = new char [100]; // a should not remain in stacktrace
    char * b = new char [1];   // but b should as it not deleted.
    (void) b;
    delete [] a;
    return nullptr;
}

void verify_that_vespamalloc_datasegment_size_exists() {
#if __GLIBC_PREREQ(2, 33)
    struct mallinfo2 info = mallinfo2();
    printf("Malloc used %zdm of memory\n", info.arena);
    assert(info.arena >= 10 * 0x100000ul);
    assert(info.arena < 10000 * 0x100000ul);
    assert(info.fordblks == 0);
    assert(info.fsmblks == 0);
    assert(info.hblkhd == 0);
    assert(info.hblks == 0);
    assert(info.keepcost == 0);
    assert(info.ordblks == 0);
    assert(info.smblks == 0);
    assert(info.uordblks == 0);
    assert(info.usmblks == 0);
#else
    struct mallinfo info = mallinfo();
    printf("Malloc used %dm of memory\n",info.arena);
    assert(info.arena >= 10);
    assert(info.arena < 10000);
    assert(info.fordblks == 0);
    assert(info.fsmblks == 0);
    assert(info.hblkhd == 0);
    assert(info.hblks == 0);
    assert(info.keepcost == 0);
    assert(info.ordblks == 0);
    assert(info.smblks == 0);
    assert(info.uordblks == 0);
    assert(info.usmblks == 0);
#endif
}

int main(int argc, char *argv[])
{
    (void) argc;
    (void) argv;
    char * a = new char [100]; // a should not remain in stacktrace
    char * b = new char [1];   // but b should as it not deleted.
    (void) b;
    delete [] a;
    pthread_t tid;
    int retval = pthread_create(&tid, nullptr, run, nullptr);
    if (retval != 0) {
        perror("pthread_create failed");
        abort();
    }
    retval = pthread_join(tid, nullptr);
    if (retval != 0) {
        perror("pthread_join failed");
        abort();
    }

    verify_that_vespamalloc_datasegment_size_exists();
}