blob: f23c4a18e06bcb474e6f315a9f5c4d24fff32700 [file] [log] [blame]
Loganeb3d12b2010-12-16 06:20:18 +08001/*
2 * copyright 2010, the android open source project
3 *
4 * licensed under the apache license, version 2.0 (the "license");
5 * you may not use this file except in compliance with the license.
6 * you may obtain a copy of the license at
7 *
8 * http://www.apache.org/licenses/license-2.0
9 *
10 * unless required by applicable law or agreed to in writing, software
11 * distributed under the license is distributed on an "as is" basis,
12 * without warranties or conditions of any kind, either express or implied.
13 * see the license for the specific language governing permissions and
14 * limitations under the license.
15 */
16
17#ifndef BCC_CONTEXTMANAGER_H
18#define BCC_CONTEXTMANAGER_H
19
Logan1dc63142011-02-25 17:14:51 +080020#include <Config.h>
21
Logan Chienc4ea07f2011-03-09 17:27:50 +080022#include <llvm/Support/Mutex.h>
Logan1dc63142011-02-25 17:14:51 +080023
Logan4dcd6792011-02-28 05:12:00 +080024#include <unistd.h>
Loganeb3d12b2010-12-16 06:20:18 +080025#include <stddef.h>
26
Loganeb3d12b2010-12-16 06:20:18 +080027
28namespace bcc {
29
Logan1dc63142011-02-25 17:14:51 +080030 class ContextManager {
31 public:
32 // Starting address of context slot address space
33 static char * const ContextFixedAddr;
Logan42598052011-01-26 22:41:13 +080034
Logan1dc63142011-02-25 17:14:51 +080035 // Number of the context slots
36 static size_t const ContextSlotCount = BCC_CONTEXT_SLOT_COUNT_;
Loganeb3d12b2010-12-16 06:20:18 +080037
Logan1dc63142011-02-25 17:14:51 +080038 // Context size
39 static size_t const ContextCodeSize = BCC_CONTEXT_CODE_SIZE_;
40 static size_t const ContextDataSize = BCC_CONTEXT_DATA_SIZE_;
41 static size_t const ContextSize = ContextCodeSize + ContextDataSize;
Loganeb3d12b2010-12-16 06:20:18 +080042
Logan1dc63142011-02-25 17:14:51 +080043 private:
44 // Context manager singleton
45 static ContextManager TheContextManager;
46
47 private:
48 // Mutex lock for context slot occupation table
Logan96d250e2011-02-28 01:25:26 +080049 mutable llvm::sys::Mutex mContextSlotOccupiedLock;
Logan1dc63142011-02-25 17:14:51 +080050
51 // Context slot occupation table
52 bool mContextSlotOccupied[ContextSlotCount];
53
54 ContextManager();
55
56 public:
57 static ContextManager &get() {
58 return TheContextManager;
59 }
60
61 char *allocateContext();
62 char *allocateContext(char *addr, int imageFd, off_t imageOffset);
63 void deallocateContext(char *addr);
64
Logan96d250e2011-02-28 01:25:26 +080065 bool isManagingContext(char *addr) const;
Logan1dc63142011-02-25 17:14:51 +080066
67 private:
68 static ssize_t getSlotIndexFromAddress(char *addr);
69
70 };
Loganeb3d12b2010-12-16 06:20:18 +080071
72} // namespace bcc
73
Loganeb3d12b2010-12-16 06:20:18 +080074#endif // BCC_CONTEXTMANAGER_H