Add data store implementation (not used yet)

This commit is contained in:
simon.kagstrom 2010-02-06 07:33:56 +00:00
parent 959483352c
commit fc4eec3d64
2 changed files with 167 additions and 0 deletions

104
Src/data_store.cpp Normal file
View File

@ -0,0 +1,104 @@
#include <string.h>
#include <data_store.hh>
#include <utils.hh>
DataStore::DataStore()
{
this->registeredData = NULL;
this->n_registeredData = 0;
/* Convention: Odd numbers for the clients */
this->key_counter = 1;
}
DataStore::~DataStore()
{
free(this->registeredData);
}
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;
return NULL;
}
struct ds_data *DataStore::embedData(void *data, size_t sz)
{
struct ds_data *out;
out = (struct ds_data *)xmalloc(sizeof(struct ds_data) + sz);
out->key = this->key_counter;
out->metadata = 0; /* Setup by the embedder */
memcpy(out->data, data, sz);
panic_if(this->registerData(out->key, out) != NULL,
"Registering new data with key %u was non-NULL\n",
out->key);
out->key += 2;
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;

63
Src/data_store.hh Normal file
View File

@ -0,0 +1,63 @@
#ifndef __DATA_STORE_HH__
#define __DATA_STORE_HH__
#include <stdint.h>
struct ds_data
{
uint32_t key;
uint32_t metadata; /* Type etc */
uint8_t data[];
};
class DataStore;
class DataStore
{
public:
DataStore();
~DataStore();
/**
* Register a new datum.
*
* @param key The key to register with
* @param data The data to register
*
* @return the old datum with that key, or NULL
*/
struct ds_data *registerData(uint32_t key, struct ds_data *data);
/**
* Embed existing data into a data store. The new data is reallocated,
* but the old is not freed
*
* @param data The data to embed.
* @param sz the size of the data
*
* @return the new data store data.
*/
struct ds_data *embedData(void *data, size_t sz);
/**
* Unregister a datum.
*
* @param key The key to unregister
*
* @return the unregistered datum, or NULL if it didn't exist
*/
struct ds_data *unregisterData(uint32_t key);
struct ds_data *getData(uint32_t key);
static DataStore *ds;
private:
struct ds_data **registeredData;
int n_registeredData;
uint32_t key_counter;
};
#endif /* __DATA_STORE_HH__ */