js/src/yarr/OSAllocator.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/yarr/OSAllocator.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + *
     1.7 + * ***** BEGIN LICENSE BLOCK *****
     1.8 + * Copyright (C) 2010 Apple Inc. All rights reserved.
     1.9 + *
    1.10 + * Redistribution and use in source and binary forms, with or without
    1.11 + * modification, are permitted provided that the following conditions
    1.12 + * are met:
    1.13 + * 1. Redistributions of source code must retain the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer.
    1.15 + * 2. Redistributions in binary form must reproduce the above copyright
    1.16 + *    notice, this list of conditions and the following disclaimer in the
    1.17 + *    documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    1.21 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.22 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
    1.23 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    1.29 + * THE POSSIBILITY OF SUCH DAMAGE.
    1.30 + *
    1.31 + * ***** END LICENSE BLOCK ***** */
    1.32 +
    1.33 +#ifndef yarr_OSAllocator_h
    1.34 +#define yarr_OSAllocator_h
    1.35 +
    1.36 +#include <stdlib.h>
    1.37 +#include "yarr/wtfbridge.h"
    1.38 +#include "assembler/wtf/VMTags.h"
    1.39 +#include "assembler/wtf/Assertions.h"
    1.40 +
    1.41 +namespace WTF {
    1.42 +
    1.43 +class OSAllocator {
    1.44 +public:
    1.45 +    enum Usage {
    1.46 +        UnknownUsage = -1,
    1.47 +        FastMallocPages = VM_TAG_FOR_TCMALLOC_MEMORY,
    1.48 +        JSGCHeapPages = VM_TAG_FOR_COLLECTOR_MEMORY,
    1.49 +        JSVMStackPages = VM_TAG_FOR_REGISTERFILE_MEMORY,
    1.50 +        JSJITCodePages = VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY
    1.51 +    };
    1.52 +
    1.53 +    // These methods are symmetric; reserveUncommitted allocates VM in an uncommitted state,
    1.54 +    // releaseDecommitted should be called on a region of VM allocated by a single reservation,
    1.55 +    // the memory must all currently be in a decommitted state.
    1.56 +    static void* reserveUncommitted(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false);
    1.57 +    static void releaseDecommitted(void*, size_t);
    1.58 +
    1.59 +    // These methods are symmetric; they commit or decommit a region of VM (uncommitted VM should
    1.60 +    // never be accessed, since the OS may not have attached physical memory for these regions).
    1.61 +    // Clients should only call commit on uncommitted regions and decommit on committed regions.
    1.62 +    static void commit(void*, size_t, bool writable, bool executable);
    1.63 +    static void decommit(void*, size_t);
    1.64 +
    1.65 +    // These methods are symmetric; reserveAndCommit allocates VM in an committed state,
    1.66 +    // decommitAndRelease should be called on a region of VM allocated by a single reservation,
    1.67 +    // the memory must all currently be in a committed state.
    1.68 +    static void* reserveAndCommit(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false);
    1.69 +    static void decommitAndRelease(void* base, size_t size);
    1.70 +
    1.71 +    // These methods are akin to reserveAndCommit/decommitAndRelease, above - however rather than
    1.72 +    // committing/decommitting the entire region additional parameters allow a subregion to be
    1.73 +    // specified.
    1.74 +    static void* reserveAndCommit(size_t reserveSize, size_t commitSize, Usage = UnknownUsage, bool writable = true, bool executable = false);
    1.75 +    static void decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize);
    1.76 +};
    1.77 +
    1.78 +inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable)
    1.79 +{
    1.80 +    void* base = reserveUncommitted(reserveSize, usage, writable, executable);
    1.81 +    commit(base, commitSize, writable, executable);
    1.82 +    return base;
    1.83 +}
    1.84 +
    1.85 +inline void OSAllocator::decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize)
    1.86 +{
    1.87 +    ASSERT(decommitBase >= releaseBase && (static_cast<char*>(decommitBase) + decommitSize) <= (static_cast<char*>(releaseBase) + releaseSize));
    1.88 +#if WTF_OS_WINCE || WTF_OS_SYMBIAN
    1.89 +    // On most platforms we can actually skip this final decommit; releasing the VM will
    1.90 +    // implicitly decommit any physical memory in the region. This is not true on WINCE.
    1.91 +    // On Symbian, this makes implementation simpler and better aligned with the RChunk API
    1.92 +    decommit(decommitBase, decommitSize);
    1.93 +#endif
    1.94 +    releaseDecommitted(releaseBase, releaseSize);
    1.95 +}
    1.96 +
    1.97 +inline void OSAllocator::decommitAndRelease(void* base, size_t size)
    1.98 +{
    1.99 +    decommitAndRelease(base, size, base, size);
   1.100 +}
   1.101 +
   1.102 +} // namespace WTF
   1.103 +
   1.104 +using WTF::OSAllocator;
   1.105 +
   1.106 +#endif /* yarr_OSAllocator_h */

mercurial