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 : #ifndef PageBlock_h
31 : #define PageBlock_h
32 :
33 : #include "mozilla/StandardInteger.h"
34 :
35 : #include <stdlib.h>
36 : #include "assembler/wtf/Platform.h"
37 :
38 : namespace WTF {
39 :
40 : size_t pageSize();
41 : inline bool isPageAligned(void* address) { return !(reinterpret_cast<intptr_t>(address) & (pageSize() - 1)); }
42 31359 : inline bool isPageAligned(size_t size) { return !(size & (pageSize() - 1)); }
43 31359 : inline bool isPowerOfTwo(size_t size) { return !(size & (size - 1)); }
44 :
45 : class PageBlock {
46 : public:
47 : PageBlock();
48 : PageBlock(const PageBlock&);
49 : PageBlock(void*, size_t);
50 :
51 125436 : void* base() const { return m_base; }
52 94077 : size_t size() const { return m_size; }
53 :
54 94077 : operator bool() const { return !!m_base; }
55 :
56 : bool contains(void* containedBase, size_t containedSize)
57 : {
58 : return containedBase >= m_base
59 : && (static_cast<char*>(containedBase) + containedSize) <= (static_cast<char*>(m_base) + m_size);
60 : }
61 :
62 : private:
63 : void* m_base;
64 : size_t m_size;
65 : };
66 :
67 31359 : inline PageBlock::PageBlock()
68 : : m_base(0)
69 31359 : , m_size(0)
70 : {
71 31359 : }
72 :
73 62718 : inline PageBlock::PageBlock(const PageBlock& other)
74 : : m_base(other.m_base)
75 62718 : , m_size(other.m_size)
76 : {
77 62718 : }
78 :
79 31359 : inline PageBlock::PageBlock(void* base, size_t size)
80 : : m_base(base)
81 31359 : , m_size(size)
82 : {
83 31359 : }
84 :
85 : } // namespace WTF
86 :
87 : using WTF::pageSize;
88 : using WTF::isPageAligned;
89 : using WTF::isPageAligned;
90 : using WTF::isPowerOfTwo;
91 :
92 : #endif // PageBlock_h
|