1f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch/*
2f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * Copyright (C) 2010 Apple Inc. All rights reserved.
3f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch *
4f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * Redistribution and use in source and binary forms, with or without
5f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * modification, are permitted provided that the following conditions
6f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * are met:
7f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * 1. Redistributions of source code must retain the above copyright
8f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch *    notice, this list of conditions and the following disclaimer.
9f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * 2. Redistributions in binary form must reproduce the above copyright
10f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch *    notice, this list of conditions and the following disclaimer in the
11f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch *    documentation and/or other materials provided with the distribution.
12f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch *
13f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch * THE POSSIBILITY OF SUCH DAMAGE.
24f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch */
25f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
26f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#include "config.h"
27f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#include "OSAllocator.h"
28f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
29f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#include "windows.h"
30f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#include <wtf/Assertions.h>
31f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
32f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochnamespace WTF {
33f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
34f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochstatic inline DWORD protection(bool writable, bool executable)
35f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch{
36f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    return executable ?
37f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch        (writable ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ) :
38f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch        (writable ? PAGE_READWRITE : PAGE_READONLY);
39f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch}
40f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
41f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochvoid* OSAllocator::reserveUncommitted(size_t bytes, Usage, bool writable, bool executable)
42f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch{
43f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void* result = VirtualAlloc(0, bytes, MEM_RESERVE, protection(writable, executable));
44f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    if (!result)
45f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch        CRASH();
46f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    return result;
47f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch}
48f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
49f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochvoid* OSAllocator::reserveAndCommit(size_t bytes, Usage, bool writable, bool executable)
50f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch{
51f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void* result = VirtualAlloc(0, bytes, MEM_RESERVE | MEM_COMMIT, protection(writable, executable));
52f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    if (!result)
53f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch        CRASH();
54f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    return result;
55f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch}
56f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
57f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochvoid OSAllocator::commit(void* address, size_t bytes, bool writable, bool executable)
58f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch{
59f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    void* result = VirtualAlloc(address, bytes, MEM_COMMIT, protection(writable, executable));
60f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    if (!result)
61f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch        CRASH();
62f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch}
63f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
64f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochvoid OSAllocator::decommit(void* address, size_t bytes)
65f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch{
66f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    bool result = VirtualFree(address, bytes, MEM_DECOMMIT);
67f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    if (!result)
68f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch        CRASH();
69f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch}
70f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
71f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochvoid OSAllocator::releaseDecommitted(void* address, size_t bytes)
72f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch{
73f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    // According to http://msdn.microsoft.com/en-us/library/aa366892(VS.85).aspx,
74f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    // dwSize must be 0 if dwFreeType is MEM_RELEASE.
75f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    bool result = VirtualFree(address, 0, MEM_RELEASE);
76f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    if (!result)
77f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch        CRASH();
78f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch}
79f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
80f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch} // namespace WTF
81