1 /*
2  * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef SHARE_VM_GC_SHARED_COLLECTEDHEAP_INLINE_HPP
26 #define SHARE_VM_GC_SHARED_COLLECTEDHEAP_INLINE_HPP
27 
28 #include "gc/shared/collectedHeap.hpp"
29 #include "oops/oop.inline.hpp"
30 #include "utilities/align.hpp"
31 
align_allocation_or_fail(HeapWord * addr,HeapWord * end,unsigned short alignment_in_bytes)32 inline HeapWord* CollectedHeap::align_allocation_or_fail(HeapWord* addr,
33                                                          HeapWord* end,
34                                                          unsigned short alignment_in_bytes) {
35   if (alignment_in_bytes <= ObjectAlignmentInBytes) {
36     return addr;
37   }
38 
39   assert(is_aligned(addr, HeapWordSize),
40          "Address " PTR_FORMAT " is not properly aligned.", p2i(addr));
41   assert(is_aligned(alignment_in_bytes, HeapWordSize),
42          "Alignment size %u is incorrect.", alignment_in_bytes);
43 
44   HeapWord* new_addr = align_up(addr, alignment_in_bytes);
45   size_t padding = pointer_delta(new_addr, addr);
46 
47   if (padding == 0) {
48     return addr;
49   }
50 
51   if (padding < CollectedHeap::min_fill_size()) {
52     padding += alignment_in_bytes / HeapWordSize;
53     assert(padding >= CollectedHeap::min_fill_size(),
54            "alignment_in_bytes %u is expect to be larger "
55            "than the minimum object size", alignment_in_bytes);
56     new_addr = addr + padding;
57   }
58 
59   assert(new_addr > addr, "Unexpected arithmetic overflow "
60          PTR_FORMAT " not greater than " PTR_FORMAT, p2i(new_addr), p2i(addr));
61   if(new_addr < end) {
62     CollectedHeap::fill_with_object(addr, padding);
63     return new_addr;
64   } else {
65     return NULL;
66   }
67 }
68 
69 #endif // SHARE_VM_GC_SHARED_COLLECTEDHEAP_INLINE_HPP
70