The Pedigree Project  0.1
MemoryPool.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef _UTILITY_MEMORY_POOL
21 #define _UTILITY_MEMORY_POOL
22 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/processor/types.h"
25 #ifdef THREADS
26 #include "pedigree/kernel/process/ConditionVariable.h"
27 #include "pedigree/kernel/process/Mutex.h"
28 #endif
29 #include "pedigree/kernel/process/MemoryPressureManager.h"
30 #include "pedigree/kernel/processor/MemoryRegion.h"
31 #include "pedigree/kernel/utilities/ExtensibleBitmap.h"
32 #include "pedigree/kernel/utilities/String.h"
33 
34 class MemoryPool;
35 
42 {
43  public:
45  virtual ~MemoryPoolPressureHandler();
46 
47  virtual const String getMemoryPressureDescription();
48 
49  virtual bool compact();
50 
51  private:
52  MemoryPool *m_Pool;
53 };
54 
60 {
61  public:
62  MemoryPool();
63  MemoryPool(const char *poolName);
64  virtual ~MemoryPool();
65 
70  bool initialise(size_t poolSize, size_t bufferSize = 1024);
71 
73  inline bool initialised()
74  {
75  return m_bInitialised;
76  }
77 
80  uintptr_t allocate();
81 
85  uintptr_t allocateNow();
86 
88  void free(uintptr_t buffer);
89 
91  bool trim();
92 
93  private:
94 #ifdef THREADS
95  ConditionVariable m_Condition;
96  Mutex m_Lock;
97 #endif
98 
100  size_t m_BufferSize;
101 
104 
105 #ifndef STANDALONE_MEMPOOL
106  MemoryRegion m_Pool;
108 #endif
109 
112 
115 
116 #ifndef STANDALONE_MEMPOOL
117  MemoryPoolPressureHandler m_PressureHandler;
119 #endif
120 
122  uintptr_t allocateDoer(bool canBlock);
123 };
124 
125 #endif
Definition: Mutex.h:58
ExtensibleBitmap m_AllocBitmap
Allocation bitmap.
Definition: MemoryPool.h:114
Definition: String.h:49
bool initialised()
Call if you aren't certain that the object has been initialised yet.
Definition: MemoryPool.h:73
bool m_bInitialised
Has this instance been initialised yet?
Definition: MemoryPool.h:111
virtual bool compact()
Definition: MemoryPool.cc:94
Special memory entity in the kernel's virtual address space.
Definition: MemoryRegion.h:35
size_t m_BufferCount
Number of buffers we have available.
Definition: MemoryPool.h:103
size_t m_BufferSize
Size of each buffer in this pool.
Definition: MemoryPool.h:100