Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / PageAllocator.h
blobc2a771eedfc65479f69d1b5cb99fb1ee807bfb21
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef WTF_PageAllocator_h
32 #define WTF_PageAllocator_h
34 #include "wtf/Assertions.h"
35 #include "wtf/CPU.h"
36 #include "wtf/WTFExport.h"
37 #include <stdint.h>
39 namespace WTF {
41 #if OS(WIN)
42 static const size_t kPageAllocationGranularityShift = 16; // 64KB
43 #else
44 static const size_t kPageAllocationGranularityShift = 12; // 4KB
45 #endif
46 static const size_t kPageAllocationGranularity = 1 << kPageAllocationGranularityShift;
47 static const size_t kPageAllocationGranularityOffsetMask = kPageAllocationGranularity - 1;
48 static const size_t kPageAllocationGranularityBaseMask = ~kPageAllocationGranularityOffsetMask;
50 // All Blink-supported systems have 4096 sized system pages and can handle
51 // permissions and commit / decommit at this granularity.
52 static const size_t kSystemPageSize = 4096;
53 static const size_t kSystemPageOffsetMask = kSystemPageSize - 1;
54 static const size_t kSystemPageBaseMask = ~kSystemPageOffsetMask;
56 enum PageAccessibilityConfiguration {
57 PageAccessible,
58 PageInaccessible,
61 // Allocate one or more pages.
62 // The requested address is just a hint; the actual address returned may
63 // differ. The returned address will be aligned at least to align bytes.
64 // len is in bytes, and must be a multiple of kPageAllocationGranularity.
65 // align is in bytes, and must be a power-of-two multiple of
66 // kPageAllocationGranularity.
67 // If addr is null, then a suitable and randomized address will be chosen
68 // automatically.
69 // PageAccessibilityConfiguration controls the permission of the
70 // allocated pages.
71 // This call will return null if the allocation cannot be satisfied.
72 WTF_EXPORT void* allocPages(void* addr, size_t len, size_t align, PageAccessibilityConfiguration);
74 // Free one or more pages.
75 // addr and len must match a previous call to allocPages().
76 WTF_EXPORT void freePages(void* addr, size_t len);
78 // Mark one or more system pages as being inaccessible.
79 // Subsequently accessing any address in the range will fault, and the
80 // addresses will not be re-used by future allocations.
81 // len must be a multiple of kSystemPageSize bytes.
82 WTF_EXPORT void setSystemPagesInaccessible(void* addr, size_t len);
84 // Mark one or more system pages as being accessible.
85 // The pages will be readable and writeable.
86 // len must be a multiple of kSystemPageSize bytes.
87 // The result bool value indicates whether the permission
88 // change succeeded or not. You must check the result
89 // (in most cases you need to RELEASE_ASSERT that it is
90 // true).
91 WTF_EXPORT WARN_UNUSED_RETURN bool setSystemPagesAccessible(void* addr, size_t len);
93 // Decommit one or more system pages. Decommitted means that the physical memory
94 // is released to the system, but the virtual address space remains reserved.
95 // System pages are re-committed by calling recommitSystemPages(). Touching
96 // a decommitted page _may_ fault.
97 // Clients should not make any assumptions about the contents of decommitted
98 // system pages, before or after they write to the page. The only guarantee
99 // provided is that the contents of the system page will be deterministic again
100 // after recommitting and writing to it. In particlar note that system pages are
101 // not guaranteed to be zero-filled upon re-commit. len must be a multiple of
102 // kSystemPageSize bytes.
103 WTF_EXPORT void decommitSystemPages(void* addr, size_t len);
105 // Recommit one or more system pages. Decommitted system pages must be
106 // recommitted before they are read are written again.
107 // Note that this operation may be a no-op on some platforms.
108 // len must be a multiple of kSystemPageSize bytes.
109 WTF_EXPORT void recommitSystemPages(void* addr, size_t len);
111 // Discard one or more system pages. Discarding is a hint to the system that
112 // the page is no longer required. The hint may:
113 // - Do nothing.
114 // - Discard the page immediately, freeing up physical pages.
115 // - Discard the page at some time in the future in response to memory pressure.
116 // Only committed pages should be discarded. Discarding a page does not
117 // decommit it, and it is valid to discard an already-discarded page.
118 // A read or write to a discarded page will not fault.
119 // Reading from a discarded page may return the original page content, or a
120 // page full of zeroes.
121 // Writing to a discarded page is the only guaranteed way to tell the system
122 // that the page is required again. Once written to, the content of the page is
123 // guaranteed stable once more. After being written to, the page content may be
124 // based on the original page content, or a page of zeroes.
125 // len must be a multiple of kSystemPageSize bytes.
126 WTF_EXPORT void discardSystemPages(void* addr, size_t len);
128 } // namespace WTF
130 #endif // WTF_PageAllocator_h