suballoc.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #if defined(__GNUC__) && defined(ALLOW_MISALIGNED)
  10. #define RARPPM_PACK_ATTR __attribute__ ((packed))
  11. #else
  12. #define RARPPM_PACK_ATTR
  13. #endif /* defined(__GNUC__) */
  14. #ifdef ALLOW_MISALIGNED
  15. #pragma pack(1)
  16. #endif
  17. struct RARPPM_MEM_BLK
  18. {
  19. ushort Stamp, NU;
  20. RARPPM_MEM_BLK* next, * prev;
  21. void insertAt(RARPPM_MEM_BLK* p)
  22. {
  23. next=(prev=p)->next;
  24. p->next=next->prev=this;
  25. }
  26. void remove()
  27. {
  28. prev->next=next;
  29. next->prev=prev;
  30. }
  31. } RARPPM_PACK_ATTR;
  32. #ifdef ALLOW_MISALIGNED
  33. #ifdef _AIX
  34. #pragma pack(pop)
  35. #else
  36. #pragma pack()
  37. #endif
  38. #endif
  39. class SubAllocator
  40. {
  41. private:
  42. static const int N1=4, N2=4, N3=4, N4=(128+3-1*N1-2*N2-3*N3)/4;
  43. static const int N_INDEXES=N1+N2+N3+N4;
  44. struct RAR_NODE
  45. {
  46. RAR_NODE* next;
  47. };
  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. inline void GlueFreeBlocks();
  53. void* AllocUnitsRare(int indx);
  54. inline RARPPM_MEM_BLK* MBPtr(RARPPM_MEM_BLK *BasePtr,int Items);
  55. long SubAllocatorSize;
  56. byte Indx2Units[N_INDEXES], Units2Indx[128], GlueCount;
  57. byte *HeapStart,*LoUnit, *HiUnit;
  58. struct RAR_NODE FreeList[N_INDEXES];
  59. public:
  60. SubAllocator();
  61. ~SubAllocator() {StopSubAllocator();}
  62. void Clean();
  63. bool StartSubAllocator(int SASize);
  64. void StopSubAllocator();
  65. void InitSubAllocator();
  66. inline void* AllocContext();
  67. inline void* AllocUnits(int NU);
  68. inline void* ExpandUnits(void* ptr,int OldNU);
  69. inline void* ShrinkUnits(void* ptr,int OldNU,int NewNU);
  70. inline void FreeUnits(void* ptr,int OldNU);
  71. long GetAllocatedMemory() {return(SubAllocatorSize);}
  72. byte *pText, *UnitsStart,*HeapEnd,*FakeUnitsStart;
  73. };
  74. #endif /* !defined(_SUBALLOC_H_) */