region.c 806 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "region.h"
  4. #include "reporting.h"
  5. void region_create(region *r)
  6. {
  7. r->len = 0;
  8. r->alloc_len = 2;
  9. r->pointers = malloc(sizeof(void*)*r->alloc_len);
  10. }
  11. void* region_malloc(region *r, size_t size)
  12. {
  13. if (r->len >= r->alloc_len) {
  14. r->alloc_len <<= 1;
  15. r->pointers = realloc(r->pointers, sizeof(void*)*r->alloc_len);
  16. }
  17. return r->pointers[r->len++] = malloc(size);
  18. }
  19. void* region_realloc(region *r, void *v, size_t size)
  20. {
  21. int i;
  22. for (i = 0; i < r->len; i++) {
  23. if (r->pointers[i] == v) {
  24. return r->pointers[i] = realloc(r->pointers[i], size);
  25. }
  26. }
  27. reporterr("Unable to realloc region [%p]", v);
  28. }
  29. void region_free(region *r)
  30. {
  31. int i;
  32. for (i = 0; i < r->len; i++) {
  33. free(r->pointers[i]);
  34. }
  35. free(r->pointers);
  36. r->pointers = NULL;
  37. }