1 /*
   2  * Copyright (c) 2019, SAP. All rights reserved.
   3  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 #include "precompiled.hpp"
  26 
  27 #include "memory/allocation.hpp"
  28 #include "memory/metaspace/abstractPool.hpp"
  29 #include "runtime/os.hpp"
  30 
  31 #include "unittest.hpp"
  32 
  33 // Test AbstractPool class
  34 
  35 template <class E, class I, I initial_size, I size_increase, I max_size>
  36 class AbstractPoolTest {
  37 
  38   typedef metaspace::AbstractPool<E, I, initial_size, size_increase, max_size> PoolType;
  39 
  40   void test_exhaustion() {
  41     PoolType pool;
  42     for (I i = 0; i < max_size + 10; i ++) {
  43       E* e = pool.allocate_element();
  44       if (i < max_size) {
  45         ASSERT_EQUALS(e != NULL);
  46         ASSERT_EQUALS(pool.get_used(), i);
  47       } else {
  48         ASSERT_EQUALS(e == NULL);
  49         ASSERT_EQUALS(pool.get_used(), max_size);
  50       }
  51     }
  52     ASSERT_EQUALS(pool.memory_footprint() == max_size * sizeof(E));
  53   }
  54 
  55   void random_alloc_free() {
  56     PoolType pool;
  57     E* elems = NEW_C_HEAP_ARRAY(E*, max_size, mtInternal);
  58     int allocated = 0;
  59     for (int iter = 0; iter < 1000; iter ++) {
  60       I idx = (I)os::random() % max_size;
  61       if (elems[idx] == NULL) {
  62         elems[idx] = pool.allocate_element();
  63       } else {
  64         pool.return_element(elems[idx]);
  65       }
  66       if ((os::random() & 1) > 0) {
  67 
  68       } else
  69       for (I i = 0; i < max_size; i ++) {
  70 
  71       }
  72     }
  73     FREE_C_HEAP_ARRAY(elems);
  74   }
  75 
  76 public:
  77 
  78   void do_test() {
  79     test_exhaustion();
  80   }
  81 
  82 
  83 };
  84 
  85