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