js/src/yarr/PageAllocation.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/yarr/PageAllocation.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     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. ``AS IS'' AND ANY
    1.20 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.22 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
    1.23 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.24 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.25 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.26 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    1.27 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.28 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.29 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 + *
    1.31 + * ***** END LICENSE BLOCK ***** */
    1.32 +
    1.33 +#ifndef yarr_PageAllocation_h
    1.34 +#define yarr_PageAllocation_h
    1.35 +
    1.36 +#include "yarr/wtfbridge.h"
    1.37 +#include "yarr/OSAllocator.h"
    1.38 +#include "yarr/PageBlock.h"
    1.39 +#include "assembler/wtf/VMTags.h"
    1.40 +
    1.41 +#if WTF_OS_DARWIN
    1.42 +#include <mach/mach_init.h>
    1.43 +#include <mach/vm_map.h>
    1.44 +#endif
    1.45 +
    1.46 +#if WTF_OS_HAIKU
    1.47 +#include <OS.h>
    1.48 +#endif
    1.49 +
    1.50 +#if WTF_OS_WINDOWS
    1.51 +#include <malloc.h>
    1.52 +#include <windows.h>
    1.53 +#endif
    1.54 +
    1.55 +#if WTF_OS_SYMBIAN
    1.56 +#include <e32hal.h>
    1.57 +#include <e32std.h>
    1.58 +#endif
    1.59 +
    1.60 +#if WTF_HAVE_ERRNO_H
    1.61 +#include <errno.h>
    1.62 +#endif
    1.63 +
    1.64 +#if WTF_HAVE_MMAP
    1.65 +#include <sys/mman.h>
    1.66 +#include <unistd.h>
    1.67 +#endif
    1.68 +
    1.69 +namespace WTF {
    1.70 +
    1.71 +/*
    1.72 +    PageAllocation
    1.73 +
    1.74 +    The PageAllocation class provides a cross-platform memory allocation interface
    1.75 +    with similar capabilities to posix mmap/munmap.  Memory is allocated by calling
    1.76 +    PageAllocation::allocate, and deallocated by calling deallocate on the
    1.77 +    PageAllocation object.  The PageAllocation holds the allocation's base pointer
    1.78 +    and size.
    1.79 +
    1.80 +    The allocate method is passed the size required (which must be a multiple of
    1.81 +    the system page size, which can be accessed using PageAllocation::pageSize).
    1.82 +    Callers may also optinally provide a flag indicating the usage (for use by
    1.83 +    system memory usage tracking tools, where implemented), and boolean values
    1.84 +    specifying the required protection (defaulting to writable, non-executable).
    1.85 +*/
    1.86 +
    1.87 +class PageAllocation : private PageBlock {
    1.88 +public:
    1.89 +    PageAllocation()
    1.90 +    {
    1.91 +    }
    1.92 +
    1.93 +    using PageBlock::size;
    1.94 +    using PageBlock::base;
    1.95 +
    1.96 +#ifndef __clang__
    1.97 +    using PageBlock::operator bool;
    1.98 +#else
    1.99 +    // FIXME: This is a workaround for <rdar://problem/8876150>, wherein Clang incorrectly emits an access
   1.100 +    // control warning when a client tries to use operator bool exposed above via "using PageBlock::operator bool".
   1.101 +    operator bool() const { return PageBlock::operator bool(); }
   1.102 +#endif
   1.103 +
   1.104 +    static PageAllocation allocate(size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false)
   1.105 +    {
   1.106 +        ASSERT(isPageAligned(size));
   1.107 +        return PageAllocation(OSAllocator::reserveAndCommit(size, usage, writable, executable), size);
   1.108 +    }
   1.109 +
   1.110 +    void deallocate()
   1.111 +    {
   1.112 +        // Clear base & size before calling release; if this is *inside* allocation
   1.113 +        // then we won't be able to clear then after deallocating the memory.
   1.114 +        PageAllocation tmp;
   1.115 +        JSC::std::swap(tmp, *this);
   1.116 +
   1.117 +        ASSERT(tmp);
   1.118 +        ASSERT(!*this);
   1.119 +
   1.120 +        OSAllocator::decommitAndRelease(tmp.base(), tmp.size());
   1.121 +    }
   1.122 +
   1.123 +private:
   1.124 +    PageAllocation(void* base, size_t size)
   1.125 +        : PageBlock(base, size)
   1.126 +    {
   1.127 +    }
   1.128 +};
   1.129 +
   1.130 +} // namespace WTF
   1.131 +
   1.132 +using WTF::PageAllocation;
   1.133 +
   1.134 +#endif /* yarr_PageAllocation_h */

mercurial