mirror of https://gitee.com/bigwinds/arangodb
Merge pull request #510 from guidoreina/experiment
A bug fix and small code improvements?
This commit is contained in:
commit
203aceccdf
|
@ -123,7 +123,7 @@ PQIndex* PQueueIndex_new (void) {
|
|||
bool ok;
|
||||
|
||||
// ..........................................................................
|
||||
// Allocate the Priority Que Index
|
||||
// Allocate the Priority Queue Index
|
||||
// ..........................................................................
|
||||
|
||||
idx = TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, sizeof(PQIndex), false);
|
||||
|
@ -136,7 +136,7 @@ PQIndex* PQueueIndex_new (void) {
|
|||
|
||||
|
||||
// ..........................................................................
|
||||
// Allocate the priority que
|
||||
// Allocate the priority queue
|
||||
// Remember to add any additional structure you need
|
||||
// ..........................................................................
|
||||
|
||||
|
@ -155,8 +155,8 @@ PQIndex* PQueueIndex_new (void) {
|
|||
|
||||
idx->_aa = TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, sizeof(TRI_associative_array_t), false);
|
||||
if (idx->_aa == NULL) {
|
||||
TRI_Free(TRI_UNKNOWN_MEM_ZONE, idx);
|
||||
TRI_Free(TRI_UNKNOWN_MEM_ZONE, idx->_pq);
|
||||
TRI_Free(TRI_UNKNOWN_MEM_ZONE, idx);
|
||||
TRI_set_errno(TRI_ERROR_OUT_OF_MEMORY);
|
||||
LOG_ERROR("out of memory when creating priority queue index");
|
||||
return NULL;
|
||||
|
@ -164,7 +164,7 @@ PQIndex* PQueueIndex_new (void) {
|
|||
|
||||
|
||||
// ..........................................................................
|
||||
// Initialise the priority que
|
||||
// Initialise the priority queue
|
||||
// ..........................................................................
|
||||
|
||||
ok = TRI_InitPQueue(idx->_pq,
|
||||
|
|
|
@ -126,22 +126,16 @@ bool TRI_InitPQueue (TRI_pqueue_t* pq, size_t initialCapacity, size_t itemSize,
|
|||
|
||||
|
||||
// ..........................................................................
|
||||
// Set the capacity and assign memeory for storage
|
||||
// Set the capacity and assign memory for storage
|
||||
// ..........................................................................
|
||||
pq->_base._capacity = initialCapacity;
|
||||
pq->_base._items = TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, pq->_base._itemSize * pq->_base._capacity, false);
|
||||
pq->_base._items = TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, pq->_base._itemSize * pq->_base._capacity, true);
|
||||
if (pq->_base._items == NULL) {
|
||||
TRI_set_errno(TRI_ERROR_OUT_OF_MEMORY);
|
||||
LOG_ERROR("out of memory when creating priority queue storage");
|
||||
return false;
|
||||
}
|
||||
|
||||
// ..........................................................................
|
||||
// Initialise the memory allcoated for the storage of pq (0 filled)
|
||||
// ..........................................................................
|
||||
|
||||
memset(pq->_base._items, 0, pq->_base._itemSize * pq->_base._capacity);
|
||||
|
||||
|
||||
// ..........................................................................
|
||||
// initialise the number of items stored
|
||||
|
@ -373,26 +367,29 @@ static void* TopPQueue(TRI_pqueue_t* pq) {
|
|||
|
||||
static bool CheckPQSize(TRI_pqueue_t* pq) {
|
||||
char* newItems;
|
||||
size_t newCapacity;
|
||||
|
||||
if (pq == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pq->_base._capacity > (pq->_base._count + 1) ) {
|
||||
if (pq->_base._capacity >= (pq->_base._count + 1) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
pq->_base._capacity = pq->_base._capacity * 2;
|
||||
// allocate and fill with NUL bytes
|
||||
newItems = TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, pq->_base._capacity * pq->_base._itemSize, true);
|
||||
newCapacity = pq->_base._capacity * 2;
|
||||
|
||||
// reallocate
|
||||
newItems = TRI_Reallocate(TRI_UNKNOWN_MEM_ZONE, pq->_base._items, newCapacity * pq->_base._itemSize);
|
||||
if (newItems == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(newItems, pq->_base._items, (pq->_base._count * pq->_base._itemSize) );
|
||||
// initialise the remaining memory allocated for the storage of pq (0 filled)
|
||||
memset(pq->_base._items + pq->_base._capacity * pq->_base._itemSize, 0, (newCapacity - pq->_base._capacity) * pq->_base._itemSize);
|
||||
|
||||
TRI_Free(TRI_UNKNOWN_MEM_ZONE, pq->_base._items);
|
||||
pq->_base._items = newItems;
|
||||
pq->_base._capacity = newCapacity;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -102,7 +102,7 @@ typedef struct TRI_pqueue_base_s {
|
|||
bool _reverse;
|
||||
|
||||
// ...........................................................................
|
||||
// Additional hidden extenral structure used outside this priority queue
|
||||
// Additional hidden external structure used outside this priority queue
|
||||
// This hidden structure is not available within this priority queue
|
||||
// ...........................................................................
|
||||
// char[n]
|
||||
|
@ -124,7 +124,7 @@ typedef struct TRI_pqueue_s {
|
|||
|
||||
|
||||
// ...........................................................................
|
||||
// default pq add, remove ,top methods
|
||||
// default pq add, remove, top methods
|
||||
// ...........................................................................
|
||||
|
||||
bool (*add) (struct TRI_pqueue_s*, void*);
|
||||
|
|
|
@ -77,6 +77,7 @@ static void AddNewElement (TRI_associative_array_t* array, void* element) {
|
|||
static void ResizeAssociativeArray (TRI_associative_array_t* array) {
|
||||
char * oldTable;
|
||||
uint32_t oldAlloc;
|
||||
uint32_t oldUsed;
|
||||
uint32_t j;
|
||||
|
||||
oldTable = array->_table;
|
||||
|
@ -96,13 +97,10 @@ static void ResizeAssociativeArray (TRI_associative_array_t* array) {
|
|||
return;
|
||||
}
|
||||
|
||||
oldUsed = array->_nrUsed;
|
||||
array->_nrUsed = 0;
|
||||
|
||||
for (j = 0; j < array->_nrAlloc; j++) {
|
||||
array->clearElement(array, array->_table + j * array->_elementSize);
|
||||
}
|
||||
|
||||
for (j = 0; j < oldAlloc; j++) {
|
||||
for (j = 0; array->_nrUsed < oldUsed; j++) {
|
||||
if (! array->isEmptyElement(array, oldTable + j * array->_elementSize)) {
|
||||
AddNewElement(array, oldTable + j * array->_elementSize);
|
||||
}
|
||||
|
@ -249,7 +247,7 @@ void* TRI_FindByKeyAssociativeArray (TRI_associative_array_t* array, void* key)
|
|||
|
||||
element = TRI_LookupByKeyAssociativeArray(array, key);
|
||||
|
||||
if (! array->isEmptyElement(array, element) && array->isEqualKeyElement(array, key, element)) {
|
||||
if (! array->isEmptyElement(array, element)) {
|
||||
return element;
|
||||
}
|
||||
|
||||
|
@ -295,7 +293,7 @@ void* TRI_FindByElementAssociativeArray (TRI_associative_array_t* array, void* e
|
|||
|
||||
element2 = TRI_LookupByElementAssociativeArray(array, element);
|
||||
|
||||
if (! array->isEmptyElement(array, element2) && array->isEqualElementElement(array, element2, element)) {
|
||||
if (! array->isEmptyElement(array, element2)) {
|
||||
return element2;
|
||||
}
|
||||
|
||||
|
|
|
@ -374,7 +374,7 @@ size_t TRI_GetLengthAssociativePointer (const TRI_associative_pointer_t* const);
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief associative array of synced pointers
|
||||
///
|
||||
/// Note that lookup, insert, and remove are proctected using a read-write lock.
|
||||
/// Note that lookup, insert, and remove are protected using a read-write lock.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
typedef struct TRI_associative_synced_s {
|
||||
|
|
Loading…
Reference in New Issue