Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: base/memory/shared_memory_allocator.h

Issue 1410213004: Create "persistent memory allocator" for persisting and sharing objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed review comments by Chris Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/atomicops.h"
11 #include "base/base_export.h"
12 #include "base/macros.h"
13
14 namespace base {
15
16 // Simple allocator for pieces of a memory block that may be shared across
17 // multiple processes.
18 //
19 // This class provides for thread-secure (i.e. safe against other threads
20 // or processes that may be compromised and thus have malicious intent)
21 // allocation of memory within a designated block and also a mechanism by
22 // which other threads can learn of the allocations with any additional
23 // shared information.
24 //
25 // There is (currently) no way to release an allocated block of data because
26 // doing so would risk invalidating pointers held by other processes and
27 // greatly complicate the allocation algorithm.
28 //
29 // Construction of this object can accept new, clean (i.e. zeroed) memory
30 // or previously initialized memory. In the first case, construction must
31 // be allowed to complete before letting other allocators attach to the same
32 // segment. In other words, don't share the segment until at least one
33 // allocator has been attached to it.
34 //
35 // It should be noted that memory doesn't need to actually have zeros written
36 // throughout; it just needs to read as zero until something diffferent is
37 // written to a location. This is an important distinction as it supports the
38 // use-case of non-pinned memory, such as from a demand-allocated region by
39 // the OS or a memory-mapped file that auto-grows from a starting size of zero.
40 class BASE_EXPORT SharedMemoryAllocator {
41 public:
42 typedef int32_t Reference;
43
44 // Internal state information when iterating over memory allocations.
45 struct Iterator {
46 Reference last;
47 uint32_t niter;
48 };
49
50 // Returned information about the internal state of the heap.
51 struct MemoryInfo {
52 size_t total;
53 size_t free;
54 };
55
56 enum : uint32_t {
57 kTypeIdAny = 0 // Match any type-id inside GetAsObject().
58 };
59
60 // The allocator operates on any arbitrary block of memory. Creation and
61 // sharing of that block with another process is the responsibility of the
62 // caller. The allocator needs to know only the block's |base| address, the
63 // total |size| of the block, and any internal |page| size (zero if not
64 // paged) across which allocations should not span.
65 //
66 // SharedMemoryAllocator does NOT take ownership of this memory block. The
67 // caller must manage it and ensure it stays available throughout the lifetime
68 // of this object.
69 //
70 // Memory segments for sharing must have had an allocator attached to them
71 // before actually being shared. If the memory segment was just created, it
72 // should be zeroed. If it was an existing segment, the values here will
73 // be compared to copies stored in the shared segment as a guard against
74 // corruption.
75 SharedMemoryAllocator(void* base, size_t size, size_t page_size);
76 ~SharedMemoryAllocator();
77
78 // Get an object referenced by a |ref|. For safety reasons, the |type_id|
79 // code and size-of(|T|) are compared to ensure the reference is valid
80 // and cannot return an object outside of the memory segment. A |type_id| of
81 // zero will match any though the size is still checked. NULL is returned
82 // if any problem is detected, such as corrupted storage or incorrect
83 // parameters. Callers MUST check that the returned value is not-null EVERY
84 // TIME before accessing it or risk crashing! Once dereferenced, the pointer
mdempsky 2015/11/10 19:47:56 I'm a little unclear on these last two sentences.
85 // is safe to reuse forever.
86 //
87 // NOTE: Though this method will guarantee that an object of the specified
88 // type can be accessed without going outside the bounds of the memory
89 // segment, it makes no guarantees of the validity of the data within the
90 // object itself. If it is expected that the contents of the segment could
91 // be compromised with malicious intent, the object must be hardened as well.
92 template <typename T>
93 T* GetAsObject(Reference ref, uint32_t type_id) {
94 return static_cast<T*>(GetBlockData(ref, type_id, sizeof(T)));
95 }
96
97 // Get the number of bytes allocated to a block. This is useful when storing
98 // arrays in order to validate the ending boundary. The returned value will
99 // include any padding added to achieve the required alignment and so could
100 // be larger than given in the original Allocate() request.
101 size_t GetAllocSize(Reference ref);
102
103 // Reserve space in the memory segment of the desired |size| and |type_id|.
104 // A return value of zero indicates the allocation failed, otherwise the
105 // returned reference can be used by any process to get a real pointer via
106 // the GetAsObject() call.
107 int32_t Allocate(size_t size, uint32_t type_id);
108
109 // Allocated objects can be added to an internal list that can then be
110 // iterated over by other processes. If an allocated object can be found
111 // another way, such as by having its reference within a different object
112 // that will be made iterable, then this call is not necessary. This always
113 // succeeds unless corruption is detected; check IsCorrupted() to find out.
114 void MakeIterable(Reference ref);
115
116 // Get the information about the amount of free space in the allocator. The
117 // amount of free space should be treated as approximate due to extras from
118 // alignment and metadata. Concurrent allocations from other threads will
119 // also make the true amount less than what is reported.
120 void GetMemoryInfo(MemoryInfo* meminfo);
121
122 // Iterating uses a |state| structure (initialized by CreateIterator) and
mdempsky 2015/11/10 19:47:56 nit: I think "uses an |Iterator| structure" is mea
123 // returns both the reference to the object as well as the |type_id| of
124 // that object. A zero return value indicates there are currently no more
125 // objects to be found but future attempts can be made without having to
126 // reset the iterator to "first".
127 void CreateIterator(Iterator* state);
128 int32_t GetNextIterable(Iterator* state, uint32_t* type_id);
mdempsky 2015/11/10 19:47:55 Should the return type be Reference?
129
130 // If there is some indication that the shared memory has become corrupted,
131 // calling this will attempt to prevent further damage by indicating to
132 // all processes that something is not as expected.
133 void SetCorrupt();
134
135 // This can be called to determine if corruption has been detected in the
136 // shared segment, possibly my a malicious actor. Once detected, future
mdempsky 2015/11/10 19:47:56 typo: s/my/by/
137 // allocations will fail and iteration may not locate all objects.
138 bool IsCorrupt();
139
140 // Flag set if an allocation has failed because memory was full.
141 bool IsFull();
142
143 private:
144 struct SharedMetadata;
145 struct BlockHeader;
146
147 // The shared metadata is always located at the top of the shared memory.
148 // This convenience function eliminates constant casting of the base pointer
mdempsky 2015/11/10 19:47:56 nit: Say "repeated casting" since "constant castin
149 // within the code.
150 SharedMetadata* shared_meta() {
151 return reinterpret_cast<SharedMetadata*>(mem_base_);
152 }
153
154 BlockHeader* GetBlock(Reference ref, uint32_t type_id, int32_t size,
155 bool queue_ok, bool free_ok);
156 void* GetBlockData(Reference ref, uint32_t type_id, int32_t size);
157
158 char* mem_base_; // Same. (char because sizeof guaranteed 1)
mdempsky 2015/11/10 19:47:56 Same as what?
159 int32_t mem_size_; // Size of entire memory segment.
160 int32_t mem_page_; // Page size allocations shouldn't cross.
161 subtle::Atomic32 corrupted_; // Local version of "corrupted" flag.
162
163 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAllocator);
164 };
165
166 } // namespace base
167
168 #endif // BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698