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

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: 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 "base/base_export.h"
9 #include "base/basictypes.h"
10
11 namespace base {
12
13 // Simple allocator for pieces of a memory block that may be shared across
14 // multiple processes.
chrisha 2015/10/29 21:05:14 More detailed comments about the thread safety gua
bcwhite 2015/10/29 23:40:58 Done.
15 class BASE_EXPORT SharedMemoryAllocator {
16 public:
17 struct Iterator {
18 int32 last;
19 };
20
21 struct MemoryInfo {
22 int32 total;
23 int32 free;
24 };
25
26 // The allocator operates on any arbitrary block of memory. Creation and
27 // sharing of that block with another process is the responsibility of the
28 // caller. The allocator needs to know only the block's |base| address, the
chrisha 2015/10/29 21:05:14 micronit: The more common pattern in Chrome is a s
bcwhite 2015/10/29 23:40:58 I can't. I'm sorry. <hanging head in shame> Bla
chrisha 2015/10/30 14:36:46 http://www.slate.com/articles/technology/technolog
bcwhite 2015/10/30 15:15:35 Heh! Yeah, I've read that. It even makes sense.
29 // total |size| of the block, and any internal |page| size (zero if not
30 // paged) across which allocations should not span.
31 //
32 // SharedMemoryAllocator does NOT take ownership of this memory block. The
33 // caller must manage it and ensure it stays available throughout the lifetime
34 // of this object.
35 SharedMemoryAllocator(void* base, int32 size, int32 page);
chrisha 2015/10/29 21:05:14 (We now use int32_t instead of int32.) Shouldn't
bcwhite 2015/10/29 23:40:57 Awww.... shoot.
chrisha 2015/10/30 14:36:46 Okay, sounds like a reasonable reason to me. Worth
bcwhite 2015/10/30 15:15:35 Done.
36 ~SharedMemoryAllocator();
37
38 // Get an object referenced by an |offset|. For safety reasons, the |type|
39 // code and size-of |unused| are compared to ensure the reference is valid
40 // and cannot return an object outside of the memory segment. A |type| of
41 // zero will match any though the size is still checked. NULL is returned
42 // if any problem is detected, such as corrupted storage or incorrect
43 // parameters. Callers MUST check that the returned value is not-null EVERY
44 // TIME before accessing it or risk crashing! Once dereferenced, the pointer
45 // is safe to reuse forever.
46 template<typename T> T* GetObject(int32 offset, int32 type, T* unused) {
chrisha 2015/10/29 21:05:14 Why pass a T* unused instead of just getting havin
bcwhite 2015/10/29 23:40:58 That works. Done.
chrisha 2015/10/30 14:36:46 The offset isn't sufficient in this case, because
bcwhite 2015/10/30 15:15:35 Acknowledged.
47 return static_cast<T*>(GetBlockData(offset, type, sizeof(T), false));
48 }
49
50 // Reserve space in the memory segment of the desired |size| and |type|.
51 // A return value of zero indicates the allocation failed, otherwise the
52 // returned offset can be used by any process to get a real pointer via
53 // the GetObject() call.
54 int32 Allocate(int32 size, int32 type);
55
56 // Get the information about the amount of free space in the allocator. The
57 // amount of free space should be treated as approximate due to extras from
58 // alignment and meta-data, but will never return less than could actually
59 // be allocated.
60 void GetMemoryInfo(MemoryInfo* meminfo);
61
62 // Allocated objects can be added to an internal list that can then be
63 // iterated over by other processes.
64 void MakeIterable(int32 offset);
chrisha 2015/10/29 21:05:14 Why do you need this? The implementation already h
bcwhite 2015/10/29 23:40:58 Unfortunately, it doesn't. If you try to make it
chrisha 2015/10/30 14:36:46 Still implementation details. This should be entir
bcwhite 2015/10/30 15:15:35 Acknowledged.
65
66 // Iterating uses a |state| structure (initialized by GetFirstIterable) and
67 // returns both the offset reference to the object as well as the |type| of
68 // that object. A zero return value indicates there are currently no more
69 // objects to be found but future attempts can be made without having to
70 // reset the iterator to "first".
71 int32 GetFirstIterable(Iterator* state, int32* type);
72 int32 GetNextIterable(Iterator* state, int32* type);
73
74 // If there is some indication that the shared memory has become corrupted,
75 // calling this will attempt to prevent further damage by indicating to
76 // all processes that something is not as expected.
77 void SetCorrupted();
78 bool IsCorrupted();
79
80 // Flag set if an allocation has failed because memory was full.
81 bool IsFull();
82
83 private:
84 struct SharedMetaData;
chrisha 2015/10/29 21:05:14 Metadata is a single word, no need to capitalize D
bcwhite 2015/10/29 23:40:58 Done.
85 struct BlockHeader;
86
87 BlockHeader* GetBlock(int32 offset, int32 type, int32 size, bool special);
88 void* GetBlockData(int32 offset, int32 type, int32 size, bool special);
89
90 SharedMetaData* shared_meta_;
91 char* mem_base_; // char because sizeof guaranteed 1 -- easy offset calc
92 int32 mem_size_;
93 int32 mem_page_;
chrisha 2015/10/29 21:05:14 Do we really care about page sizes? What's the har
bcwhite 2015/10/29 23:40:58 Probably not. However, if this were to be used ag
chrisha 2015/10/30 14:36:46 Okay, I'm fine with that.
94 int32 last_seen_;
95 bool corrupted_;
96
97 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAllocator);
98 };
99 } // namespace base
100
101 #endif // BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698