vmalloc.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include <vmalloc/vmalloc.h>
  11. #include <stdbool.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. /** make room to store a new pointer we are about to allocate
  15. *
  16. * @param vm Vmalloc to operate on
  17. * @returns true on success
  18. */
  19. static bool make_space(Vmalloc_t *vm) {
  20. if (vm->size == vm->capacity) {
  21. // expand our allocation storage
  22. size_t c = vm->capacity == 0 ? 1 : vm->capacity * 2;
  23. void **p = realloc(vm->allocated, sizeof(vm->allocated[0]) * c);
  24. if (p == NULL) {
  25. return false;
  26. }
  27. // save the new array
  28. vm->allocated = p;
  29. vm->capacity = c;
  30. }
  31. return true;
  32. }
  33. void *vmalloc(Vmalloc_t *vm, size_t size) {
  34. if (!make_space(vm)) {
  35. return NULL;
  36. }
  37. void *p = malloc(size);
  38. if (p == NULL) {
  39. return NULL;
  40. }
  41. vm->allocated[vm->size] = p;
  42. ++vm->size;
  43. return p;
  44. }
  45. void vmfree(Vmalloc_t *vm, void *data) {
  46. if (!data) { // ANSI-ism
  47. return;
  48. }
  49. // find the pointer we previously allocated
  50. for (size_t i = 0; i < vm->size; ++i) {
  51. if (vm->allocated[i] == data) {
  52. // clear this slot
  53. size_t extent = sizeof(vm->allocated[0]) * (vm->size - i - 1);
  54. memmove(vm->allocated + i, vm->allocated + i + 1, extent);
  55. --vm->size;
  56. // give this back to the underlying allocator
  57. free(data);
  58. return;
  59. }
  60. }
  61. // we did not find this pointer; free() of something we did not allocate
  62. }