DataStorage.cpp 650 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "DataStorage.h"
  5. DataStorage::DataStorage()
  6. {
  7. bytes = (char *)calloc(4096,1);
  8. capacity = 4096;
  9. length = 0;
  10. }
  11. DataStorage::DataStorage(size_t size)
  12. {
  13. capacity = 4096;
  14. while(capacity < size) {
  15. capacity += 4096;
  16. }
  17. bytes = (char *)calloc(capacity,1);
  18. length = size;
  19. }
  20. DataStorage::~DataStorage()
  21. {
  22. free(bytes);
  23. }
  24. void DataStorage::appendBytes(const void *ptr, size_t size)
  25. {
  26. if(capacity < length + size) {
  27. do {
  28. capacity += 4096;
  29. } while(capacity < length + size);
  30. bytes = (char *)realloc(bytes, capacity);
  31. }
  32. memcpy(bytes+length, ptr, size);
  33. length += size;
  34. }