Crazy Eddie's GUI System 0.8.7
Loading...
Searching...
No Matches
MemoryStdAllocator.h
1/***********************************************************************
2 created: 14/10/2010
3 author: Martin Preisler
4
5 purpose: Implements the default "dummy" allocator
6*************************************************************************/
7/***************************************************************************
8 * Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * a copy of this software and associated documentation files (the
12 * "Software"), to deal in the Software without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sublicense, and/or sell copies of the Software, and to
15 * permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 ***************************************************************************/
29#ifndef _CEGUIMemoryStdAllocator_h_
30#define _CEGUIMemoryStdAllocator_h_
31
32#include <stdlib.h>
33#include <limits>
34
35namespace CEGUI
36{
37
38class CEGUIEXPORT StdAllocator
39{
40public:
41 static inline void* allocateBytes(size_t count)
42 {
43 void* ptr = malloc(count);
44#if defined(CEGUI_CUSTOM_ALLOCATORS_USAGE)
45 if (ptr == NULL)
46 return NULL;
47 getAllocationSize(count);
48#endif
49 return ptr;
50 }
51
52 static inline void deallocateBytes(void* ptr)
53 {
54#if defined(CEGUI_CUSTOM_ALLOCATORS_USAGE)
55 if (ptr == NULL)
56 return;
57 size_t count = 0;
58#if defined(_MSC_VER)
59 count = _msize(ptr);
60#elif defined(__linux__)
62#elif defined(__APPLE__)
63 count = malloc_size(ptr);
64#endif
65 getAllocationSize((size_t)0 - count);
66#endif
67 free(ptr);
68 }
69
70 // !!! IF YOU GET AN ERROR HERE:
71 // that says something like: "You can't call allocateBytes with 4 arguments",
72 // you are using StdAllocator and trying to enable CEGUI_CUSTOM_ALLOCATORS_DEBUG, you
73 // have to provide your own custom memory allocator if you want memory debugging.
74
76 static inline size_t getMaxAllocationSize()
77 {
78 return std::numeric_limits<size_t>::max();
79 }
80
81 static inline size_t getAllocationSize(size_t adjust = 0)
82 {
83 static size_t allocationSize = 0;
84 if (adjust == 0)
85 return allocationSize;
86#if defined(CEGUI_CUSTOM_ALLOCATORS_USAGE)
87#if defined(_M_IX86)
88 _InterlockedExchangeAdd(reinterpret_cast<long*>(&allocationSize), adjust);
89#elif defined(_M_AMD64)
90 _InterlockedExchangeAdd64(reinterpret_cast<long long*>(&allocationSize), adjust);
91#elif defined(__GNUC__)
93#endif
94#endif
95 return allocationSize;
96 }
97};
98
99CEGUI_SET_DEFAULT_ALLOCATOR(StdAllocator)
100
101}
102
103#endif // end of guard _CEGUIMemoryStdAllocator_h_
Definition MemoryStdAllocator.h:39
static size_t getMaxAllocationSize()
Get the maximum size of a single allocation.
Definition MemoryStdAllocator.h:76
base class for properties able to do native set/get
Definition TypedProperty.h:50
Main namespace for Crazy Eddie's GUI Library.
Definition arch_overview.dox:1