X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fsrc%2Fanbox%2Fcommon%2Fvariable_length_array.h;fp=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fsrc%2Fanbox%2Fcommon%2Fvariable_length_array.h;h=52fedbf77de359f1e1a5a2bac2774bad8a2e3619;hb=e26c1ec581be598521517829adba8c8dd23a768f;hp=0000000000000000000000000000000000000000;hpb=6699c1aea74eeb0eb400e6299079f0c7576f716f;p=iec.git diff --git a/src/type3_AndroidCloud/anbox-master/src/anbox/common/variable_length_array.h b/src/type3_AndroidCloud/anbox-master/src/anbox/common/variable_length_array.h new file mode 100644 index 0000000..52fedbf --- /dev/null +++ b/src/type3_AndroidCloud/anbox-master/src/anbox/common/variable_length_array.h @@ -0,0 +1,58 @@ +/* + * Copyright © 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * Authored by: Alexandros Frantzis + */ + +#ifndef ANBOX_COMMON_VARIABLE_LENGTH_ARRAY_H_ +#define ANBOX_COMMON_VARIABLE_LENGTH_ARRAY_H_ + +#include +#include + +namespace anbox { +template +class VariableLengthArray { + public: + explicit VariableLengthArray(size_t size) : size_{size} { + /* Don't call resize if the initial values of member variables are valid */ + if (size > BuiltInBufferSize) resize(size); + } + + void resize(size_t size) { + if (size > BuiltInBufferSize) + effective_buffer = BufferUPtr{new unsigned char[size], heap_deleter}; + else + effective_buffer = BufferUPtr{builtin_buffer, null_deleter}; + + size_ = size; + } + + unsigned char* data() const { return effective_buffer.get(); } + size_t size() const { return size_; } + + private: + typedef std::unique_ptr BufferUPtr; + + static void null_deleter(unsigned char*) {} + static void heap_deleter(unsigned char* b) { delete[] b; } + + unsigned char builtin_buffer[BuiltInBufferSize]; + BufferUPtr effective_buffer{builtin_buffer, null_deleter}; + size_t size_; +}; +} // namespace anbox + +#endif