Welcome to mirror list, hosted at ThFree Co, Russian Federation.

suballoc.hpp « unrar « thirdparty « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0280289cfc0bd499af7eb1c80ff39a7db2c42e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/****************************************************************************
 *  This file is part of PPMd project                                       *
 *  Written and distributed to public domain by Dmitry Shkarin 1997,        *
 *  1999-2000                                                               *
 *  Contents: interface to memory allocation routines                       *
 ****************************************************************************/
#if !defined(_SUBALLOC_H_)
#define _SUBALLOC_H_

#if defined(__GNUC__) && defined(ALLOW_MISALIGNED)
#define RARPPM_PACK_ATTR __attribute__ ((packed))
#else
#define RARPPM_PACK_ATTR
#endif /* defined(__GNUC__) */

#ifdef ALLOW_MISALIGNED
#pragma pack(1)
#endif

struct RARPPM_MEM_BLK 
{
  ushort Stamp, NU;
  RARPPM_MEM_BLK* next, * prev;
  void insertAt(RARPPM_MEM_BLK* p) 
  {
    next=(prev=p)->next;
    p->next=next->prev=this;
  }
  void remove() 
  {
    prev->next=next;
    next->prev=prev;
  }
} RARPPM_PACK_ATTR;

#ifdef ALLOW_MISALIGNED
#ifdef _AIX
#pragma pack(pop)
#else
#pragma pack()
#endif
#endif


class SubAllocator
{
  private:
    static const int N1=4, N2=4, N3=4, N4=(128+3-1*N1-2*N2-3*N3)/4;
    static const int N_INDEXES=N1+N2+N3+N4;

    struct RAR_NODE
    {
      RAR_NODE* next;
    };

    inline void InsertNode(void* p,int indx);
    inline void* RemoveNode(int indx);
    inline uint U2B(int NU);
    inline void SplitBlock(void* pv,int OldIndx,int NewIndx);
    uint GetUsedMemory();
    inline void GlueFreeBlocks();
    void* AllocUnitsRare(int indx);
    inline RARPPM_MEM_BLK* MBPtr(RARPPM_MEM_BLK *BasePtr,int Items);

    long SubAllocatorSize;
    byte Indx2Units[N_INDEXES], Units2Indx[128], GlueCount;
    byte *HeapStart,*LoUnit, *HiUnit;
    struct RAR_NODE FreeList[N_INDEXES];
  public:
    SubAllocator();
    ~SubAllocator() {StopSubAllocator();}
    void Clean();
    bool StartSubAllocator(int SASize);
    void StopSubAllocator();
    void  InitSubAllocator();
    inline void* AllocContext();
    inline void* AllocUnits(int NU);
    inline void* ExpandUnits(void* ptr,int OldNU);
    inline void* ShrinkUnits(void* ptr,int OldNU,int NewNU);
    inline void  FreeUnits(void* ptr,int OldNU);
    long GetAllocatedMemory() {return(SubAllocatorSize);};

    byte *pText, *UnitsStart,*HeapEnd,*FakeUnitsStart;
};


#endif /* !defined(_SUBALLOC_H_) */