aboutsummaryrefslogtreecommitdiff
path: root/vec.c
blob: 58977dc4fc301d78d400e0e12da669f1dc5348f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <string.h>

#include "vec.h"

Vec *_Vec_new(Arena *a, int size, ptrdiff_t cap, int align) {
  Vec *v = new (a, Vec, 1);
  v->data = alloc(a, cap, size, align);
  v->size = size;
  v->align = align;
  v->cap = cap;
  v->len = 0;
  return v;
}

Vec *Vec_push(Arena *a, Vec *v, void *data) {
  if (v->len == v->cap) {
    void *cpy = alloc(a, v->cap * 2, v->size, v->align);
    memcpy(cpy, v->data, v->len * v->size);
    v->cap *= 2;
  }
  memcpy(v->data + v->len * v->size, data, v->size);
  v->len++;
  return v;
}

void *_Vec_get(Vec *v, int pos) {
  // printf("get: length=%lu, cap=%lu, pos=%d\n", v->len, v->cap, pos);
  // printf("get: start=%p, asked=%p\n", v->data, v->data + v->size * pos);
  return v->data + v->size * pos;
}