1 /*
   2  * Copyright (c) 2016, 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 #include "precompiled.hpp"
  25 #include "gc/z/zGlobals.hpp"
  26 #include "gc/z/zPhysicalMemory.inline.hpp"
  27 #include "utilities/debug.hpp"
  28 #include "unittest.hpp"
  29 
  30 #if defined(AMD64)
  31 
  32 TEST(ZPhysicalMemorySegmentTest, split) {
  33   ZPhysicalMemorySegment seg(0, 10 * ZGranuleSize);
  34 
  35   ZPhysicalMemorySegment seg_split0 = seg.split(0 * ZGranuleSize);
  36   EXPECT_EQ(seg_split0.size(),  0 * ZGranuleSize);
  37   EXPECT_EQ(       seg.size(), 10 * ZGranuleSize);
  38 
  39   ZPhysicalMemorySegment seg_split1 = seg.split(5 * ZGranuleSize);
  40   EXPECT_EQ(seg_split1.size(),  5 * ZGranuleSize);
  41   EXPECT_EQ(       seg.size(),  5 * ZGranuleSize);
  42 
  43   ZPhysicalMemorySegment seg_split2 = seg.split(5 * ZGranuleSize);
  44   EXPECT_EQ(seg_split2.size(),  5 * ZGranuleSize);
  45   EXPECT_EQ(       seg.size(),  0 * ZGranuleSize);
  46 
  47   ZPhysicalMemorySegment seg_split3 = seg.split(0 * ZGranuleSize);
  48   EXPECT_EQ(seg_split3.size(),  0 * ZGranuleSize);
  49   EXPECT_EQ(       seg.size(),  0 * ZGranuleSize);
  50 }
  51 
  52 TEST(ZPhysicalMemoryTest, split) {
  53   ZPhysicalMemoryManager pmem_manager(10 * ZGranuleSize);
  54 
  55   pmem_manager.try_ensure_unused_capacity(10 * ZGranuleSize);
  56   EXPECT_EQ(pmem_manager.unused_capacity(), 10 * ZGranuleSize);
  57 
  58   ZPhysicalMemory pmem = pmem_manager.alloc(8 * ZGranuleSize);
  59   EXPECT_EQ(pmem.nsegments(), 1u) << "wrong number of segments";
  60 
  61   ZPhysicalMemory split0_pmem = pmem.split(ZGranuleSize);
  62   EXPECT_EQ(split0_pmem.nsegments(), 1u);
  63   EXPECT_EQ(       pmem.nsegments(), 1u);
  64   EXPECT_EQ(split0_pmem.size(), 1 * ZGranuleSize);
  65   EXPECT_EQ(       pmem.size(), 7 * ZGranuleSize);
  66 
  67   ZPhysicalMemory split1_pmem = pmem.split(2 * ZGranuleSize);
  68   EXPECT_EQ(split1_pmem.nsegments(), 1u);
  69   EXPECT_EQ(       pmem.nsegments(), 1u);
  70   EXPECT_EQ(split1_pmem.size(), 2 * ZGranuleSize);
  71   EXPECT_EQ(       pmem.size(), 5 * ZGranuleSize);
  72 
  73   ZPhysicalMemory split2_pmem = pmem.split(5 * ZGranuleSize);
  74   EXPECT_EQ(split2_pmem.nsegments(), 1u);
  75   EXPECT_EQ(       pmem.nsegments(), 1u);
  76   EXPECT_EQ(split2_pmem.size(), 5 * ZGranuleSize);
  77   EXPECT_EQ(       pmem.size(), 0 * ZGranuleSize);
  78 }
  79 
  80 #endif