suballoc.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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: memory allocation routines *
  6. ****************************************************************************/
  7. static const uint UNIT_SIZE=Max(sizeof(RARPPM_CONTEXT),sizeof(RARPPM_MEM_BLK));
  8. static const uint FIXED_UNIT_SIZE=12;
  9. SubAllocator::SubAllocator()
  10. {
  11. Clean();
  12. }
  13. void SubAllocator::Clean()
  14. {
  15. SubAllocatorSize=0;
  16. }
  17. inline void SubAllocator::InsertNode(void* p,int indx)
  18. {
  19. ((RAR_NODE*) p)->next=FreeList[indx].next;
  20. FreeList[indx].next=(RAR_NODE*) p;
  21. }
  22. inline void* SubAllocator::RemoveNode(int indx)
  23. {
  24. RAR_NODE* RetVal=FreeList[indx].next;
  25. FreeList[indx].next=RetVal->next;
  26. return RetVal;
  27. }
  28. inline uint SubAllocator::U2B(int NU)
  29. {
  30. // We calculate the size of units in bytes based on real UNIT_SIZE.
  31. // In original implementation it was 8*NU+4*NU.
  32. return UNIT_SIZE*NU;
  33. }
  34. // Calculate RARPPM_MEM_BLK+Items address. Real RARPPM_MEM_BLK size must be
  35. // equal to UNIT_SIZE, so we cannot just add Items to RARPPM_MEM_BLK address.
  36. inline RARPPM_MEM_BLK* SubAllocator::MBPtr(RARPPM_MEM_BLK *BasePtr,int Items)
  37. {
  38. return((RARPPM_MEM_BLK*)( ((byte *)(BasePtr))+U2B(Items) ));
  39. }
  40. inline void SubAllocator::SplitBlock(void* pv,int OldIndx,int NewIndx)
  41. {
  42. int i, UDiff=Indx2Units[OldIndx]-Indx2Units[NewIndx];
  43. byte* p=((byte*) pv)+U2B(Indx2Units[NewIndx]);
  44. if (Indx2Units[i=Units2Indx[UDiff-1]] != UDiff)
  45. {
  46. InsertNode(p,--i);
  47. p += U2B(i=Indx2Units[i]);
  48. UDiff -= i;
  49. }
  50. InsertNode(p,Units2Indx[UDiff-1]);
  51. }
  52. void SubAllocator::StopSubAllocator()
  53. {
  54. if ( SubAllocatorSize )
  55. {
  56. SubAllocatorSize=0;
  57. free(HeapStart);
  58. }
  59. }
  60. bool SubAllocator::StartSubAllocator(int SASize)
  61. {
  62. uint t=SASize << 20;
  63. if (SubAllocatorSize == t)
  64. return true;
  65. StopSubAllocator();
  66. // Original algorithm expects FIXED_UNIT_SIZE, but actual structure size
  67. // can be larger. So let's recalculate the allocated size and add two more
  68. // units: one as reserve for HeapEnd overflow checks and another
  69. // to provide the space to correctly align UnitsStart.
  70. uint AllocSize=t/FIXED_UNIT_SIZE*UNIT_SIZE+2*UNIT_SIZE;
  71. if ((HeapStart=(byte *)malloc(AllocSize)) == NULL)
  72. {
  73. ErrHandler.MemoryError();
  74. return false;
  75. }
  76. // HeapEnd did not present in original algorithm. We added it to control
  77. // invalid memory access attempts when processing corrupt archived data.
  78. HeapEnd=HeapStart+AllocSize-UNIT_SIZE;
  79. SubAllocatorSize=t;
  80. return true;
  81. }
  82. void SubAllocator::InitSubAllocator()
  83. {
  84. int i, k;
  85. memset(FreeList,0,sizeof(FreeList));
  86. pText=HeapStart;
  87. // Original algorithm operates with 12 byte FIXED_UNIT_SIZE, but actual
  88. // size of RARPPM_MEM_BLK and RARPPM_CONTEXT structures can exceed this value
  89. // because of alignment and larger pointer fields size.
  90. // So we define UNIT_SIZE for this larger size and adjust memory
  91. // pointers accordingly.
  92. // Size2 is (HiUnit-LoUnit) memory area size to allocate as originally
  93. // supposed by compression algorithm. It is 7/8 of total allocated size.
  94. uint Size2=FIXED_UNIT_SIZE*(SubAllocatorSize/8/FIXED_UNIT_SIZE*7);
  95. // RealSize2 is the real adjusted size of (HiUnit-LoUnit) memory taking
  96. // into account that our UNIT_SIZE can be larger than FIXED_UNIT_SIZE.
  97. uint RealSize2=Size2/FIXED_UNIT_SIZE*UNIT_SIZE;
  98. // Size1 is the size of memory area from HeapStart to FakeUnitsStart
  99. // as originally supposed by compression algorithm. This area can contain
  100. // different data types, both single symbols and structures.
  101. uint Size1=SubAllocatorSize-Size2;
  102. // Real size of this area. We correct it according to UNIT_SIZE vs
  103. // FIXED_UNIT_SIZE difference. Also we add one more UNIT_SIZE
  104. // to compensate a possible reminder from Size1/FIXED_UNIT_SIZE,
  105. // which would be lost otherwise. We add UNIT_SIZE instead of
  106. // this Size1%FIXED_UNIT_SIZE reminder, because it allows to align
  107. // UnitsStart easily and adding more than reminder is ok for algorithm.
  108. uint RealSize1=Size1/FIXED_UNIT_SIZE*UNIT_SIZE+UNIT_SIZE;
  109. // RealSize1 must be divided by UNIT_SIZE without a reminder, so UnitsStart
  110. // is aligned to UNIT_SIZE. It is important for those architectures,
  111. // where a proper memory alignment is mandatory. Since we produce RealSize1
  112. // multiplying by UNIT_SIZE, this condition is always true. So LoUnit,
  113. // UnitsStart, HeapStart are properly aligned,
  114. LoUnit=UnitsStart=HeapStart+RealSize1;
  115. // When we reach FakeUnitsStart, we restart the model. It is where
  116. // the original algorithm expected to see UnitsStart. Real UnitsStart
  117. // can have a larger value.
  118. FakeUnitsStart=HeapStart+Size1;
  119. HiUnit=LoUnit+RealSize2;
  120. for (i=0,k=1;i < N1 ;i++,k += 1)
  121. Indx2Units[i]=k;
  122. for (k++;i < N1+N2 ;i++,k += 2)
  123. Indx2Units[i]=k;
  124. for (k++;i < N1+N2+N3 ;i++,k += 3)
  125. Indx2Units[i]=k;
  126. for (k++;i < N1+N2+N3+N4;i++,k += 4)
  127. Indx2Units[i]=k;
  128. for (GlueCount=k=i=0;k < 128;k++)
  129. {
  130. i += (Indx2Units[i] < k+1);
  131. Units2Indx[k]=i;
  132. }
  133. }
  134. inline void SubAllocator::GlueFreeBlocks()
  135. {
  136. RARPPM_MEM_BLK s0, * p, * p1;
  137. int i, k, sz;
  138. if (LoUnit != HiUnit)
  139. *LoUnit=0;
  140. for (i=0, s0.next=s0.prev=&s0;i < N_INDEXES;i++)
  141. while ( FreeList[i].next )
  142. {
  143. p=(RARPPM_MEM_BLK*)RemoveNode(i);
  144. p->insertAt(&s0);
  145. p->Stamp=0xFFFF;
  146. p->NU=Indx2Units[i];
  147. }
  148. for (p=s0.next;p != &s0;p=p->next)
  149. while ((p1=MBPtr(p,p->NU))->Stamp == 0xFFFF && int(p->NU)+p1->NU < 0x10000)
  150. {
  151. p1->remove();
  152. p->NU += p1->NU;
  153. }
  154. while ((p=s0.next) != &s0)
  155. {
  156. for (p->remove(), sz=p->NU;sz > 128;sz -= 128, p=MBPtr(p,128))
  157. InsertNode(p,N_INDEXES-1);
  158. if (Indx2Units[i=Units2Indx[sz-1]] != sz)
  159. {
  160. k=sz-Indx2Units[--i];
  161. InsertNode(MBPtr(p,sz-k),k-1);
  162. }
  163. InsertNode(p,i);
  164. }
  165. }
  166. void* SubAllocator::AllocUnitsRare(int indx)
  167. {
  168. if ( !GlueCount )
  169. {
  170. GlueCount = 255;
  171. GlueFreeBlocks();
  172. if ( FreeList[indx].next )
  173. return RemoveNode(indx);
  174. }
  175. int i=indx;
  176. do
  177. {
  178. if (++i == N_INDEXES)
  179. {
  180. GlueCount--;
  181. i=U2B(Indx2Units[indx]);
  182. int j=FIXED_UNIT_SIZE*Indx2Units[indx];
  183. if (FakeUnitsStart - pText > j)
  184. {
  185. FakeUnitsStart -= j;
  186. UnitsStart -= i;
  187. return UnitsStart;
  188. }
  189. return NULL;
  190. }
  191. } while ( !FreeList[i].next );
  192. void* RetVal=RemoveNode(i);
  193. SplitBlock(RetVal,i,indx);
  194. return RetVal;
  195. }
  196. inline void* SubAllocator::AllocUnits(int NU)
  197. {
  198. int indx=Units2Indx[NU-1];
  199. if ( FreeList[indx].next )
  200. return RemoveNode(indx);
  201. void* RetVal=LoUnit;
  202. LoUnit += U2B(Indx2Units[indx]);
  203. if (LoUnit <= HiUnit)
  204. return RetVal;
  205. LoUnit -= U2B(Indx2Units[indx]);
  206. return AllocUnitsRare(indx);
  207. }
  208. void* SubAllocator::AllocContext()
  209. {
  210. if (HiUnit != LoUnit)
  211. return (HiUnit -= UNIT_SIZE);
  212. if ( FreeList->next )
  213. return RemoveNode(0);
  214. return AllocUnitsRare(0);
  215. }
  216. void* SubAllocator::ExpandUnits(void* OldPtr,int OldNU)
  217. {
  218. int i0=Units2Indx[OldNU-1], i1=Units2Indx[OldNU-1+1];
  219. if (i0 == i1)
  220. return OldPtr;
  221. void* ptr=AllocUnits(OldNU+1);
  222. if ( ptr )
  223. {
  224. memcpy(ptr,OldPtr,U2B(OldNU));
  225. InsertNode(OldPtr,i0);
  226. }
  227. return ptr;
  228. }
  229. void* SubAllocator::ShrinkUnits(void* OldPtr,int OldNU,int NewNU)
  230. {
  231. int i0=Units2Indx[OldNU-1], i1=Units2Indx[NewNU-1];
  232. if (i0 == i1)
  233. return OldPtr;
  234. if ( FreeList[i1].next )
  235. {
  236. void* ptr=RemoveNode(i1);
  237. memcpy(ptr,OldPtr,U2B(NewNU));
  238. InsertNode(OldPtr,i0);
  239. return ptr;
  240. }
  241. else
  242. {
  243. SplitBlock(OldPtr,i0,i1);
  244. return OldPtr;
  245. }
  246. }
  247. void SubAllocator::FreeUnits(void* ptr,int OldNU)
  248. {
  249. InsertNode(ptr,Units2Indx[OldNU-1]);
  250. }