http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/hash_map.c ---------------------------------------------------------------------- diff --git a/utils/private/src/hash_map.c b/utils/private/src/hash_map.c deleted file mode 100644 index bc514a7..0000000 --- a/utils/private/src/hash_map.c +++ /dev/null @@ -1,607 +0,0 @@ -/** - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * hash_map.c - * - * \date Jul 21, 2010 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ -#include "celixbool.h" -#include <stdio.h> -#include <stdlib.h> -#include <math.h> -#include <stdint.h> -#include <string.h> - -#include "hash_map.h" -#include "hash_map_private.h" - -static unsigned int DEFAULT_INITIAL_CAPACITY = 16; -static float DEFAULT_LOAD_FACTOR = 0.75f; -static unsigned int MAXIMUM_CAPACITY = 1 << 30; - -unsigned int hashMap_hashCode(const void * toHash) { - intptr_t address = (intptr_t) toHash; - return address; -} - -int hashMap_equals(const void * toCompare, const void * compare) { - return toCompare == compare; -} - -int hashMap_entryEquals(hash_map_pt map, hash_map_entry_pt entry, hash_map_entry_pt compare) { - if (entry->key == compare->key || map->equalsKey(entry->key, compare->key)) { - if (entry->value == compare->value || map->equalsValue(entry->value, compare->value)) { - return true; - } - } - return false; -} - -static unsigned int hashMap_hash(unsigned int h) { - h += ~(h << 9); - h ^= ((h >> 14) | (h << 18)); /* >>> */ - h += (h << 4); - h ^= ((h >> 10) | (h << 22)); /* >>> */ - return h; -} - -static unsigned int hashMap_indexFor(unsigned int h, unsigned int length) { - return h & (length - 1); -} - -hash_map_pt hashMap_create(unsigned int (*keyHash)(const void *), unsigned int (*valueHash)(const void *), - int (*keyEquals)(const void *, const void *), int (*valueEquals)(const void *, const void *)) { - hash_map_pt map = (hash_map_pt) malloc(sizeof(*map)); - map->treshold = (unsigned int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); - map->table = (hash_map_entry_pt *) calloc(DEFAULT_INITIAL_CAPACITY, sizeof(hash_map_entry_pt)); - map->size = 0; - map->modificationCount = 0; - map->tablelength = DEFAULT_INITIAL_CAPACITY; - map->hashKey = hashMap_hashCode; - map->hashValue = hashMap_hashCode; - map->equalsKey = hashMap_equals; - map->equalsValue = hashMap_equals; - - if (keyHash != NULL) { - map->hashKey = keyHash; - } - if (valueHash != NULL) { - map->hashValue = valueHash; - } - if (keyEquals != NULL) { - map->equalsKey = keyEquals; - } - if (valueEquals != NULL) { - map->equalsValue = valueEquals; - } - - return map; -} - -void hashMap_destroy(hash_map_pt map, bool freeKeys, bool freeValues) { - hashMap_clear(map, freeKeys, freeValues); - free(map->table); - free(map); -} - -int hashMap_size(hash_map_pt map) { - return map->size; -} - -bool hashMap_isEmpty(hash_map_pt map) { - return hashMap_size(map) == 0; -} - -void * hashMap_get(hash_map_pt map, const void* key) { - unsigned int hash; - if (key == NULL) { - hash_map_entry_pt entry; - for (entry = map->table[0]; entry != NULL; entry = entry->next) { - if (entry->key == NULL) { - return entry->value; - } - } - return NULL; - } - - hash = hashMap_hash(map->hashKey(key)); - hash_map_entry_pt entry = NULL; - for (entry = map->table[hashMap_indexFor(hash, map->tablelength)]; entry != NULL; entry = entry->next) { - if (entry->hash == hash && (entry->key == key || map->equalsKey(key, entry->key))) { - return entry->value; - } - } - return NULL; -} - -bool hashMap_containsKey(hash_map_pt map, const void* key) { - return hashMap_getEntry(map, key) != NULL; -} - -hash_map_entry_pt hashMap_getEntry(hash_map_pt map, const void* key) { - unsigned int hash = (key == NULL) ? 0 : hashMap_hash(map->hashKey(key)); - hash_map_entry_pt entry; - int index = hashMap_indexFor(hash, map->tablelength); - for (entry = map->table[index]; entry != NULL; entry = entry->next) { - if (entry->hash == hash && (entry->key == key || map->equalsKey(key, entry->key))) { - return entry; - } - } - return NULL; -} - -void * hashMap_put(hash_map_pt map, void * key, void * value) { - unsigned int hash; - int i; - if (key == NULL) { - hash_map_entry_pt entry; - for (entry = map->table[0]; entry != NULL; entry = entry->next) { - if (entry->key == NULL) { - void * oldValue = entry->value; - entry->value = value; - return oldValue; - } - } - map->modificationCount++; - hashMap_addEntry(map, 0, NULL, value, 0); - return NULL; - } - hash = hashMap_hash(map->hashKey(key)); - i = hashMap_indexFor(hash, map->tablelength); - - hash_map_entry_pt entry; - for (entry = map->table[i]; entry != NULL; entry = entry->next) { - if (entry->hash == hash && (entry->key == key || map->equalsKey(key, entry->key))) { - void * oldValue = entry->value; - entry->value = value; - return oldValue; - } - } - map->modificationCount++; - hashMap_addEntry(map, hash, key, value, i); - return NULL; -} - -void hashMap_resize(hash_map_pt map, int newCapacity) { - hash_map_entry_pt * newTable; - unsigned int j; - if (map->tablelength == MAXIMUM_CAPACITY) { - return; - } - - newTable = (hash_map_entry_pt *) calloc(newCapacity, sizeof(hash_map_entry_pt)); - - for (j = 0; j < map->tablelength; j++) { - hash_map_entry_pt entry = map->table[j]; - if (entry != NULL) { - map->table[j] = NULL; - do { - hash_map_entry_pt next = entry->next; - int i = hashMap_indexFor(entry->hash, newCapacity); - entry->next = newTable[i]; - newTable[i] = entry; - entry = next; - } while (entry != NULL); - } - } - free(map->table); - map->table = newTable; - map->tablelength = newCapacity; - map->treshold = (unsigned int) ceil(newCapacity * DEFAULT_LOAD_FACTOR); -} - -void * hashMap_remove(hash_map_pt map, const void* key) { - hash_map_entry_pt entry = hashMap_removeEntryForKey(map, key); - void * value = (entry == NULL ? NULL : entry->value); - if (entry != NULL) { - entry->key = NULL; - entry->value = NULL; - free(entry); - } - return value; -} - -hash_map_entry_pt hashMap_removeEntryForKey(hash_map_pt map, const void* key) { - unsigned int hash = (key == NULL) ? 0 : hashMap_hash(map->hashKey(key)); - int i = hashMap_indexFor(hash, map->tablelength); - hash_map_entry_pt prev = map->table[i]; - hash_map_entry_pt entry = prev; - - while (entry != NULL) { - hash_map_entry_pt next = entry->next; - if (entry->hash == hash && (entry->key == key || (key != NULL && map->equalsKey(key, entry->key)))) { - map->modificationCount++; - map->size--; - if (prev == entry) { - map->table[i] = next; - } else { - prev->next = next; - } - return entry; - } - prev = entry; - entry = next; - } - - return entry; -} - -hash_map_entry_pt hashMap_removeMapping(hash_map_pt map, hash_map_entry_pt entry) { - unsigned int hash; - hash_map_entry_pt prev; - hash_map_entry_pt e; - int i; - if (entry == NULL) { - return NULL; - } - hash = (entry->key == NULL) ? 0 : hashMap_hash(map->hashKey(entry->key)); - i = hashMap_indexFor(hash, map->tablelength); - prev = map->table[i]; - e = prev; - - while (e != NULL) { - hash_map_entry_pt next = e->next; - if (e->hash == hash && hashMap_entryEquals(map, e, entry)) { - map->modificationCount++; - map->size--; - if (prev == e) { - map->table[i] = next; - } else { - prev->next = next; - } - return e; - } - prev = e; - e = next; - } - - return e; -} - -void hashMap_clear(hash_map_pt map, bool freeKey, bool freeValue) { - unsigned int i; - hash_map_entry_pt * table; - map->modificationCount++; - table = map->table; - - for (i = 0; i < map->tablelength; i++) { - hash_map_entry_pt entry = table[i]; - while (entry != NULL) { - hash_map_entry_pt f = entry; - entry = entry->next; - if (freeKey && f->key != NULL) - free(f->key); - if (freeValue && f->value != NULL) - free(f->value); - free(f); - } - table[i] = NULL; - } - map->size = 0; -} - -bool hashMap_containsValue(hash_map_pt map, const void* value) { - unsigned int i; - if (value == NULL) { - for (i = 0; i < map->tablelength; i++) { - hash_map_entry_pt entry; - for (entry = map->table[i]; entry != NULL; entry = entry->next) { - if (entry->value == NULL) { - return true; - } - } - } - return false; - } - for (i = 0; i < map->tablelength; i++) { - hash_map_entry_pt entry; - for (entry = map->table[i]; entry != NULL; entry = entry->next) { - if (entry->value == value || map->equalsValue(entry->value, value)) { - return true; - } - } - } - return false; -} - -void hashMap_addEntry(hash_map_pt map, int hash, void* key, void* value, int bucketIndex) { - hash_map_entry_pt entry = map->table[bucketIndex]; - hash_map_entry_pt new = (hash_map_entry_pt) malloc(sizeof(*new)); - new->hash = hash; - new->key = key; - new->value = value; - new->next = entry; - map->table[bucketIndex] = new; - if (map->size++ >= map->treshold) { - hashMap_resize(map, 2 * map->tablelength); - } -} - -hash_map_iterator_pt hashMapIterator_alloc(void) { - return calloc(1, sizeof(hash_map_iterator_t)); -} - -void hashMapIterator_dealloc(hash_map_iterator_pt iterator) { - free(iterator); -} - -hash_map_iterator_pt hashMapIterator_create(hash_map_pt map) { - hash_map_iterator_pt iterator = hashMapIterator_alloc(); - hashMapIterator_init(map, iterator); - return iterator; -} - -UTILS_EXPORT hash_map_iterator_t hashMapIterator_construct(hash_map_pt map) { - hash_map_iterator_t iter; - memset(&iter, 0, sizeof(iter)); - hashMapIterator_init(map, &iter); - return iter; -} - -void hashMapIterator_init(hash_map_pt map, hash_map_iterator_pt iterator) { - iterator->map = map; - iterator->expectedModCount = map->modificationCount; - iterator->index = 0; - iterator->next = NULL; - iterator->current = NULL; - if (map->size > 0) { - while (iterator->index < map->tablelength && (iterator->next = map->table[iterator->index++]) == NULL) { - } - } -} - -void hashMapIterator_deinit(hash_map_iterator_pt iterator) { - iterator->current = NULL; - iterator->expectedModCount = 0; - iterator->index = 0; - iterator->map = NULL; - iterator->next = NULL; -} - -void hashMapIterator_destroy(hash_map_iterator_pt iterator) { - hashMapIterator_deinit(iterator); - hashMapIterator_dealloc(iterator); -} - -bool hashMapIterator_hasNext(hash_map_iterator_pt iterator) { - return iterator->next != NULL; -} - -void hashMapIterator_remove(hash_map_iterator_pt iterator) { - void * key; - hash_map_entry_pt entry; - if (iterator->current == NULL) { - return; - } - if (iterator->expectedModCount != iterator->map->modificationCount) { - return; - } - key = iterator->current->key; - iterator->current = NULL; - entry = hashMap_removeEntryForKey(iterator->map, key); - free(entry); - iterator->expectedModCount = iterator->map->modificationCount; -} - -void * hashMapIterator_nextValue(hash_map_iterator_pt iterator) { - hash_map_entry_pt entry; - if (iterator->expectedModCount != iterator->map->modificationCount) { - return NULL; - } - entry = iterator->next; - if (entry == NULL) { - return NULL; - } - if ((iterator->next = entry->next) == NULL) { - while (iterator->index < iterator->map->tablelength && (iterator->next = iterator->map->table[iterator->index++]) == NULL) { - } - } - iterator->current = entry; - return entry->value; -} - -void * hashMapIterator_nextKey(hash_map_iterator_pt iterator) { - hash_map_entry_pt entry; - if (iterator->expectedModCount != iterator->map->modificationCount) { - return NULL; - } - entry = iterator->next; - if (entry == NULL) { - return NULL; - } - if ((iterator->next = entry->next) == NULL) { - while (iterator->index < iterator->map->tablelength && (iterator->next = iterator->map->table[iterator->index++]) == NULL) { - } - } - iterator->current = entry; - return entry->key; -} - -hash_map_entry_pt hashMapIterator_nextEntry(hash_map_iterator_pt iterator) { - hash_map_entry_pt entry; - if (iterator->expectedModCount != iterator->map->modificationCount) { - return NULL; - } - entry = iterator->next; - if (entry == NULL) { - return NULL; - } - if ((iterator->next = entry->next) == NULL) { - while (iterator->index < iterator->map->tablelength && (iterator->next = iterator->map->table[iterator->index++]) == NULL) { - } - } - iterator->current = entry; - return entry; -} - -hash_map_key_set_pt hashMapKeySet_create(hash_map_pt map) { - hash_map_key_set_pt keySet = (hash_map_key_set_pt) malloc(sizeof(*keySet)); - keySet->map = map; - - return keySet; -} - -void hashMapKeySet_destroy(hash_map_key_set_pt keySet){ - keySet->map = NULL; - free(keySet); -} - -int hashMapKeySet_size(hash_map_key_set_pt keySet) { - return keySet->map->size; -} - -bool hashMapKeySet_contains(hash_map_key_set_pt keySet, const void* key) { - return hashMap_containsKey(keySet->map, key); -} - -bool hashMapKeySet_remove(hash_map_key_set_pt keySet, const void* key) { - hash_map_entry_pt entry = hashMap_removeEntryForKey(keySet->map, key); - bool removed = entry != NULL; - free(entry); - return removed; -} - -void hashMapKeySet_clear(hash_map_key_set_pt keySet) { - hashMap_clear(keySet->map, false, false); -} - -bool hashMapKeySet_isEmpty(hash_map_key_set_pt keySet) { - return hashMapKeySet_size(keySet) == 0; -} - -hash_map_values_pt hashMapValues_create(hash_map_pt map) { - hash_map_values_pt values = (hash_map_values_pt) malloc(sizeof(*values)); - values->map = map; - - return values; -} - -void hashMapValues_destroy(hash_map_values_pt values) { - values->map = NULL; - free(values); -} - -hash_map_iterator_pt hashMapValues_iterator(hash_map_values_pt values) { - return hashMapIterator_create(values->map); -} - -int hashMapValues_size(hash_map_values_pt values) { - return values->map->size; -} - -bool hashMapValues_contains(hash_map_values_pt values, const void* value) { - return hashMap_containsValue(values->map, value); -} - -void hashMapValues_toArray(hash_map_values_pt values, void* *array[], unsigned int *size) { - hash_map_iterator_pt it; - int i = 0; - int vsize = hashMapValues_size(values); - *size = vsize; - *array = malloc(vsize * sizeof(**array)); - it = hashMapValues_iterator(values); - while(hashMapIterator_hasNext(it) && i<vsize){ - (*array)[i++] = hashMapIterator_nextValue(it); - } - hashMapIterator_destroy(it); -} - -bool hashMapValues_remove(hash_map_values_pt values, const void* value) { - hash_map_iterator_pt iterator = hashMapValues_iterator(values); - if (value == NULL) { - while (hashMapIterator_hasNext(iterator)) { - if (hashMapIterator_nextValue(iterator) == NULL) { - hashMapIterator_remove(iterator); - hashMapIterator_destroy(iterator); - return true; - } - } - } else { - while (hashMapIterator_hasNext(iterator)) { - if (values->map->equalsValue(value, hashMapIterator_nextValue(iterator))) { - hashMapIterator_remove(iterator); - hashMapIterator_destroy(iterator); - return true; - } - } - } - hashMapIterator_destroy(iterator); - return false; -} - -void hashMapValues_clear(hash_map_values_pt values) { - hashMap_clear(values->map, false, false); -} - -bool hashMapValues_isEmpty(hash_map_values_pt values) { - return hashMapValues_size(values) == 0; -} - -hash_map_entry_set_pt hashMapEntrySet_create(hash_map_pt map) { - hash_map_entry_set_pt entrySet = (hash_map_entry_set_pt) malloc(sizeof(*entrySet)); - entrySet->map = map; - - return entrySet; -} - -void hashMapEntrySet_destroy(hash_map_entry_set_pt entrySet){ - entrySet->map = NULL; - free(entrySet); -} - -int hashMapEntrySet_size(hash_map_entry_set_pt entrySet) { - return entrySet->map->size; -} - -bool hashMapEntrySet_contains(hash_map_entry_set_pt entrySet, hash_map_entry_pt entry) { - return hashMap_containsValue(entrySet->map, entry); -} - -bool hashMapEntrySet_remove(hash_map_entry_set_pt entrySet, hash_map_entry_pt entry) { - hash_map_entry_pt temp = hashMap_removeMapping(entrySet->map, entry); - if (temp != NULL) { - free(temp); - return true; - } else { - return false; - } -} - -void hashMapEntrySet_clear(hash_map_entry_set_pt entrySet) { - hashMap_clear(entrySet->map, false, false); -} - -bool hashMapEntrySet_isEmpty(hash_map_entry_set_pt entrySet) { - return hashMapEntrySet_size(entrySet) == 0; -} - -void * hashMapEntry_getKey(hash_map_entry_pt entry) { - return entry->key; -} - -void * hashMapEntry_getValue(hash_map_entry_pt entry) { - return entry->value; -} - - - - -
http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/linked_list.c ---------------------------------------------------------------------- diff --git a/utils/private/src/linked_list.c b/utils/private/src/linked_list.c deleted file mode 100644 index 235c3e7..0000000 --- a/utils/private/src/linked_list.c +++ /dev/null @@ -1,268 +0,0 @@ -/** - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * linked_list.c - * - * \date Jul 16, 2010 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ -#include "celixbool.h" -#include <stdio.h> -#include <stdlib.h> - -#include "linked_list.h" -#include "linked_list_private.h" - -celix_status_t linkedList_create(linked_list_pt *list) { - linked_list_pt linked_list = malloc(sizeof(*linked_list)); - if (linked_list) { - linked_list->header = (linked_list_entry_pt) malloc(sizeof(*(linked_list->header))); - if (linked_list->header) { - linked_list->header->element = NULL; - linked_list->header->next = linked_list->header; - linked_list->header->previous = linked_list->header; - linked_list->size = 0; - linked_list->modificationCount = 0; - - *list = linked_list; - - return CELIX_SUCCESS; - } - } - - return CELIX_ENOMEM; -} - -UTILS_EXPORT celix_status_t linkedList_destroy(linked_list_pt list) { - celix_status_t status = CELIX_SUCCESS; - - linked_list_entry_pt current = NULL; - linked_list_entry_pt next = NULL; - - current = list->header->next; - - while (current != list->header) { - next = current->next; - free(current); - current = next; - } - - free(list->header); - free(list); - - return status; -} - -celix_status_t linkedList_clone(linked_list_pt list, linked_list_pt *clone) { - celix_status_t status; - - status = linkedList_create(clone); - if (status == CELIX_SUCCESS) { - struct linked_list_entry *e; - for (e = list->header->next; e != list->header; e = e->next) { - linkedList_addElement(*clone, e->element); - } - } - - return status; -} - -void * linkedList_getFirst(linked_list_pt list) { - if (list->size == 0) { - return NULL; - } - return list->header->next->element; -} - -void * linkedList_getLast(linked_list_pt list) { - if (list->size == 0) { - return NULL; - } - return list->header->previous->element; -} - -void * linkedList_removeFirst(linked_list_pt list) { - return linkedList_removeEntry(list, list->header->next); -} - -void * linkedList_removeLast(linked_list_pt list) { - return linkedList_removeEntry(list, list->header->previous); -} - -void linkedList_addFirst(linked_list_pt list, void * element) { - linkedList_addBefore(list, element, list->header->next); -} - -void linkedList_addLast(linked_list_pt list, void * element) { - linkedList_addBefore(list, element, list->header); -} - -bool linkedList_contains(linked_list_pt list, void * element) { - return linkedList_indexOf(list, element) != -1; -} - -int linkedList_size(linked_list_pt list) { - return list->size; -} - -bool linkedList_isEmpty(linked_list_pt list) { - return linkedList_size(list) == 0; -} - -bool linkedList_addElement(linked_list_pt list, void * element) { - linkedList_addBefore(list, element, list->header); - return true; -} - -bool linkedList_removeElement(linked_list_pt list, void * element) { - if (element == NULL) { - linked_list_entry_pt entry; - for (entry = list->header->next; entry != list->header; entry = entry->next) { - if (entry->element == NULL) { - linkedList_removeEntry(list, entry); - return true; - } - } - } else { - linked_list_entry_pt entry; - for (entry = list->header->next; entry != list->header; entry = entry->next) { - if (element == entry->element) { - linkedList_removeEntry(list, entry); - return true; - } - } - } - return false; -} - -void linkedList_clear(linked_list_pt list) { - linked_list_entry_pt entry = list->header->next; - while (entry != list->header) { - linked_list_entry_pt next = entry->next; - entry->next = entry->previous = NULL; - // free(entry->element); - entry->element = NULL; - free(entry); - entry = next; - } - list->header->next = list->header->previous = list->header; - list->size = 0; - list->modificationCount++; -} - -void * linkedList_get(linked_list_pt list, int index) { - return linkedList_entry(list, index)->element; -} -void * linkedList_set(linked_list_pt list, int index, void * element) { - linked_list_entry_pt entry = linkedList_entry(list, index); - void * old = entry->element; - entry->element = element; - return old; -} - -void linkedList_addIndex(linked_list_pt list, int index, void * element) { - linkedList_addBefore(list, element, (index == list->size ? list->header : linkedList_entry(list, index))); -} - -void * linkedList_removeIndex(linked_list_pt list, int index) { - return linkedList_removeEntry(list, linkedList_entry(list, index)); -} - -linked_list_entry_pt linkedList_entry(linked_list_pt list, int index) { - linked_list_entry_pt entry; - int i; - if (index < 0 || index >= list->size) { - return NULL; - } - - entry = list->header; - if (index < (list->size >> 1)) { - for (i = 0; i <= index; i++) { - entry = entry->next; - } - } else { - for (i = list->size; i > index; i--) { - entry = entry->previous; - } - } - return entry; -} - -int linkedList_indexOf(linked_list_pt list, void * element) { - int index = 0; - if (element == NULL) { - linked_list_entry_pt entry; - for (entry = list->header->next; entry != list->header; entry = entry->next) { - if (entry->element == NULL) { - return index; - } - index++; - } - } else { - linked_list_entry_pt entry; - for (entry = list->header->next; entry != list->header; entry = entry->next) { - if (element == entry->element) { - return index; - } - index++; - } - } - return -1; -} - -linked_list_entry_pt linkedList_addBefore(linked_list_pt list, void * element, linked_list_entry_pt entry) { - linked_list_entry_pt new = NULL; - - new = malloc(sizeof(*new)); - if (new != NULL) { - - new->element = element; - new->next = entry; - new->previous = entry->previous; - - new->previous->next = new; - new->next->previous = new; - - list->size++; - list->modificationCount++; - } - - return new; -} - -void * linkedList_removeEntry(linked_list_pt list, linked_list_entry_pt entry) { - void * result; - if (entry == list->header) { - return NULL; - } - - result = entry->element; - - entry->previous->next = entry->next; - entry->next->previous = entry->previous; - - entry->next = entry->previous = NULL; - free(entry); - - list->size--; - list->modificationCount++; - - return result; -} http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/linked_list_iterator.c ---------------------------------------------------------------------- diff --git a/utils/private/src/linked_list_iterator.c b/utils/private/src/linked_list_iterator.c deleted file mode 100644 index dc0e5c4..0000000 --- a/utils/private/src/linked_list_iterator.c +++ /dev/null @@ -1,153 +0,0 @@ -/** - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * linked_list_iterator.c - * - * \date Jul 16, 2010 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ -#include <stdio.h> -#include <stdlib.h> - -#include "linked_list_iterator.h" -#include "linked_list_private.h" - -struct linkedListIterator { - linked_list_entry_pt lastReturned; - linked_list_entry_pt next; - int nextIndex; - linked_list_pt list; - int expectedModificationCount; -}; - -linked_list_iterator_pt linkedListIterator_create(linked_list_pt list, unsigned int index) { - linked_list_iterator_pt iterator; - if (index > list->size) { - return NULL; - } - iterator = (linked_list_iterator_pt) malloc(sizeof(*iterator)); - iterator->lastReturned = list->header; - iterator->list = list; - iterator->expectedModificationCount = list->modificationCount; - if (index < (list->size >> 1)) { - iterator->next = iterator->list->header->next; - for (iterator->nextIndex = 0; iterator->nextIndex < index; iterator->nextIndex++) { - iterator->next = iterator->next->next; - } - } else { - iterator->next = list->header; - for (iterator->nextIndex = list->size; iterator->nextIndex > index; iterator->nextIndex--) { - iterator->next = iterator->next->previous; - } - } - return iterator; -} - -void linkedListIterator_destroy(linked_list_iterator_pt iterator) { - iterator->expectedModificationCount = 0; - iterator->lastReturned = NULL; - iterator->list = NULL; - iterator->next = NULL; - iterator->nextIndex = 0; - free(iterator); -} - -bool linkedListIterator_hasNext(linked_list_iterator_pt iterator) { - return iterator->nextIndex != iterator->list->size; -} - -void * linkedListIterator_next(linked_list_iterator_pt iterator) { - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return NULL; - } - if (iterator->nextIndex == iterator->list->size) { - return NULL; - } - iterator->lastReturned = iterator->next; - iterator->next = iterator->next->next; - iterator->nextIndex++; - return iterator->lastReturned->element; -} - -bool linkedListIterator_hasPrevious(linked_list_iterator_pt iterator) { - return iterator->nextIndex != 0; -} - -void * linkedListIterator_previous(linked_list_iterator_pt iterator) { - if (iterator->nextIndex == 0) { - return NULL; - } - - iterator->lastReturned = iterator->next = iterator->next->previous; - iterator->nextIndex--; - - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return NULL; - } - - return iterator->lastReturned->element; -} - -int linkedListIterator_nextIndex(linked_list_iterator_pt iterator) { - return iterator->nextIndex; -} - -int linkedListIterator_previousIndex(linked_list_iterator_pt iterator) { - return iterator->nextIndex-1; -} - -void linkedListIterator_remove(linked_list_iterator_pt iterator) { - linked_list_entry_pt lastNext; - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return; - } - lastNext = iterator->lastReturned->next; - if (linkedList_removeEntry(iterator->list, iterator->lastReturned) == NULL) { - return; - } - if (iterator->next == iterator->lastReturned) { - iterator->next = lastNext; - } else { - iterator->nextIndex--; - } - iterator->lastReturned = iterator->list->header; - iterator->expectedModificationCount++; -} - -void linkedListIterator_set(linked_list_iterator_pt iterator, void * element) { - if (iterator->lastReturned == iterator->list->header) { - return; - } - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return; - } - iterator->lastReturned->element = element; -} - -void linkedListIterator_add(linked_list_iterator_pt iterator, void * element) { - if (iterator->list->modificationCount != iterator->expectedModificationCount) { - return; - } - iterator->lastReturned = iterator->list->header; - linkedList_addBefore(iterator->list, element, iterator->next); - iterator->nextIndex++; - iterator->expectedModificationCount++; -} - http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/memstream/fmemopen.c ---------------------------------------------------------------------- diff --git a/utils/private/src/memstream/fmemopen.c b/utils/private/src/memstream/fmemopen.c deleted file mode 100644 index cb1b0c0..0000000 --- a/utils/private/src/memstream/fmemopen.c +++ /dev/null @@ -1,76 +0,0 @@ - -/* - * fmem.c : fmemopen() on top of BSD's funopen() - * 20081017 AF - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -struct fmem { - size_t pos; - size_t size; - char *buffer; -}; -typedef struct fmem fmem_t; - -static int readfn(void *handler, char *buf, int size) -{ - int count = 0; - fmem_t *mem = handler; - size_t available = mem->size - mem->pos; - - if(size > available) size = available; - for(count=0; count < size; mem->pos++, count++) - buf[count] = mem->buffer[mem->pos]; - - return count; -} - -static int writefn(void *handler, const char *buf, int size) -{ - int count = 0; - fmem_t *mem = handler; - size_t available = mem->size - mem->pos; - - if(size > available) size = available; - for(count=0; count < size; mem->pos++, count++) - mem->buffer[mem->pos] = buf[count]; - - return count; // ? count : size; -} - -static fpos_t seekfn(void *handler, fpos_t offset, int whence) -{ - size_t pos; - fmem_t *mem = handler; - - switch(whence) { - case SEEK_SET: pos = offset; break; - case SEEK_CUR: pos = mem->pos + offset; break; - case SEEK_END: pos = mem->size + offset; break; - default: return -1; - } - - if(pos > mem->size) return -1; - - mem->pos = pos; - return (fpos_t) pos; -} - -static int closefn(void *handler) -{ - free(handler); - return 0; -} - -/* simple, but portable version of fmemopen for OS X / BSD */ -FILE *fmemopen(void *buf, size_t size, const char *mode) -{ - fmem_t *mem = (fmem_t *) malloc(sizeof(fmem_t)); - - memset(mem, 0, sizeof(fmem_t)); - mem->size = size, mem->buffer = buf; - return funopen(mem, readfn, writefn, seekfn, closefn); -} http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/memstream/open_memstream.c ---------------------------------------------------------------------- diff --git a/utils/private/src/memstream/open_memstream.c b/utils/private/src/memstream/open_memstream.c deleted file mode 100644 index 6bc4f01..0000000 --- a/utils/private/src/memstream/open_memstream.c +++ /dev/null @@ -1,130 +0,0 @@ -/* Use funopen(3) to provide open_memstream(3) like functionality. */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> - -struct memstream { - char **cp; - size_t *lenp; - size_t offset; -}; - -static void -memstream_grow(struct memstream *ms, size_t newsize) -{ - char *buf; - - if (newsize > *ms->lenp) { - buf = realloc(*ms->cp, newsize + 1); - if (buf != NULL) { -#ifdef DEBUG - fprintf(stderr, "MS: %p growing from %zd to %zd\n", - ms, *ms->lenp, newsize); -#endif - memset(buf + *ms->lenp + 1, 0, newsize - *ms->lenp); - *ms->cp = buf; - *ms->lenp = newsize; - } - } -} - -static int -memstream_read(void *cookie, char *buf, int len) -{ - struct memstream *ms; - int tocopy; - - ms = cookie; - memstream_grow(ms, ms->offset + len); - tocopy = *ms->lenp - ms->offset; - if (len < tocopy) - tocopy = len; - memcpy(buf, *ms->cp + ms->offset, tocopy); - ms->offset += tocopy; -#ifdef DEBUG - fprintf(stderr, "MS: read(%p, %d) = %d\n", ms, len, tocopy); -#endif - return (tocopy); -} - -static int -memstream_write(void *cookie, const char *buf, int len) -{ - struct memstream *ms; - int tocopy; - - ms = cookie; - memstream_grow(ms, ms->offset + len); - tocopy = *ms->lenp - ms->offset; - if (len < tocopy) - tocopy = len; - memcpy(*ms->cp + ms->offset, buf, tocopy); - ms->offset += tocopy; -#ifdef DEBUG - fprintf(stderr, "MS: write(%p, %d) = %d\n", ms, len, tocopy); -#endif - return (tocopy); -} - -static fpos_t -memstream_seek(void *cookie, fpos_t pos, int whence) -{ - struct memstream *ms; -#ifdef DEBUG - size_t old; -#endif - - ms = cookie; -#ifdef DEBUG - old = ms->offset; -#endif - switch (whence) { - case SEEK_SET: - ms->offset = pos; - break; - case SEEK_CUR: - ms->offset += pos; - break; - case SEEK_END: - ms->offset = *ms->lenp + pos; - break; - } -#ifdef DEBUG - fprintf(stderr, "MS: seek(%p, %zd, %d) %zd -> %zd\n", ms, pos, whence, - old, ms->offset); -#endif - return (ms->offset); -} - -static int -memstream_close(void *cookie) -{ - - free(cookie); - return (0); -} - -FILE * -open_memstream(char **cp, size_t *lenp) -{ - struct memstream *ms; - int save_errno; - FILE *fp; - - *cp = NULL; - *lenp = 0; - ms = malloc(sizeof(*ms)); - ms->cp = cp; - ms->lenp = lenp; - ms->offset = 0; - fp = funopen(ms, memstream_read, memstream_write, memstream_seek, - memstream_close); - if (fp == NULL) { - save_errno = errno; - free(ms); - errno = save_errno; - } - return (fp); -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/properties.c ---------------------------------------------------------------------- diff --git a/utils/private/src/properties.c b/utils/private/src/properties.c deleted file mode 100644 index 0bd6dc3..0000000 --- a/utils/private/src/properties.c +++ /dev/null @@ -1,302 +0,0 @@ -/** - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * properties.c - * - * \date Apr 27, 2010 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <ctype.h> -#include "celixbool.h" -#include "properties.h" -#include "utils.h" - -#define MALLOC_BLOCK_SIZE 5 - -static void parseLine(const char* line, properties_pt props); - -properties_pt properties_create(void) { - return hashMap_create(utils_stringHash, utils_stringHash, utils_stringEquals, utils_stringEquals); -} - -void properties_destroy(properties_pt properties) { - hash_map_iterator_pt iter = hashMapIterator_create(properties); - while (hashMapIterator_hasNext(iter)) { - hash_map_entry_pt entry = hashMapIterator_nextEntry(iter); - free(hashMapEntry_getKey(entry)); - free(hashMapEntry_getValue(entry)); - } - hashMapIterator_destroy(iter); - hashMap_destroy(properties, false, false); -} - -properties_pt properties_load(const char* filename) { - FILE *file = fopen(filename, "r"); - if(file==NULL){ - return NULL; - } - properties_pt props = properties_loadWithStream(file); - fclose(file); - return props; -} - -properties_pt properties_loadWithStream(FILE *file) { - properties_pt props = NULL; - - - if (file != NULL ) { - char *saveptr; - char *filebuffer = NULL; - char *line = NULL; - size_t file_size = 0; - - props = properties_create(); - fseek(file, 0, SEEK_END); - file_size = ftell(file); - fseek(file, 0, SEEK_SET); - - if(file_size > 0){ - filebuffer = calloc(file_size + 1, sizeof(char)); - if(filebuffer) { - size_t rs = fread(filebuffer, sizeof(char), file_size, file); - if(rs != file_size){ - fprintf(stderr,"fread read only %lu bytes out of %lu\n",rs,file_size); - } - filebuffer[file_size]='\0'; - line = strtok_r(filebuffer, "\n", &saveptr); - while ( line != NULL ) { - parseLine(line, props); - line = strtok_r(NULL, "\n", &saveptr); - } - free(filebuffer); - } - } - } - - return props; -} - - -/** - * Header is ignored for now, cannot handle comments yet - */ -void properties_store(properties_pt properties, const char* filename, const char* header) { - FILE *file = fopen ( filename, "w+" ); - char *str; - - if (file != NULL) { - if (hashMap_size(properties) > 0) { - hash_map_iterator_pt iterator = hashMapIterator_create(properties); - while (hashMapIterator_hasNext(iterator)) { - hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator); - str = hashMapEntry_getKey(entry); - for (int i = 0; i < strlen(str); i += 1) { - if (str[i] == '#' || str[i] == '!' || str[i] == '=' || str[i] == ':') { - fputc('\\', file); - } - fputc(str[i], file); - } - - fputc('=', file); - - str = hashMapEntry_getValue(entry); - for (int i = 0; i < strlen(str); i += 1) { - if (str[i] == '#' || str[i] == '!' || str[i] == '=' || str[i] == ':') { - fputc('\\', file); - } - fputc(str[i], file); - } - - fputc('\n', file); - - } - hashMapIterator_destroy(iterator); - } - fclose(file); - } else { - perror("File is null"); - } -} - -celix_status_t properties_copy(properties_pt properties, properties_pt *out) { - celix_status_t status = CELIX_SUCCESS; - properties_pt copy = properties_create(); - - if (copy != NULL) { - hash_map_iterator_pt iter = hashMapIterator_create(properties); - while (hashMapIterator_hasNext(iter)) { - hash_map_entry_pt entry = hashMapIterator_nextEntry(iter); - char *key = hashMapEntry_getKey(entry); - char *value = hashMapEntry_getValue(entry); - properties_set(copy, key, value); - } - hashMapIterator_destroy(iter); - } else { - status = CELIX_ENOMEM; - } - - if (status == CELIX_SUCCESS) { - *out = copy; - } - - return status; -} - -const char* properties_get(properties_pt properties, const char* key) { - return hashMap_get(properties, (void*)key); -} - -const char* properties_getWithDefault(properties_pt properties, const char* key, const char* defaultValue) { - const char* value = properties_get(properties, key); - return value == NULL ? defaultValue : value; -} - -void properties_set(properties_pt properties, const char* key, const char* value) { - hash_map_entry_pt entry = hashMap_getEntry(properties, key); - char* oldValue = NULL; - if (entry != NULL) { - char* oldKey = hashMapEntry_getKey(entry); - oldValue = hashMapEntry_getValue(entry); - hashMap_put(properties, oldKey, strndup(value, 1024*10)); - } else { - hashMap_put(properties, strndup(key, 1024*10), strndup(value, 1024*10)); - } - free(oldValue); -} - -static void updateBuffers(char **key, char ** value, char **output, int outputPos, int *key_len, int *value_len) { - if (*output == *key) { - if (outputPos == (*key_len) - 1) { - (*key_len) += MALLOC_BLOCK_SIZE; - *key = realloc(*key, *key_len); - *output = *key; - } - } - else { - if (outputPos == (*value_len) - 1) { - (*value_len) += MALLOC_BLOCK_SIZE; - *value = realloc(*value, *value_len); - *output = *value; - } - } -} - -static void parseLine(const char* line, properties_pt props) { - int linePos = 0; - bool precedingCharIsBackslash = false; - bool isComment = false; - int outputPos = 0; - char *output = NULL; - int key_len = MALLOC_BLOCK_SIZE; - int value_len = MALLOC_BLOCK_SIZE; - linePos = 0; - precedingCharIsBackslash = false; - isComment = false; - output = NULL; - outputPos = 0; - - //Ignore empty lines - if (line[0] == '\n' && line[1] == '\0') { - return; - } - - char *key = calloc(1, key_len); - char *value = calloc(1, value_len); - key[0] = '\0'; - value[0] = '\0'; - - while (line[linePos] != '\0') { - if (line[linePos] == ' ' || line[linePos] == '\t') { - if (output == NULL) { - //ignore - linePos += 1; - continue; - } - } - else { - if (output == NULL) { - output = key; - } - } - if (line[linePos] == '=' || line[linePos] == ':' || line[linePos] == '#' || line[linePos] == '!') { - if (precedingCharIsBackslash) { - //escaped special character - output[outputPos++] = line[linePos]; - updateBuffers(&key, &value, &output, outputPos, &key_len, &value_len); - precedingCharIsBackslash = false; - } - else { - if (line[linePos] == '#' || line[linePos] == '!') { - if (outputPos == 0) { - isComment = true; - break; - } - else { - output[outputPos++] = line[linePos]; - updateBuffers(&key, &value, &output, outputPos, &key_len, &value_len); - } - } - else { // = or : - if (output == value) { //already have a seperator - output[outputPos++] = line[linePos]; - updateBuffers(&key, &value, &output, outputPos, &key_len, &value_len); - } - else { - output[outputPos++] = '\0'; - updateBuffers(&key, &value, &output, outputPos, &key_len, &value_len); - output = value; - outputPos = 0; - } - } - } - } - else if (line[linePos] == '\\') { - if (precedingCharIsBackslash) { //double backslash -> backslash - output[outputPos++] = '\\'; - updateBuffers(&key, &value, &output, outputPos, &key_len, &value_len); - } - precedingCharIsBackslash = true; - } - else { //normal character - precedingCharIsBackslash = false; - output[outputPos++] = line[linePos]; - updateBuffers(&key, &value, &output, outputPos, &key_len, &value_len); - } - linePos += 1; - } - if (output != NULL) { - output[outputPos] = '\0'; - } - - if (!isComment) { - //printf("putting 'key'/'value' '%s'/'%s' in properties\n", utils_stringTrim(key), utils_stringTrim(value)); - properties_set(props, utils_stringTrim(key), utils_stringTrim(value)); - } - if(key) { - free(key); - } - if(value) { - free(value); - } - -} http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/thpool.c ---------------------------------------------------------------------- diff --git a/utils/private/src/thpool.c b/utils/private/src/thpool.c deleted file mode 100644 index 5121fca..0000000 --- a/utils/private/src/thpool.c +++ /dev/null @@ -1,535 +0,0 @@ -/* ******************************** - * Author: Johan Hanssen Seferidis - * License: MIT - * Description: Library providing a threading pool where you can add - * work. For usage, check the thpool.h file or README.md - * - *//** @file thpool.h *//* - * - ********************************/ - - -#include <unistd.h> -#include <signal.h> -#include <stdio.h> -#include <stdlib.h> -#include <pthread.h> -#include <errno.h> -#include <time.h> -#include "thpool.h" - -#ifdef THPOOL_DEBUG -#define THPOOL_DEBUG 1 -#else -#define THPOOL_DEBUG 0 -#endif - -static volatile int threads_keepalive; -static volatile int threads_on_hold; - - - -/* ========================== STRUCTURES ============================ */ - - -/* Binary semaphore */ -typedef struct bsem { - pthread_mutex_t mutex; - pthread_cond_t cond; - int v; -} bsem; - - -/* Job */ -typedef struct job{ - struct job* prev; /* pointer to previous job */ - void* (*function)(void* arg); /* function pointer */ - void* arg; /* function's argument */ -} job; - - -/* Job queue */ -typedef struct jobqueue{ - pthread_mutex_t rwmutex; /* used for queue r/w access */ - job *front; /* pointer to front of queue */ - job *rear; /* pointer to rear of queue */ - bsem *has_jobs; /* flag as binary semaphore */ - int len; /* number of jobs in queue */ -} jobqueue; - - -/* Thread */ -typedef struct thread{ - int id; /* friendly id */ - pthread_t pthread; /* pointer to actual thread */ - struct thpool_* thpool_p; /* access to thpool */ -} thread; - - -/* Threadpool */ -typedef struct thpool_{ - thread** threads; /* pointer to threads */ - volatile int num_threads_alive; /* threads currently alive */ - volatile int num_threads_working; /* threads currently working */ - pthread_mutex_t thcount_lock; /* used for thread count etc */ - pthread_cond_t threads_all_idle; /* signal to thpool_wait */ - jobqueue* jobqueue_p; /* pointer to the job queue */ -} thpool_; - - - - - -/* ========================== PROTOTYPES ============================ */ - - -static void thread_init(thpool_* thpool_p, struct thread** thread_p, int id); -static void* thread_do(struct thread* thread_p); -static void thread_hold(); -static void thread_destroy(struct thread* thread_p); - -static int jobqueue_init(thpool_* thpool_p); -static void jobqueue_clear(thpool_* thpool_p); -static void jobqueue_push(thpool_* thpool_p, struct job* newjob_p); -static struct job* jobqueue_pull(thpool_* thpool_p); -static void jobqueue_destroy(thpool_* thpool_p); - -static void bsem_init(struct bsem *bsem_p, int value); -static void bsem_reset(struct bsem *bsem_p); -static void bsem_post(struct bsem *bsem_p); -static void bsem_post_all(struct bsem *bsem_p); -static void bsem_wait(struct bsem *bsem_p); - - - - - -/* ========================== THREADPOOL ============================ */ - - -/* Initialise thread pool */ -struct thpool_* thpool_init(int num_threads){ - - threads_on_hold = 0; - threads_keepalive = 1; - - if ( num_threads < 0){ - num_threads = 0; - } - - /* Make new thread pool */ - thpool_* thpool_p; - thpool_p = (struct thpool_*)malloc(sizeof(struct thpool_)); - if (thpool_p == NULL){ - fprintf(stderr, "thpool_init(): Could not allocate memory for thread pool\n"); - return NULL; - } - thpool_p->num_threads_alive = 0; - thpool_p->num_threads_working = 0; - - /* Initialise the job queue */ - if (jobqueue_init(thpool_p) == -1){ - fprintf(stderr, "thpool_init(): Could not allocate memory for job queue\n"); - free(thpool_p); - return NULL; - } - - /* Make threads in pool */ - thpool_p->threads = (struct thread**)malloc(num_threads * sizeof(struct thread*)); - if (thpool_p->threads == NULL){ - fprintf(stderr, "thpool_init(): Could not allocate memory for threads\n"); - jobqueue_destroy(thpool_p); - free(thpool_p->jobqueue_p); - free(thpool_p); - return NULL; - } - - pthread_mutex_init(&(thpool_p->thcount_lock), NULL); - pthread_cond_init(&thpool_p->threads_all_idle, NULL); - - /* Thread init */ - int n; - for (n=0; n<num_threads; n++){ - thread_init(thpool_p, &thpool_p->threads[n], n); - if (THPOOL_DEBUG) - printf("THPOOL_DEBUG: Created thread %d in pool \n", n); - } - - /* Wait for threads to initialize */ - while (thpool_p->num_threads_alive != num_threads) {} - - return thpool_p; -} - - -/* Add work to the thread pool */ -int thpool_add_work(thpool_* thpool_p, void *(*function_p)(void*), void* arg_p){ - job* newjob; - - newjob=(struct job*)malloc(sizeof(struct job)); - if (newjob==NULL){ - fprintf(stderr, "thpool_add_work(): Could not allocate memory for new job\n"); - return -1; - } - - /* add function and argument */ - newjob->function=function_p; - newjob->arg=arg_p; - - /* add job to queue */ - pthread_mutex_lock(&thpool_p->jobqueue_p->rwmutex); - jobqueue_push(thpool_p, newjob); - pthread_mutex_unlock(&thpool_p->jobqueue_p->rwmutex); - - return 0; -} - - -/* Wait until all jobs have finished */ -void thpool_wait(thpool_* thpool_p){ - pthread_mutex_lock(&thpool_p->thcount_lock); - while (thpool_p->jobqueue_p->len || thpool_p->num_threads_working) { - pthread_cond_wait(&thpool_p->threads_all_idle, &thpool_p->thcount_lock); - } - pthread_mutex_unlock(&thpool_p->thcount_lock); -} - - -/* Destroy the threadpool */ -void thpool_destroy(thpool_* thpool_p){ - - volatile int threads_total = thpool_p->num_threads_alive; - - /* End each thread 's infinite loop */ - threads_keepalive = 0; - - /* Give one second to kill idle threads */ - double TIMEOUT = 1.0; - time_t start, end; - double tpassed = 0.0; - time (&start); - while (tpassed < TIMEOUT && thpool_p->num_threads_alive){ - bsem_post_all(thpool_p->jobqueue_p->has_jobs); - time (&end); - tpassed = difftime(end,start); - } - - /* Poll remaining threads */ - while (thpool_p->num_threads_alive){ - bsem_post_all(thpool_p->jobqueue_p->has_jobs); - sleep(1); - } - - /* Job queue cleanup */ - jobqueue_destroy(thpool_p); - free(thpool_p->jobqueue_p); - - /* Deallocs */ - int n; - for (n=0; n < threads_total; n++){ - thread_destroy(thpool_p->threads[n]); - } - free(thpool_p->threads); - free(thpool_p); -} - - -/* Pause all threads in threadpool */ -void thpool_pause(thpool_* thpool_p) { - int n; - for (n=0; n < thpool_p->num_threads_alive; n++){ - pthread_kill(thpool_p->threads[n]->pthread, SIGUSR1); - } -} - - -/* Resume all threads in threadpool */ -void thpool_resume(thpool_* thpool_p) { - threads_on_hold = 0; -} - - - - - -/* ============================ THREAD ============================== */ - - -/* Initialize a thread in the thread pool - * - * @param thread address to the pointer of the thread to be created - * @param id id to be given to the thread - * - */ -static void thread_init (thpool_* thpool_p, struct thread** thread_p, int id){ - - *thread_p = (struct thread*)malloc(sizeof(struct thread)); - if (*thread_p == NULL){ - fprintf(stderr, "thpool_init(): Could not allocate memory for thread\n"); - exit(1); - } - - (*thread_p)->thpool_p = thpool_p; - (*thread_p)->id = id; - - pthread_create(&(*thread_p)->pthread, NULL, (void *)thread_do, (*thread_p)); - pthread_detach((*thread_p)->pthread); - -} - - -/* Sets the calling thread on hold */ -static void thread_hold () { - threads_on_hold = 1; - while (threads_on_hold){ - sleep(1); - } -} - - -/* What each thread is doing -* -* In principle this is an endless loop. The only time this loop gets interuppted is once -* thpool_destroy() is invoked or the program exits. -* -* @param thread thread that will run this function -* @return nothing -*/ -static void* thread_do(struct thread* thread_p){ - - /* Set thread name for profiling and debuging */ - char thread_name[128] = {0}; - sprintf(thread_name, "thread-pool-%d", thread_p->id); - -#if defined(__linux__) - pthread_setname_np(thread_p->pthread, thread_name); -#elif defined(__APPLE__) && defined(__MACH__) - pthread_setname_np(thread_name); -#else - fprintf(stderr, "thread_do(): pthread_setname_np is not supported on this system"); -#endif - - /* Assure all threads have been created before starting serving */ - thpool_* thpool_p = thread_p->thpool_p; - - /* Register signal handler */ - struct sigaction act; - sigemptyset(&act.sa_mask); - act.sa_flags = 0; - act.sa_handler = thread_hold; - if (sigaction(SIGUSR1, &act, NULL) == -1) { - fprintf(stderr, "thread_do(): cannot handle SIGUSR1"); - } - - /* Mark thread as alive (initialized) */ - pthread_mutex_lock(&thpool_p->thcount_lock); - thpool_p->num_threads_alive += 1; - pthread_mutex_unlock(&thpool_p->thcount_lock); - - while(threads_keepalive){ - - bsem_wait(thpool_p->jobqueue_p->has_jobs); - - if (threads_keepalive){ - - pthread_mutex_lock(&thpool_p->thcount_lock); - thpool_p->num_threads_working++; - pthread_mutex_unlock(&thpool_p->thcount_lock); - - /* Read job from queue and execute it */ - void*(*func_buff)(void* arg); - void* arg_buff; - job* job_p; - pthread_mutex_lock(&thpool_p->jobqueue_p->rwmutex); - job_p = jobqueue_pull(thpool_p); - pthread_mutex_unlock(&thpool_p->jobqueue_p->rwmutex); - if (job_p) { - func_buff = job_p->function; - arg_buff = job_p->arg; - func_buff(arg_buff); - free(job_p); - } - - pthread_mutex_lock(&thpool_p->thcount_lock); - thpool_p->num_threads_working--; - if (!thpool_p->num_threads_working) { - pthread_cond_signal(&thpool_p->threads_all_idle); - } - pthread_mutex_unlock(&thpool_p->thcount_lock); - - } - } - pthread_mutex_lock(&thpool_p->thcount_lock); - thpool_p->num_threads_alive --; - pthread_mutex_unlock(&thpool_p->thcount_lock); - - return NULL; -} - - -/* Frees a thread */ -static void thread_destroy (thread* thread_p){ - free(thread_p); -} - - - - - -/* ============================ JOB QUEUE =========================== */ - - -/* Initialize queue */ -static int jobqueue_init(thpool_* thpool_p){ - - thpool_p->jobqueue_p = (struct jobqueue*)malloc(sizeof(struct jobqueue)); - if (thpool_p->jobqueue_p == NULL){ - return -1; - } - thpool_p->jobqueue_p->len = 0; - thpool_p->jobqueue_p->front = NULL; - thpool_p->jobqueue_p->rear = NULL; - - thpool_p->jobqueue_p->has_jobs = (struct bsem*)malloc(sizeof(struct bsem)); - if (thpool_p->jobqueue_p->has_jobs == NULL){ - return -1; - } - - pthread_mutex_init(&(thpool_p->jobqueue_p->rwmutex), NULL); - bsem_init(thpool_p->jobqueue_p->has_jobs, 0); - - return 0; -} - - -/* Clear the queue */ -static void jobqueue_clear(thpool_* thpool_p){ - - while(thpool_p->jobqueue_p->len){ - free(jobqueue_pull(thpool_p)); - } - - thpool_p->jobqueue_p->front = NULL; - thpool_p->jobqueue_p->rear = NULL; - bsem_reset(thpool_p->jobqueue_p->has_jobs); - thpool_p->jobqueue_p->len = 0; - -} - - -/* Add (allocated) job to queue - * - * Notice: Caller MUST hold a mutex - */ -static void jobqueue_push(thpool_* thpool_p, struct job* newjob){ - - newjob->prev = NULL; - - switch(thpool_p->jobqueue_p->len){ - - case 0: /* if no jobs in queue */ - thpool_p->jobqueue_p->front = newjob; - thpool_p->jobqueue_p->rear = newjob; - break; - - default: /* if jobs in queue */ - thpool_p->jobqueue_p->rear->prev = newjob; - thpool_p->jobqueue_p->rear = newjob; - - } - thpool_p->jobqueue_p->len++; - - bsem_post(thpool_p->jobqueue_p->has_jobs); -} - - -/* Get first job from queue(removes it from queue) - * - * Notice: Caller MUST hold a mutex - */ -static struct job* jobqueue_pull(thpool_* thpool_p){ - - job* job_p; - job_p = thpool_p->jobqueue_p->front; - - switch(thpool_p->jobqueue_p->len){ - - case 0: /* if no jobs in queue */ - break; - - case 1: /* if one job in queue */ - thpool_p->jobqueue_p->front = NULL; - thpool_p->jobqueue_p->rear = NULL; - thpool_p->jobqueue_p->len = 0; - break; - - default: /* if >1 jobs in queue */ - thpool_p->jobqueue_p->front = job_p->prev; - thpool_p->jobqueue_p->len--; - /* more than one job in queue -> post it */ - bsem_post(thpool_p->jobqueue_p->has_jobs); - - } - - return job_p; -} - - -/* Free all queue resources back to the system */ -static void jobqueue_destroy(thpool_* thpool_p){ - jobqueue_clear(thpool_p); - free(thpool_p->jobqueue_p->has_jobs); -} - - - - - -/* ======================== SYNCHRONISATION ========================= */ - - -/* Init semaphore to 1 or 0 */ -static void bsem_init(bsem *bsem_p, int value) { - if (value < 0 || value > 1) { - fprintf(stderr, "bsem_init(): Binary semaphore can take only values 1 or 0"); - exit(1); - } - pthread_mutex_init(&(bsem_p->mutex), NULL); - pthread_cond_init(&(bsem_p->cond), NULL); - bsem_p->v = value; -} - - -/* Reset semaphore to 0 */ -static void bsem_reset(bsem *bsem_p) { - bsem_init(bsem_p, 0); -} - - -/* Post to at least one thread */ -static void bsem_post(bsem *bsem_p) { - pthread_mutex_lock(&bsem_p->mutex); - bsem_p->v = 1; - pthread_cond_signal(&bsem_p->cond); - pthread_mutex_unlock(&bsem_p->mutex); -} - - -/* Post to all threads */ -static void bsem_post_all(bsem *bsem_p) { - pthread_mutex_lock(&bsem_p->mutex); - bsem_p->v = 1; - pthread_cond_broadcast(&bsem_p->cond); - pthread_mutex_unlock(&bsem_p->mutex); -} - - -/* Wait on semaphore until semaphore has value 0 */ -static void bsem_wait(bsem* bsem_p) { - pthread_mutex_lock(&bsem_p->mutex); - while (bsem_p->v != 1) { - pthread_cond_wait(&bsem_p->cond, &bsem_p->mutex); - } - bsem_p->v = 0; - pthread_mutex_unlock(&bsem_p->mutex); -} http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/utils.c ---------------------------------------------------------------------- diff --git a/utils/private/src/utils.c b/utils/private/src/utils.c deleted file mode 100644 index fc4d538..0000000 --- a/utils/private/src/utils.c +++ /dev/null @@ -1,141 +0,0 @@ -/** - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * utils.c - * - * \date Jul 27, 2010 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ -#include <stdlib.h> -#include <string.h> - -#include "utils.h" - -unsigned int utils_stringHash(const void* strPtr) { - const char* string = strPtr; - unsigned int hc = 5381; - char ch; - while((ch = *string++) != '\0'){ - hc = (hc << 5) + hc + ch; - } - - return hc; -} - -int utils_stringEquals(const void* string, const void* toCompare) { - return strcmp((const char*)string, (const char*)toCompare) == 0; -} - -char * string_ndup(const char *s, size_t n) { - size_t len = strlen(s); - char *ret; - - if (len <= n) { - return strdup(s); - } - - ret = malloc(n + 1); - strncpy(ret, s, n); - ret[n] = '\0'; - return ret; -} - -char * utils_stringTrim(char * string) { - char* copy = string; - - char *end; - // Trim leading space - while (isspace(*copy)) { - copy++; - } - - // Trim trailing space - end = copy + strlen(copy) - 1; - while(end > copy && isspace(*end)) { - *(end) = '\0'; - end--; - } - - if (copy != string) { - //beginning whitespaces -> move char in copy to to begin string - //This to ensure free still works on the same pointer. - char* nstring = string; - while(*copy != '\0') { - *(nstring++) = *(copy++); - } - (*nstring) = '\0'; - } - - return string; -} - -bool utils_isStringEmptyOrNull(const char * const str) { - bool empty = true; - if (str != NULL) { - int i; - for (i = 0; i < strnlen(str, 1024 * 1024); i += 1) { - if (!isspace(str[i])) { - empty = false; - break; - } - } - } - - return empty; -} - -celix_status_t thread_equalsSelf(celix_thread_t thread, bool *equals) { - celix_status_t status = CELIX_SUCCESS; - - celix_thread_t self = celixThread_self(); - if (status == CELIX_SUCCESS) { - *equals = celixThread_equals(self, thread); - } - - return status; -} - -celix_status_t utils_isNumeric(const char *number, bool *ret) { - celix_status_t status = CELIX_SUCCESS; - *ret = true; - while(*number) { - if(!isdigit(*number) && *number != '.') { - *ret = false; - break; - } - number++; - } - return status; -} - - -int utils_compareServiceIdsAndRanking(unsigned long servId, long servRank, unsigned long otherServId, long otherServRank) { - int result; - - if (servId == otherServId) { - result = 0; - } else if (servRank != otherServRank) { - result = servRank < otherServRank ? -1 : 1; - } else { //equal service rank, compare service ids - result = servId < otherServId ? 1 : -1; - } - - return result; -} http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/version.c ---------------------------------------------------------------------- diff --git a/utils/private/src/version.c b/utils/private/src/version.c deleted file mode 100644 index cb2703d..0000000 --- a/utils/private/src/version.c +++ /dev/null @@ -1,264 +0,0 @@ -/** - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * version.c - * - * \date Jul 12, 2010 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "celix_errno.h" -#include "version_private.h" - -celix_status_t version_createVersion(int major, int minor, int micro, char * qualifier, version_pt *version) { - celix_status_t status = CELIX_SUCCESS; - - if (*version != NULL) { - status = CELIX_ILLEGAL_ARGUMENT; - } else { - *version = (version_pt) malloc(sizeof(**version)); - if (!*version) { - status = CELIX_ENOMEM; - } else { - unsigned int i; - - (*version)->major = major; - (*version)->minor = minor; - (*version)->micro = micro; - if (qualifier == NULL) { - qualifier = ""; - } - (*version)->qualifier = strdup(qualifier); - - if (major < 0) { - status = CELIX_ILLEGAL_ARGUMENT; - } - if (minor < 0) { - status = CELIX_ILLEGAL_ARGUMENT; - } - if (micro < 0) { - status = CELIX_ILLEGAL_ARGUMENT; - } - - for (i = 0; i < strlen(qualifier); i++) { - char ch = qualifier[i]; - if (('A' <= ch) && (ch <= 'Z')) { - continue; - } - if (('a' <= ch) && (ch <= 'z')) { - continue; - } - if (('0' <= ch) && (ch <= '9')) { - continue; - } - if ((ch == '_') || (ch == '-')) { - continue; - } - status = CELIX_ILLEGAL_ARGUMENT; - break; - } - } - } - - return status; -} - -celix_status_t version_clone(version_pt version, version_pt *clone) { - return version_createVersion(version->major, version->minor, version->micro, version->qualifier, clone); -} - -celix_status_t version_destroy(version_pt version) { - version->major = 0; - version->minor = 0; - version->micro = 0; - free(version->qualifier); - version->qualifier = NULL; - free(version); - return CELIX_SUCCESS; -} - -celix_status_t version_createVersionFromString(const char * versionStr, version_pt *version) { - celix_status_t status = CELIX_SUCCESS; - - int major = 0; - int minor = 0; - int micro = 0; - char * qualifier = NULL; - - char delims[] = "."; - char *token = NULL; - char *last = NULL; - - int i = 0; - - char* versionWrkStr = strdup(versionStr); - - token = strtok_r(versionWrkStr, delims, &last); - if (token != NULL) { - for (i = 0; i < strlen(token); i++) { - char ch = token[i]; - if (('0' <= ch) && (ch <= '9')) { - continue; - } - status = CELIX_ILLEGAL_ARGUMENT; - break; - } - major = atoi(token); - token = strtok_r(NULL, delims, &last); - if (token != NULL) { - for (i = 0; i < strlen(token); i++) { - char ch = token[i]; - if (('0' <= ch) && (ch <= '9')) { - continue; - } - status = CELIX_ILLEGAL_ARGUMENT; - break; - } - minor = atoi(token); - token = strtok_r(NULL, delims, &last); - if (token != NULL) { - for (i = 0; i < strlen(token); i++) { - char ch = token[i]; - if (('0' <= ch) && (ch <= '9')) { - continue; - } - status = CELIX_ILLEGAL_ARGUMENT; - break; - } - micro = atoi(token); - token = strtok_r(NULL, delims, &last); - if (token != NULL) { - qualifier = strdup(token); - token = strtok_r(NULL, delims, &last); - if (token != NULL) { - *version = NULL; - status = CELIX_ILLEGAL_ARGUMENT; - } - } - } - } - } - - free(versionWrkStr); - - if (status == CELIX_SUCCESS) { - status = version_createVersion(major, minor, micro, qualifier, version); - } - - if (qualifier != NULL) { - free(qualifier); - } - - return status; -} - -celix_status_t version_createEmptyVersion(version_pt *version) { - return version_createVersion(0, 0, 0, "", version); -} - -celix_status_t version_getMajor(version_pt version, int *major) { - celix_status_t status = CELIX_SUCCESS; - *major = version->major; - return status; -} - -celix_status_t version_getMinor(version_pt version, int *minor) { - celix_status_t status = CELIX_SUCCESS; - *minor = version->minor; - return status; -} - -celix_status_t version_getMicro(version_pt version, int *micro) { - celix_status_t status = CELIX_SUCCESS; - *micro = version->micro; - return status; -} - -celix_status_t version_getQualifier(version_pt version, const char **qualifier) { - celix_status_t status = CELIX_SUCCESS; - *qualifier = version->qualifier; - return status; -} - -celix_status_t version_compareTo(version_pt version, version_pt compare, int *result) { - celix_status_t status = CELIX_SUCCESS; - if (compare == version) { - *result = 0; - } else { - int res = version->major - compare->major; - if (res != 0) { - *result = res; - } else { - res = version->minor - compare->minor; - if (res != 0) { - *result = res; - } else { - res = version->micro - compare->micro; - if (res != 0) { - *result = res; - } else { - *result = strcmp(version->qualifier, compare->qualifier); - } - } - } - } - - return status; -} - -celix_status_t version_toString(version_pt version, char **string) { - celix_status_t status = CELIX_SUCCESS; - if (strlen(version->qualifier) > 0) { - char str[512]; - int written = snprintf(str, 512, "%d.%d.%d.%s", version->major, version->minor, version->micro, version->qualifier); - if (written >= 512 || written < 0) { - status = CELIX_BUNDLE_EXCEPTION; - } - *string = strdup(str); - } else { - char str[512]; - int written = snprintf(str, 512, "%d.%d.%d", version->major, version->minor, version->micro); - if (written >= 512 || written < 0) { - status = CELIX_BUNDLE_EXCEPTION; - } - *string = strdup(str); - } - return status; -} - -celix_status_t version_isCompatible(version_pt user, version_pt provider, bool* isCompatible) { - celix_status_t status = CELIX_SUCCESS; - bool result = false; - - if (user == NULL || provider == NULL) { - return CELIX_ILLEGAL_ARGUMENT; - } - - if (user->major == provider->major) { - result = (provider->minor >= user->minor); - } - - *isCompatible = result; - - return status; -} http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/private/src/version_range.c ---------------------------------------------------------------------- diff --git a/utils/private/src/version_range.c b/utils/private/src/version_range.c deleted file mode 100644 index ed681fd..0000000 --- a/utils/private/src/version_range.c +++ /dev/null @@ -1,233 +0,0 @@ -/** - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * version_range.c - * - * \date Jul 12, 2010 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ - -#include <stdlib.h> -#include <string.h> - -#include "version_range_private.h" - -celix_status_t versionRange_createVersionRange(version_pt low, bool isLowInclusive, - version_pt high, bool isHighInclusive, version_range_pt *range) { - celix_status_t status = CELIX_SUCCESS; - *range = (version_range_pt) malloc(sizeof(**range)); - if (!*range) { - status = CELIX_ENOMEM; - } else { - (*range)->low = low; - (*range)->isLowInclusive = isLowInclusive; - (*range)->high = high; - (*range)->isHighInclusive = isHighInclusive; - } - - return status; -} - -celix_status_t versionRange_destroy(version_range_pt range) { - if (range->high != NULL) { - version_destroy(range->high); - } - if (range->low != NULL) { - version_destroy(range->low); - } - - range->high = NULL; - range->isHighInclusive = false; - range->low = NULL; - range->isLowInclusive = false; - - free(range); - - return CELIX_SUCCESS; -} - -celix_status_t versionRange_createInfiniteVersionRange(version_range_pt *range) { - celix_status_t status; - - version_pt version = NULL; - status = version_createEmptyVersion(&version); - if (status == CELIX_SUCCESS) { - status = versionRange_createVersionRange(version, true, NULL, true, range); - } - - return status; -} - -celix_status_t versionRange_isInRange(version_range_pt versionRange, version_pt version, bool *inRange) { - celix_status_t status; - if (versionRange->high == NULL) { - int cmp; - status = version_compareTo(version, versionRange->low, &cmp); - if (status == CELIX_SUCCESS) { - *inRange = (cmp >= 0); - } - } else if (versionRange->isLowInclusive && versionRange->isHighInclusive) { - int low, high; - status = version_compareTo(version, versionRange->low, &low); - if (status == CELIX_SUCCESS) { - status = version_compareTo(version, versionRange->high, &high); - if (status == CELIX_SUCCESS) { - *inRange = (low >= 0) && (high <= 0); - } - } - } else if (versionRange->isHighInclusive) { - int low, high; - status = version_compareTo(version, versionRange->low, &low); - if (status == CELIX_SUCCESS) { - status = version_compareTo(version, versionRange->high, &high); - if (status == CELIX_SUCCESS) { - *inRange = (low > 0) && (high <= 0); - } - } - } else if (versionRange->isLowInclusive) { - int low, high; - status = version_compareTo(version, versionRange->low, &low); - if (status == CELIX_SUCCESS) { - status = version_compareTo(version, versionRange->high, &high); - if (status == CELIX_SUCCESS) { - *inRange = (low >= 0) && (high < 0); - } - } - } else { - int low, high; - status = version_compareTo(version, versionRange->low, &low); - if (status == CELIX_SUCCESS) { - status = version_compareTo(version, versionRange->high, &high); - if (status == CELIX_SUCCESS) { - *inRange = (low > 0) && (high < 0); - } - } - } - - return status; -} - -celix_status_t versionRange_getLowVersion(version_range_pt versionRange, version_pt *lowVersion) { - celix_status_t status = CELIX_SUCCESS; - - if (versionRange == NULL) { - status = CELIX_ILLEGAL_ARGUMENT; - } - else { - *lowVersion = versionRange->low; - } - - return status; -} - -celix_status_t versionRange_isLowInclusive(version_range_pt versionRange, bool *isLowInclusive) { - celix_status_t status = CELIX_SUCCESS; - - if (versionRange == NULL) { - status = CELIX_ILLEGAL_ARGUMENT; - } - else { - *isLowInclusive = versionRange->isLowInclusive; - } - - return status; -} - -celix_status_t versionRange_getHighVersion(version_range_pt versionRange, version_pt *highVersion) { - celix_status_t status = CELIX_SUCCESS; - - if (versionRange == NULL) { - status = CELIX_ILLEGAL_ARGUMENT; - } - else { - *highVersion = versionRange->high; - } - - return status; -} - -celix_status_t versionRange_isHighInclusive(version_range_pt versionRange, bool *isHighInclusive) { - celix_status_t status = CELIX_SUCCESS; - - if (versionRange == NULL) { - status = CELIX_ILLEGAL_ARGUMENT; - } - else { - *isHighInclusive = versionRange->isHighInclusive; - } - - return status; -} - - -celix_status_t versionRange_parse(const char * rangeStr, version_range_pt *range) { - celix_status_t status; - if (strchr(rangeStr, ',') != NULL) { - int vlowL = strcspn(rangeStr+1, ","); - char * vlow = (char *) malloc(sizeof(char) * (vlowL + 1)); - if (!vlow) { - status = CELIX_ENOMEM; - } else { - int vhighL; - char * vhigh; - vlow = strncpy(vlow, rangeStr+1, vlowL); - vlow[vlowL] = '\0'; - vhighL = strlen(rangeStr+1) - vlowL - 2; - vhigh = (char *) malloc(sizeof(char) * (vhighL+1)); - if (!vhigh) { - status = CELIX_ENOMEM; - } else { - version_pt versionLow = NULL; - int rangeL = strlen(rangeStr); - char start = rangeStr[0]; - char end = rangeStr[rangeL-1]; - - vhigh = strncpy(vhigh, rangeStr+vlowL+2, vhighL); - vhigh[vhighL] = '\0'; - status = version_createVersionFromString(vlow, &versionLow); - if (status == CELIX_SUCCESS) { - version_pt versionHigh = NULL; - status = version_createVersionFromString(vhigh, &versionHigh); - if (status == CELIX_SUCCESS) { - status = versionRange_createVersionRange( - versionLow, - start == '[', - versionHigh, - end ==']', - range - ); - } - } - free(vhigh); - } - free(vlow); - - } - } else { - version_pt version = NULL; - status = version_createVersionFromString(rangeStr, &version); - if (status == CELIX_SUCCESS) { - status = versionRange_createVersionRange(version, true, NULL, false, range); - } - } - - return status; -} - http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/public/include/array_list.h ---------------------------------------------------------------------- diff --git a/utils/public/include/array_list.h b/utils/public/include/array_list.h deleted file mode 100644 index 4f83a45..0000000 --- a/utils/public/include/array_list.h +++ /dev/null @@ -1,99 +0,0 @@ -/** - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * array_list.h - * - * \date Aug 4, 2010 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ - -#ifndef ARRAY_LIST_H_ -#define ARRAY_LIST_H_ - -#include "celixbool.h" -#include "exports.h" -#include "celix_errno.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct arrayList *array_list_pt; - -typedef struct arrayListIterator *array_list_iterator_pt; - -typedef celix_status_t (*array_list_element_equals_pt)(const void *, const void *, bool *equals); - -UTILS_EXPORT celix_status_t arrayList_create(array_list_pt *list); - -UTILS_EXPORT celix_status_t arrayList_createWithEquals(array_list_element_equals_pt equals, array_list_pt *list); - -UTILS_EXPORT void arrayList_destroy(array_list_pt list); - -UTILS_EXPORT void arrayList_trimToSize(array_list_pt list); - -UTILS_EXPORT void arrayList_ensureCapacity(array_list_pt list, int capacity); - -UTILS_EXPORT unsigned int arrayList_size(array_list_pt list); - -UTILS_EXPORT bool arrayList_isEmpty(array_list_pt list); - -UTILS_EXPORT bool arrayList_contains(array_list_pt list, void *element); - -UTILS_EXPORT int arrayList_indexOf(array_list_pt list, void *element); - -UTILS_EXPORT int arrayList_lastIndexOf(array_list_pt list, void *element); - -UTILS_EXPORT void *arrayList_get(array_list_pt list, unsigned int index); - -UTILS_EXPORT void *arrayList_set(array_list_pt list, unsigned int index, void *element); - -UTILS_EXPORT bool arrayList_add(array_list_pt list, void *element); - -UTILS_EXPORT int arrayList_addIndex(array_list_pt list, unsigned int index, void *element); - -UTILS_EXPORT bool arrayList_addAll(array_list_pt list, array_list_pt toAdd); - -UTILS_EXPORT void *arrayList_remove(array_list_pt list, unsigned int index); - -UTILS_EXPORT bool arrayList_removeElement(array_list_pt list, void *element); - -UTILS_EXPORT void arrayList_clear(array_list_pt list); - -UTILS_EXPORT array_list_pt arrayList_clone(array_list_pt list); - -UTILS_EXPORT array_list_iterator_pt arrayListIterator_create(array_list_pt list); - -UTILS_EXPORT void arrayListIterator_destroy(array_list_iterator_pt iterator); - -UTILS_EXPORT bool arrayListIterator_hasNext(array_list_iterator_pt iterator); - -UTILS_EXPORT void *arrayListIterator_next(array_list_iterator_pt iterator); - -UTILS_EXPORT bool arrayListIterator_hasPrevious(array_list_iterator_pt iterator); - -UTILS_EXPORT void *arrayListIterator_previous(array_list_iterator_pt iterator); - -UTILS_EXPORT void arrayListIterator_remove(array_list_iterator_pt iterator); - -#ifdef __cplusplus -} -#endif -#endif /* ARRAY_LIST_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/public/include/celix_errno.h ---------------------------------------------------------------------- diff --git a/utils/public/include/celix_errno.h b/utils/public/include/celix_errno.h deleted file mode 100644 index b51540b..0000000 --- a/utils/public/include/celix_errno.h +++ /dev/null @@ -1,119 +0,0 @@ -/* - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * celix_errno.h - * - * \date Feb 15, 2011 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ -/*! - \file - \brief Error codes - \defgroup framework Celix Framework - */ -#ifndef CELIX_ERRNO_H_ -#define CELIX_ERRNO_H_ - -#include <stddef.h> -#include <errno.h> - -#include "exports.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/*! - * Helper macro which check the current status and executes the provided expression if the - * status is still CELIX_SUCCESS (0) - */ -#define CELIX_DO_IF(status, expr) ((status) == CELIX_SUCCESS) ? (expr) : (status) - -/*! - * \defgroup celix_errno Error Codes - * \ingroup framework - * \{ - */ - -struct celix_status { - int code; - char *error; -}; - -/*! - * Status type returned by all functions in Celix - */ -typedef int celix_status_t; - -/*! - * Return a readable string for the given error code. - * - */ -UTILS_EXPORT char *celix_strerror(celix_status_t errorcode, char *buffer, size_t bufferSize); - -/*! - * Error code indicating successful execution of the function. - */ -#define CELIX_SUCCESS 0 - -/*! - * Starting point for Celix errors. - */ -#define CELIX_START_ERROR 70000 - -/*! - * The range for Celix errors. - */ -#define CELIX_ERRSPACE_SIZE 1000 - -/*! - * The start error number user application can use. - */ -#define CELIX_START_USERERR (CELIX_START_ERROR + CELIX_ERRSPACE_SIZE) - -/*! - * Exception indicating a problem with a bundle - */ -#define CELIX_BUNDLE_EXCEPTION (CELIX_START_ERROR + 1) -/*! - * Invalid bundle context is used - */ -#define CELIX_INVALID_BUNDLE_CONTEXT (CELIX_START_ERROR + 2) -/*! - * Argument is not correct - */ -#define CELIX_ILLEGAL_ARGUMENT (CELIX_START_ERROR + 3) -#define CELIX_INVALID_SYNTAX (CELIX_START_ERROR + 4) -#define CELIX_FRAMEWORK_SHUTDOWN (CELIX_START_ERROR + 5) -#define CELIX_ILLEGAL_STATE (CELIX_START_ERROR + 6) -#define CELIX_FRAMEWORK_EXCEPTION (CELIX_START_ERROR + 7) -#define CELIX_FILE_IO_EXCEPTION (CELIX_START_ERROR + 8) -#define CELIX_SERVICE_EXCEPTION (CELIX_START_ERROR + 9) - -#define CELIX_ENOMEM ENOMEM - -/** - * \} - */ -#ifdef __cplusplus -} -#endif - -#endif /* CELIX_ERRNO_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/public/include/celix_threads.h ---------------------------------------------------------------------- diff --git a/utils/public/include/celix_threads.h b/utils/public/include/celix_threads.h deleted file mode 100644 index 6af57bb..0000000 --- a/utils/public/include/celix_threads.h +++ /dev/null @@ -1,135 +0,0 @@ -/** - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * celix_threads.h - * - * \date 4 Jun 2014 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ - -#ifndef CELIX_THREADS_H_ -#define CELIX_THREADS_H_ - -#include <pthread.h> -#include <stdbool.h> - -#include "celix_errno.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct celix_thread { - bool threadInitialized; - pthread_t thread; -}; - -typedef pthread_once_t celix_thread_once_t; -#define CELIX_THREAD_ONCE_INIT PTHREAD_ONCE_INIT - -typedef struct celix_thread celix_thread_t; -typedef pthread_attr_t celix_thread_attr_t; - -typedef void *(*celix_thread_start_t)(void *); - -static const celix_thread_t celix_thread_default = {0, 0}; - -celix_status_t -celixThread_create(celix_thread_t *new_thread, celix_thread_attr_t *attr, celix_thread_start_t func, void *data); - -void celixThread_exit(void *exitStatus); - -celix_status_t celixThread_detach(celix_thread_t thread); - -celix_status_t celixThread_join(celix_thread_t thread, void **status); - -celix_status_t celixThread_kill(celix_thread_t thread, int sig); - -celix_thread_t celixThread_self(void); - -int celixThread_equals(celix_thread_t thread1, celix_thread_t thread2); - -bool celixThread_initalized(celix_thread_t thread); - - -typedef pthread_mutex_t celix_thread_mutex_t; -typedef pthread_mutexattr_t celix_thread_mutexattr_t; - -//MUTEX TYPES -enum { - CELIX_THREAD_MUTEX_NORMAL, - CELIX_THREAD_MUTEX_RECURSIVE, - CELIX_THREAD_MUTEX_ERRORCHECK, - CELIX_THREAD_MUTEX_DEFAULT -}; - - -celix_status_t celixThreadMutex_create(celix_thread_mutex_t *mutex, celix_thread_mutexattr_t *attr); - -celix_status_t celixThreadMutex_destroy(celix_thread_mutex_t *mutex); - -celix_status_t celixThreadMutex_lock(celix_thread_mutex_t *mutex); - -celix_status_t celixThreadMutex_unlock(celix_thread_mutex_t *mutex); - -celix_status_t celixThreadMutexAttr_create(celix_thread_mutexattr_t *attr); - -celix_status_t celixThreadMutexAttr_destroy(celix_thread_mutexattr_t *attr); - -celix_status_t celixThreadMutexAttr_settype(celix_thread_mutexattr_t *attr, int type); - -typedef pthread_rwlock_t celix_thread_rwlock_t; -typedef pthread_rwlockattr_t celix_thread_rwlockattr_t; - -celix_status_t celixThreadRwlock_create(celix_thread_rwlock_t *lock, celix_thread_rwlockattr_t *attr); - -celix_status_t celixThreadRwlock_destroy(celix_thread_rwlock_t *lock); - -celix_status_t celixThreadRwlock_readLock(celix_thread_rwlock_t *lock); - -celix_status_t celixThreadRwlock_writeLock(celix_thread_rwlock_t *lock); - -celix_status_t celixThreadRwlock_unlock(celix_thread_rwlock_t *lock); - -celix_status_t celixThreadRwlockAttr_create(celix_thread_rwlockattr_t *attr); - -celix_status_t celixThreadRwlockAttr_destroy(celix_thread_rwlockattr_t *attr); -//NOTE: No support yet for setting specific rw lock attributes - - -typedef pthread_cond_t celix_thread_cond_t; -typedef pthread_condattr_t celix_thread_condattr_t; - -celix_status_t celixThreadCondition_init(celix_thread_cond_t *condition, celix_thread_condattr_t *attr); - -celix_status_t celixThreadCondition_destroy(celix_thread_cond_t *condition); - -celix_status_t celixThreadCondition_wait(celix_thread_cond_t *cond, celix_thread_mutex_t *mutex); - -celix_status_t celixThreadCondition_broadcast(celix_thread_cond_t *cond); - -celix_status_t celixThreadCondition_signal(celix_thread_cond_t *cond); - -celix_status_t celixThread_once(celix_thread_once_t *once_control, void (*init_routine)(void)); - -#ifdef __cplusplus -} -#endif -#endif /* CELIX_THREADS_H_ */ http://git-wip-us.apache.org/repos/asf/celix/blob/a1c30887/utils/public/include/celixbool.h ---------------------------------------------------------------------- diff --git a/utils/public/include/celixbool.h b/utils/public/include/celixbool.h deleted file mode 100644 index 526392b..0000000 --- a/utils/public/include/celixbool.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - *Licensed to the Apache Software Foundation (ASF) under one - *or more contributor license agreements. See the NOTICE file - *distributed with this work for additional information - *regarding copyright ownership. The ASF licenses this file - *to you under the Apache License, Version 2.0 (the - *"License"); you may not use this file except in compliance - *with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, - *software distributed under the License is distributed on an - *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - *specific language governing permissions and limitations - *under the License. - */ -/* - * celixbool.h - * - * \date Jun 16, 2011 - * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> - * \copyright Apache License, Version 2.0 - */ - -#ifndef CELIXBOOL_H_ -#define CELIXBOOL_H_ - - -#if defined(__STDC__) -# define C89 -# if defined(__STDC_VERSION__) -# define C90 -# if (__STDC_VERSION__ >= 199409L) -# define C94 -# endif -# if (__STDC_VERSION__ >= 199901L) -# define C99 -# endif -# endif -#endif - - -#if __STDC_VERSION__ < 199901L && __GNUC__ < 3 -// #ifndef C99 - -typedef int _Bool; - -#define bool _Bool -#define false 0 -#define true 1 - - -#else - -#include <stdbool.h> - -#endif - -#endif /* CELIXBOOL_H_ */
