2010-02-06 08:33:56 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <data_store.hh>
|
|
|
|
#include <utils.hh>
|
|
|
|
|
|
|
|
DataStore::DataStore()
|
|
|
|
{
|
|
|
|
this->registeredData = NULL;
|
|
|
|
this->n_registeredData = 0;
|
|
|
|
|
2010-02-07 15:24:50 +01:00
|
|
|
this->key_counter = 0;
|
2010-02-06 08:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DataStore::~DataStore()
|
|
|
|
{
|
|
|
|
free(this->registeredData);
|
|
|
|
}
|
|
|
|
|
2010-02-07 10:37:21 +01:00
|
|
|
struct ds_data *DataStore::registerNetworkData(uint32_t key, uint32_t metadata,
|
|
|
|
void *data, size_t data_sz)
|
|
|
|
{
|
|
|
|
struct ds_data *out;
|
|
|
|
|
|
|
|
out = (struct ds_data *)xmalloc(sizeof(struct ds_data) + data_sz);
|
|
|
|
out->key = key;
|
|
|
|
out->metadata = metadata;
|
2010-02-10 19:10:31 +01:00
|
|
|
out->sz = data_sz;
|
2010-02-07 10:37:21 +01:00
|
|
|
|
|
|
|
memcpy(out->data, data, data_sz);
|
|
|
|
|
|
|
|
return this->registerData(key, out);
|
|
|
|
}
|
|
|
|
|
2010-02-06 08:33:56 +01:00
|
|
|
struct ds_data *DataStore::registerData(uint32_t key,
|
|
|
|
struct ds_data *data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Replacing an existing entry? */
|
|
|
|
for (i = 0; i < this->n_registeredData; i++)
|
|
|
|
{
|
|
|
|
if (this->registeredData[i] &&
|
|
|
|
this->registeredData[i]->key == key)
|
|
|
|
{
|
|
|
|
struct ds_data *old = this->registeredData[i];
|
|
|
|
|
|
|
|
this->registeredData[i] = data;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < this->n_registeredData; i++)
|
|
|
|
{
|
|
|
|
/* Found free spot? */
|
|
|
|
if (this->registeredData[i] == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= this->n_registeredData)
|
|
|
|
{
|
|
|
|
this->n_registeredData++;
|
|
|
|
this->registeredData = (struct ds_data **)xrealloc(this->registeredData,
|
|
|
|
this->n_registeredData * sizeof(void*));
|
|
|
|
}
|
|
|
|
this->registeredData[i] = data;
|
2010-02-07 10:37:21 +01:00
|
|
|
data->key = key;
|
2010-02-06 08:33:56 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-07 10:52:36 +01:00
|
|
|
uint32_t DataStore::getNextKey()
|
2010-02-06 08:33:56 +01:00
|
|
|
{
|
2010-02-07 10:52:36 +01:00
|
|
|
uint32_t out = this->key_counter;
|
2010-02-06 08:33:56 +01:00
|
|
|
|
2010-02-07 15:24:50 +01:00
|
|
|
this->key_counter++;
|
2010-02-06 08:33:56 +01:00
|
|
|
|
2010-02-07 15:24:50 +01:00
|
|
|
if (this->key_counter >= DATA_KEY_RANGE)
|
|
|
|
this->key_counter = 0;
|
2010-02-06 08:33:56 +01:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ds_data *DataStore::unregisterData(uint32_t key)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < this->n_registeredData; i++)
|
|
|
|
{
|
|
|
|
if (this->registeredData[i] &&
|
|
|
|
this->registeredData[i]->key == key)
|
|
|
|
{
|
|
|
|
struct ds_data *old = this->registeredData[i];
|
|
|
|
|
|
|
|
this->registeredData[i] = NULL;
|
|
|
|
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ds_data *DataStore::getData(uint32_t key)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < this->n_registeredData; i++)
|
|
|
|
{
|
|
|
|
if (this->registeredData[i] &&
|
|
|
|
this->registeredData[i]->key == key)
|
|
|
|
return this->registeredData[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataStore *DataStore::ds = NULL;
|