Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/os/linux/vm/ifaddrs/LocalArray.h
48773 views
1
/*
2
* Copyright (C) 2009 The Android Open Source Project
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
#ifndef LOCAL_ARRAY_H_included
17
#define LOCAL_ARRAY_H_included
18
#include <cstddef>
19
#include <new>
20
/**
21
* A fixed-size array with a size hint. That number of bytes will be allocated
22
* on the stack, and used if possible, but if more bytes are requested at
23
* construction time, a buffer will be allocated on the heap (and deallocated
24
* by the destructor).
25
*
26
* The API is intended to be a compatible subset of C++0x's std::array.
27
*/
28
template <size_t STACK_BYTE_COUNT>
29
class LocalArray {
30
public:
31
/**
32
* Allocates a new fixed-size array of the given size. If this size is
33
* less than or equal to the template parameter STACK_BYTE_COUNT, an
34
* internal on-stack buffer will be used. Otherwise a heap buffer will
35
* be allocated.
36
*/
37
LocalArray(size_t desiredByteCount) : mSize(desiredByteCount) {
38
if (desiredByteCount > STACK_BYTE_COUNT) {
39
mPtr = new char[mSize];
40
} else {
41
mPtr = &mOnStackBuffer[0];
42
}
43
}
44
/**
45
* Frees the heap-allocated buffer, if there was one.
46
*/
47
~LocalArray() {
48
if (mPtr != &mOnStackBuffer[0]) {
49
delete[] mPtr;
50
}
51
}
52
// Capacity.
53
size_t size() { return mSize; }
54
bool empty() { return mSize == 0; }
55
// Element access.
56
char& operator[](size_t n) { return mPtr[n]; }
57
const char& operator[](size_t n) const { return mPtr[n]; }
58
private:
59
char mOnStackBuffer[STACK_BYTE_COUNT];
60
char* mPtr;
61
size_t mSize;
62
// Disallow copy and assignment.
63
LocalArray(const LocalArray&);
64
void operator=(const LocalArray&);
65
};
66
#endif // LOCAL_ARRAY_H_included
67
68