diff --git a/src/system/runtime_loader/utility.h b/src/system/runtime_loader/utility.h index 1d166c1ff7..dd61d1aa6c 100644 --- a/src/system/runtime_loader/utility.h +++ b/src/system/runtime_loader/utility.h @@ -1,4 +1,5 @@ /* + * Copyright 2014, Paweł Dziepak, pdziepak@quarnos.org. * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. * Distributed under the terms of the MIT License. */ @@ -7,6 +8,10 @@ #include +#include + +#include + #define PAGE_MASK (B_PAGE_SIZE - 1) @@ -15,14 +20,73 @@ #define TO_PAGE_SIZE(x) ((x + (PAGE_MASK)) & ~(PAGE_MASK)) -#ifdef __cplusplus -extern "C" { -#endif +extern "C" void dprintf(const char *format, ...); -void dprintf(const char *format, ...); -#ifdef __cplusplus +namespace utility { + + +template +class vector { +public: + inline vector(); + + void push_back(const T& value); + void pop_back() { fSize--; } + + T& back() { return fData[fSize - 1]; } + + T& operator[](size_t idx) { return fData[idx]; } + const T& operator[](size_t idx) const { return fData[idx]; } + + size_t size() const { return fSize; } + bool empty() const { return size() == 0; } + +private: + void _Grow(); + + size_t fMaxSize; + size_t fSize; + T* fData; +}; + + +template +vector::vector() + : + fMaxSize(0), + fSize(0), + fData(NULL) +{ } -#endif + + +template +void +vector::push_back(const T& value) +{ + if (fSize + 1 > fMaxSize) + _Grow(); + fData[fSize++] = value; +} + + +template +void +vector::_Grow() +{ + size_t newSize = std::max(fMaxSize * 2, size_t(4)); + T* newBuffer = new T[newSize]; + if (fSize > 0) { + memcpy(newBuffer, fData, fSize * sizeof(T)); + delete[] fData; + } + fData = newBuffer; + fMaxSize = newSize; +} + + +} // namespace utility + #endif // UTILITY_H