gwnavruntime/kernel/SF_SysAlloc.h Source File

SF_SysAlloc.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 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 #pragma once
8 
10 
11 namespace Kaim {
12 
13 // ***** SysAllocBase
14 
15 // SysAllocBase is a base class for SysAlloc.
16 // This class is used to provide link-independent initialization for
17 // different memory heap engines in System class and has no public APIs.
19 {
20  friend class System;
21 protected:
22  virtual ~SysAllocBase() { }
23 
24  // Initializes heap system, creating and initializing GlobalHeap.
25  virtual bool initHeapEngine(const void* heapDesc) { KY_UNUSED(heapDesc); return false; }
26  // Shuts down heap system, clearing out global heap.
27  virtual void shutdownHeapEngine() { }
28 };
29 
30 
31 //------------------------------------------------------------------------
32 // ***** SysAlloc
33 
34 // SysAlloc defines a memory allocation interface that developers can override
35 // to to provide memory for GFx; an instance of this class is typically created on
36 // application startup and passed into System or Intrnl::System constructor.
37 //
38 // SysAlloc is more memory efficient when delegating to malloc/dlmalloc based implementation,
39 // as it doesn't require large alignment and will return more memory blocks
40 // to the application once content is unloaded.
41 //
42 // Users implementing this interface must provide three functions: Alloc, Free,
43 // and Realloc. Implementations of these functions must honor the requested alignment.
44 // Although arbitrary alignment requests are possible, requested alignment will
45 // typically be small, such as 16 bytes or less.
46 //
47 // SysAlloc links to the MemoryHeapMH implementation, which groups small
48 // allocations into 4K-sized blocks belonging to the heap. Allocations larger
49 // then 512 bytes will delegated directly to SysAlloc and released immediately
50 // once no longer used, making more memory available to the application.
51 
52 class SysAlloc : public SysAllocBase
53 {
54 public:
55  SysAlloc() {}
56  virtual ~SysAlloc() {}
57 
58  virtual void* Alloc(UPInt size, UPInt align) = 0;
59  virtual void Free(void* ptr, UPInt size, UPInt align) = 0;
60  virtual void* Realloc(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align) = 0;
61 
62 protected:
63  // Implementation of SysAllocBase based on MemoryHeapMH.
64  virtual bool initHeapEngine(const void* heapDesc);
65  virtual void shutdownHeapEngine();
66 };
67 
68 
69 //------------------------------------------------------------------------
70 // ***** SysAllocBase_SingletonSupport
71 
72 // SysAllocBase_SingletonSupport is a SysAllocBase class wrapper that implements
73 // the InitSystemSingleton static function, used to create a global singleton
74 // used for the GFxSystem default argument initialization.
75 //
76 // End users implementing custom SysAlloc/Paged interface don't need to make use of this base
77 // class; they can just create an instance of their own class on stack and pass it to System.
78 
79 template<class A, class B>
81 {
82  struct SysAllocContainer
83  {
84  UPInt Data[(sizeof(A) + sizeof(UPInt)-1) / sizeof(UPInt)];
85  bool Initialized;
86  SysAllocContainer() : Initialized(0) { }
87  };
88 
89  SysAllocContainer* pContainer;
90 
91 public:
92  SysAllocBase_SingletonSupport() : pContainer(0) { }
93 
94  static B* InitSystemSingleton()
95  {
96  static SysAllocContainer Container;
97  KY_ASSERT(Container.Initialized == false);
98 
99  #undef new
100 
101  SysAllocBase_SingletonSupport<A,B> *presult = ::new((void*)Container.Data) A;
102 
103  // Redefine operator 'new' if necessary.
104  #if defined(KY_DEFINE_NEW)
105  #define new KY_DEFINE_NEW
106  #endif
107 
108  presult->pContainer = &Container;
109  Container.Initialized = true;
110  return presult;
111  }
112 
113 protected:
114  virtual void shutdownHeapEngine()
115  {
116  B::shutdownHeapEngine();
117  if (pContainer)
118  {
119  pContainer->Initialized = false;
120  ((B*)this)->~B();
121  pContainer = 0;
122  }
123  }
124 };
125 
126 }
A base class for an object that manages memory allocations and de-allocations.
Definition: SF_SysAlloc.h:18
The SysAlloc class specifies an interface for allocating and freeing memory.
Definition: SF_SysAlloc.h:52
Adds to the SysAlloc class support for restricting instantiation to a single object.
Definition: SF_SysAlloc.h:80
virtual void * Realloc(void *oldPtr, UPInt oldSize, UPInt newSize, UPInt align)=0
Re-allocates a buffer to a new size.
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
virtual void * Alloc(UPInt size, UPInt align)=0
Allocates a buffer of the specified size, with the specified alignment.
virtual void Free(void *ptr, UPInt size, UPInt align)=0
Frees the specified memory buffer.