Optional Two-phase heap tracing
[hiphop-php.git] / hphp / util / read-only-arena.h
blob2c2579af694517187c224aafb26be937cce7c54e
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #ifndef incl_HPHP_READ_ONLY_ARENA_H_
17 #define incl_HPHP_READ_ONLY_ARENA_H_
19 #include <cstdlib>
20 #include <mutex>
21 #include <thread>
23 #include <folly/Range.h>
25 #include "hphp/util/alloc.h"
27 namespace HPHP {
29 //////////////////////////////////////////////////////////////////////
32 * ReadOnlyArena is an arena allocator that can be used for arena-lifetime read
33 * only data. In practice this is used in HPHP for process-lifetime cold
34 * runtime data.
36 * When allocating from this arena, you have to provide the data that's
37 * supposed to go in the block. The allocator will temporarily make the page
38 * writable and put the data in there, then mprotect it back to read only.
40 * One read only arena may safely be concurrently accessed by multiple threads.
42 struct ReadOnlyArena {
44 * All pointers returned from ReadOnlyArena will have at least this
45 * alignment.
47 static constexpr size_t kMinimalAlignment = 8;
50 * Create a ReadOnlyArena that uses at least `minChunkSize' bytes for each
51 * call to the allocator. `minChunkSize' will be rounded up to the nearest
52 * multiple of the system page size (s_pageSize).
54 * Note: s_pageSize is a dynamically initialized static, so do not
55 * create global ReadOnlyArenas.
57 explicit ReadOnlyArena(size_t minChunkSize);
60 * Destroying a ReadOnlyArena will munmap all the chunks it allocated, but
61 * generally ReadOnlyArenas should be used for extremely long-lived data.
63 ~ReadOnlyArena();
65 ReadOnlyArena(const ReadOnlyArena&) = delete;
66 ReadOnlyArena& operator=(const ReadOnlyArena&) = delete;
69 * Returns: the number of bytes we've allocated (from malloc) in this arena.
71 size_t capacity() const;
74 * Returns: a pointer to a read only memory region that contains a copy of
75 * [data, data + dataLen).
77 * Throws: if we fail to allocate memory.
79 const void* allocate(const void* data, size_t dataLen);
81 private:
82 void ensureFree(size_t bytes);
84 private:
85 size_t const m_minChunkSize;
87 mutable std::mutex m_mutex;
88 unsigned char* m_frontier;
89 unsigned char* m_end;
90 std::vector<folly::Range<unsigned char*>> m_chunks;
93 //////////////////////////////////////////////////////////////////////
98 #endif