Merge "Fix build break by removing TIZEN_RECORDING_SURFACE_SET" into tizen_2.1
[framework/web/webkit-efl.git] / Source / WTF / wtf / OSAllocator.h
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef OSAllocator_h
27 #define OSAllocator_h
28
29 #include <algorithm>
30 #include <wtf/UnusedParam.h>
31 #include <wtf/VMTags.h>
32
33 namespace WTF {
34
35 class OSAllocator {
36 public:
37     enum Usage {
38         UnknownUsage = -1,
39         FastMallocPages = VM_TAG_FOR_TCMALLOC_MEMORY,
40         JSGCHeapPages = VM_TAG_FOR_COLLECTOR_MEMORY,
41         JSVMStackPages = VM_TAG_FOR_REGISTERFILE_MEMORY,
42         JSJITCodePages = VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY,
43     };
44
45     // These methods are symmetric; reserveUncommitted allocates VM in an uncommitted state,
46     // releaseDecommitted should be called on a region of VM allocated by a single reservation,
47     // the memory must all currently be in a decommitted state.
48     static void* reserveUncommitted(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false, bool includesGuardPages = false);
49     WTF_EXPORT_PRIVATE static void releaseDecommitted(void*, size_t);
50
51     // These methods are symmetric; they commit or decommit a region of VM (uncommitted VM should
52     // never be accessed, since the OS may not have attached physical memory for these regions).
53     // Clients should only call commit on uncommitted regions and decommit on committed regions.
54     static void commit(void*, size_t, bool writable, bool executable);
55     static void decommit(void*, size_t);
56
57     // These methods are symmetric; reserveAndCommit allocates VM in an committed state,
58     // decommitAndRelease should be called on a region of VM allocated by a single reservation,
59     // the memory must all currently be in a committed state.
60     WTF_EXPORT_PRIVATE static void* reserveAndCommit(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false, bool includesGuardPages = false);
61     static void decommitAndRelease(void* base, size_t size);
62
63     // These methods are akin to reserveAndCommit/decommitAndRelease, above - however rather than
64     // committing/decommitting the entire region additional parameters allow a subregion to be
65     // specified.
66     static void* reserveAndCommit(size_t reserveSize, size_t commitSize, Usage = UnknownUsage, bool writable = true, bool executable = false);
67     static void decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize);
68
69     // Reallocate an existing, committed allocation.
70     // The prior allocation must be fully comitted, and the new size will also be fully committed.
71     // This interface is provided since it may be possible to optimize this operation on some platforms.
72     template<typename T>
73     static T* reallocateCommitted(T*, size_t oldSize, size_t newSize, Usage = UnknownUsage, bool writable = true, bool executable = false);
74 };
75
76 inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable)
77 {
78     void* base = reserveUncommitted(reserveSize, usage, writable, executable);
79     commit(base, commitSize, writable, executable);
80     return base;
81 }
82
83 inline void OSAllocator::decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize)
84 {
85     ASSERT(decommitBase >= releaseBase && (static_cast<char*>(decommitBase) + decommitSize) <= (static_cast<char*>(releaseBase) + releaseSize));
86 #if OS(WINCE)
87     // On most platforms we can actually skip this final decommit; releasing the VM will
88     // implicitly decommit any physical memory in the region. This is not true on WINCE.
89     decommit(decommitBase, decommitSize);
90 #else
91     UNUSED_PARAM(decommitBase);
92     UNUSED_PARAM(decommitSize);
93 #endif
94     releaseDecommitted(releaseBase, releaseSize);
95 }
96
97 inline void OSAllocator::decommitAndRelease(void* base, size_t size)
98 {
99     decommitAndRelease(base, size, base, size);
100 }
101
102 template<typename T>
103 inline T* OSAllocator::reallocateCommitted(T* oldBase, size_t oldSize, size_t newSize, Usage usage, bool writable, bool executable)
104 {
105     void* newBase = reserveAndCommit(newSize, usage, writable, executable);
106     memcpy(newBase, oldBase, std::min(oldSize, newSize));
107     decommitAndRelease(oldBase, oldSize);
108     return static_cast<T*>(newBase);
109 }
110
111 } // namespace WTF
112
113 using WTF::OSAllocator;
114
115 #endif // OSAllocator_h