Home | History | Annotate | Download | only in wtf
      1 /*
      2  * Copyright (C) 2013 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef WTF_PageAllocator_h
     32 #define WTF_PageAllocator_h
     33 
     34 #include "wtf/Assertions.h"
     35 #include "wtf/CPU.h"
     36 #include "wtf/WTFExport.h"
     37 #include <stdint.h>
     38 
     39 namespace WTF {
     40 
     41 #if OS(WIN)
     42 static const size_t kPageAllocationGranularityShift = 16; // 64KB
     43 #else
     44 static const size_t kPageAllocationGranularityShift = 12; // 4KB
     45 #endif
     46 static const size_t kPageAllocationGranularity = 1 << kPageAllocationGranularityShift;
     47 static const size_t kPageAllocationGranularityOffsetMask = kPageAllocationGranularity - 1;
     48 static const size_t kPageAllocationGranularityBaseMask = ~kPageAllocationGranularityOffsetMask;
     49 
     50 // All Blink-supported systems have 4096 sized system pages and can handle
     51 // permissions and commit / decommit at this granularity.
     52 static const size_t kSystemPageSize = 4096;
     53 static const size_t kSystemPageOffsetMask = kSystemPageSize - 1;
     54 static const size_t kSystemPageBaseMask = ~kSystemPageOffsetMask;
     55 
     56 // Allocate one or more pages. Addresses in the range will be readable and
     57 // writeable but not executable.
     58 // The requested address is just a hint; the actual address returned may
     59 // differ. The returned address will be aligned at least to align bytes.
     60 // len is in bytes, and must be a multiple of kPageAllocationGranularity.
     61 // align is in bytes, and must be a power-of-two multiple of
     62 // kPageAllocationGranularity.
     63 // If addr is null, then a suitable and randomized address will be chosen
     64 // automatically.
     65 // This call will return null if the allocation cannot be satisfied.
     66 WTF_EXPORT void* allocPages(void* addr, size_t len, size_t align);
     67 
     68 // Free one or more pages.
     69 // addr and len must match a previous call to allocPages().
     70 WTF_EXPORT void freePages(void* addr, size_t len);
     71 
     72 // Mark one or more system pages as being inaccessible.
     73 // Subsequently accessing any address in the range will fault, and the
     74 // addresses will not be re-used by future allocations.
     75 // len must be a multiple of kSystemPageSize bytes.
     76 WTF_EXPORT void setSystemPagesInaccessible(void* addr, size_t len);
     77 
     78 // Mark one or more system pages as being accessible.
     79 // The pages will be readable and writeable.
     80 // len must be a multiple of kSystemPageSize bytes.
     81 WTF_EXPORT void setSystemPagesAccessible(void* addr, size_t len);
     82 
     83 // Decommit one or more system pages. Decommitted means that the physical memory
     84 // is released to the system, but the virtual address space remains reserved.
     85 // System pages are re-committed by calling recommitSystemPages(). Touching
     86 // a decommitted page _may_ fault.
     87 // Clients should not make any assumptions about the contents of decommitted
     88 // system pages, before or after they write to the page. The only guarantee
     89 // provided is that the contents of the system page will be deterministic again
     90 // after recommitting and writing to it. In particlar note that system pages are// not guaranteed to be zero-filled upon re-commit.
     91 // len must be a multiple of kSystemPageSize bytes.
     92 WTF_EXPORT void decommitSystemPages(void* addr, size_t len);
     93 
     94 // Recommit one or more system pages. Decommitted system pages must be
     95 // recommitted before they are read are written again.
     96 // Note that this operation may be a no-op on some platforms.
     97 // len must be a multiple of kSystemPageSize bytes.
     98 WTF_EXPORT void recommitSystemPages(void* addr, size_t len);
     99 
    100 } // namespace WTF
    101 
    102 #endif // WTF_PageAllocator_h
    103