The Pedigree Project  0.1
MemoryPressureManager.cc
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 #include "pedigree/kernel/process/MemoryPressureManager.h"
21 #include "pedigree/kernel/Log.h"
22 #include "pedigree/kernel/utilities/Iterator.h"
23 #include "pedigree/kernel/utilities/utility.h"
24 
25 MemoryPressureManager MemoryPressureManager::m_Instance;
26 
27 MemoryPressureHandler::MemoryPressureHandler() = default;
28 MemoryPressureHandler::~MemoryPressureHandler() = default;
29 
31 {
32  for (size_t i = 0; i < MAX_MEMPRESSURE_PRIORITY; ++i)
33  {
34  for (List<MemoryPressureHandler *>::Iterator it = m_Handlers[i].begin();
35  it != m_Handlers[i].end(); ++it)
36  {
37  NOTICE("Compact: " << (*it)->getMemoryPressureDescription());
38  if ((*it)->compact())
39  {
40  NOTICE(" -> pages released!");
41  return true;
42  }
43  NOTICE(" -> no pages released.");
44  }
45  }
46 
47  return false;
48 }
49 
50 MemoryPressureManager::MemoryPressureManager() = default;
51 MemoryPressureManager::~MemoryPressureManager() = default;
52 
54  size_t prio, MemoryPressureHandler *pHandler)
55 {
56  if (prio >= MAX_MEMPRESSURE_PRIORITY)
57  prio = MAX_MEMPRESSURE_PRIORITY - 1;
58 
59  m_Handlers[prio].pushBack(pHandler);
60 }
61 
63 {
64  for (size_t i = 0; i < MAX_MEMPRESSURE_PRIORITY; ++i)
65  {
66  for (List<MemoryPressureHandler *>::Iterator it = m_Handlers[i].begin();
67  it != m_Handlers[i].end();)
68  {
69  if ((*it) == pHandler)
70  {
71  it = m_Handlers[i].erase(it);
72  }
73  else
74  {
75  ++it;
76  }
77  }
78  }
79 }
void pushBack(const T &value)
Definition: List.h:232
Iterator erase(Iterator &Iter)
Definition: List.h:343
Definition: List.h:64
#define NOTICE(text)
Definition: Log.h:74
void removeHandler(MemoryPressureHandler *pHandler)
Iterator end()
Definition: List.h:135
void registerHandler(size_t prio, MemoryPressureHandler *pHandler)