gwnavruntime/kernel/SF_AllocAddr.h Source File

SF_AllocAddr.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 /**************************************************************************
8 
9 PublicHeader: Kernel
10 Filename : KY_AllocAddr.h
11 Content : Abstract address space allocator
12 Created : 2009
13 Authors : Maxim Shemanarev
14 
15 Notes : An abstract address space allocator based on binary
16  trees. Used when the memory itself is not available
17  (address space, video memory, etc).
18 
19 **************************************************************************/
20 
21 #ifndef INC_KY_Kernel_Memory_AllocAddr_H
22 #define INC_KY_Kernel_Memory_AllocAddr_H
23 
27 
28 namespace Kaim {
29 
30 //------------------------------------------------------------------------
31 struct AllocAddrNode : ListNode<AllocAddrNode>
32 {
33  AllocAddrNode* AddrParent;
34  AllocAddrNode* AddrChild[2];
35  AllocAddrNode* SizeParent;
36  AllocAddrNode* SizeChild[2];
37  UPInt Addr;
38  UPInt Size;
39 };
40 
41 class MemoryHeap;
42 
43 //------------------------------------------------------------------------
44 
45 // AllocAddr is an allocator for blocks of address space that doesn't store
46 // any header management information in the managed memory; it uses the provided
47 // memory heap for descriptor nodes instead. Intended for vertex/index buffer allocation
48 // and video memory management.
49 
50 class AllocAddr
51 {
52  typedef AllocAddrNode FreeNode;
53 
54 public:
55  //--------------------------------------------------------------------
56  struct SizeAccessor
57  {
58  static UPInt GetKey (const FreeNode* n) { return n->Size; }
59  static FreeNode* GetChild ( FreeNode* n, UPInt i) { return n->SizeChild[i]; }
60  static const FreeNode* GetChild (const FreeNode* n, UPInt i) { return n->SizeChild[i]; }
61  static FreeNode** GetChildPtr( FreeNode* n, UPInt i) { return &n->SizeChild[i]; }
62 
63  static FreeNode* GetParent ( FreeNode* n) { return n->SizeParent; }
64  static const FreeNode* GetParent (const FreeNode* n) { return n->SizeParent; }
65 
66  static void SetParent (FreeNode* n, FreeNode* p) { n->SizeParent = p; }
67  static void SetChild (FreeNode* n, UPInt i, FreeNode* c) { n->SizeChild[i] = c; }
68  static void ClearLinks(FreeNode* n) { n->SizeParent = n->SizeChild[0] = n->SizeChild[1] = 0; }
69  };
70 
71  //--------------------------------------------------------------------
72  struct AddrAccessor
73  {
74  static UPInt GetKey (const FreeNode* n) { return n->Addr; }
75  static FreeNode* GetChild ( FreeNode* n, UPInt i) { return n->AddrChild[i]; }
76  static const FreeNode* GetChild (const FreeNode* n, UPInt i) { return n->AddrChild[i]; }
77  static FreeNode** GetChildPtr( FreeNode* n, UPInt i) { return &n->AddrChild[i]; }
78 
79  static FreeNode* GetParent ( FreeNode* n) { return n->AddrParent; }
80  static const FreeNode* GetParent (const FreeNode* n) { return n->AddrParent; }
81 
82  static void SetParent (FreeNode* n, FreeNode* p) { n->AddrParent = p; }
83  static void SetChild (FreeNode* n, UPInt i, FreeNode* c) { n->AddrChild[i] = c; }
84  static void ClearLinks(FreeNode* n) { n->AddrParent = n->AddrChild[0] = n->AddrChild[1] = 0; }
85  };
86 
87 public:
88  AllocAddr(MemoryHeap* nodeHeap);
89  AllocAddr(MemoryHeap* nodeHeap, UPInt start, UPInt size);
90  ~AllocAddr();
91 
92  // Add/remove segments. The new segments will be used in the common
93  // tree, so that, the allocation may occur in any segment. If the
94  // segments are coalesced they will be merged. Technically it's OK
95  // to remove a part of the segment, in case it is empty. If not, an
96  // assertion will fail in debug mode.
97  void AddSegment(UPInt addr, UPInt size);
98  void RemoveSegment(UPInt addr, UPInt size);
99 
100  // Allocates a block of address space of specified size.
101  // Returns ~UPInt(0) on fail.
102  UPInt Alloc(UPInt size);
103 
104  // Frees an allocation at address. Must pass the original allocated size.
105  // Returns the size of the merged block that becomes available after
106  // free, this will be >= size.
107  UPInt Free(UPInt addr, UPInt size);
108 
109  // Obtain the size of the largest block available for allocation.
110  UPInt GetLargestAvailable()
111  {
112  const FreeNode* node = SizeTree.FindBestLeEq(~UPInt(0));
113  return node ? node->Size : 0;
114  }
115 
116 private:
117  //--------------------------------------------------------------------
118  typedef RadixTreeMulti<FreeNode, SizeAccessor> SizeTreeType;
119  typedef RadixTree <FreeNode, AddrAccessor> AddrTreeType;
120 
121  //--------------------------------------------------------------------
122  void destroyAll();
123  void pushNode(FreeNode* node, UPInt addr, UPInt size);
124  void pullNode(FreeNode* node);
125  FreeNode* pullBest(UPInt size);
126  void splitNode(FreeNode* node, UPInt addr, UPInt size);
127  UPInt mergeNodes(FreeNode* prev, FreeNode* next, UPInt addr, UPInt size);
128 
129 
130  //--------------------------------------------------------------------
131  MemoryHeap* pNodeHeap;
132  SizeTreeType SizeTree;
133  AddrTreeType AddrTree;
134 };
135 
136 } // Scaleform
137 
138 #endif
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17