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 "gc/shared/fill.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "utilities/align.hpp"
  32 
  33 inline HeapWord* CollectedHeap::align_allocation_or_fail(HeapWord* addr,
  34                                                          HeapWord* end,
  35                                                          unsigned short alignment_in_bytes) {
  36   if (alignment_in_bytes <= ObjectAlignmentInBytes) {
  37     return addr;
  38   }
  39 
  40   assert(is_aligned(addr, HeapWordSize),
  41          "Address " PTR_FORMAT " is not properly aligned.", p2i(addr));
  42   assert(is_aligned(alignment_in_bytes, HeapWordSize),
  43          "Alignment size %u is incorrect.", alignment_in_bytes);
  44 
  45   HeapWord* new_addr = align_up(addr, alignment_in_bytes);
  46   size_t padding = pointer_delta(new_addr, addr);
  47 
  48   if (padding == 0) {
  49     return addr;
  50   }
  51 
  52   if (padding < Fill::min_size()) {
  53     padding += alignment_in_bytes / HeapWordSize;
  54     assert(padding >= Fill::min_size(),
  55            "alignment_in_bytes %u is expect to be larger "
  56            "than the minimum object size", alignment_in_bytes);
  57     new_addr = addr + padding;
  58   }
  59 
  60   assert(new_addr > addr, "Unexpected arithmetic overflow "
  61          PTR_FORMAT " not greater than " PTR_FORMAT, p2i(new_addr), p2i(addr));
  62   if(new_addr < end) {
  63     Fill::range(addr, padding);
  64     return new_addr;
  65   } else {
  66     return NULL;
  67   }
  68 }
  69 
  70 #endif // SHARE_VM_GC_SHARED_COLLECTEDHEAP_INLINE_HPP