The Pedigree Project  0.1
MemoryPressureManager.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 MEMORY_PRESSURE_MANAGER_H
21 #define MEMORY_PRESSURE_MANAGER_H
22 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/processor/types.h"
25 #include "pedigree/kernel/utilities/List.h"
26 #include "pedigree/kernel/utilities/String.h"
27 #include "pedigree/kernel/utilities/new"
28 
30 #define MAX_MEMPRESSURE_PRIORITY 16
31 
36 {
37  public:
39  virtual ~MemoryPressureHandler();
40 
41  virtual const String getMemoryPressureDescription() = 0;
42 
48  virtual bool compact() = 0;
49 };
50 
59 {
60  public:
63 
64  const static size_t HighestPriority = 0;
65  const static size_t HighPriority = MAX_MEMPRESSURE_PRIORITY / 3;
66  const static size_t MediumPriority = MAX_MEMPRESSURE_PRIORITY / 2;
67  const static size_t LowPriority = (MAX_MEMPRESSURE_PRIORITY * 2) / 3;
68  const static size_t LowestPriority = MAX_MEMPRESSURE_PRIORITY - 1;
69 
70  static MemoryPressureManager &instance()
71  {
72  return m_Instance;
73  }
74 
75  static size_t getHighWatermark()
76  {
77  // Once the system has only this or less pages free, we begin
78  // doing compacts. We do not want to wait until the system is
79  // actually out of memory, as some compact mechanisms require
80  // allocating memory.
81  return 16;
82  }
83 
84  static size_t getLowWatermark()
85  {
86  // Caches can begin voluntarily evicting cache pages at this mark.
87  // Should no action be taken, the system will take over forcefully
88  // at the high water mark.
89  return 32;
90  }
91 
96  bool compact();
97 
101  void registerHandler(size_t prio, MemoryPressureHandler *pHandler);
102 
106  void removeHandler(MemoryPressureHandler *pHandler);
107 
108  private:
109  static MemoryPressureManager m_Instance;
110 
111  List<MemoryPressureHandler *> m_Handlers[MAX_MEMPRESSURE_PRIORITY];
112 };
113 
114 #endif
Definition: String.h:49