astyle --recursive --indent=spaces=2 --style=kr --indent-col1-comments --break-blocks --convert-tabs --max-code-length=200

This is done for better merging branch/LG_PRE_RELEASE_0.3 into trunk (same process is applied on this branch)

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@7073 818b1a75-f10b-46b9-bf7c-635c3b92a50f
This commit is contained in:
Lionel Gauthier
2015-04-09 09:29:12 +00:00
parent f0a8a0b3b5
commit 059a0a3fdc
2044 changed files with 1182666 additions and 1145123 deletions
+330 -336
View File
@@ -53,8 +53,8 @@
*/
hash_table_element_t * hash_table_element_new()
{
//INFO("creating a new hash table element");
return calloc(1, hash_table_element_s);
//INFO("creating a new hash table element");
return calloc(1, hash_table_element_s);
}
/**
@@ -64,17 +64,15 @@ hash_table_element_t * hash_table_element_new()
*/
void hash_table_element_delete(omg_hash_table_t * table, hash_table_element_t * element)
{
//INFO("Deleting an hash table element");
if (table->mode == MODE_COPY)
{
free(element->value);
free(element->key);
}
else if (table->mode == MODE_VALUEREF)
{
free(element->key);
}
free(element);
//INFO("Deleting an hash table element");
if (table->mode == MODE_COPY) {
free(element->value);
free(element->key);
} else if (table->mode == MODE_VALUEREF) {
free(element->key);
}
free(element);
}
// hash table operations
@@ -86,24 +84,26 @@ void hash_table_element_delete(omg_hash_table_t * table, hash_table_element_t *
*/
omg_hash_table_t * hash_table_new(hash_table_mode_t mode)
{
//INFO("Creating a new hash table");
omg_hash_table_t *table = calloc(1, SIZEOF_HASH_TABLE);
if (!table)
{
//INFO("No Memory while allocating hash_table");
return NULL;
}
table->mode = mode;
table->key_num = 128;
table->key_ratio = 4;
table->store_house = (hash_table_element_t **) calloc(table->key_num, sizeof(hash_table_element_t *));
if (!table->store_house)
{
//INFO("No Memory while allocating hash_table store house");
free(table);
return NULL;
}
return table;
//INFO("Creating a new hash table");
omg_hash_table_t *table = calloc(1, SIZEOF_HASH_TABLE);
if (!table) {
//INFO("No Memory while allocating hash_table");
return NULL;
}
table->mode = mode;
table->key_num = 128;
table->key_ratio = 4;
table->store_house = (hash_table_element_t **) calloc(table->key_num, sizeof(hash_table_element_t *));
if (!table->store_house) {
//INFO("No Memory while allocating hash_table store house");
free(table);
return NULL;
}
return table;
}
/**
@@ -112,19 +112,19 @@ omg_hash_table_t * hash_table_new(hash_table_mode_t mode)
*/
void hash_table_delete(omg_hash_table_t * table)
{
//INFO("Deleating a hash table");
size_t i=0;
for (;i<HASH_LEN;i++)
{
while (table->store_house[i])
{
hash_table_element_t * temp = table->store_house[i];
table->store_house[i] = table->store_house[i]->next;
hash_table_element_delete(table, temp);
}
//INFO("Deleating a hash table");
size_t i=0;
for (; i<HASH_LEN; i++) {
while (table->store_house[i]) {
hash_table_element_t * temp = table->store_house[i];
table->store_house[i] = table->store_house[i]->next;
hash_table_element_delete(table, temp);
}
free(table->store_house);
free(table);
}
free(table->store_house);
free(table);
}
/**
@@ -139,109 +139,99 @@ void hash_table_delete(omg_hash_table_t * table)
*/
int hash_table_add(omg_hash_table_t * table, void * key, size_t key_len, void * value, size_t value_len)
{
if ((table->key_count / table->key_num) >= table->key_ratio)
{
//LOG("Ratio(%d) reached the set limit %d\nExpanding hash_table", (table->key_count / table->key_num), table->key_ratio);
hash_table_resize(table, table->key_num*2);
//exit(0);
if ((table->key_count / table->key_num) >= table->key_ratio) {
//LOG("Ratio(%d) reached the set limit %d\nExpanding hash_table", (table->key_count / table->key_num), table->key_ratio);
hash_table_resize(table, table->key_num*2);
//exit(0);
}
size_t hash = HASH(key, key_len);
hash_table_element_t * element = hash_table_element_new();
if (!element) {
//INFO("Cannot allocate memory for element");
return -1; // No Memory
}
if (table->mode == MODE_COPY) {
//LOG("Adding a key-value pair to the hash table with hash -> %d, in COPY MODE", (int)hash);
element->key = malloc(key_len);
element->value = malloc(value_len);
if (element->key && element->value) {
memcpy(element->key, key, key_len);
memcpy(element->value, value, value_len);
} else {
if (element->key) {
free(element->key);
//INFO("Cannot allocate memory for value");
}
if (element->value) {
free(element->value);
//INFO("Cannot allocate memory for key");
}
free(element);
return -1; //No Memory
}
size_t hash = HASH(key, key_len);
hash_table_element_t * element = hash_table_element_new();
if (!element)
{
//INFO("Cannot allocate memory for element");
return -1; // No Memory
} else if (table->mode == MODE_VALUEREF) {
//LOG("Adding a key-value pair to the hash table with hash -> %d, in VALUEREF MODE", (int)hash);
element->key = malloc(key_len);
if (element->key) {
memcpy(element->key, key, key_len);
} else {
//INFO("Cannot allocate memory for key");
free(element);
return -1; //No Memory
}
if (table->mode == MODE_COPY)
{
//LOG("Adding a key-value pair to the hash table with hash -> %d, in COPY MODE", (int)hash);
element->key = malloc(key_len);
element->value = malloc(value_len);
if (element->key && element->value)
{
memcpy(element->key, key, key_len);
memcpy(element->value, value, value_len);
}
else
{
if (element->key)
{
free(element->key);
//INFO("Cannot allocate memory for value");
}
if (element->value)
{
free(element->value);
//INFO("Cannot allocate memory for key");
}
free(element);
return -1; //No Memory
element->value = value;
} else if (table->mode == MODE_ALLREF) {
//LOG("Adding a key-value pair to the hash table with hash -> %d, in ALLREF MODE", (int)hash);
element->key = key;
element->value = value;
}
element->key_len = key_len;
element->value_len = value_len;
element->next = NULL;
// find the key position for chaining
if (!table->store_house[hash]) {
//LOG("No Conflicts adding the first element at %d", (int)hash);
table->store_house[hash] = element;
table->key_count++;
} else {
//LOG("Conflicts adding element at %d", (int)hash);
hash_table_element_t * temp = table->store_house[hash];
while(temp->next) {
while(temp->next && temp->next->key_len!=key_len) {
temp = temp->next;
}
if(temp->next) {
if (!memcmp(temp->next->key, key, key_len)) {
//LOG("Found Key at hash -> %d", (int)hash);
hash_table_element_t *to_delete = temp->next;
temp->next = element;
element->next = to_delete->next;
hash_table_element_delete(table, to_delete);
// since we are replacing values no need to change key_count
return 0;
} else {
temp = temp->next;
}
}
}
else if (table->mode == MODE_VALUEREF)
{
//LOG("Adding a key-value pair to the hash table with hash -> %d, in VALUEREF MODE", (int)hash);
element->key = malloc(key_len);
if (element->key)
{
memcpy(element->key, key, key_len);
}
else
{
//INFO("Cannot allocate memory for key");
free(element);
return -1; //No Memory
}
element->value = value;
}
else if (table->mode == MODE_ALLREF)
{
//LOG("Adding a key-value pair to the hash table with hash -> %d, in ALLREF MODE", (int)hash);
element->key = key;
element->value = value;
}
element->key_len = key_len;
element->value_len = value_len;
element->next = NULL;
// find the key position for chaining
if (!table->store_house[hash])
{
//LOG("No Conflicts adding the first element at %d", (int)hash);
table->store_house[hash] = element;
table->key_count++;
}
else
{
//LOG("Conflicts adding element at %d", (int)hash);
hash_table_element_t * temp = table->store_house[hash];
while(temp->next)
{
while(temp->next && temp->next->key_len!=key_len)
{
temp = temp->next;
}
if(temp->next)
{
if (!memcmp(temp->next->key, key, key_len))
{
//LOG("Found Key at hash -> %d", (int)hash);
hash_table_element_t *to_delete = temp->next;
temp->next = element;
element->next = to_delete->next;
hash_table_element_delete(table, to_delete);
// since we are replacing values no need to change key_count
return 0;
}
else
{
temp = temp->next;
}
}
}
temp->next = element;
table->key_count++;
}
return 0;
temp->next = element;
table->key_count++;
}
return 0;
}
/**
@@ -254,51 +244,50 @@ int hash_table_add(omg_hash_table_t * table, void * key, size_t key_len, void *
*/
int hash_table_remove(omg_hash_table_t * table, void * key, size_t key_len)
{
//INFO("Deleting a key-value pair from the hash table");
if ((table->key_num/ table->key_count) >= table->key_ratio)
{
//LOG("Ratio(%d) reached the set limit %d\nContracting hash_table", (table->key_num / table->key_count), table->key_ratio);
hash_table_resize(table, table->key_num/2);
//exit(0);
}
size_t hash = HASH(key, key_len);
if (!table->store_house[hash])
{
//LOG("Key Not Found -> No element at %d", (int)hash);
return -1; // key not found
}
hash_table_element_t *temp = table->store_house[hash];
hash_table_element_t *prev = temp;
while(temp)
{
while(temp && temp->key_len!=key_len)
{
prev = temp;
temp = temp->next;
}
if(temp)
{
if (!memcmp(temp->key, key, key_len))
{
if (prev == table->store_house[hash])
{
table->store_house[hash] = temp->next;
}
else
{
prev->next = temp->next;
}
hash_table_element_delete(table, temp);
//INFO("Deleted a key-value pair from the hash table");
table->key_count--;
return 0;
}
prev=temp;
temp=temp->next;
}
}
//INFO("Key Not Found");
//INFO("Deleting a key-value pair from the hash table");
if ((table->key_num/ table->key_count) >= table->key_ratio) {
//LOG("Ratio(%d) reached the set limit %d\nContracting hash_table", (table->key_num / table->key_count), table->key_ratio);
hash_table_resize(table, table->key_num/2);
//exit(0);
}
size_t hash = HASH(key, key_len);
if (!table->store_house[hash]) {
//LOG("Key Not Found -> No element at %d", (int)hash);
return -1; // key not found
}
hash_table_element_t *temp = table->store_house[hash];
hash_table_element_t *prev = temp;
while(temp) {
while(temp && temp->key_len!=key_len) {
prev = temp;
temp = temp->next;
}
if(temp) {
if (!memcmp(temp->key, key, key_len)) {
if (prev == table->store_house[hash]) {
table->store_house[hash] = temp->next;
} else {
prev->next = temp->next;
}
hash_table_element_delete(table, temp);
//INFO("Deleted a key-value pair from the hash table");
table->key_count--;
return 0;
}
prev=temp;
temp=temp->next;
}
}
//INFO("Key Not Found");
return -1; // key not found
}
/**
@@ -311,35 +300,33 @@ int hash_table_remove(omg_hash_table_t * table, void * key, size_t key_len)
*/
void * hash_table_lookup(omg_hash_table_t * table, void * key, size_t key_len)
{
size_t hash = HASH(key, key_len);
//LOG("Looking up a key-value pair for hash -> %d", (int)hash);
if (!table->store_house[hash])
{
//LOG("Key not found at hash %d, no entries", (int)hash);
return NULL; // key not found
}
hash_table_element_t *temp = table->store_house[hash];
while(temp)
{
while(temp && temp->key_len!=key_len)
{
temp = temp->next;
}
if(temp)
{
if (!memcmp(temp->key, key, key_len))
{
//LOG("Found Key at hash -> %d", (int)hash);
return temp->value;
}
else
{
temp = temp->next;
}
}
}
//LOG("Key not found at hash %d", (int)hash);
size_t hash = HASH(key, key_len);
//LOG("Looking up a key-value pair for hash -> %d", (int)hash);
if (!table->store_house[hash]) {
//LOG("Key not found at hash %d, no entries", (int)hash);
return NULL; // key not found
}
hash_table_element_t *temp = table->store_house[hash];
while(temp) {
while(temp && temp->key_len!=key_len) {
temp = temp->next;
}
if(temp) {
if (!memcmp(temp->key, key, key_len)) {
//LOG("Found Key at hash -> %d", (int)hash);
return temp->value;
} else {
temp = temp->next;
}
}
}
//LOG("Key not found at hash %d", (int)hash);
return NULL; // key not found
}
/**
@@ -351,32 +338,33 @@ void * hash_table_lookup(omg_hash_table_t * table, void * key, size_t key_len)
*/
int hash_table_has_key(omg_hash_table_t * table, void * key, size_t key_len)
{
size_t hash = HASH(key, key_len);
//LOG("Searching for key with hash -> %d", (int)hash);
if (!table->store_house[hash])
{
//LOG("Key not found with hash -> %d, no entries", (int)hash);
return 0; // key not found
}
hash_table_element_t *temp = table->store_house[hash];
while(temp)
{
while(temp && temp->key_len!=key_len)
{
temp = temp->next;
}
if(temp)
{
if (!memcmp(temp->key, key, key_len))
{
//LOG("Key Found with hash -> %d", (int)hash);
return 1; // key found
}
temp=temp->next;
}
}
//LOG("Key not found with hash -> %d", (int)hash);
size_t hash = HASH(key, key_len);
//LOG("Searching for key with hash -> %d", (int)hash);
if (!table->store_house[hash]) {
//LOG("Key not found with hash -> %d, no entries", (int)hash);
return 0; // key not found
}
hash_table_element_t *temp = table->store_house[hash];
while(temp) {
while(temp && temp->key_len!=key_len) {
temp = temp->next;
}
if(temp) {
if (!memcmp(temp->key, key, key_len)) {
//LOG("Key Found with hash -> %d", (int)hash);
return 1; // key found
}
temp=temp->next;
}
}
//LOG("Key not found with hash -> %d", (int)hash);
return 0; // key not found
}
/**
@@ -387,32 +375,33 @@ int hash_table_has_key(omg_hash_table_t * table, void * key, size_t key_len)
*/
size_t hash_table_get_keys(omg_hash_table_t * table, void ** keys)
{
size_t i = 0;
size_t count = 0;
keys = calloc(table->key_count, sizeof(void *));
for(i=0;i<HASH_LEN;i++)
{
if (table->store_house[i])
{
keys[count++] = table->store_house[i];
hash_table_element_t *temp = table->store_house[i];
//#ifdef DEBUG
//size_t num = 1;
//#endif
while(temp->next)
{
keys[count++] = temp->next;
temp = temp->next;
//#ifdef DEBUG
//num++;
//#endif
}
//#ifdef DEBUG
//LOG("found %d key(s) at hash -> %d", (int)num, (int)i);
//#endif
}
size_t i = 0;
size_t count = 0;
keys = calloc(table->key_count, sizeof(void *));
for(i=0; i<HASH_LEN; i++) {
if (table->store_house[i]) {
keys[count++] = table->store_house[i];
hash_table_element_t *temp = table->store_house[i];
//#ifdef DEBUG
//size_t num = 1;
//#endif
while(temp->next) {
keys[count++] = temp->next;
temp = temp->next;
//#ifdef DEBUG
//num++;
//#endif
}
//#ifdef DEBUG
//LOG("found %d key(s) at hash -> %d", (int)num, (int)i);
//#endif
}
return count;
}
return count;
}
/**
@@ -424,37 +413,38 @@ size_t hash_table_get_keys(omg_hash_table_t * table, void ** keys)
*/
size_t hash_table_get_elements(omg_hash_table_t * table, hash_table_element_t *** elements)
{
size_t i = 0;
size_t count = 0;
(*elements) = (hash_table_element_t **) calloc(table->key_count, sizeof(hash_table_element_t *));
if (!*elements)
{
//INFO("No Memory to allocate elements array");
return 1;
size_t i = 0;
size_t count = 0;
(*elements) = (hash_table_element_t **) calloc(table->key_count, sizeof(hash_table_element_t *));
if (!*elements) {
//INFO("No Memory to allocate elements array");
return 1;
}
for(i=0; i<HASH_LEN; i++) {
if (table->store_house[i]) {
(*elements)[count++] = table->store_house[i];
hash_table_element_t *temp = table->store_house[i];
//#ifdef DEBUG
//size_t num = 1;
//#endif
while(temp->next) {
(*elements)[count++] = temp->next;
temp = temp->next;
//#ifdef DEBUG
//num++;
//#endif
}
//#ifdef DEBUG
//LOG("found %d key(s) at hash -> %d", (int)num, (int)i);
//#endif
}
for(i=0;i<HASH_LEN;i++)
{
if (table->store_house[i])
{
(*elements)[count++] = table->store_house[i];
hash_table_element_t *temp = table->store_house[i];
//#ifdef DEBUG
//size_t num = 1;
//#endif
while(temp->next)
{
(*elements)[count++] = temp->next;
temp = temp->next;
//#ifdef DEBUG
//num++;
//#endif
}
//#ifdef DEBUG
//LOG("found %d key(s) at hash -> %d", (int)num, (int)i);
//#endif
}
}
return count;
}
return count;
}
/**
@@ -466,16 +456,17 @@ size_t hash_table_get_elements(omg_hash_table_t * table, hash_table_element_t **
*/
uint16_t hash_table_do_hash(void * key, size_t key_len, uint16_t max_key)
{
uint16_t *ptr = (uint16_t *) key;
uint16_t hash = 0xbabe; // WHY NOT
size_t i = 0;
for(;i<(key_len/2);i++)
{
hash^=(i<<4 ^ *ptr<<8 ^ *ptr);
ptr++;
}
hash = hash % max_key;
return hash;
uint16_t *ptr = (uint16_t *) key;
uint16_t hash = 0xbabe; // WHY NOT
size_t i = 0;
for(; i<(key_len/2); i++) {
hash^=(i<<4 ^ *ptr<<8 ^ *ptr);
ptr++;
}
hash = hash % max_key;
return hash;
}
/**
@@ -488,38 +479,41 @@ uint16_t hash_table_do_hash(void * key, size_t key_len, uint16_t max_key)
*/
int hash_table_resize(omg_hash_table_t *table, size_t len)
{
//LOG("resizing hash table from %d to %d", table->key_num, len);
hash_table_element_t ** elements;
size_t count;
// FIXME traversing the elements twice, change it some time soon
count = hash_table_get_elements(table, &elements);
if (!count)
{
//INFO("Got No Elements from the hash table");
return -1;
}
// keep the current store house in case we dont get more memory
hash_table_element_t ** temp = table->store_house;
table->store_house = calloc(len, sizeof(hash_table_element_t *));
if (!table->store_house)
{
table->store_house = temp;
//INFO("No Memory for new store house");
return -2;
}
table->key_num = len;
// fool the new hash table so if refers even previously copied values
int mode = table->mode;
table->mode = MODE_ALLREF;
while(count>0)
{
hash_table_element_t *elem = elements[--count];
hash_table_add(table, elem->key, elem->key_len, elem->value, elem->value_len);
}
table->mode = mode;
// free old store house
free(temp);
return 0;
//LOG("resizing hash table from %d to %d", table->key_num, len);
hash_table_element_t ** elements;
size_t count;
// FIXME traversing the elements twice, change it some time soon
count = hash_table_get_elements(table, &elements);
if (!count) {
//INFO("Got No Elements from the hash table");
return -1;
}
// keep the current store house in case we dont get more memory
hash_table_element_t ** temp = table->store_house;
table->store_house = calloc(len, sizeof(hash_table_element_t *));
if (!table->store_house) {
table->store_house = temp;
//INFO("No Memory for new store house");
return -2;
}
table->key_num = len;
// fool the new hash table so if refers even previously copied values
int mode = table->mode;
table->mode = MODE_ALLREF;
while(count>0) {
hash_table_element_t *elem = elements[--count];
hash_table_add(table, elem->key, elem->key_len, elem->value, elem->value_len);
}
table->mode = mode;
// free old store house
free(temp);
return 0;
}