// create a test sequence which never uses more than max_used_memory // and allocates a total of max_used_memory*allocation_factor SEQLIST *generate_sequence(int max_used_memory, int allocation_factor) { int used_memory = 0; int total_allocated = 0; int next_block_size = 0; int allocated_blocks = 0; int actual_max_used_memory = 0; SEQLIST *test_sequence = (SEQLIST *) 0; SEQLIST *tail_sequence = (SEQLIST *) 0; SEQLIST *tofree = (SEQLIST *) 0; unsigned char *new_block_ref; while (total_allocated < allocation_factor * max_used_memory) { next_block_size = random_block_size(max_used_memory); // first see if we need to free anything in order to // accommodate the new allocation while (used_memory + next_block_size > max_used_memory) { // randomly pick a block to free SEQLIST *tofree = find_nth_allocated_block(test_sequence, random_int(allocated_blocks)); // add the free tail_sequence = seq_set_next_free(tofree, tail_sequence); // reclaim the memory used_memory -= seq_size(tofree); allocated_blocks--; // mark the old block as something that has been freed seq_free(tofree); } // allocate a reference buffer for the new block new_block_ref = allocate_and_fill(next_block_size); // now allocate that block if (seq_null(test_sequence)) { // special case for first allocation test_sequence = seq_add_front(next_block_size, new_block_ref, (SEQLIST *) 0); tail_sequence = test_sequence; } else { // typical case we add at the end tail_sequence = seq_set_next_allocate(next_block_size, new_block_ref, tail_sequence); } // debug //seq_print(tail_sequence); // just prints the new one total_allocated += next_block_size; used_memory += next_block_size; if (used_memory > actual_max_used_memory) actual_max_used_memory = used_memory; allocated_blocks++; } // just so can manually see this is doing something sensible printf("Actual maximum memory usage %d (%f)\n", actual_max_used_memory, ((double) actual_max_used_memory / (double) max_used_memory)); return test_sequence; }
void vector<T>::fill_and_initialize(size_type n, const T& x) { iterator result = allocate_and_fill(n, x); _start = result; _end = result + n; _capacity = _capacity + n; }