aboutsummaryrefslogtreecommitdiff
path: root/util/sokol_memtrack.h
blob: c7ba20b9b243b443dbb3a42c8cdb5791bccd5076 (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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#if defined(SOKOL_IMPL) && !defined(SOKOL_MEMTRACK_IMPL)
#define SOKOL_MEMTRACK_IMPL
#endif
#ifndef SOKOL_MEMTRACK_INCLUDED
/*
    sokol_memtrack.h -- memory allocation wrapper to track memory usage
                        of sokol libraries

    Project URL: https://github.com/floooh/sokol

    Simply include this file before the sokol header implementation includes
    and after the SOKOL_IMPL macro:

    #define SOKOL_IMPL
    #include "sokol_memtrack.h"
    #include "sokol_app.h"
    #include "sokol_gfx.h"
    ...

    Optionally provide the following defines with your own implementations:

    SOKOL_MEMTRACK_API_DECL - public function declaration prefix (default: extern)
    SOKOL_API_DECL      - same as SOKOL_MEMTRACK_API_DECL
    SOKOL_API_IMPL      - public function implementation prefix (default: -)

    If sokol_memtrack.h is compiled as a DLL, define the following before
    including the declaration or implementation:

    SOKOL_DLL

    The sokol_memtrack.h header will redirect the macros SOKOL_MALLOC,
    SOKOL_FREE and SOKOL_CALLOC to use its own function wrappers which
    keep track of number and size of allocations. The wrapper functions
    then call the CRT functions malloc(), calloc() or free().

    Naturally, only the memory management calls in sokol header code are tracked,
    not in underlying APIs such as D3D11 or OpenGL.

    To get the current number and overall size of allocations, call:

        smemtrack_info_t alloc_info = smemtrack_info();

        int num_allocations = info.num_alloc;
        int allocation_size = info.num_bytes;

    The allocation wrapper functions are *not* thread-safe (which shouldn't
    be a problem because the sokol headers don't allocate or deallocate
    in threads).

    LICENSE
    =======

    zlib/libpng license

    Copyright (c) 2018 Andre Weissflog

    This software is provided 'as-is', without any express or implied warranty.
    In no event will the authors be held liable for any damages arising from the
    use of this software.

    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:

        1. The origin of this software must not be misrepresented; you must not
        claim that you wrote the original software. If you use this software in a
        product, an acknowledgment in the product documentation would be
        appreciated but is not required.

        2. Altered source versions must be plainly marked as such, and must not
        be misrepresented as being the original software.

        3. This notice may not be removed or altered from any source
        distribution.
*/
#define SOKOL_MEMTRACK_INCLUDED (1)
#include <stdint.h>

#if defined(SOKOL_API_DECL) && !defined(SOKOL_MEMTRACK_API_DECL)
#define SOKOL_MEMTRACK_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_MEMTRACK_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_MEMTRACK_IMPL)
#define SOKOL_MEMTRACK_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_MEMTRACK_API_DECL __declspec(dllimport)
#else
#define SOKOL_MEMTRACK_API_DECL extern
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

typedef struct smemtrack_info_t {
    int num_allocs;
    int num_bytes;
} smemtrack_info_t;

SOKOL_MEMTRACK_API_DECL smemtrack_info_t smemtrack_info(void);

#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SOKOL_MEMTRACK_INCLUDED */

/*=== IMPLEMENTATION =========================================================*/
#ifdef SOKOL_MEMTRACK_IMPL
#define SOKOL_MEMTRACK_IMPL_INCLUDED (1)
#include <stdlib.h> /* malloc, free, calloc */
#include <string.h> /* memset */
#include <stddef.h> /* size_t */

#ifndef SOKOL_API_IMPL
    #define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
    #ifndef NDEBUG
        #define SOKOL_DEBUG (1)
    #endif
#endif
#ifndef _SOKOL_PRIVATE
    #if defined(__GNUC__) || defined(__clang__)
        #define _SOKOL_PRIVATE __attribute__((unused)) static
    #else
        #define _SOKOL_PRIVATE static
    #endif
#endif

#define SOKOL_MALLOC(s) _smemtrack_malloc(s)
#define SOKOL_FREE(p) _smemtrack_free(p)
#define SOKOL_CALLOC(n,s) _smemtrack_calloc(n,s)

#define _SMEMTRACK_HEADER_SIZE (16)

static struct {
    smemtrack_info_t state;
} _smemtrack;

_SOKOL_PRIVATE void* _smemtrack_malloc(size_t size) {
    _smemtrack.state.num_allocs++;
    _smemtrack.state.num_bytes += (int) size;
    uint8_t* ptr = (uint8_t*) malloc(size + _SMEMTRACK_HEADER_SIZE);
    *(size_t*)ptr = size;
    return ptr + _SMEMTRACK_HEADER_SIZE;
}

_SOKOL_PRIVATE void _smemtrack_free(void* ptr) {
    uint8_t* alloc_ptr = ((uint8_t*)ptr) - _SMEMTRACK_HEADER_SIZE;
    size_t size = *(size_t*)alloc_ptr;
    _smemtrack.state.num_allocs--;
    _smemtrack.state.num_bytes -= (int) size;
    free(alloc_ptr);
}

_SOKOL_PRIVATE void* _smemtrack_calloc(size_t num, size_t size) {
    size_t mem_size = num * size;
    _smemtrack.state.num_allocs++;
    _smemtrack.state.num_bytes += (int) mem_size;
    uint8_t* ptr = (uint8_t*) malloc(mem_size + _SMEMTRACK_HEADER_SIZE);
    memset(ptr + _SMEMTRACK_HEADER_SIZE, 0, mem_size);
    *(size_t*)ptr = size;
    return ptr + _SMEMTRACK_HEADER_SIZE;
}

SOKOL_API_IMPL smemtrack_info_t smemtrack_info(void) {
    return _smemtrack.state;
}

#endif /* SOKOL_MEMTRACK_IMPL */