summaryrefslogtreecommitdiffstats
path: root/JavaScriptCore/wtf/PageReservation.h
blob: cfc7cd9b2d0c56da044b292ad7f9347d0a7538af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright (C) 2010 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#ifndef PageReservation_h
#define PageReservation_h

#include <wtf/PageAllocation.h>

namespace WTF {

/*
    PageReservation

    Like PageAllocation, the PageReservation class provides a cross-platform memory
    allocation interface, but with a set of capabilities more similar to that of
    VirtualAlloc than posix mmap.  PageReservation can be used to allocate virtual
    memory without committing physical memory pages using PageReservation::reserve.
    Following a call to reserve all memory in the region is in a decommited state,
    in which the memory should not be used (accessing the memory may cause a fault).

    Before using memory it must be committed by calling commit, which is passed start
    and size values (both of which require system page size granularity).  One the
    committed memory is no longer needed 'decommit' may be called to return the
    memory to its devommitted state.  Commit should only be called on memory that is
    currently decommitted, and decommit should only be called on memory regions that
    are currently committed.  All memory should be decommited before the reservation
    is deallocated.  Values in memory may not be retained accross a pair of calls if
    the region of memory is decommitted and then committed again.

    Where HAVE(PAGE_ALLOCATE_AT) is available a PageReservation::reserveAt method
    also exists, with behaviour mirroring PageAllocation::allocateAt.

    Memory protection should not be changed on decommitted memory, and if protection
    is changed on memory while it is committed it should be returned to the orignal
    protection before decommit is called.

    Note: Inherits from PageAllocation privately to prevent clients accidentally
    calling PageAllocation::deallocate on a PageReservation.
*/
class PageReservation : private PageAllocation {
public:
    PageReservation()
    {
    }

    using PageAllocation::operator!;
    using PageAllocation::base;
    using PageAllocation::size;

    bool commit(void* start, size_t size)
    {
        ASSERT(m_base);
        ASSERT(isPageAligned(start));
        ASSERT(isPageAligned(size));

        bool commited = systemCommit(start, size);
#ifndef NDEBUG
        if (commited)
            m_committed += size;
#endif
        return commited;
    }
    void decommit(void* start, size_t size)
    {
        ASSERT(m_base);
        ASSERT(isPageAligned(start));
        ASSERT(isPageAligned(size));

#ifndef NDEBUG
        m_committed -= size;
#endif
        systemDecommit(start, size);
    }

    static PageReservation reserve(size_t size, Usage usage = UnknownUsage, bool writable = true, bool executable = false)
    {
        ASSERT(isPageAligned(size));
        return systemReserve(size, usage, writable, executable);
    }

#if HAVE(PAGE_ALLOCATE_AT)
    static PageReservation reserveAt(void* address, bool fixed, size_t size, Usage usage = UnknownUsage, bool writable = true, bool executable = false)
    {
        ASSERT(isPageAligned(address));
        ASSERT(isPageAligned(size));
        return systemReserveAt(address, fixed, size, usage, writable, executable);
    }
#endif

    void deallocate()
    {
        ASSERT(m_base);
        ASSERT(!m_committed);
        systemDeallocate(false);
    }

#ifndef NDEBUG
    using PageAllocation::lastError;
#endif

private:
#if OS(SYMBIAN)
    PageReservation(void* base, size_t size, RChunk* chunk)
        : PageAllocation(base, size, chunk)
#else
    PageReservation(void* base, size_t size)
        : PageAllocation(base, size)
#endif
#ifndef NDEBUG
        , m_committed(0)
#endif
    {
    }

    bool systemCommit(void*, size_t);
    void systemDecommit(void*, size_t);
    static PageReservation systemReserve(size_t, Usage, bool, bool);
#if HAVE(PAGE_ALLOCATE_AT)
    static PageReservation systemReserveAt(void*, bool, size_t, Usage, bool, bool);
#endif

#if HAVE(VIRTUALALLOC)
    DWORD m_protection;
#endif
#ifndef NDEBUG
    size_t m_committed;
#endif
};


#if HAVE(MMAP)


inline bool PageReservation::systemCommit(void* start, size_t size)
{
#if HAVE(MADV_FREE_REUSE)
    while (madvise(start, size, MADV_FREE_REUSE) == -1 && errno == EAGAIN) { }
#else
    UNUSED_PARAM(start);
    UNUSED_PARAM(size);
#endif
    return true;
}

inline void PageReservation::systemDecommit(void* start, size_t size)
{
#if HAVE(MADV_FREE_REUSE)
    while (madvise(start, size, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { }
#elif HAVE(MADV_FREE)
    while (madvise(start, size, MADV_FREE) == -1 && errno == EAGAIN) { }
#elif HAVE(MADV_DONTNEED)
    while (madvise(start, size, MADV_DONTNEED) == -1 && errno == EAGAIN) { }
#else
    UNUSED_PARAM(start);
    UNUSED_PARAM(size);
#endif
}

inline PageReservation PageReservation::systemReserve(size_t size, Usage usage, bool writable, bool executable)
{
    return systemReserveAt(0, false, size, usage, writable, executable);
}

inline PageReservation PageReservation::systemReserveAt(void* address, bool fixed, size_t size, Usage usage, bool writable, bool executable)
{
    void* base = systemAllocateAt(address, fixed, size, usage, writable, executable).base();
#if HAVE(MADV_FREE_REUSE)
    // When using MADV_FREE_REUSE we keep all decommitted memory marked as REUSABLE.
    // We call REUSE on commit, and REUSABLE on decommit.
    if (base)
        while (madvise(base, size, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { }
#endif
    return PageReservation(base, size);
}


#elif HAVE(VIRTUALALLOC)


inline bool PageReservation::systemCommit(void* start, size_t size)
{
    return VirtualAlloc(start, size, MEM_COMMIT, m_protection) == start;
}

inline void PageReservation::systemDecommit(void* start, size_t size)
{
    VirtualFree(start, size, MEM_DECOMMIT);
}

inline PageReservation PageReservation::systemReserve(size_t size, Usage usage, bool writable, bool executable)
{
    // Record the protection for use during commit.
    DWORD protection = executable ?
        (writable ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ) :
        (writable ? PAGE_READWRITE : PAGE_READONLY);
    PageReservation reservation(VirtualAlloc(0, size, MEM_RESERVE, protection), size);
    reservation.m_protection = protection;
    return reservation;
}


#elif OS(SYMBIAN)


inline bool PageReservation::systemCommit(void* start, size_t size)
{
    intptr_t offset = reinterpret_cast<intptr_t>(start) - reinterpret_cast<intptr_t>(m_base);
    m_chunk->Commit(offset, size);
    return true;
}

inline void PageReservation::systemDecommit(void* start, size_t size)
{
    intptr_t offset = reinterpret_cast<intptr_t>(start) - reinterpret_cast<intptr_t>(m_base);
    m_chunk->Decommit(offset, size);
}

inline PageReservation PageReservation::systemReserve(size_t size, Usage usage, bool writable, bool executable)
{
    RChunk* rchunk = new RChunk();
    if (executable)
        rchunk->CreateLocalCode(0, size);
    else
        rchunk->CreateDisconnectedLocal(0, 0, size);
    return PageReservation(rchunk->Base(), size, rchunk);
}


#endif


}

using WTF::PageReservation;

#endif // PageReservation_h