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#include "pedigree/kernel/compiler.h"
23#include "pedigree/kernel/processor/types.h"
24
25#include <config.h>
26#if THREADS
27#include "pedigree/kernel/process/ConditionVariable.h"
28#include "pedigree/kernel/process/Mutex.h"
29#endif
30#include "pedigree/kernel/process/MemoryPressureManager.h"
31#include "pedigree/kernel/process/TerminationDeferral.h"
32#include "pedigree/kernel/processor/MemoryRegion.h"
33#include "pedigree/kernel/utilities/ExtensibleBitmap.h"
34#include "pedigree/kernel/utilities/String.h"
35
36class MemoryPool;
37
44 public:
47
48 virtual const char* getMemoryPressureDescription();
49
50 virtual bool compact();
51
52 private:
53 MemoryPool* m_Pool;
54};
55
60class EXPORTED_PUBLIC MemoryPool {
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 return m_bInitialised;
75 }
76
79 uintptr_t allocate();
80
84 uintptr_t allocateNow();
85
87 void free(uintptr_t buffer);
88
90 bool trim();
91
92#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
94 void acquireHostedOperationLock();
95 void releaseHostedOperationLock();
96 void acquireHostedMappingLock();
97 void releaseHostedMappingLock();
98 size_t getHostedActiveOperationCount();
99#endif
100
101 private:
102#if THREADS
103 ConditionVariable m_Condition;
104 ConditionVariable m_DrainCondition;
105 Mutex m_Lock;
106 Mutex m_MappingLock;
107#endif
108
110 public:
111 explicit ActiveOperation(MemoryPool& pool);
113
114 explicit operator bool() const {
115 return m_Pool != nullptr;
116 }
117
118 private:
119 ActiveOperation(const ActiveOperation&) = delete;
120 ActiveOperation& operator=(const ActiveOperation&) = delete;
121
122 TerminationDeferral m_TerminationDeferral;
123 MemoryPool* m_Pool;
124 };
125
126 bool beginOperation();
127 void endOperation();
128
131
134
135#ifndef STANDALONE_MEMPOOL
138#endif
139
142
145
148
151
152#ifndef STANDALONE_MEMPOOL
155#endif
156
158 uintptr_t allocateDoer(bool canBlock);
159};
160
161#endif
virtual const char * getMemoryPressureDescription()
Definition MemoryPool.cc:60
size_t m_BufferSize
Size of each buffer in this pool.
Definition MemoryPool.h:130
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:141
ExtensibleBitmap m_AllocBitmap
Allocation bitmap.
Definition MemoryPool.h:150
size_t m_ActiveOperations
Operations which entered before destruction began.
Definition MemoryPool.h:147
bool m_bClosing
Destruction has begun; no new operations may enter.
Definition MemoryPool.h:144
size_t m_BufferCount
Number of buffers we have available.
Definition MemoryPool.h:133
MemoryRegion m_Pool
MemoryRegion describing the actual pool of memory.
Definition MemoryPool.h:137
MemoryPoolPressureHandler m_PressureHandler
Memory pressure handler for this pool.
Definition MemoryPool.h:154
Special memory entity in the kernel's virtual address space.
Definition Mutex.h:56