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