Buffer.c 625 B

123456789101112131415161718192021222324252627282930
  1. #define IMPORT_CORE
  2. #include "core/Buffer.h"
  3. #include <string.h>
  4. #include "core/Utility.h"
  5. void initBuffer(Buffer* b) {
  6. b->buffer = nullptr;
  7. b->capacity = 0;
  8. b->size = 0;
  9. }
  10. void destroyBuffer(Buffer* b) {
  11. cFree(b->buffer);
  12. initBuffer(b);
  13. }
  14. void addSizedBufferData(Buffer* b, const void* data, size_t size) {
  15. while(b->size + size >= b->capacity) {
  16. b->capacity = b->capacity == 0 ? 8 : (b->capacity * 5) / 4;
  17. b->buffer = cReallocate(b->buffer, b->capacity);
  18. }
  19. memcpy(b->buffer + b->size, data, size);
  20. b->size += size;
  21. }
  22. void clearBuffer(Buffer* b) {
  23. b->size = 0;
  24. }