runtime_loader: add simple vector implementation

std::vector requires libstdc++ which may be unsafe to use in runtime_loader.
This commit is contained in:
Pawel Dziepak 2014-05-04 02:26:08 +02:00
parent 634bc98a3c
commit bc472ab8f9

View File

@ -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 <OS.h>
#include <string.h>
#include <algorithm>
#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<typename T>
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<typename T>
vector<T>::vector()
:
fMaxSize(0),
fSize(0),
fData(NULL)
{
}
#endif
template<typename T>
void
vector<T>::push_back(const T& value)
{
if (fSize + 1 > fMaxSize)
_Grow();
fData[fSize++] = value;
}
template<typename T>
void
vector<T>::_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