aboutsummaryrefslogtreecommitdiff
path: root/src/mem_profile.hpp
blob: 3b13b7680b653f401cd7da59b441f426ae60c7f2 (plain)
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
/*
 * Copyright (c) 2020 Andrew Kelley
 *
 * This file is part of zig, which is MIT licensed.
 * See http://opensource.org/licenses/MIT
 */

#ifndef ZIG_MEM_PROFILE_HPP
#define ZIG_MEM_PROFILE_HPP

#include "config.h"

#ifdef ZIG_ENABLE_MEM_PROFILE

#include <stdio.h>

#include "mem.hpp"
#include "mem_hash_map.hpp"
#include "util.hpp"

namespace mem {

struct Profile {
    void init(const char *name, const char *kind);
    void deinit();

    void record_alloc(const TypeInfo &info, size_t count);
    void record_dealloc(const TypeInfo &info, size_t count);

    void print_report(FILE *file = nullptr);

    struct Entry {
        TypeInfo info;

        struct Use {
            size_t calls;
            size_t objects;
        } alloc, dealloc;
    };

private:
    const char *name;
    const char *kind;

    struct UsageKey {
        const char *name_ptr;
        size_t name_len;
    };

    static uint32_t usage_hash(UsageKey key);
    static bool usage_equal(UsageKey a, UsageKey b);

    HashMap<UsageKey, Entry, usage_hash, usage_equal> usage_table;
};

struct InternCounters {
    size_t x_undefined;
    size_t x_void;
    size_t x_null;
    size_t x_unreachable;
    size_t zero_byte;

    void print_report(FILE *file = nullptr);
};

extern InternCounters intern_counters;

} // namespace mem

#endif
#endif