int make_vector(int n) {
  int next_loc;
  int vector_loc;
  next_loc = vector_pool_idx + n + 1;
  if (next_loc >= VECTOR_POOL_SIZE) {
    log_error(VECTOR_POOL_EXHAUSTION, 0);
    cleanup();
  }
  vector_loc = vector_pool_idx;
  vector_pool_idx = next_loc;
  // size
  vector_pool[vector_loc] = n;
  // TODO zero init vector
  return vector_loc;
}

int vector_length(int v) {
  return vector_pool[v];
}

int vector_ref(int v, int n) {
  if (n >= vector_length(v)) {
    log_error(VECTOR_REFERENCE_OUT_OF_RANGE, 0);
    cleanup();
  }
  return vector_pool[v + n + 1]; 
}

void vector_set(int v, int n, int val) {
  if (n >= vector_length(v)) {
    log_error(VECTOR_REFERENCE_OUT_OF_RANGE, 0);
    cleanup();
  }
  vector_pool[v + n + 1] = val;
  return;
}

int vector_resize(int v) {
  int size;
  int new_v;
  int i;
  size = vector_length(v);
  new_v = make_vector(size << 1);
  for (i = 0; i < size; i++)
    vector_set(new_v, i, vector_ref(v, i));
  return new_v;
}

int is_out_of_range_vector(int v, int n) {
  return n >= vector_length(v);
}
