1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=99 ft=cpp:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Copyright (C) 2010 Apple Inc. All rights reserved.
6 : *
7 : * Redistribution and use in source and binary forms, with or without
8 : * modification, are permitted provided that the following conditions
9 : * are met:
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * 2. Redistributions in binary form must reproduce the above copyright
13 : * notice, this list of conditions and the following disclaimer in the
14 : * documentation and/or other materials provided with the distribution.
15 : *
16 : * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
17 : * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 : * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 : * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
20 : * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 : * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 : * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 : * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 : * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 : * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 : * THE POSSIBILITY OF SUCH DAMAGE.
27 : *
28 : * ***** END LICENSE BLOCK ***** */
29 :
30 : #ifndef OSAllocator_h
31 : #define OSAllocator_h
32 :
33 : #include <stdlib.h>
34 : #include "wtfbridge.h"
35 : #include "assembler/wtf/VMTags.h"
36 : #include "assembler/wtf/Assertions.h"
37 :
38 : namespace WTF {
39 :
40 : class OSAllocator {
41 : public:
42 : enum Usage {
43 : UnknownUsage = -1,
44 : FastMallocPages = VM_TAG_FOR_TCMALLOC_MEMORY,
45 : JSGCHeapPages = VM_TAG_FOR_COLLECTOR_MEMORY,
46 : JSVMStackPages = VM_TAG_FOR_REGISTERFILE_MEMORY,
47 : JSJITCodePages = VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY
48 : };
49 :
50 : // These methods are symmetric; reserveUncommitted allocates VM in an uncommitted state,
51 : // releaseDecommitted should be called on a region of VM allocated by a single reservation,
52 : // the memory must all currently be in a decommitted state.
53 : static void* reserveUncommitted(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false);
54 : static void releaseDecommitted(void*, size_t);
55 :
56 : // These methods are symmetric; they commit or decommit a region of VM (uncommitted VM should
57 : // never be accessed, since the OS may not have attached physical memory for these regions).
58 : // Clients should only call commit on uncommitted regions and decommit on committed regions.
59 : static void commit(void*, size_t, bool writable, bool executable);
60 : static void decommit(void*, size_t);
61 :
62 : // These methods are symmetric; reserveAndCommit allocates VM in an committed state,
63 : // decommitAndRelease should be called on a region of VM allocated by a single reservation,
64 : // the memory must all currently be in a committed state.
65 : static void* reserveAndCommit(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false);
66 : static void decommitAndRelease(void* base, size_t size);
67 :
68 : // These methods are akin to reserveAndCommit/decommitAndRelease, above - however rather than
69 : // committing/decommitting the entire region additional parameters allow a subregion to be
70 : // specified.
71 : static void* reserveAndCommit(size_t reserveSize, size_t commitSize, Usage = UnknownUsage, bool writable = true, bool executable = false);
72 : static void decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize);
73 : };
74 :
75 : inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable)
76 : {
77 : void* base = reserveUncommitted(reserveSize, usage, writable, executable);
78 : commit(base, commitSize, writable, executable);
79 : return base;
80 : }
81 :
82 31359 : inline void OSAllocator::decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize)
83 : {
84 31359 : ASSERT(decommitBase >= releaseBase && (static_cast<char*>(decommitBase) + decommitSize) <= (static_cast<char*>(releaseBase) + releaseSize));
85 : #if WTF_OS_WINCE || WTF_OS_SYMBIAN
86 : // On most platforms we can actually skip this final decommit; releasing the VM will
87 : // implicitly decommit any physical memory in the region. This is not true on WINCE.
88 : // On Symbian, this makes implementation simpler and better aligned with the RChunk API
89 : decommit(decommitBase, decommitSize);
90 : #endif
91 31359 : releaseDecommitted(releaseBase, releaseSize);
92 31359 : }
93 :
94 31359 : inline void OSAllocator::decommitAndRelease(void* base, size_t size)
95 : {
96 31359 : decommitAndRelease(base, size, base, size);
97 31359 : }
98 :
99 : } // namespace WTF
100 :
101 : using WTF::OSAllocator;
102 :
103 : #endif // OSAllocator_h
|