summaryrefslogtreecommitdiff
path: root/src/wasm-vector.h
blob: 8f50e103b382a1d94b45844ecaabce12c209f66c (plain)
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
31
32
33
34
35
36
37
38
39
#ifndef WASM_VECTOR_H_
#define WASM_VECTOR_H_

#include <stdlib.h>

#define DECLARE_VECTOR(name, type)                               \
  typedef struct type##Vector {                                  \
    type* data;                                                  \
    size_t size;                                                 \
    size_t capacity;                                             \
  } type##Vector;                                                \
  EXTERN_C void wasm_destroy_##name##_vector(type##Vector* vec); \
  EXTERN_C type* wasm_append_##name(type##Vector* vec);          \
  EXTERN_C int wasm_extend_##name##s(type##Vector* dst, type##Vector* src);

#define DEFINE_VECTOR(name, type)                                              \
  void wasm_destroy_##name##_vector(type##Vector* vec) { free(vec->data); }    \
  type* wasm_append_##name(type##Vector* vec) {                                \
    return append_element((void**)&vec->data, &vec->size, &vec->capacity,      \
                          sizeof(type));                                       \
  }                                                                            \
  int wasm_extend_##name##s(type##Vector* dst, type##Vector* src) {            \
    return extend_elements((void**)&dst->data, &dst->size, &dst->capacity,     \
                           (const void**)&src->data, src->size, sizeof(type)); \
  }

void* append_element(void** data,
                     size_t* size,
                     size_t* capacity,
                     size_t elt_byte_size);

int extend_elements(void** dst,
                    size_t* dst_size,
                    size_t* dst_capacity,
                    const void** src,
                    size_t src_size,
                    size_t elt_byte_size);

#endif /* WASM_VECTOR_H_ */