1/*
2 * Copyright (c) 2014, 2019, 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_UTILITIES_CHUNKEDLIST_HPP
26#define SHARE_UTILITIES_CHUNKEDLIST_HPP
27
28#include "memory/allocation.hpp"
29#include "utilities/debug.hpp"
30
31template <class T, MEMFLAGS F> class ChunkedList : public CHeapObj<F> {
32 template <class U> friend class TestChunkedList;
33
34 static const size_t BufferSize = 64;
35
36 T _values[BufferSize];
37 T* _top;
38
39 ChunkedList<T, F>* _next_used;
40 ChunkedList<T, F>* _next_free;
41
42 T const * end() const {
43 return &_values[BufferSize];
44 }
45
46 public:
47 ChunkedList<T, F>() : _top(_values), _next_used(NULL), _next_free(NULL) {}
48
49 bool is_full() const {
50 return _top == end();
51 }
52
53 void clear() {
54 _top = _values;
55 // Don't clear the next pointers since that would interfere
56 // with other threads trying to iterate through the lists.
57 }
58
59 void push(T m) {
60 assert(!is_full(), "Buffer is full");
61 *_top = m;
62 _top++;
63 }
64
65 void set_next_used(ChunkedList<T, F>* buffer) { _next_used = buffer; }
66 void set_next_free(ChunkedList<T, F>* buffer) { _next_free = buffer; }
67
68 ChunkedList<T, F>* next_used() const { return _next_used; }
69 ChunkedList<T, F>* next_free() const { return _next_free; }
70
71 size_t size() const {
72 return pointer_delta(_top, _values, sizeof(T));
73 }
74
75 T at(size_t i) {
76 assert(i < size(), "IOOBE i: " SIZE_FORMAT " size(): " SIZE_FORMAT, i, size());
77 return _values[i];
78 }
79};
80
81#endif // SHARE_UTILITIES_CHUNKEDLIST_HPP
82