Alloc.h 1.9 KB

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