errorCode addEmptyDynEntry(DynArray* dynArray, void** entry, Index* entryID) { void** base; Index* count; if(dynArray == NULL) return NULL_POINTER_REF; base = (void **)(dynArray + 1); count = (Index*)(base + 1); if(dynArray->arrayEntries == *count) // The dynamic array must be extended first { void* ptr = EXIP_REALLOC(*base, dynArray->entrySize * (*count + dynArray->chunkEntries)); if(ptr == NULL) return MEMORY_ALLOCATION_ERROR; *base = ptr; dynArray->arrayEntries = dynArray->arrayEntries + dynArray->chunkEntries; } *entry = (void*)((unsigned char *)(*base) + (*count * dynArray->entrySize)); *entryID = *count; *count += 1; return ERR_OK; }
static int hashtable_expand(struct hashtable *h) { /* Double the size of the table to accommodate more entries */ struct entry **newtable; struct entry *e; struct entry **pE; unsigned int newsize, i, index; /* Check we're not hitting max capacity */ if (h->primeindex == (prime_table_length - 1) || primes[h->primeindex + 1] > MAX_HASH_TABLE_SIZE) return 0; newsize = primes[++(h->primeindex)]; newtable = (struct entry **)EXIP_MALLOC(sizeof(struct entry*) * newsize); if (NULL != newtable) { memset(newtable, 0, newsize * sizeof(struct entry *)); /* This algorithm is not 'stable'. ie. it reverses the list * when it transfers entries between the tables */ for (i = 0; i < h->tablelength; i++) { while (NULL != (e = h->table[i])) { h->table[i] = e->next; index = indexFor(newsize,e->hash); e->next = newtable[index]; newtable[index] = e; } } EXIP_MFREE(h->table); h->table = newtable; } /* Plan B: realloc instead */ else { newtable = (struct entry **) EXIP_REALLOC(h->table, newsize * sizeof(struct entry *)); if (NULL == newtable) { (h->primeindex)--; return 0; } h->table = newtable; memset(newtable[h->tablelength], 0, newsize - h->tablelength); for (i = 0; i < h->tablelength; i++) { for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE) { index = indexFor(newsize,e->hash); if (index == i) { pE = &(e->next); } else { *pE = e->next; e->next = newtable[index]; newtable[index] = e; } } } } h->tablelength = newsize; h->loadlimit = CEIL(newsize * max_load_factor); return -1; }
errorCode insertZeroProduction(DynGrammarRule* rule, EventType eventType, SmallIndex nonTermID, QNameID* qnameId, boolean hasSecondLevelProd) { if(rule->pCount == rule->prodDim) // The dynamic array rule->production needs to be resized { void* ptr = EXIP_REALLOC(rule->production, sizeof(Production)*(rule->prodDim + DEFAULT_PROD_ARRAY_DIM)); if(ptr == NULL) return MEMORY_ALLOCATION_ERROR; rule->production = ptr; rule->prodDim += DEFAULT_PROD_ARRAY_DIM; } SET_PROD_EXI_EVENT(rule->production[rule->pCount].content, eventType); SET_PROD_NON_TERM(rule->production[rule->pCount].content, nonTermID); rule->production[rule->pCount].typeId = INDEX_MAX; rule->production[rule->pCount].qnameId = *qnameId; rule->pCount += 1; return ERR_OK; }
errorCode insertZeroProduction(DynGrammarRule* rule, EventType eventType, SmallIndex nonTermID, QNameID* qnameId) { if(rule->part[0].count == rule->part0Dimension) // The dynamic array rule->prods[0] needs to be resized { void* ptr = EXIP_REALLOC(rule->part[0].prod, sizeof(Production)*(rule->part0Dimension + DEFAULT_PROD_ARRAY_DIM)); if(ptr == NULL) return MEMORY_ALLOCATION_ERROR; rule->part[0].prod = ptr; rule->part0Dimension += DEFAULT_PROD_ARRAY_DIM; } rule->part[0].prod[rule->part[0].count].eventType = eventType; rule->part[0].prod[rule->part[0].count].typeId = INDEX_MAX; rule->part[0].prod[rule->part[0].count].nonTermID = nonTermID; rule->part[0].prod[rule->part[0].count].qnameId = *qnameId; rule->part[0].count += 1; rule->part[0].bits = getBitsNumber(rule->part[0].count - 1 + (rule->part[1].count + rule->part[2].count > 0)); return ERR_OK; }