suballoc.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /****************************************************************************
  2. * This file is part of PPMd project *
  3. * Written and distributed to public domain by Dmitry Shkarin 1997, *
  4. * 1999-2000 *
  5. * Contents: interface to memory allocation routines *
  6. ****************************************************************************/
  7. #if !defined(_SUBALLOC_H_)
  8. #define _SUBALLOC_H_
  9. const int N1=4, N2=4, N3=4, N4=(128+3-1*N1-2*N2-3*N3)/4;
  10. const int N_INDEXES=N1+N2+N3+N4;
  11. #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT_REQUIRED)
  12. #define _PACK_ATTR __attribute__ ((packed))
  13. #else
  14. #define _PACK_ATTR
  15. #endif /* defined(__GNUC__) */
  16. #ifndef STRICT_ALIGNMENT_REQUIRED
  17. #pragma pack(1)
  18. #endif
  19. struct RAR_MEM_BLK
  20. {
  21. ushort Stamp, NU;
  22. RAR_MEM_BLK* next, * prev;
  23. void insertAt(RAR_MEM_BLK* p)
  24. {
  25. next=(prev=p)->next;
  26. p->next=next->prev=this;
  27. }
  28. void remove()
  29. {
  30. prev->next=next;
  31. next->prev=prev;
  32. }
  33. } _PACK_ATTR;
  34. #ifndef STRICT_ALIGNMENT_REQUIRED
  35. #ifdef _AIX
  36. #pragma pack(pop)
  37. #else
  38. #pragma pack()
  39. #endif
  40. #endif
  41. struct RAR_NODE
  42. {
  43. RAR_NODE* next;
  44. };
  45. class SubAllocator
  46. {
  47. private:
  48. inline void InsertNode(void* p,int indx);
  49. inline void* RemoveNode(int indx);
  50. inline uint U2B(int NU);
  51. inline void SplitBlock(void* pv,int OldIndx,int NewIndx);
  52. uint GetUsedMemory();
  53. inline void GlueFreeBlocks();
  54. void* AllocUnitsRare(int indx);
  55. inline RAR_MEM_BLK* MBPtr(RAR_MEM_BLK *BasePtr,int Items);
  56. long SubAllocatorSize;
  57. byte Indx2Units[N_INDEXES], Units2Indx[128], GlueCount;
  58. byte *HeapStart,*LoUnit, *HiUnit;
  59. struct RAR_NODE FreeList[N_INDEXES];
  60. public:
  61. SubAllocator();
  62. ~SubAllocator() {StopSubAllocator();}
  63. void Clean();
  64. bool StartSubAllocator(int SASize);
  65. void StopSubAllocator();
  66. void InitSubAllocator();
  67. inline void* AllocContext();
  68. inline void* AllocUnits(int NU);
  69. inline void* ExpandUnits(void* ptr,int OldNU);
  70. inline void* ShrinkUnits(void* ptr,int OldNU,int NewNU);
  71. inline void FreeUnits(void* ptr,int OldNU);
  72. long GetAllocatedMemory() {return(SubAllocatorSize);};
  73. byte *pText, *UnitsStart,*HeapEnd,*FakeUnitsStart;
  74. };
  75. #endif /* !defined(_SUBALLOC_H_) */