Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 20 | #include <Config.h> |
| 21 | |
Logan Chien | c4ea07f | 2011-03-09 17:27:50 +0800 | [diff] [blame^] | 22 | #include <llvm/Support/Mutex.h> |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 23 | |
Logan | 4dcd679 | 2011-02-28 05:12:00 +0800 | [diff] [blame] | 24 | #include <unistd.h> |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 25 | #include <stddef.h> |
| 26 | |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 27 | |
| 28 | namespace bcc { |
| 29 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 30 | class ContextManager { |
| 31 | public: |
| 32 | // Starting address of context slot address space |
| 33 | static char * const ContextFixedAddr; |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 34 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 35 | // Number of the context slots |
| 36 | static size_t const ContextSlotCount = BCC_CONTEXT_SLOT_COUNT_; |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 37 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 38 | // 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; |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 42 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 43 | private: |
| 44 | // Context manager singleton |
| 45 | static ContextManager TheContextManager; |
| 46 | |
| 47 | private: |
| 48 | // Mutex lock for context slot occupation table |
Logan | 96d250e | 2011-02-28 01:25:26 +0800 | [diff] [blame] | 49 | mutable llvm::sys::Mutex mContextSlotOccupiedLock; |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 50 | |
| 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 | |
Logan | 96d250e | 2011-02-28 01:25:26 +0800 | [diff] [blame] | 65 | bool isManagingContext(char *addr) const; |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 66 | |
| 67 | private: |
| 68 | static ssize_t getSlotIndexFromAddress(char *addr); |
| 69 | |
| 70 | }; |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 71 | |
| 72 | } // namespace bcc |
| 73 | |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 74 | #endif // BCC_CONTEXTMANAGER_H |