From 112b53b116a2195842850f38596bc5f060af9946 Mon Sep 17 00:00:00 2001 From: Dario Manesku Date: Wed, 21 Jan 2015 18:01:32 +0100 Subject: [PATCH 1/3] Added possibility to override default tinystl allocator for 'example-common' project. --- examples/common/bgfx_utils.cpp | 2 ++ examples/common/common.h | 19 +++++++++++++++++++ examples/common/entry/cmd.cpp | 1 + examples/common/entry/entry_windows.cpp | 1 + examples/common/entry/input.cpp | 1 + 5 files changed, 24 insertions(+) diff --git a/examples/common/bgfx_utils.cpp b/examples/common/bgfx_utils.cpp index a9516af16..0035076d8 100644 --- a/examples/common/bgfx_utils.cpp +++ b/examples/common/bgfx_utils.cpp @@ -5,6 +5,8 @@ #include // strlen +#include "common.h" + #include #include #include diff --git a/examples/common/common.h b/examples/common/common.h index fbcea73cc..0f2a4a1d0 100644 --- a/examples/common/common.h +++ b/examples/common/common.h @@ -3,7 +3,26 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#ifndef COMMON_H_HEADER_GUARD +#define COMMON_H_HEADER_GUARD + #include #include #include "entry/entry.h" + +// For a custom tinystl allocator, define this and implement TinyStlCustomAllocator somewhere in the project. +#ifndef COMMON_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR +# define COMMON_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR 0 +#endif // COMMON_CONFIG_USE_TINYSTL + +#if COMMON_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR +struct TinyStlCustomAllocator +{ + static void* static_allocate(size_t _bytes); + static void static_deallocate(void* _ptr, size_t /*_bytes*/); +}; +# define TINYSTL_ALLOCATOR TinyStlCustomAllocator +#endif //COMMON_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR + +#endif // COMMON_H_HEADER_GUARD diff --git a/examples/common/entry/cmd.cpp b/examples/common/entry/cmd.cpp index 6dfcb37d8..7464176d6 100644 --- a/examples/common/entry/cmd.cpp +++ b/examples/common/entry/cmd.cpp @@ -10,6 +10,7 @@ #include #include +#include "../common.h" #include "dbg.h" #include "cmd.h" diff --git a/examples/common/entry/entry_windows.cpp b/examples/common/entry/entry_windows.cpp index 1b3f4f2d4..279441295 100644 --- a/examples/common/entry/entry_windows.cpp +++ b/examples/common/entry/entry_windows.cpp @@ -4,6 +4,7 @@ */ #include "entry_p.h" +#include "../common.h" #if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_WINDOWS diff --git a/examples/common/entry/input.cpp b/examples/common/entry/input.cpp index 851f22643..ecfeb0557 100644 --- a/examples/common/entry/input.cpp +++ b/examples/common/entry/input.cpp @@ -5,6 +5,7 @@ #include +#include "../common.h" #include "entry_p.h" #include "input.h" From 122129b3512b3c80f1d1775654e334632efc7806 Mon Sep 17 00:00:00 2001 From: Dario Manesku Date: Fri, 6 Feb 2015 19:24:15 +0100 Subject: [PATCH 2/3] Moving TinyStlCustomAllocator inside entry. --- examples/common/common.h | 14 -------------- examples/common/entry/cmd.cpp | 2 +- examples/common/entry/entry.h | 14 ++++++++++++++ examples/common/entry/entry_windows.cpp | 1 - examples/common/entry/input.cpp | 1 - 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/examples/common/common.h b/examples/common/common.h index 0f2a4a1d0..3e80ee67f 100644 --- a/examples/common/common.h +++ b/examples/common/common.h @@ -11,18 +11,4 @@ #include "entry/entry.h" -// For a custom tinystl allocator, define this and implement TinyStlCustomAllocator somewhere in the project. -#ifndef COMMON_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR -# define COMMON_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR 0 -#endif // COMMON_CONFIG_USE_TINYSTL - -#if COMMON_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR -struct TinyStlCustomAllocator -{ - static void* static_allocate(size_t _bytes); - static void static_deallocate(void* _ptr, size_t /*_bytes*/); -}; -# define TINYSTL_ALLOCATOR TinyStlCustomAllocator -#endif //COMMON_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR - #endif // COMMON_H_HEADER_GUARD diff --git a/examples/common/entry/cmd.cpp b/examples/common/entry/cmd.cpp index 7464176d6..bbe5a9a64 100644 --- a/examples/common/entry/cmd.cpp +++ b/examples/common/entry/cmd.cpp @@ -10,9 +10,9 @@ #include #include -#include "../common.h" #include "dbg.h" #include "cmd.h" +#include "entry.h" //TinyStlCustomAllocator #include #include diff --git a/examples/common/entry/entry.h b/examples/common/entry/entry.h index 7f6524b8a..0c92440d4 100644 --- a/examples/common/entry/entry.h +++ b/examples/common/entry/entry.h @@ -18,6 +18,20 @@ extern "C" int _main_(int _argc, char** _argv); #define ENTRY_WINDOW_FLAG_ASPECT_RATIO UINT32_C(0x00000001) #define ENTRY_WINDOW_FLAG_FRAME UINT32_C(0x00000002) +// For a custom tinystl allocator, define this and implement TinyStlCustomAllocator somewhere in the project. +#ifndef ENTRY_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR +# define ENTRY_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR 0 +#endif // ENTRY_CONFIG_USE_TINYSTL + +#if ENTRY_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR + struct TinyStlCustomAllocator + { + static void* static_allocate(size_t _bytes); + static void static_deallocate(void* _ptr, size_t /*_bytes*/); + }; +# define TINYSTL_ALLOCATOR TinyStlCustomAllocator +#endif //ENTRY_CONFIG_USE_TINYSTL_CUSTOM_ALLOCATOR + namespace entry { struct WindowHandle { uint16_t idx; }; diff --git a/examples/common/entry/entry_windows.cpp b/examples/common/entry/entry_windows.cpp index 279441295..1b3f4f2d4 100644 --- a/examples/common/entry/entry_windows.cpp +++ b/examples/common/entry/entry_windows.cpp @@ -4,7 +4,6 @@ */ #include "entry_p.h" -#include "../common.h" #if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_WINDOWS diff --git a/examples/common/entry/input.cpp b/examples/common/entry/input.cpp index ecfeb0557..851f22643 100644 --- a/examples/common/entry/input.cpp +++ b/examples/common/entry/input.cpp @@ -5,7 +5,6 @@ #include -#include "../common.h" #include "entry_p.h" #include "input.h" From 18307976c85ae8f817d3f4d1cca6cc1cefb9e03f Mon Sep 17 00:00:00 2001 From: Dario Manesku Date: Fri, 6 Feb 2015 19:48:14 +0100 Subject: [PATCH 3/3] Added possibility to override imgui malloc/free. --- examples/common/imgui/imgui.cpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/common/imgui/imgui.cpp b/examples/common/imgui/imgui.cpp index 163f18a81..9df31fdcc 100644 --- a/examples/common/imgui/imgui.cpp +++ b/examples/common/imgui/imgui.cpp @@ -65,15 +65,25 @@ static const int32_t SCROLL_AREA_PADDING = 6; static const int32_t AREA_HEADER = 20; static const float s_tabStops[4] = {150, 210, 270, 330}; -static void* imguiMalloc(size_t size, void* /*_userptr*/) -{ - return malloc(size); -} +// For a custom allocator, define this and implement imguiMalloc and imguiFree somewhere in the project. +#ifndef IMGUI_CONFIG_CUSTOM_ALLOCATOR +# define IMGUI_CONFIG_CUSTOM_ALLOCATOR 0 +#endif // ENTRY_CONFIG_USE_TINYSTL -static void imguiFree(void* _ptr, void* /*_userptr*/) -{ - free(_ptr); -} +#if IMGUI_CONFIG_CUSTOM_ALLOCATOR + void* imguiMalloc(size_t size, void* /*_userptr*/); + void imguiFree(void* _ptr, void* /*_userptr*/); +#else + static void* imguiMalloc(size_t _size, void* /*_userptr*/) + { + return malloc(_size); + } + + static void imguiFree(void* _ptr, void* /*_userptr*/) + { + free(_ptr); + } +#endif //IMGUI_CONFIG_CUSTOM_ALLOCATOR #define IMGUI_MIN(_a, _b) (_a)<(_b)?(_a):(_b) #define IMGUI_MAX(_a, _b) (_a)>(_b)?(_a):(_b)