src/test/SDL_test_memory.c
changeset 11613 8b7a69b52e7b
child 11616 ed240b398305
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/SDL_test_memory.c	Thu Oct 12 14:20:17 2017 -0700
     1.3 @@ -0,0 +1,274 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "SDL_config.h"
    1.25 +#include "SDL_assert.h"
    1.26 +#include "SDL_stdinc.h"
    1.27 +#include "SDL_log.h"
    1.28 +#include "SDL_test_crc32.h"
    1.29 +#include "SDL_test_memory.h"
    1.30 +
    1.31 +#ifdef HAVE_LIBUNWIND_H
    1.32 +#include <libunwind.h>
    1.33 +#endif
    1.34 +
    1.35 +/* This is a simple tracking allocator to demonstrate the use of SDL's
    1.36 +   memory allocation replacement functionality.
    1.37 +
    1.38 +   It gets slow with large numbers of allocations and shouldn't be used
    1.39 +   for production code.
    1.40 +*/
    1.41 +
    1.42 +typedef struct SDL_tracked_allocation
    1.43 +{
    1.44 +    void *mem;
    1.45 +    size_t size;
    1.46 +    Uint64 stack[10];
    1.47 +    char stack_names[10][256];
    1.48 +    struct SDL_tracked_allocation *next;
    1.49 +} SDL_tracked_allocation;
    1.50 +
    1.51 +static SDLTest_Crc32Context s_crc32_context;
    1.52 +static SDL_malloc_func SDL_malloc_orig = NULL;
    1.53 +static SDL_calloc_func SDL_calloc_orig = NULL;
    1.54 +static SDL_realloc_func SDL_realloc_orig = NULL;
    1.55 +static SDL_free_func SDL_free_orig = NULL;
    1.56 +static int s_previous_allocations = 0;
    1.57 +static SDL_tracked_allocation *s_tracked_allocations[256];
    1.58 +
    1.59 +static unsigned int get_allocation_bucket(void *mem)
    1.60 +{
    1.61 +    CrcUint32 crc_value;
    1.62 +    unsigned int index;
    1.63 +    SDLTest_Crc32Calc(&s_crc32_context, (CrcUint8 *)&mem, sizeof(mem), &crc_value);
    1.64 +    index = (crc_value & (SDL_arraysize(s_tracked_allocations) - 1));
    1.65 +    return index;
    1.66 +}
    1.67 + 
    1.68 +static SDL_bool SDL_IsAllocationTracked(void *mem)
    1.69 +{
    1.70 +    SDL_tracked_allocation *entry;
    1.71 +    int index = get_allocation_bucket(mem);
    1.72 +    for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
    1.73 +        if (mem == entry->mem) {
    1.74 +            return SDL_TRUE;
    1.75 +        }
    1.76 +    }
    1.77 +    return SDL_FALSE;
    1.78 +}
    1.79 +
    1.80 +static void SDL_TrackAllocation(void *mem, size_t size)
    1.81 +{
    1.82 +    SDL_tracked_allocation *entry;
    1.83 +    int index = get_allocation_bucket(mem);
    1.84 +
    1.85 +    if (SDL_IsAllocationTracked(mem)) {
    1.86 +        return;
    1.87 +    }
    1.88 +    entry = (SDL_tracked_allocation *)SDL_malloc_orig(sizeof(*entry));
    1.89 +    if (!entry) {
    1.90 +        return;
    1.91 +    }
    1.92 +    entry->mem = mem;
    1.93 +    entry->size = size;
    1.94 +
    1.95 +    /* Generate the stack trace for the allocation */
    1.96 +    SDL_zero(entry->stack);
    1.97 +#ifdef HAVE_LIBUNWIND_H
    1.98 +    {
    1.99 +        int stack_index;
   1.100 +        unw_cursor_t cursor;
   1.101 +        unw_context_t context;
   1.102 +
   1.103 +        unw_getcontext(&context);
   1.104 +        unw_init_local(&cursor, &context);
   1.105 +
   1.106 +        stack_index = 0;
   1.107 +        while (unw_step(&cursor) > 0) {
   1.108 +            unw_word_t offset, pc;
   1.109 +            char sym[256];
   1.110 +
   1.111 +            unw_get_reg(&cursor, UNW_REG_IP, &pc);
   1.112 +            entry->stack[stack_index] = pc;
   1.113 +
   1.114 +            if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
   1.115 +                snprintf(entry->stack_names[stack_index], sizeof(entry->stack_names[stack_index]), "%s+0x%llx", sym, offset);
   1.116 +            }
   1.117 +            ++stack_index;
   1.118 +
   1.119 +            if (stack_index == SDL_arraysize(entry->stack)) {
   1.120 +                break;
   1.121 +            }
   1.122 +        }
   1.123 +    }
   1.124 +#endif /* HAVE_LIBUNWIND_H */
   1.125 +
   1.126 +    entry->next = s_tracked_allocations[index];
   1.127 +    s_tracked_allocations[index] = entry;
   1.128 +}
   1.129 +
   1.130 +static void SDL_UntrackAllocation(void *mem)
   1.131 +{
   1.132 +    SDL_tracked_allocation *entry, *prev;
   1.133 +    int index = get_allocation_bucket(mem);
   1.134 +
   1.135 +    prev = NULL;
   1.136 +    for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
   1.137 +        if (mem == entry->mem) {
   1.138 +            if (prev) {
   1.139 +                prev->next = entry->next;
   1.140 +            } else {
   1.141 +                s_tracked_allocations[index] = entry->next;
   1.142 +            }
   1.143 +            SDL_free_orig(entry);
   1.144 +            return;
   1.145 +        }
   1.146 +        prev = entry;
   1.147 +    }
   1.148 +}
   1.149 +
   1.150 +static void *SDLTest_TrackedMalloc(size_t size)
   1.151 +{
   1.152 +    void *mem;
   1.153 +
   1.154 +    mem = SDL_malloc_orig(size);
   1.155 +    if (mem) {
   1.156 +        SDL_TrackAllocation(mem, size);
   1.157 +    }
   1.158 +    return mem;
   1.159 +}
   1.160 +
   1.161 +static void *SDLTest_TrackedCalloc(size_t nmemb, size_t size)
   1.162 +{
   1.163 +    void *mem;
   1.164 +
   1.165 +    mem = SDL_calloc_orig(nmemb, size);
   1.166 +    if (mem) {
   1.167 +        SDL_TrackAllocation(mem, nmemb * size);
   1.168 +    }
   1.169 +    return mem;
   1.170 +}
   1.171 +
   1.172 +static void *SDLTest_TrackedRealloc(void *ptr, size_t size)
   1.173 +{
   1.174 +    void *mem;
   1.175 +
   1.176 +    SDL_assert(!ptr || SDL_IsAllocationTracked(ptr));
   1.177 +    mem = SDL_realloc_orig(ptr, size);
   1.178 +    if (mem && mem != ptr) {
   1.179 +        if (ptr) {
   1.180 +            SDL_UntrackAllocation(ptr);
   1.181 +        }
   1.182 +        SDL_TrackAllocation(mem, size);
   1.183 +    }
   1.184 +    return mem;
   1.185 +}
   1.186 +
   1.187 +static void SDLTest_TrackedFree(void *ptr)
   1.188 +{
   1.189 +    if (!ptr) {
   1.190 +        return;
   1.191 +    }
   1.192 +
   1.193 +    if (!s_previous_allocations) {
   1.194 +        SDL_assert(SDL_IsAllocationTracked(ptr));
   1.195 +    }
   1.196 +    SDL_UntrackAllocation(ptr);
   1.197 +    SDL_free_orig(ptr);
   1.198 +}
   1.199 +
   1.200 +int SDLTest_TrackAllocations()
   1.201 +{
   1.202 +    if (SDL_malloc_orig) {
   1.203 +        return 0;
   1.204 +    }
   1.205 +
   1.206 +    SDLTest_Crc32Init(&s_crc32_context);
   1.207 +
   1.208 +    s_previous_allocations = SDL_GetNumAllocations();
   1.209 +    if (s_previous_allocations != 0) {
   1.210 +        SDL_Log("SDLTest_TrackAllocations(): There are %d previous allocations, disabling free() validation", s_previous_allocations);
   1.211 +    }
   1.212 +
   1.213 +    SDL_GetMemoryFunctions(&SDL_malloc_orig,
   1.214 +                           &SDL_calloc_orig,
   1.215 +                           &SDL_realloc_orig,
   1.216 +                           &SDL_free_orig);
   1.217 +
   1.218 +    SDL_SetMemoryFunctions(SDLTest_TrackedMalloc,
   1.219 +                           SDLTest_TrackedCalloc,
   1.220 +                           SDLTest_TrackedRealloc,
   1.221 +                           SDLTest_TrackedFree);
   1.222 +    return 0;
   1.223 +}
   1.224 +
   1.225 +void SDLTest_LogAllocations()
   1.226 +{
   1.227 +    char *message = NULL;
   1.228 +    size_t message_size = 0;
   1.229 +    char line[128], *tmp;
   1.230 +    SDL_tracked_allocation *entry;
   1.231 +    int index, count, stack_index;
   1.232 +    Uint64 total_allocated;
   1.233 +
   1.234 +    if (!SDL_malloc_orig) {
   1.235 +        return;
   1.236 +    }
   1.237 +
   1.238 +#define ADD_LINE() \
   1.239 +    message_size += (SDL_strlen(line) + 1); \
   1.240 +    tmp = (char *)SDL_realloc_orig(message, message_size); \
   1.241 +    if (!tmp) { \
   1.242 +        return; \
   1.243 +    } \
   1.244 +    message = tmp; \
   1.245 +    SDL_strlcat(message, line, message_size)
   1.246 +
   1.247 +    SDL_strlcpy(line, "Memory allocations:\n", sizeof(line));
   1.248 +    ADD_LINE();
   1.249 +    SDL_strlcpy(line, "Expect 2 allocations from within SDL_GetErrBuf()\n", sizeof(line));
   1.250 +    ADD_LINE();
   1.251 +
   1.252 +    count = 0;
   1.253 +    total_allocated = 0;
   1.254 +    for (index = 0; index < SDL_arraysize(s_tracked_allocations); ++index) {
   1.255 +        for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
   1.256 +            SDL_snprintf(line, sizeof(line), "Allocation %d: %lu bytes\n", count, entry->size);
   1.257 +            ADD_LINE();
   1.258 +            /* Start at stack index 1 to skip our tracking functions */
   1.259 +            for (stack_index = 1; stack_index < SDL_arraysize(entry->stack); ++stack_index) {
   1.260 +                if (!entry->stack[stack_index]) {
   1.261 +                    break;
   1.262 +                }
   1.263 +                SDL_snprintf(line, sizeof(line), "\t0x%llx: %s\n", entry->stack[stack_index], entry->stack_names[stack_index]);
   1.264 +                ADD_LINE();
   1.265 +            }
   1.266 +            total_allocated += entry->size;
   1.267 +            ++count;
   1.268 +        }
   1.269 +    }
   1.270 +    SDL_snprintf(line, sizeof(line), "Total: %.2f Kb in %d allocations\n", (float)total_allocated / 1024, count);
   1.271 +    ADD_LINE();
   1.272 +#undef ADD_LINE
   1.273 +
   1.274 +    SDL_Log("%s", message);
   1.275 +}
   1.276 +
   1.277 +/* vi: set ts=4 sw=4 expandtab: */