Newer
Older
Import / applications / RocketMan / Source Code / DebugMemory.h
/*
 *  DebugMemory.h
 *  iphone-gl-app
 *
 *  Created by John Ryland on 22/06/09.
 *  Copyright 2009 InvertedLogic. All rights reserved.
 *
 */

#ifndef DEBUG_MEMORY_H
#define DEBUG_MEMORY_H


#include <stdlib.h> // Avoid conflicts for malloc etc.
#include <string.h> // Avoid conflicts for strdup
#include <unistd.h> // Avoid conflicts for valloc
#include "Debug.h"


class DebugMemory
{
public:
    // Public API
    static void enable();
    static void disable();
    static void showMemoryLeaks();
    static void showLocatableMemoryLeaks();
    static void showMemoryAllocations();
    static void logMemoryAllocations(int level);
    // Internal implementation function
    static void *addLog(int t, void *p, unsigned int num, unsigned int size, const char *file, int line);
};


#endif // DEBUG_MEMORY_H


#ifdef DEBUG
#ifndef DEBUG_MEMORY_DISABLE // Logging enabled in debug builds
#ifndef DEBUG_MEMORY_IMPL    // Avoid recursion
#define calloc(num,siz)         DebugMemory::addLog(1,    0, num, siz, __FILE__, __LINE__)
#define valloc(siz)             DebugMemory::addLog(2,    0,   1, siz, __FILE__, __LINE__)
#define realloc(ptr,siz)        DebugMemory::addLog(3,  ptr,   1, siz, __FILE__, __LINE__)
#define reallocf(ptr,siz)       DebugMemory::addLog(4,  ptr,   1, siz, __FILE__, __LINE__)
#define malloc(siz)             DebugMemory::addLog(5,    0,   1, siz, __FILE__, __LINE__)
#define strdup(ptr)             (char*)DebugMemory::addLog(13, (void*)(ptr),   0,   0, __FILE__, __LINE__)
#define free(ptr)               DebugMemory::addLog(10, ptr,   0,   0, __FILE__, __LINE__), (void)0
#define new                     (DebugMemory::addLog(0,   0,   0,   0, __FILE__, __LINE__)) ? 0 : new
#define delete                  (DebugMemory::addLog(0,   0,   0,   0, __FILE__, __LINE__)) ? (void)0 : delete
#endif // DEBUG_MEMORY_IMPL
#endif // DEBUG_MEMORY_DISABLE
#endif // DEBUG