BufferAllocator.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef BufferAllocator_H
  16. #define BufferAllocator_H
  17. #include "memory/Allocator.h"
  18. #include "util/UniqueName.h"
  19. #include "util/Gcc.h"
  20. #include "util/Linker.h"
  21. Linker_require("memory/BufferAllocator.c")
  22. /**
  23. * Create a new Allocator which allocates from to a user supplied buffer.
  24. * This allocator will reset the pointer to the beginning of the buffer when
  25. * free() is called on it, it is up to the user to free their buffer.
  26. * Allocator_child(allocator) always returns NULL.
  27. *
  28. * @param buffer an array to write to.
  29. * @param length the size of the array. If more is written than this length,
  30. * further allocations will fail and return NULL.
  31. */
  32. struct Allocator* BufferAllocator__new(void* buffer,
  33. unsigned long length,
  34. char* file,
  35. int line);
  36. #define BufferAllocator_new(a,b) BufferAllocator__new((a),(b),Gcc_SHORT_FILE,Gcc_LINE)
  37. // This relies on the fact that UniqueName is only unique on a per-line basis.
  38. #define BufferAllocator_STACK(name, length) \
  39. uint8_t UniqueName_get()[length]; \
  40. name = BufferAllocator_new(UniqueName_last(), length);
  41. #endif