Introducing the BufferCache announced on the mailing list, and some small changes. L4Sys is still WIP.

git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1316 8c4709b5-6ec9-48aa-a5cd-a96041d1645a
This commit is contained in:
unzner
2012-06-06 15:13:16 +00:00
parent 3284fba7d3
commit 715a393598
12 changed files with 237 additions and 24 deletions

View File

@ -0,0 +1,74 @@
#include "BufferCache.hpp"
#include "Event.hpp"
namespace fi {
template<class T> int BufferCache<T>::add(T val) {
size_t new_size = get_count() + 1;
size_t new_last_index = get_count();
int res = reallocate_buffer(new_size);
if (res == 0) {
set(new_last_index, val);
}
return res;
}
template<class T> int BufferCache<T>::remove(T val) {
bool do_remove = false;
for (size_t i = 0; i < get_count(); i++) {
if (get(i) == val) {
do_remove = true;
}
if (do_remove) {
if (i > get_count() - 1) {
set(i, get(i + 1));
}
}
}
int res = 0;
if (do_remove) {
size_t new_size = get_count() - 1;
res = reallocate_buffer(new_size);
}
return res;
}
template<class T> void BufferCache<T>::clear() {
set_count(0);
free(m_Buffer);
m_Buffer = NULL;
}
template<class T> int BufferCache<T>::erase(int idx) {
for (size_t i = idx; i < get_count() - 1; i++) {
set(i, get(i + 1));
}
size_t new_size = get_count() - 1;
if (reallocate_buffer(new_size) != 0)
return -1;
return idx;
}
template<class T> int BufferCache<T>::reallocate_buffer(size_t new_size) {
if (new_size == 0) {
clear();
return 0;
}
void *new_buffer = realloc(m_Buffer, new_size * sizeof(T));
if (new_buffer == NULL)
return 10;
m_Buffer = static_cast<T*>(new_buffer);
set_count(new_size);
return 0;
}
//declare whatever instances of the template
//you are going to use here:
template class BufferCache<BPEvent*>;
} /* namespace fi */