A collection of basic/generally desirable code I use across multiple C++ projects.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.0 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #pragma once
  2. #ifndef ULE_ALLOC_H
  3. #define ULE_ALLOC_H
  4. #include "config.h"
  5. #include "types.h"
  6. // define a consistent memory allocation interface
  7. // trailing void* is a pointer to some allocator state, if relevant.
  8. // will be unused for malloc/calloc/realloc/free, as the allocator state is internal to the OS.
  9. // overloads should exist which do nothing with the trailing paramter.
  10. typedef void* (*mallocator) (size_t, void*);
  11. typedef void* (*callocator) (size_t, size_t, void*);
  12. typedef void* (*reallocator) (void*, size_t, void*);
  13. typedef void (*freeer) (void*, void*);
  14. typedef void (*clearer) ( void*);
  15. typedef void (*destroyer) ( void*);
  16. // operating system allocator wrappers
  17. // the overloads with the 'void*' are to make them conform to the allocator interface above.
  18. extern void* pMalloc(size_t size);
  19. extern void* pMalloc(size_t size, void* allocatorState);
  20. extern void* pCalloc(size_t maxNumOfElements, size_t elementSize);
  21. extern void* pCalloc(size_t maxNumOfElements, size_t elementSize, void* allocatorState);
  22. extern void* pRealloc(void* buffer, size_t newSize);
  23. extern void* pRealloc(void* buffer, size_t newSize, void* allocatorState);
  24. extern void pFree(void* ptr);
  25. extern void pFree(void* ptr, void* allocatorState);
  26. extern void pFree(const void* ptr);
  27. extern void pFree(const void* ptr, void* allocatorState);
  28. struct Allocator {
  29. mallocator mallocate;
  30. callocator callocate;
  31. reallocator reallocate;
  32. freeer free; // releases a specific piece/chunk of memory, identified by pointer.
  33. clearer clear; // should release all the memory owned by this allocator at once.
  34. destroyer destroy; // releases all the memory owned by this allocator, and also destroys the allocator.
  35. void* state;
  36. static Allocator& GetDefault();
  37. Allocator() {
  38. this->state = null;
  39. this->mallocate = pMalloc;
  40. this->callocate = pCalloc;
  41. this->reallocate = pRealloc;
  42. this->free = pFree;
  43. this->clear = null;
  44. this->destroy = null;
  45. }
  46. Allocator(mallocator mallocate, freeer free) {
  47. this->state = null;
  48. this->mallocate = mallocate;
  49. this->callocate = null;
  50. this->reallocate = null;
  51. this->free = free;
  52. this->clear = null;
  53. this->destroy = null;
  54. }
  55. Allocator(
  56. mallocator mallocate,
  57. callocator callocate = null,
  58. reallocator reallocate = null,
  59. freeer free = null,
  60. clearer clear = null,
  61. destroyer destroy = null,
  62. void* state = null
  63. ) {
  64. this->mallocate = mallocate;
  65. this->callocate = callocate;
  66. this->reallocate = reallocate;
  67. this->free = free;
  68. this->clear = clear;
  69. this->destroy = destroy;
  70. this->state = state;
  71. }
  72. };
  73. struct Arena {
  74. u32 bufferSizeInBytes;
  75. u32 index;
  76. u8* buffer;
  77. static Arena* Init(u32 sizeInBytes = 1024);
  78. void* Alloc(u32 sizeInBytes);
  79. void Clear();
  80. float PercentUsed();
  81. };
  82. #endif