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