linear_allocator.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @file linear_allocator.h
  3. * @author Travis Vroman ([email protected])
  4. * @brief This file contains the linear allocator implementation.
  5. * @details A linear allocator allocates memory from its internal block of memory
  6. * in a linear fashion. That is, one after another, moving a pointer along as it goes.
  7. * A linear allocator can perform allocations of any size, but allocation sizes are
  8. * not stored, and thus allocations made in this way are not individually freeable.
  9. * Only the entire thing can be freed. This comes with the benefit of speed at a cost
  10. * of flexibility.
  11. * @version 1.0
  12. * @date 2022-01-10
  13. *
  14. * @copyright Kohi Game Engine is Copyright (c) Travis Vroman 2021-2022
  15. *
  16. */
  17. #pragma once
  18. #include "defines.h"
  19. /**
  20. * @brief The data structure for a linear allocator.
  21. */
  22. typedef struct linear_allocator {
  23. /** @brief The total size of memory in the allocator. */
  24. u64 total_size;
  25. /** @brief The amount of memory currently allocated. */
  26. u64 allocated;
  27. /** @brief The internal block of memory used by the allocator. */
  28. void* memory;
  29. /**
  30. * @brief Indicates if the allocator owns the memory (meaning it
  31. * performed the allocation itself) or whether it was provided by an outside source.
  32. */
  33. b8 owns_memory;
  34. } linear_allocator;
  35. /**
  36. * @brief Creates a linear allocator of the given size.
  37. *
  38. * @param total_size The total amount in bytes the allocator will hold.
  39. * @param memory Allocated block of memory matching size above, or 0. If 0, a dynamic allocation is performed
  40. * and this allocator is considered to own that memory.
  41. * @param out_allocator A pointer to hold the new allocator.
  42. */
  43. KAPI void linear_allocator_create(u64 total_size, void* memory, linear_allocator* out_allocator);
  44. /**
  45. * @brief Destroys the given allocator. If the allocator owns its memory, it is freed at this time.
  46. *
  47. * @param allocator A pointer to the allocator to be destroyed.
  48. */
  49. KAPI void linear_allocator_destroy(linear_allocator* allocator);
  50. /**
  51. * @brief Allocates the given amount from the allocator.
  52. *
  53. * @param allocator A pointer to the allocator to allocate from.
  54. * @param size The size to be allocated.
  55. * @return A pointer to a block of memory as allocated. If this fails, 0 is returned.
  56. */
  57. KAPI void* linear_allocator_allocate(linear_allocator* allocator, u64 size);
  58. /**
  59. * @brief Frees everything in the allocator, effectively moving its pointer back to the beginning.
  60. * Does not free internal memory, if owned. Only resets the pointer.
  61. *
  62. * @param allocator A pointer to the allocator to free.
  63. * @param clear Indicates whether or not to clear/zero the memory. Enabling this obviously takes more processing power.
  64. */
  65. KAPI void linear_allocator_free_all(linear_allocator* allocator, b8 clear);