aboutsummaryrefslogtreecommitdiff
path: root/src/rt/rust_srv.cpp
blob: 9239f643ebe03064611e6642715b309b5fbf6eac (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
/*
 *
 */

#include "rust_internal.h"
#include "rust_srv.h"

#define TRACK_ALLOCATIONS

rust_srv::rust_srv() : _live_allocations(0) {
    // Nop.
}

rust_srv::~rust_srv() {
    if (_live_allocations != 0) {
        char msg[128];
        snprintf(msg, sizeof(msg),
                 "leaked memory in rust main loop (%" PRIuPTR " objects)",
                 _live_allocations);
#ifdef TRACK_ALLOCATIONS
        for (size_t i = 0; i < _allocation_list.size(); i++) {
            if (_allocation_list[i] != NULL) {
                printf("allocation 0x%" PRIxPTR " was not freed\n",
                        (uintptr_t) _allocation_list[i]);
            }
        }
#endif
        fatal(msg, __FILE__, __LINE__, "");
    }
}

void *
rust_srv::malloc(size_t bytes) {
    ++_live_allocations;
    void * val = ::malloc(bytes);
#ifdef TRACK_ALLOCATIONS
    _allocation_list.append(val);
#endif
    return val;
}

void *
rust_srv::realloc(void *p, size_t bytes) {
    if (!p) {
        _live_allocations++;
    }
    void * val = ::realloc(p, bytes);
#ifdef TRACK_ALLOCATIONS
    if (_allocation_list.replace(p, val) == false) {
        printf("realloc: ptr 0x%" PRIxPTR " is not in allocation_list\n",
               (uintptr_t) p);
        fatal("not in allocation_list", __FILE__, __LINE__, "");
    }
#endif
    return val;
}

void
rust_srv::free(void *p) {
#ifdef TRACK_ALLOCATIONS
    if (_allocation_list.replace(p, NULL) == false) {
        printf("free: ptr 0x%" PRIxPTR " is not in allocation_list\n",
               (uintptr_t) p);
        fatal("not in allocation_list", __FILE__, __LINE__, "");
    }
#endif
    if (_live_allocations < 1) {
        fatal("live_allocs < 1", __FILE__, __LINE__, "");
    }
    _live_allocations--;
    ::free(p);
}

void
rust_srv::log(char const *msg) {
    printf("rt: %s\n", msg);
}

void
rust_srv::fatal(const char *expression,
    const char *file,
    size_t line,
    const char *format,
    ...) {
    char buf[1024];
    va_list args;
    va_start(args, format);
    vsnprintf(buf, sizeof(buf), format, args);
    va_end(args);

    char msg[1024];
    snprintf(msg, sizeof(msg),
             "fatal, '%s' failed, %s:%d %s",
             expression, file, (int)line, buf);
    log(msg);
    exit(1);
}

void
rust_srv::warning(char const *expression,
    char const *file,
    size_t line,
    const char *format,
    ...) {
    char buf[1024];
    va_list args;
    va_start(args, format);
    vsnprintf(buf, sizeof(buf), format, args);
    va_end(args);

    char msg[1024];
    snprintf(msg, sizeof(msg),
             "warning: '%s', at: %s:%d %s",
             expression, file, (int)line, buf);
    log(msg);
}

rust_srv *
rust_srv::clone() {
    return new rust_srv();
}