netwerk/sctp/src/LocalArray.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/sctp/src/LocalArray.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,75 @@
     1.4 +/*
     1.5 + * Copyright (C) 2009 The Android Open Source Project
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +
    1.20 +#ifndef LOCAL_ARRAY_H_included
    1.21 +#define LOCAL_ARRAY_H_included
    1.22 +
    1.23 +#include <cstddef>
    1.24 +#include <new>
    1.25 +
    1.26 +/**
    1.27 + * A fixed-size array with a size hint. That number of bytes will be allocated
    1.28 + * on the stack, and used if possible, but if more bytes are requested at
    1.29 + * construction time, a buffer will be allocated on the heap (and deallocated
    1.30 + * by the destructor).
    1.31 + *
    1.32 + * The API is intended to be a compatible subset of C++0x's std::array.
    1.33 + */
    1.34 +template <size_t STACK_BYTE_COUNT>
    1.35 +class LocalArray {
    1.36 +public:
    1.37 +    /**
    1.38 +     * Allocates a new fixed-size array of the given size. If this size is
    1.39 +     * less than or equal to the template parameter STACK_BYTE_COUNT, an
    1.40 +     * internal on-stack buffer will be used. Otherwise a heap buffer will
    1.41 +     * be allocated.
    1.42 +     */
    1.43 +    LocalArray(size_t desiredByteCount) : mSize(desiredByteCount) {
    1.44 +        if (desiredByteCount > STACK_BYTE_COUNT) {
    1.45 +            mPtr = new char[mSize];
    1.46 +        } else {
    1.47 +            mPtr = &mOnStackBuffer[0];
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51 +    /**
    1.52 +     * Frees the heap-allocated buffer, if there was one.
    1.53 +     */
    1.54 +    ~LocalArray() {
    1.55 +        if (mPtr != &mOnStackBuffer[0]) {
    1.56 +            delete[] mPtr;
    1.57 +        }
    1.58 +    }
    1.59 +
    1.60 +    // Capacity.
    1.61 +    size_t size() { return mSize; }
    1.62 +    bool empty() { return mSize == 0; }
    1.63 +
    1.64 +    // Element access.
    1.65 +    char& operator[](size_t n) { return mPtr[n]; }
    1.66 +    const char& operator[](size_t n) const { return mPtr[n]; }
    1.67 +
    1.68 +private:
    1.69 +    char mOnStackBuffer[STACK_BYTE_COUNT];
    1.70 +    char* mPtr;
    1.71 +    size_t mSize;
    1.72 +
    1.73 +    // Disallow copy and assignment.
    1.74 +    LocalArray(const LocalArray&);
    1.75 +    void operator=(const LocalArray&);
    1.76 +};
    1.77 +
    1.78 +#endif // LOCAL_ARRAY_H_included

mercurial