Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2017, Justin Crawford <Justin@stacksmash.net>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any purpose
  5. * with or without fee is hereby granted, provided that the above copyright notice
  6. * and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
  9. * THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
  10. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include <cstring>
  16. #include <cstdlib>
  17. #include <cassert>
  18. #include <new>
  19. #include <algorithm>
  20. #include "ManagedBuffer.h"
  21.  
  22. ManagedBuffer::ManagedBuffer()
  23. {
  24. this->data = new internaldata_t;
  25. memset(this->data, 0, sizeof(internaldata_t));
  26. this->data->refs++;
  27. this->data->allocatedsz = 1024;
  28. this->data->size = 0;
  29. this->data->data = malloc(this->data->allocatedsz);
  30. if (!this->data->data)
  31. throw std::bad_alloc();
  32.  
  33. // Initialize the buffer.
  34. memset(this->data->data, 0, this->data->allocatedsz);
  35. }
  36.  
  37. ManagedBuffer::ManagedBuffer(const ManagedBuffer &other)
  38. {
  39. this->data = other.data;
  40. this->data->refs++;
  41. }
  42.  
  43. ManagedBuffer &ManagedBuffer::operator= (const ManagedBuffer &other)
  44. {
  45. this->data = other.data;
  46. this->data->refs++;
  47. return *this;
  48. }
  49.  
  50. ManagedBuffer::~ManagedBuffer()
  51. {
  52. this->data->refs--;
  53. if (!this->data->refs)
  54. {
  55. // condition should never happen.
  56. assert(this->data && this->data->data);
  57. free(this->data->data);
  58. delete this->data;
  59. this->data = nullptr;
  60. }
  61. }
  62.  
  63.  
  64. void ManagedBuffer::Write(const void *ddata, size_t size)
  65. {
  66. if (this->data->size + size > this->data->allocatedsz)
  67. {
  68. size_t newsz = std::max(this->data->size + size, this->data->size + 1024UL);
  69. void *ptr = realloc(this->data->data, newsz);
  70. if (!ptr)
  71. throw std::bad_alloc();
  72.  
  73. this->data->data = ptr;
  74. this->data->allocatedsz = newsz;
  75. // Initialize the new area
  76. memset(reinterpret_cast<uint8_t*>(this->data->data) + this->data->size, 0, newsz - this->data->size);
  77. }
  78.  
  79. // Copy our data.
  80. memcpy(reinterpret_cast<uint8_t*>(this->data->data) + this->data->size, ddata, size);
  81. this->data->size += size;
  82. }
  83.  
  84. void ManagedBuffer::AllocateAhead(size_t sz)
  85. {
  86. size_t newsz = this->data->allocatedsz + sz;
  87. void *ptr = realloc(this->data->data, newsz);
  88. if (!ptr)
  89. throw std::bad_alloc();
  90.  
  91. this->data->data = ptr;
  92. this->data->allocatedsz = newsz;
  93. memset(reinterpret_cast<uint8_t*>(this->data->data) + this->data->size, 0, newsz - this->data->size);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement