Initial commits of the sharedDebug and sharedMemoryManager projects

This commit is contained in:
Anonymous
2014-01-13 09:07:22 -07:00
parent 5940c017b7
commit b632e73284
91 changed files with 12704 additions and 0 deletions
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 2.8)
project(sharedMemoryManager)
if(WIN32)
add_definitions(/D_CRT_SECURE_NO_WARNINGS)
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/public)
add_subdirectory(src)
@@ -0,0 +1,7 @@
#if defined(PLATFORM_WIN32)
#include "../../src/win32/OsMemory.h"
#elif defined(PLATFORM_LINUX)
#include "../../src/linux/OsMemory.h"
#else
#error unsupported platform
#endif
@@ -0,0 +1 @@
#include "../../src/shared/FirstSharedMemoryManager.h"
@@ -0,0 +1 @@
#include "../../src/shared/MemoryManager.h"
@@ -0,0 +1,7 @@
#if defined(PLATFORM_WIN32)
#include "../../src/win32/OsNewDel.h"
#elif defined(PLATFORM_LINUX)
#include "../../src/linux/OsNewDel.h"
#else
#error unsupported platform
#endif
@@ -0,0 +1,37 @@
set(SHARED_SOURCES
shared/FirstSharedMemoryManager.cpp
shared/FirstSharedMemoryManager.h
shared/MemoryManager.cpp
shared/MemoryManager.h
)
if(WIN32)
set(PLATFORM_SOURCES
win32/OsMemory.cpp
win32/OsMemory.h
win32/OsNewDel.cpp
win32/OsNewDel.h
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/win32)
else()
set(PLATFORM_SOURCES
linux/OsMemory.cpp
linux/OsMemory.h
linux/OsNewDel.cpp
linux/OsNewDel.h
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/linux)
endif()
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/shared
${SWG_ENGINE_SOURCE_DIR}/shared/library/sharedFoundationTypes/include/public
)
add_library(sharedMemoryManager STATIC
${SHARED_SOURCES}
${PLATFORM_SOURCES}
)
@@ -0,0 +1,40 @@
// ======================================================================
//
// OsMemory.cpp
//
// Copyright 2002 Sony Online Entertainment
//
// ======================================================================
#include "sharedMemoryManager/FirstSharedMemoryManager.h"
#include "sharedMemoryManager/OsMemory.h"
// ======================================================================
void OsMemory::install()
{
}
// ----------------------------------------------------------------------
void OsMemory::remove()
{
}
// ----------------------------------------------------------------------
void *OsMemory::commit(void *, size_t bytes)
{
return ::malloc(bytes);
}
// ----------------------------------------------------------------------
bool OsMemory::free(void *addr, size_t)
{
::free(addr);
return true;
}
// ======================================================================
@@ -0,0 +1,29 @@
// ======================================================================
//
// OsMemory.h
//
// Copyright 2002 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_OsMemory_H
#define INCLUDED_OsMemory_H
// ======================================================================
class OsMemory
{
public:
static void install();
static void remove();
static void * reserve(size_t bytes);
static void * commit(void *addr, size_t bytes);
static bool free(void *addr, size_t bytes);
static bool protect(void *addr, size_t bytes, bool allowAccess);
};
// ======================================================================
#endif // INCLUDED_OsMemory_H
@@ -0,0 +1,134 @@
// ======================================================================
//
// OsNewDel.cpp
//
// Copyright 2002 Sony Online Entertainment
//
// ======================================================================
#include "sharedMemoryManager/FirstSharedMemoryManager.h"
#include "sharedMemoryManager/MemoryManager.h"
#include "sharedMemoryManager/OsNewDel.h"
#include <malloc.h>
#include <unistd.h>
#include <sys/mman.h>
static MemoryManager memoryManager __attribute__ ((init_priority (101)));
#define USE_LIBC_MALLOC_HOOKS 0
#if USE_LIBC_MALLOC_HOOKS
extern "C"
{
void memoryManagerFreeHook(__malloc_ptr_t __ptr, __const __malloc_ptr_t)
{
delete[] (char *)__ptr;
}
__malloc_ptr_t memoryManagerMallocHook(size_t __size, const __malloc_ptr_t)
{
return new char[__size];
}
__malloc_ptr_t memoryManagerReallocHook(__malloc_ptr_t __ptr, size_t size, __const __malloc_ptr_t)
{
if(! __ptr)
return new char[size];
return MemoryManager::reallocate(__ptr, size);
}
__malloc_ptr_t memoryManagerMemAlignHook(size_t alignment, size_t size, __const __malloc_ptr_t)
{
DEBUG_FATAL(true, ("memalign not implemented!"));
return new char[size];
}
static void memoryManagerMallocInitializeHook(void)
{
__free_hook = memoryManagerFreeHook;
__malloc_hook = memoryManagerMallocHook;
__realloc_hook = memoryManagerReallocHook;
__memalign_hook = memoryManagerMemAlignHook;
}
void (*__malloc_initialize_hook) (void) = memoryManagerMallocInitializeHook;
}
#endif//USE_LIBC_MALLOC_HOOKS
// ======================================================================
void *operator new(size_t size, MemoryManagerNotALeak) throw (std::bad_alloc)
{
return MemoryManager::allocate(size, reinterpret_cast<uint32>(__builtin_return_address(0)), false, false);
}
// ----------------------------------------------------------------------
void *operator new(size_t size) throw (std::bad_alloc)
{
return MemoryManager::allocate(size, reinterpret_cast<uint32>(__builtin_return_address(0)), false, true);
}
// ----------------------------------------------------------------------
void *operator new[](size_t size) throw (std::bad_alloc)
{
return MemoryManager::allocate(size, reinterpret_cast<uint32>(__builtin_return_address(0)), true, true);
}
// ----------------------------------------------------------------------
void *operator new(size_t size, const char *file, int line) throw (std::bad_alloc)
{
return MemoryManager::allocate(size, reinterpret_cast<uint32>(__builtin_return_address(0)), false, true);
}
// ----------------------------------------------------------------------
void *operator new[](size_t size, const char *file, int line) throw (std::bad_alloc)
{
return MemoryManager::allocate(size, reinterpret_cast<uint32>(__builtin_return_address(0)), true, true);
}
// ----------------------------------------------------------------------
void operator delete(void *pointer) throw()
{
if (pointer)
MemoryManager::free(pointer, false);
}
// ----------------------------------------------------------------------
void operator delete[](void *pointer) throw()
{
if (pointer)
MemoryManager::free(pointer, true);
}
// ----------------------------------------------------------------------
void operator delete(void *pointer, const char *file, int line) throw()
{
UNREF(file);
UNREF(line);
if (pointer)
MemoryManager::free(pointer, false);
}
// ----------------------------------------------------------------------
void operator delete[](void *pointer, const char *file, int line) throw()
{
UNREF(file);
UNREF(line);
if (pointer)
MemoryManager::free(pointer, true);
}
// ======================================================================
@@ -0,0 +1,33 @@
// ======================================================================
//
// OsNewDel.h
//
// Copyright 2002 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_OsNewDel_H
#define INCLUDED_OsNewDel_H
// ======================================================================
enum MemoryManagerNotALeak
{
MM_notALeak
};
void *operator new(size_t size, MemoryManagerNotALeak) throw(std::bad_alloc);
void *operator new(size_t size) throw(std::bad_alloc);
void *operator new[](size_t size) throw(std::bad_alloc);
void *operator new(size_t size, char const *file, int line) throw(std::bad_alloc);
void *operator new[](size_t size, char const *file, int line) throw(std::bad_alloc);
void operator delete(void *pointer) throw();
void operator delete[](void *pointer) throw();
void operator delete(void *pointer, char const *file, int line) throw();
void operator delete[](void *pointer, char const *file, int line) throw();
// ======================================================================
#endif // INCLUDED_OsNewDel_H
@@ -0,0 +1,8 @@
// ======================================================================
//
// FirstMemoryManager.cpp
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedMemoryManager/FirstSharedMemoryManager.h"
@@ -0,0 +1,19 @@
// ======================================================================
//
// FirstMemoryManager.h
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_FirstMemoryManager_H
#define INCLUDED_FirstMemoryManager_H
// ======================================================================
#include "sharedFoundationTypes/FoundationTypes.h"
#include "sharedDebug/FirstSharedDebug.h"
#include "sharedFoundation/FirstSharedFoundation.h"
// ======================================================================
#endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,84 @@
// ======================================================================
//
// MemoryManager.h
// Portions copyright 1998 Bootprint Entertainment
// Portions copyright 2002 Sony Online Entertainment
// All Rights Reserved
//
// ======================================================================
#ifndef INCLUDED_MemoryManager_H
#define INCLUDED_MemoryManager_H
// ======================================================================
#include "sharedDebug/DebugHelp.h"
// ======================================================================
#include <new>
#include "sharedMemoryManager/OsNewDel.h"
// ======================================================================
// Memory manager class.
//
// This class API is multi-thread safe.
//
// This class provides extensive debugging features for applications, including
// overwrite guard bands, initialize pattern fills, free pattern fills, and
// memory tracking.
class MemoryManager
{
public:
MemoryManager();
~MemoryManager();
static void setLimit(int megabytes, bool hardLimit, bool preallocate);
static int getLimit();
static bool isHardLimit();
static void registerDebugFlags();
static void debugReport();
static void debugReportMap();
static bool reportToFile(const char * fileName, bool leak);
static int getCurrentNumberOfAllocations();
static unsigned long getCurrentNumberOfBytesAllocated(const int processId = 0);
static unsigned long getCurrentNumberOfBytesAllocatedNoLeakTest();
static int getMaximumNumberOfAllocations();
static unsigned long getMaximumNumberOfBytesAllocated();
static int getSystemMemoryAllocatedMegabytes();
#ifndef _WIN32
static int getProcessVmSizeKBytes(const int processId = 0);
#endif
static DLLEXPORT void *allocate(size_t size, uint32 owner, bool array, bool leakTest);
static DLLEXPORT void free(void *pointer, bool array);
static DLLEXPORT void own(void *pointer);
static void * reallocate(void *userPointer, size_t newSize);
static void verify(bool guardPatterns, bool freePatterns);
static void setReportAllocations(bool reportAllocations);
static void report();
private:
// disabled
MemoryManager(MemoryManager const &);
MemoryManager &operator =(MemoryManager const &);
};
// ======================================================================
#ifdef _DEBUG
#define MEM_OWN(a) MemoryManager::own(a)
#else
#define MEM_OWN(a) UNREF(a)
#endif
// ======================================================================
#endif
@@ -0,0 +1,56 @@
// ======================================================================
//
// OsMemory.cpp
//
// Copyright 2002 Sony Online Entertainment
//
// ======================================================================
#include "sharedMemoryManager/FirstSharedMemoryManager.h"
#include "sharedMemoryManager/OsMemory.h"
// ======================================================================
void OsMemory::install()
{
}
// ----------------------------------------------------------------------
void OsMemory::remove()
{
}
// ----------------------------------------------------------------------
void *OsMemory::reserve(size_t bytes)
{
return VirtualAlloc(0, bytes, MEM_RESERVE, PAGE_READWRITE);
}
// ----------------------------------------------------------------------
void *OsMemory::commit(void *addr, size_t bytes)
{
return VirtualAlloc(addr, bytes, MEM_COMMIT, PAGE_READWRITE);
}
// ----------------------------------------------------------------------
bool OsMemory::free(void *addr, size_t bytes)
{
UNREF(bytes);
return VirtualFree(addr, 0, MEM_RELEASE) ? true : false;
}
// ----------------------------------------------------------------------
bool OsMemory::protect(void *addr, size_t bytes, bool allowAccess)
{
DWORD old;
BOOL result = VirtualProtect(addr, bytes, allowAccess ? PAGE_READWRITE : PAGE_NOACCESS, &old);
return result ? true : false;
}
// ======================================================================
@@ -0,0 +1,29 @@
// ======================================================================
//
// OsMemory.h
//
// Copyright 2002 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_OsMemory_H
#define INCLUDED_OsMemory_H
// ======================================================================
class OsMemory
{
public:
static void install();
static void remove();
static void * reserve(size_t bytes);
static void * commit(void *addr, size_t bytes);
static bool free(void *addr, size_t bytes);
static bool protect(void *addr, size_t bytes, bool allowAccess);
};
// ======================================================================
#endif INCLUDED_OsMemory_H
@@ -0,0 +1,211 @@
// ======================================================================
//
// OsNewDel.cpp
//
// Copyright 2002 Sony Online Entertainment
//
// ======================================================================
#include "sharedMemoryManager/FirstSharedMemoryManager.h"
#include "sharedMemoryManager/OsNewDel.h"
// ======================================================================
// this is here because MSVC won't call MemoryManager::allocate() from asm directly
static void * __cdecl localAllocate(size_t size, uint32 owner, bool array, bool leakTest)
{
return MemoryManager::allocate(size, owner, array, leakTest);
}
// ----------------------------------------------------------------------
// We are using the arguments (except for file and line), but MSVC can't tell that.
#pragma warning(disable: 4100)
// ----------------------------------------------------------------------
__declspec(naked) void *operator new(size_t size, MemoryManagerNotALeak)
{
_asm
{
// setup local call stack
push ebp
mov ebp, esp
// MemoryManager::alloc(size, [return address], false, false)
push 0
push 0
mov eax, dword ptr [ebp+4]
push eax
mov eax, dword ptr [ebp+8]
push eax
call localAllocate
add esp, 12
mov esp, ebp
pop ebp
ret
}
}
// ----------------------------------------------------------------------
__declspec(naked) void *operator new(size_t size)
{
_asm
{
// setup local call stack
push ebp
mov ebp, esp
// MemoryManager::alloc(size, [return address], false, true)
push 1
push 0
mov eax, dword ptr [ebp+4]
push eax
mov eax, dword ptr [ebp+8]
push eax
call localAllocate
add esp, 12
mov esp, ebp
pop ebp
ret
}
}
// ----------------------------------------------------------------------
__declspec(naked) void *operator new[](size_t size)
{
_asm
{
// setup local call stack
push ebp
mov ebp, esp
// MemoryManager::alloc(size, [return address], true, true)
push 1
push 1
mov eax, dword ptr [ebp+4]
push eax
mov eax, dword ptr [ebp+8]
push eax
call localAllocate
add esp, 12
mov esp, ebp
pop ebp
ret
}
}
// ----------------------------------------------------------------------
__declspec(naked) void *operator new(size_t size, const char *file, int line)
{
_asm
{
// setup local call stack
push ebp
mov ebp, esp
// MemoryManager::alloc(size, [return address], false, true)
push 1
push 0
mov eax, dword ptr [ebp+4]
push eax
mov eax, dword ptr [ebp+8]
push eax
call localAllocate
add esp, 12
mov esp, ebp
pop ebp
ret
}
}
// ----------------------------------------------------------------------
__declspec(naked) void *operator new[](size_t size, const char *file, int line)
{
_asm
{
// setup local call stack
push ebp
mov ebp, esp
// MemoryManager::alloc(size, [return address], true, true)
push 1
push 1
mov eax, dword ptr [ebp+4]
push eax
mov eax, dword ptr [ebp+8]
push eax
call localAllocate
add esp, 12
mov esp, ebp
pop ebp
ret
}
}
// ----------------------------------------------------------------------
#pragma warning(default: 4100)
// ----------------------------------------------------------------------
void operator delete(void *pointer)
{
if (pointer)
MemoryManager::free(pointer, false);
}
// ----------------------------------------------------------------------
void operator delete[](void *pointer)
{
if (pointer)
MemoryManager::free(pointer, true);
}
// ----------------------------------------------------------------------
void operator delete(void *pointer, const char *file, int line)
{
UNREF(file);
UNREF(line);
if (pointer)
MemoryManager::free(pointer, false);
}
// ----------------------------------------------------------------------
void operator delete[](void *pointer, const char *file, int line)
{
UNREF(file);
UNREF(line);
if (pointer)
MemoryManager::free(pointer, true);
}
// ======================================================================
// WARNING!!!!!!!
//
// The init_seg pragma command is used to create certain static objects first, before other static objects are created.
// However, multiple static variables that use the same init_seg category(i.e. compiler) are not guaranteed to destroy in any
// particular order. It is completely random based on how all the linking of static objects occurs. Since this command is being
// used on our memory manager(to overwrite new/delete) - NO OTHER STATIC SHOULD EVER USE INIT_SEG(COMPILER)!!!! This static object
// *MUST* be the final static object that is destroyed.
//
#pragma warning(disable: 4074)
#pragma init_seg(compiler) // ^-Read warning above.-^
static MemoryManager memoryManager;
// ======================================================================
@@ -0,0 +1,52 @@
// ======================================================================
//
// OsNewDel.h
//
// Copyright 2002 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_OsNewDel_H
#define INCLUDED_OsNewDel_H
// ======================================================================
enum MemoryManagerNotALeak
{
MM_notALeak
};
void * __cdecl operator new(size_t size, MemoryManagerNotALeak);
void * __cdecl operator new(size_t size);
void * __cdecl operator new[](size_t size);
void * __cdecl operator new(size_t size, char const *file, int line);
void * __cdecl operator new[](size_t size, char const *file, int line);
void * __cdecl operator new(size_t size, void *placement);
void operator delete(void *pointer);
void operator delete[](void *pointer);
void operator delete(void *pointer, char const *file, int line);
void operator delete[](void *pointer, char const *file, int line);
void operator delete(void *pointer, void *placement);
#ifndef __PLACEMENT_NEW_INLINE
#define __PLACEMENT_NEW_INLINE
inline void *operator new(size_t size, void *placement)
{
static_cast<void>(size);
return placement;
}
inline void operator delete(void *pointer, void *placement)
{
static_cast<void>(pointer);
static_cast<void>(placement);
}
#endif // __PLACEMENT_NEW_INLINE
// ======================================================================
#endif INCLUDED_OsNewDel_H