ByteCode.c 801 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "vm/ByteCode.h"
  5. ByteCode* bcInit() {
  6. ByteCode* bc = malloc(sizeof(ByteCode));
  7. bc->capacity = 16;
  8. bc->length = 0;
  9. bc->code = malloc(bc->capacity);
  10. return bc;
  11. }
  12. void bcDelete(ByteCode* bc) {
  13. free(bc->code);
  14. free(bc);
  15. }
  16. int bcReserveBytes(ByteCode* bc, int length) {
  17. while(bc->length + length > bc->capacity) {
  18. bc->capacity *= 2;
  19. bc->code = realloc(bc->code, bc->capacity);
  20. }
  21. int p = bc->length;
  22. bc->length += length;
  23. return p;
  24. }
  25. void bcSetBytes(ByteCode* bc, int p, const void* data, int length) {
  26. memcpy(bc->code + p, data, length);
  27. }
  28. void bcAddBytes(ByteCode* bc, const void* data, int length) {
  29. bcSetBytes(bc, bcReserveBytes(bc, length), data, length);
  30. }