gwnavruntime/kernel/HeapMH/HeapMH_SysAllocMalloc.h Source File

HeapMH_SysAllocMalloc.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: Kernel
10 Filename : HeapMH_SysAllocMalloc.h
11 Content : System Allocator Interface
12 Created : 2009
13 Authors : Maxim Shemanarev
14 
15 Notes : Interface to the system allocator.
16 
17 **************************************************************************/
18 
19 #ifndef INC_KY_Kernel_HeapMH_SysAllocMalloc_H
20 #define INC_KY_Kernel_HeapMH_SysAllocMalloc_H
21 
23 
24 #if defined(KY_OS_WIN32) || defined(KY_OS_WINCE) || defined(KY_OS_XBOX) || defined(KY_OS_XBOX360)
25 #include <malloc.h>
26 #elif defined(KY_OS_WII)
27 #include <string.h>
28 #else
29 #include <stdlib.h>
30 #include <string.h>
31 #endif
32 
33 namespace Kaim {
34 
35 class SysAllocMalloc : public SysAllocBase_SingletonSupport<SysAllocMalloc, SysAlloc>
36 {
37 public:
38 #if defined(KY_OS_WII)
39  // Amount of memory to reserve in MEM2 arena; 0 is all available space
40  SysAllocMalloc(UPInt size = 0)
41  {
42  UByte *m2arenaLo = (UByte*) OSGetMEM2ArenaLo();
43  UByte *m2arenaHi = (UByte*) OSGetMEM2ArenaHi();
44  if (size > 0 && (m2arenaLo + size < m2arenaHi))
45  m2arenaHi = m2arenaLo + size;
46  OSSetMEM2ArenaLo(m2arenaHi);
47  //SF_DEBUG_WARNING(m2arenaHi-m2arenaLo < 65536, "GSysAllocMalloc: Less than 64k memory available");
48 
49  ::new(PrivateData) SysAllocStatic(m2arenaLo, (UByte*)m2arenaHi-(UByte*)m2arenaLo);
50  }
51 #else
52  SysAllocMalloc() {}
53 #endif
54  virtual ~SysAllocMalloc() {}
55 
56 #if (defined(KY_OS_WIN32) || defined(KY_OS_WINCE) || defined(KY_OS_XBOX) || defined(KY_OS_XBOX360)) && !defined(KY_CC_GNU)
57  virtual void* Alloc(UPInt size, UPInt align)
58  {
59  return _aligned_malloc(size, align);
60  }
61 
62  virtual void Free(void* ptr, UPInt size, UPInt align)
63  {
64  KY_UNUSED2(size, align);
65  _aligned_free(ptr);
66  }
67 
68  virtual void* Realloc(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align)
69  {
70  KY_UNUSED(oldSize);
71  return _aligned_realloc(oldPtr, newSize, align);
72  }
73 #elif defined(KY_OS_PS3)
74  virtual void* Alloc(UPInt size, UPInt align)
75  {
76  return memalign(align, size);
77  }
78 
79  virtual void Free(void* ptr, UPInt size, UPInt align)
80  {
81  KY_UNUSED2(size, align);
82  free(ptr);
83  }
84 
85  virtual void* Realloc(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align)
86  {
87  KY_UNUSED(oldSize);
88  return reallocalign(oldPtr, newSize, align);
89  }
90 
91 #elif defined(KY_OS_WII)
92  virtual void* Alloc(UPInt size, UPInt align)
93  {
94  return GetAllocator()->Alloc(size, align);
95  }
96 
97  virtual void Free(void* ptr, UPInt size, UPInt align)
98  {
99  GetAllocator()->Free(ptr, size, align);
100  }
101 
102  virtual void* Realloc(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align)
103  {
104  void* newPtr = NULL;
105  if (GetAllocator()->ReallocInPlace(oldPtr, oldSize, newSize, align))
106  newPtr = oldPtr;
107  else {
108  newPtr = GetAllocator()->Alloc(newSize, align);
109  if (newPtr)
110  {
111  memcpy(newPtr, oldPtr, (newSize < oldSize) ? newSize : oldSize);
112  GetAllocator()->Free(oldPtr, oldSize, align);
113  }
114  }
115  return newPtr;
116  }
117 
118 private:
119  UPInt PrivateData[(sizeof(SysAllocStatic) + sizeof(UPInt)) / sizeof(UPInt)];
120  SysAllocStatic* GetAllocator() { return (SysAllocStatic*) PrivateData; }
121 
122 #else
123  virtual void* Alloc(UPInt size, UPInt align)
124  {
125  if (align < sizeof(void*))
126  align = sizeof(void*);
127 
128  UPInt ptr = (UPInt)malloc(size+(align-1)+sizeof(UPInt));
129  UPInt aligned = 0;
130  if (ptr)
131  {
132  aligned = (UPInt(ptr) + sizeof(UPInt) + (align-1)) & ~(align-1);
133  if (aligned == ptr)
134  aligned += align;
135  *(((UPInt*)aligned)-1) = aligned-ptr;
136  }
137  return (void*)aligned;
138  }
139 
140  virtual void Free(void* ptr, UPInt size, UPInt align)
141  {
142  if(ptr)
143  {
144  UPInt src = UPInt(ptr) - *(((UPInt*)ptr)-1);
145  free((void*)src);
146  }
147  }
148 
149  virtual void* Realloc(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align)
150  {
151  void* newPtr = Alloc(newSize, align);
152  if (newPtr)
153  {
154  memcpy(newPtr, oldPtr, (newSize < oldSize) ? newSize : oldSize);
155  Free(oldPtr, oldSize, align);
156  }
157  return newPtr;
158  }
159 #endif
160 };
161 
162 
163 } // Scaleform
164 
165 #endif
virtual void Free(void *ptr, UPInt size, UPInt align)
Frees the specified memory buffer.
Definition: HeapMH_SysAllocMalloc.h:63
virtual void * Alloc(UPInt size, UPInt align)
Allocates a buffer of the specified size, with the specified alignment.
Definition: HeapMH_SysAllocMalloc.h:58
This implementation of SysAlloc is used by default by the BaseSystem to allocate and free memory...
Definition: HeapMH_SysAllocMalloc.h:35
Adds to the SysAlloc class support for restricting instantiation to a single object.
Definition: SF_SysAlloc.h:267
Definition: gamekitcrowddispersion.h:20
virtual void * Realloc(void *oldPtr, UPInt oldSize, UPInt newSize, UPInt align)
Re-allocates a buffer to a new size.
Definition: HeapMH_SysAllocMalloc.h:69