memblock.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef _MEMBLOCK_H
  2. #define _MEMBLOCK_H
  3. //#include <bfc/wasabi_std.h>
  4. #include <bfc/std_mem.h>
  5. //#include <wasabicfg.h>
  6. #ifdef _DEBUG
  7. extern int memblocks_totalsize;
  8. #endif
  9. class VoidMemBlock {
  10. protected:
  11. VoidMemBlock(int size=0, const void *data=0);
  12. ~VoidMemBlock();
  13. void *setSize(int newsize);
  14. void *setMinimumSize(int newminsize, int increment=0);
  15. void *getMemory() const { return mem; }
  16. void setMemory(const void *data, int datalen, int offsetby=0);
  17. int getSize() const;
  18. int isMine(void *ptr);
  19. void zeroMemory();
  20. private:
  21. void *mem;
  22. int size;
  23. };
  24. // just a convenient resizeable block of memory wrapper
  25. // doesn't handle constructors or anything, meant for int, char, etc.
  26. template <class T>
  27. class MemBlock : private VoidMemBlock {
  28. public:
  29. MemBlock(int _size = 0, const T *data = NULL) : VoidMemBlock(_size*sizeof(T), data) {}
  30. MemBlock(const MemBlock<T> &source) : VoidMemBlock(source.getSizeInBytes(), source.getMemory()) {}
  31. MemBlock(const MemBlock<T> *source) : VoidMemBlock(source->getSizeInBytes(), source->getMemory()) {}
  32. T *setSize(int newsize) {
  33. return static_cast<T*>(VoidMemBlock::setSize(newsize * sizeof(T)));
  34. }
  35. T *setMinimumSize(int newminsize, int increment = 0) {
  36. return static_cast<T*>(VoidMemBlock::setMinimumSize(newminsize * sizeof(T), increment*sizeof(T)));
  37. }
  38. T *getMemory() const { return static_cast<T *>(VoidMemBlock::getMemory()); }
  39. T *getMemory(int offset) const { return static_cast<T *>(VoidMemBlock::getMemory())+offset; } // note pointer arithmetic
  40. T *m() const { return getMemory(); }
  41. T *m(int offset) const { return getMemory(offset); }
  42. operator T *() const { return getMemory(); }
  43. T& operator() (unsigned int ofs) { return getMemory()[ofs]; }
  44. const T& operator() (unsigned int ofs) const { return getMemory()[ofs]; }
  45. void copyTo(T *dest) { MEMCPY(getMemory(), dest, getSizeInBytes()); }
  46. int getSize() const { return VoidMemBlock::getSize()/sizeof(T); }// # of T's
  47. int getSizeInBytes() const { return VoidMemBlock::getSize(); }
  48. int isMine(T *ptr) { return VoidMemBlock::isMine(ptr); }
  49. void setMemory(const T *data, int datalen, int offsetby = 0) { VoidMemBlock::setMemory(data, datalen*sizeof(T), offsetby*sizeof(T)); } // # of T's
  50. void zeroMemory() { VoidMemBlock::zeroMemory(); }
  51. void reverse() {
  52. const int nitems = getSize();
  53. if (nitems <= 1) return;
  54. MemBlock<T> nm(this);
  55. T *mymem = getMemory(), *revmem = nm.getMemory();
  56. for (int i = 0, j = nitems-1; j >= 0; i++, j--) {
  57. MEMCPY(mymem+i, revmem+j, sizeof(T));
  58. }
  59. }
  60. };
  61. // just a convenience class for a 2d MemBlock<>
  62. template <class T>
  63. class MemMatrix : public MemBlock<T> {
  64. public:
  65. MemMatrix(int _width, int height, const T *data = NULL) : MemBlock<T>(_width * height, data), width(_width) {}
  66. T *setSize(int _width, int height) {
  67. MemBlock<T>::setSize(width * height);
  68. width = _width;
  69. }
  70. // () access i.e. obj(x,y)
  71. T& operator() (unsigned int x, unsigned int y) {
  72. return *MemBlock<T>::m(x + y * width);
  73. }
  74. const T& operator() (unsigned int x, unsigned int y) const {
  75. return *MemBlock<T>::m(x + y * width);
  76. }
  77. private:
  78. int width;
  79. };
  80. #endif