OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_ |
| 6 #define BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/base_export.h" |
| 11 #include "base/macros.h" |
| 12 |
| 13 namespace base { |
| 14 |
| 15 // Simple allocator for pieces of a memory block that may be shared across |
| 16 // multiple processes. |
| 17 // |
| 18 // This class provides for thread-safe (even across different processes) |
| 19 // allocation of memory within a designated block and also a mechanism by |
| 20 // which other threads can learn of the allocations with any additional |
| 21 // shared information. |
| 22 // |
| 23 // There is (currently) no way to release an allocated block of data because |
| 24 // doing so would risk invalidating pointers held by other processes and |
| 25 // greatly complicate the allocation algorithm. |
| 26 class BASE_EXPORT SharedMemoryAllocator { |
| 27 public: |
| 28 struct Iterator { |
| 29 int32_t last; |
| 30 }; |
| 31 |
| 32 struct MemoryInfo { |
| 33 int32_t total; |
| 34 int32_t free; |
| 35 }; |
| 36 |
| 37 // The allocator operates on any arbitrary block of memory. Creation and |
| 38 // sharing of that block with another process is the responsibility of the |
| 39 // caller. The allocator needs to know only the block's |base| address, the |
| 40 // total |size| of the block, and any internal |page| size (zero if not |
| 41 // paged) across which allocations should not span. |
| 42 // |
| 43 // SharedMemoryAllocator does NOT take ownership of this memory block. The |
| 44 // caller must manage it and ensure it stays available throughout the lifetime |
| 45 // of this object. |
| 46 SharedMemoryAllocator(void* base, int32_t size, int32_t page); |
| 47 ~SharedMemoryAllocator(); |
| 48 |
| 49 // Get an object referenced by an |offset|. For safety reasons, the |type| |
| 50 // code and size-of |unused| are compared to ensure the reference is valid |
| 51 // and cannot return an object outside of the memory segment. A |type| of |
| 52 // zero will match any though the size is still checked. NULL is returned |
| 53 // if any problem is detected, such as corrupted storage or incorrect |
| 54 // parameters. Callers MUST check that the returned value is not-null EVERY |
| 55 // TIME before accessing it or risk crashing! Once dereferenced, the pointer |
| 56 // is safe to reuse forever. |
| 57 template<typename T> T* GetObject(int32_t offset, int32_t type) { |
| 58 return static_cast<T*>(GetBlockData(offset, type, sizeof(T), false)); |
| 59 } |
| 60 |
| 61 // Reserve space in the memory segment of the desired |size| and |type|. |
| 62 // A return value of zero indicates the allocation failed, otherwise the |
| 63 // returned offset can be used by any process to get a real pointer via |
| 64 // the GetObject() call. |
| 65 int32_t Allocate(int32_t size, int32_t type); |
| 66 |
| 67 // Get the information about the amount of free space in the allocator. The |
| 68 // amount of free space should be treated as approximate due to extras from |
| 69 // alignment and metadata, but will never return less than could actually |
| 70 // be allocated. |
| 71 void GetMemoryInfo(MemoryInfo* meminfo); |
| 72 |
| 73 // Allocated objects can be added to an internal list that can then be |
| 74 // iterated over by other processes. |
| 75 void MakeIterable(int32_t offset); |
| 76 |
| 77 // Iterating uses a |state| structure (initialized by GetFirstIterable) and |
| 78 // returns both the offset reference to the object as well as the |type| of |
| 79 // that object. A zero return value indicates there are currently no more |
| 80 // objects to be found but future attempts can be made without having to |
| 81 // reset the iterator to "first". |
| 82 int32_t GetFirstIterable(Iterator* state, int32_t* type); |
| 83 int32_t GetNextIterable(Iterator* state, int32_t* type); |
| 84 |
| 85 // If there is some indication that the shared memory has become corrupted, |
| 86 // calling this will attempt to prevent further damage by indicating to |
| 87 // all processes that something is not as expected. |
| 88 void SetCorrupted(); |
| 89 bool IsCorrupted(); |
| 90 |
| 91 // Flag set if an allocation has failed because memory was full. |
| 92 bool IsFull(); |
| 93 |
| 94 private: |
| 95 struct SharedMetadata; |
| 96 struct BlockHeader; |
| 97 |
| 98 BlockHeader* GetBlock(int32_t offset, int32_t type, int32_t size, |
| 99 bool special); |
| 100 void* GetBlockData(int32_t offset, int32_t type, int32_t size, bool special); |
| 101 |
| 102 SharedMetadata* shared_meta_; |
| 103 char* mem_base_; // char because sizeof guaranteed 1 -- easy offset calc |
| 104 int32_t mem_size_; |
| 105 int32_t mem_page_; |
| 106 int32_t last_seen_; |
| 107 bool corrupted_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAllocator); |
| 110 }; |
| 111 |
| 112 } // namespace base |
| 113 |
| 114 #endif // BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_ |
OLD | NEW |