BufferAllocator.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 <http://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 "exception/Except.h"
  20. /**
  21. * Create a new Allocator which allocates from to a user supplied buffer.
  22. * This allocator will reset the pointer to the beginning of the buffer when
  23. * free() is called on it, it is up to the user to free their buffer.
  24. * Allocator_child(allocator) always returns NULL.
  25. *
  26. * @param buffer an array to write to.
  27. * @param length the size of the array. If more is written than this length,
  28. * further allocations will fail and return NULL.
  29. */
  30. struct Allocator* BufferAllocator_newWithIdentity(void* buffer,
  31. unsigned long length,
  32. char* file,
  33. int line);
  34. #define BufferAllocator_new(buffer, length) \
  35. BufferAllocator_newWithIdentity(buffer, length, __FILE__, __LINE__)
  36. // This relies on the fact that UniqueName is only unique on a per-line basis.
  37. #define BufferAllocator_STACK(name, length) \
  38. uint8_t UniqueName_get()[length]; \
  39. name = BufferAllocator_new(UniqueName_get(), length);
  40. /**
  41. * @param bufferAllocator the buffer allocator to set this on.
  42. * @param eh an exception handler.
  43. */
  44. void BufferAllocator_onOOM(struct Allocator* bufferAllocator,
  45. struct Except* eh);
  46. #endif