/* * Extend the table: make it twice as large. */ static void stbl_extend(stbl_t *sym_table) { stbl_rec_t **tmp; stbl_rec_t *list; uint32_t i, n, old_size, mask; old_size = sym_table->size; n = old_size << 1; if (n == 0 || n >= MAX_STBL_SIZE) { // overflow: cannot expand out_of_memory(); } assert(is_power_of_two(n)); // new data array tmp = (stbl_rec_t **) safe_malloc(n * sizeof(stbl_rec_t *)); for (i=0; i<n; i++) { tmp[i] = NULL; } // move the data lists to tmp mask = n-1; for (i=0; i<old_size; i++) { list = sym_table->data[i]; if (list != NULL) { stbl_restore_list(tmp, mask, list); } } // clean up safe_free(sym_table->data); sym_table->data = tmp; sym_table->size = n; }
/* * Extend the table: make it twice as large. * - do nothing if malloc fails or if the table has maximal size already */ static void stbl_extend(stbl_t *sym_table) { stbl_rec_t **tmp; stbl_rec_t *list; uint32_t i, n, old_size, mask; old_size = sym_table->size; n = old_size << 1; if (n == 0 || n >= MAX_STBL_SIZE) { // overflow: cannot expand return; } assert(is_power_of_two(n)); // new data array tmp = (stbl_rec_t **) malloc(n * sizeof(stbl_rec_t *)); if (tmp == NULL) return; for (i=0; i<n; i++) { tmp[i] = NULL; } // move the data lists to tmp mask = n-1; for (i=0; i<old_size; i++) { list = sym_table->data[i]; if (list != NULL) { stbl_restore_list(tmp, mask, list); } } // clean up safe_free(sym_table->data); sym_table->data = tmp; sym_table->size = n; #if TRACE_RESIZE printf("resize table %p: cost = %"PRIu32", nelems = %"PRIu32", ndeleted = %"PRIu32 ", old size = %"PRIu32", new size = %"PRIu32"\n", sym_table, sym_table->cost, sym_table->nelems, sym_table->ndeleted, old_size, n); fflush(stdout); #endif }