1
0

memblock.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "memblock.h"
  2. #include <bfc/bfc_assert.h>
  3. #ifdef _DEBUG
  4. int memblocks_totalsize=0;
  5. #endif
  6. VoidMemBlock::VoidMemBlock(int _size, const void *data) {
  7. mem = NULL;
  8. size = 0;
  9. setSize(_size);
  10. if (data != NULL && size > 0) MEMCPY(mem, data, size);
  11. }
  12. VoidMemBlock::~VoidMemBlock() {
  13. #ifdef _DEBUG
  14. memblocks_totalsize -= size;
  15. #endif
  16. FREE(mem);
  17. }
  18. void *VoidMemBlock::setSize(int newsize) {
  19. #ifdef _DEBUG
  20. memblocks_totalsize -= size;
  21. #endif
  22. ASSERT(newsize >= 0);
  23. if (newsize < 0) newsize = 0;
  24. if (newsize == 0) {
  25. FREE(mem);
  26. mem = NULL;
  27. } else if (size != newsize) {
  28. mem = REALLOC(mem, newsize);
  29. }
  30. size = newsize;
  31. #ifdef _DEBUG
  32. memblocks_totalsize += size;
  33. #endif
  34. return getMemory();
  35. }
  36. void *VoidMemBlock::setMinimumSize(int newminsize, int increment) {
  37. if (newminsize > size) setSize(newminsize+increment);
  38. return getMemory();
  39. }
  40. void VoidMemBlock::setMemory(const void *data, int datalen, int offsetby) {
  41. if (datalen <= 0) return;
  42. ASSERT(mem != NULL);
  43. ASSERT(offsetby >= 0);
  44. char *ptr = reinterpret_cast<char *>(mem);
  45. ASSERT(ptr + offsetby + datalen <= ptr + size);
  46. MEMCPY(ptr + offsetby, data, datalen);
  47. }
  48. int VoidMemBlock::getSize() const {
  49. return size;
  50. }
  51. int VoidMemBlock::isMine(void *ptr) {
  52. return (ptr >= mem && ptr < (char*)mem + size);
  53. }
  54. void VoidMemBlock::zeroMemory() {
  55. if (mem == NULL || size < 1) return;
  56. MEMZERO(mem, size);
  57. }