PageRenderTime 26ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/js/src/yarr/OSAllocatorWin.cpp

http://github.com/zpao/v8monkey
C++ | 89 lines | 46 code | 13 blank | 30 comment | 6 complexity | a2a761449789716656b4d854a62ffe71 MD5 | raw file
 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#include "assembler/wtf/Platform.h"
31
32#if ENABLE_ASSEMBLER && WTF_OS_WINDOWS
33
34#include "windows.h"
35#include "wtf/Assertions.h"
36
37#include "OSAllocator.h"
38
39namespace WTF {
40
41static inline DWORD protection(bool writable, bool executable)
42{
43    return executable ?
44        (writable ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ) :
45        (writable ? PAGE_READWRITE : PAGE_READONLY);
46}
47
48void* OSAllocator::reserveUncommitted(size_t bytes, Usage, bool writable, bool executable)
49{
50    void* result = VirtualAlloc(0, bytes, MEM_RESERVE, protection(writable, executable));
51    if (!result)
52        CRASH();
53    return result;
54}
55
56void* OSAllocator::reserveAndCommit(size_t bytes, Usage, bool writable, bool executable)
57{
58    void* result = VirtualAlloc(0, bytes, MEM_RESERVE | MEM_COMMIT, protection(writable, executable));
59    if (!result)
60        CRASH();
61    return result;
62}
63
64void OSAllocator::commit(void* address, size_t bytes, bool writable, bool executable)
65{
66    void* result = VirtualAlloc(address, bytes, MEM_COMMIT, protection(writable, executable));
67    if (!result)
68        CRASH();
69}
70
71void OSAllocator::decommit(void* address, size_t bytes)
72{
73    bool result = VirtualFree(address, bytes, MEM_DECOMMIT);
74    if (!result)
75        CRASH();
76}
77
78void OSAllocator::releaseDecommitted(void* address, size_t bytes)
79{
80    // According to http://msdn.microsoft.com/en-us/library/aa366892(VS.85).aspx,
81    // dwSize must be 0 if dwFreeType is MEM_RELEASE.
82    bool result = VirtualFree(address, 0, MEM_RELEASE);
83    if (!result)
84        CRASH();
85}
86
87} // namespace WTF
88
89#endif