xref: /aosp_15_r20/external/lzma/C/Alloc.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1*f6dc9357SAndroid Build Coastguard Worker /* Alloc.h -- Memory allocation functions
2*f6dc9357SAndroid Build Coastguard Worker 2024-01-22 : Igor Pavlov : Public domain */
3*f6dc9357SAndroid Build Coastguard Worker 
4*f6dc9357SAndroid Build Coastguard Worker #ifndef ZIP7_INC_ALLOC_H
5*f6dc9357SAndroid Build Coastguard Worker #define ZIP7_INC_ALLOC_H
6*f6dc9357SAndroid Build Coastguard Worker 
7*f6dc9357SAndroid Build Coastguard Worker #include "7zTypes.h"
8*f6dc9357SAndroid Build Coastguard Worker 
9*f6dc9357SAndroid Build Coastguard Worker EXTERN_C_BEGIN
10*f6dc9357SAndroid Build Coastguard Worker 
11*f6dc9357SAndroid Build Coastguard Worker /*
12*f6dc9357SAndroid Build Coastguard Worker   MyFree(NULL)        : is allowed, as free(NULL)
13*f6dc9357SAndroid Build Coastguard Worker   MyAlloc(0)          : returns NULL : but malloc(0)        is allowed to return NULL or non_NULL
14*f6dc9357SAndroid Build Coastguard Worker   MyRealloc(NULL, 0)  : returns NULL : but realloc(NULL, 0) is allowed to return NULL or non_NULL
15*f6dc9357SAndroid Build Coastguard Worker MyRealloc() is similar to realloc() for the following cases:
16*f6dc9357SAndroid Build Coastguard Worker   MyRealloc(non_NULL, 0)         : returns NULL and always calls MyFree(ptr)
17*f6dc9357SAndroid Build Coastguard Worker   MyRealloc(NULL, non_ZERO)      : returns NULL, if allocation failed
18*f6dc9357SAndroid Build Coastguard Worker   MyRealloc(non_NULL, non_ZERO)  : returns NULL, if reallocation failed
19*f6dc9357SAndroid Build Coastguard Worker */
20*f6dc9357SAndroid Build Coastguard Worker 
21*f6dc9357SAndroid Build Coastguard Worker void *MyAlloc(size_t size);
22*f6dc9357SAndroid Build Coastguard Worker void MyFree(void *address);
23*f6dc9357SAndroid Build Coastguard Worker void *MyRealloc(void *address, size_t size);
24*f6dc9357SAndroid Build Coastguard Worker 
25*f6dc9357SAndroid Build Coastguard Worker void *z7_AlignedAlloc(size_t size);
26*f6dc9357SAndroid Build Coastguard Worker void  z7_AlignedFree(void *p);
27*f6dc9357SAndroid Build Coastguard Worker 
28*f6dc9357SAndroid Build Coastguard Worker #ifdef _WIN32
29*f6dc9357SAndroid Build Coastguard Worker 
30*f6dc9357SAndroid Build Coastguard Worker #ifdef Z7_LARGE_PAGES
31*f6dc9357SAndroid Build Coastguard Worker void SetLargePageSize(void);
32*f6dc9357SAndroid Build Coastguard Worker #endif
33*f6dc9357SAndroid Build Coastguard Worker 
34*f6dc9357SAndroid Build Coastguard Worker void *MidAlloc(size_t size);
35*f6dc9357SAndroid Build Coastguard Worker void MidFree(void *address);
36*f6dc9357SAndroid Build Coastguard Worker void *BigAlloc(size_t size);
37*f6dc9357SAndroid Build Coastguard Worker void BigFree(void *address);
38*f6dc9357SAndroid Build Coastguard Worker 
39*f6dc9357SAndroid Build Coastguard Worker /* #define Z7_BIG_ALLOC_IS_ZERO_FILLED */
40*f6dc9357SAndroid Build Coastguard Worker 
41*f6dc9357SAndroid Build Coastguard Worker #else
42*f6dc9357SAndroid Build Coastguard Worker 
43*f6dc9357SAndroid Build Coastguard Worker #define MidAlloc(size)    z7_AlignedAlloc(size)
44*f6dc9357SAndroid Build Coastguard Worker #define MidFree(address)  z7_AlignedFree(address)
45*f6dc9357SAndroid Build Coastguard Worker #define BigAlloc(size)    z7_AlignedAlloc(size)
46*f6dc9357SAndroid Build Coastguard Worker #define BigFree(address)  z7_AlignedFree(address)
47*f6dc9357SAndroid Build Coastguard Worker 
48*f6dc9357SAndroid Build Coastguard Worker #endif
49*f6dc9357SAndroid Build Coastguard Worker 
50*f6dc9357SAndroid Build Coastguard Worker extern const ISzAlloc g_Alloc;
51*f6dc9357SAndroid Build Coastguard Worker 
52*f6dc9357SAndroid Build Coastguard Worker #ifdef _WIN32
53*f6dc9357SAndroid Build Coastguard Worker extern const ISzAlloc g_BigAlloc;
54*f6dc9357SAndroid Build Coastguard Worker extern const ISzAlloc g_MidAlloc;
55*f6dc9357SAndroid Build Coastguard Worker #else
56*f6dc9357SAndroid Build Coastguard Worker #define g_BigAlloc g_AlignedAlloc
57*f6dc9357SAndroid Build Coastguard Worker #define g_MidAlloc g_AlignedAlloc
58*f6dc9357SAndroid Build Coastguard Worker #endif
59*f6dc9357SAndroid Build Coastguard Worker 
60*f6dc9357SAndroid Build Coastguard Worker extern const ISzAlloc g_AlignedAlloc;
61*f6dc9357SAndroid Build Coastguard Worker 
62*f6dc9357SAndroid Build Coastguard Worker 
63*f6dc9357SAndroid Build Coastguard Worker typedef struct
64*f6dc9357SAndroid Build Coastguard Worker {
65*f6dc9357SAndroid Build Coastguard Worker   ISzAlloc vt;
66*f6dc9357SAndroid Build Coastguard Worker   ISzAllocPtr baseAlloc;
67*f6dc9357SAndroid Build Coastguard Worker   unsigned numAlignBits; /* ((1 << numAlignBits) >= sizeof(void *)) */
68*f6dc9357SAndroid Build Coastguard Worker   size_t offset;         /* (offset == (k * sizeof(void *)) && offset < (1 << numAlignBits) */
69*f6dc9357SAndroid Build Coastguard Worker } CAlignOffsetAlloc;
70*f6dc9357SAndroid Build Coastguard Worker 
71*f6dc9357SAndroid Build Coastguard Worker void AlignOffsetAlloc_CreateVTable(CAlignOffsetAlloc *p);
72*f6dc9357SAndroid Build Coastguard Worker 
73*f6dc9357SAndroid Build Coastguard Worker 
74*f6dc9357SAndroid Build Coastguard Worker EXTERN_C_END
75*f6dc9357SAndroid Build Coastguard Worker 
76*f6dc9357SAndroid Build Coastguard Worker #endif
77