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

Unified 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, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: base/memory/shared_memory_allocator.h
diff --git a/base/memory/shared_memory_allocator.h b/base/memory/shared_memory_allocator.h
new file mode 100644
index 0000000000000000000000000000000000000000..74ad9eb742d7f37634634479acb1f5a188d8434d
--- /dev/null
+++ b/base/memory/shared_memory_allocator.h
@@ -0,0 +1,101 @@
+// Copyright (c) 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_
+#define BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_
+
+#include "base/base_export.h"
+#include "base/basictypes.h"
+
+namespace base {
+
+// Simple allocator for pieces of a memory block that may be shared across
+// 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.
+class BASE_EXPORT SharedMemoryAllocator {
+ public:
+ struct Iterator {
+ int32 last;
+ };
+
+ struct MemoryInfo {
+ int32 total;
+ int32 free;
+ };
+
+ // The allocator operates on any arbitrary block of memory. Creation and
+ // sharing of that block with another process is the responsibility of the
+ // 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.
+ // total |size| of the block, and any internal |page| size (zero if not
+ // paged) across which allocations should not span.
+ //
+ // SharedMemoryAllocator does NOT take ownership of this memory block. The
+ // caller must manage it and ensure it stays available throughout the lifetime
+ // of this object.
+ 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.
+ ~SharedMemoryAllocator();
+
+ // Get an object referenced by an |offset|. For safety reasons, the |type|
+ // code and size-of |unused| are compared to ensure the reference is valid
+ // and cannot return an object outside of the memory segment. A |type| of
+ // zero will match any though the size is still checked. NULL is returned
+ // if any problem is detected, such as corrupted storage or incorrect
+ // parameters. Callers MUST check that the returned value is not-null EVERY
+ // TIME before accessing it or risk crashing! Once dereferenced, the pointer
+ // is safe to reuse forever.
+ 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.
+ return static_cast<T*>(GetBlockData(offset, type, sizeof(T), false));
+ }
+
+ // Reserve space in the memory segment of the desired |size| and |type|.
+ // A return value of zero indicates the allocation failed, otherwise the
+ // returned offset can be used by any process to get a real pointer via
+ // the GetObject() call.
+ int32 Allocate(int32 size, int32 type);
+
+ // Get the information about the amount of free space in the allocator. The
+ // amount of free space should be treated as approximate due to extras from
+ // alignment and meta-data, but will never return less than could actually
+ // be allocated.
+ void GetMemoryInfo(MemoryInfo* meminfo);
+
+ // Allocated objects can be added to an internal list that can then be
+ // iterated over by other processes.
+ 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.
+
+ // Iterating uses a |state| structure (initialized by GetFirstIterable) and
+ // returns both the offset reference to the object as well as the |type| of
+ // that object. A zero return value indicates there are currently no more
+ // objects to be found but future attempts can be made without having to
+ // reset the iterator to "first".
+ int32 GetFirstIterable(Iterator* state, int32* type);
+ int32 GetNextIterable(Iterator* state, int32* type);
+
+ // If there is some indication that the shared memory has become corrupted,
+ // calling this will attempt to prevent further damage by indicating to
+ // all processes that something is not as expected.
+ void SetCorrupted();
+ bool IsCorrupted();
+
+ // Flag set if an allocation has failed because memory was full.
+ bool IsFull();
+
+ private:
+ 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.
+ struct BlockHeader;
+
+ BlockHeader* GetBlock(int32 offset, int32 type, int32 size, bool special);
+ void* GetBlockData(int32 offset, int32 type, int32 size, bool special);
+
+ SharedMetaData* shared_meta_;
+ char* mem_base_; // char because sizeof guaranteed 1 -- easy offset calc
+ int32 mem_size_;
+ 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.
+ int32 last_seen_;
+ bool corrupted_;
+
+ DISALLOW_COPY_AND_ASSIGN(SharedMemoryAllocator);
+};
+} // namespace base
+
+#endif // BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_

Powered by Google App Engine
This is Rietveld 408576698