1 /* 2 * Copyright (c) 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 #include "precompiled.hpp" 26 #include "gc/shared/ptrQueue.hpp" 27 #include "runtime/mutex.hpp" 28 #include "unittest.hpp" 29 30 // Some basic testing of BufferNode::Allocator. 31 TEST_VM(PtrQueueBufferAllocatorTest, test) { 32 Mutex m(Mutex::leaf, "PtrQueueBufferAllocatorTest", 33 false, Mutex::_safepoint_check_never); 34 BufferNode::Allocator allocator(256, &m); 35 36 // Allocate some new nodes for use in testing. 37 BufferNode* nodes[10] = {}; 38 const size_t node_count = ARRAY_SIZE(nodes); 39 for (size_t i = 0; i < node_count; ++i) { 40 ASSERT_EQ(0u, allocator.free_count()); 41 nodes[i] = allocator.allocate(); 42 ASSERT_EQ((BufferNode*)NULL, nodes[i]->next()); 43 } 44 45 // Release the nodes, adding them to the allocator's free list. 46 for (size_t i = 0; i < node_count; ++i) { 47 ASSERT_EQ(i, allocator.free_count()); 48 allocator.release(nodes[i]); 49 if (i == 0) { 50 ASSERT_EQ((BufferNode*)NULL, nodes[i]->next()); 51 } else { 52 ASSERT_EQ(nodes[i - 1], nodes[i]->next()); 53 } 54 } 55 56 // Allocate nodes from the free list. 57 for (size_t i = 0; i < node_count; ++i) { 58 size_t j = node_count - i; 59 ASSERT_EQ(j, allocator.free_count()); 60 ASSERT_EQ(nodes[j - 1], allocator.allocate()); 61 } 62 ASSERT_EQ(0u, allocator.free_count()); 63 64 // Release nodes back to the free list. 65 for (size_t i = 0; i < node_count; ++i) { 66 allocator.release(nodes[i]); 67 } 68 ASSERT_EQ(node_count, allocator.free_count()); 69 70 // Destroy some nodes in the free list. 71 // We don't have a way to verify destruction, but we can at 72 // leat verify we don't crash along the way. 73 allocator.reduce_free_list(); 74 // destroy allocator. 75 }