LiquidFun
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
b2TrackedBlock.h
1 /*
2 * Copyright (c) 2014 Google, Inc.
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 #ifndef B2_TRACKED_BLOCK_H
19 #define B2_TRACKED_BLOCK_H
20 
21 #include <Box2D/Common/b2IntrusiveList.h>
23 
25 const int32 b2_mallocAlignment = 32;
26 
28 class b2TrackedBlock : public b2TypedIntrusiveListNode<b2TrackedBlock>
29 {
30 private:
31  // Initialize this block with a reference to "this".
33  // Remove the block from the list.
34  ~b2TrackedBlock() { }
35 
36 public:
38  void* GetMemory() const;
39 
40 private:
41  // Padding required to align the pointer to user memory in the block
42  // to b2_mallocAlignment.
43  uint8 m_padding[b2_mallocAlignment + sizeof(b2TrackedBlock**)];
44 
45 public:
48  static void* Allocate(uint32 size);
49 
52  static b2TrackedBlock* GetFromMemory(void *memory);
53 
55  static void Free(void *memory);
56 
58  static void Free(b2TrackedBlock *block);
59 };
60 
63 {
64 public:
69  {
70  FreeAll();
71  }
72 
74  void* Allocate(uint32 size);
75 
77  void Free(void *memory);
78 
80  void FreeAll();
81 
82  // Get the list of allocated blocks.
83  const b2TypedIntrusiveListNode<b2TrackedBlock>& GetList() const
84  {
85  return m_blocks;
86  }
87 
88 private:
90 };
91 
92 #endif // B2_TRACKED_BLOCK_H
void * Allocate(uint32 size)
Allocate a block of size bytes using b2TrackedBlock::Allocate().
Definition: b2TrackedBlock.cpp:91
void FreeAll()
Free all allocated blocks.
Definition: b2TrackedBlock.cpp:105
b2TrackedBlockAllocator()
Initialize.
Definition: b2TrackedBlock.h:66
static void * Allocate(uint32 size)
Definition: b2TrackedBlock.cpp:53
Definition: b2IntrusiveList.h:272
void * GetMemory() const
Get the allocated memory associated with this block.
Definition: b2TrackedBlock.cpp:33
static b2TrackedBlock * GetFromMemory(void *memory)
Definition: b2TrackedBlock.cpp:66
Allocated block of memory that can be tracked in a b2IntrusiveList.
Definition: b2TrackedBlock.h:28
~b2TrackedBlockAllocator()
Free all allocated blocks.
Definition: b2TrackedBlock.h:68
static void Free(void *memory)
Free a block of memory returned by b2TrackedBlock::Allocate()
Definition: b2TrackedBlock.cpp:77
Allocator of blocks which are tracked in a list.
Definition: b2TrackedBlock.h:62
void Free(void *memory)
Free a block returned by Allocate().
Definition: b2TrackedBlock.cpp:99