CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Alloc.cc
Go to the documentation of this file.
1 /* Alloc.c -- Memory allocation functions
2 2008-09-24
3 Igor Pavlov
4 Public domain */
5 
6 #ifdef _WIN32
7 #include <windows.h>
8 #endif
9 #include <cstdlib>
10 
11 #include "Alloc.h"
12 
13 /* #define _SZ_ALLOC_DEBUG */
14 
15 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
16 #ifdef _SZ_ALLOC_DEBUG
17 #include <stdio.h>
18 int g_allocCount = 0;
19 int g_allocCountMid = 0;
20 int g_allocCountBig = 0;
21 #endif
22 
23 void *MyAlloc(size_t size) {
24  if (size == 0)
25  return nullptr;
26 #ifdef _SZ_ALLOC_DEBUG
27  {
28  void *p = malloc(size);
29  fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p);
30  return p;
31  }
32 #else
33  return malloc(size);
34 #endif
35 }
36 
37 void MyFree(void *address) {
38 #ifdef _SZ_ALLOC_DEBUG
39  if (address != 0)
40  fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address);
41 #endif
42  free(address);
43 }
44 
45 #ifdef _WIN32
46 
47 void *MidAlloc(size_t size) {
48  if (size == 0)
49  return 0;
50 #ifdef _SZ_ALLOC_DEBUG
51  fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);
52 #endif
53  return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
54 }
55 
56 void MidFree(void *address) {
57 #ifdef _SZ_ALLOC_DEBUG
58  if (address != 0)
59  fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
60 #endif
61  if (address == 0)
62  return;
63  VirtualFree(address, 0, MEM_RELEASE);
64 }
65 
66 #ifndef MEM_LARGE_PAGES
67 #undef _7ZIP_LARGE_PAGES
68 #endif
69 
70 #ifdef _7ZIP_LARGE_PAGES
71 SIZE_T g_LargePageSize = 0;
72 typedef SIZE_T(WINAPI *GetLargePageMinimumP)();
73 #endif
74 
75 void SetLargePageSize() {
76 #ifdef _7ZIP_LARGE_PAGES
77  SIZE_T size = 0;
78  GetLargePageMinimumP largePageMinimum =
79  (GetLargePageMinimumP)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
80  if (largePageMinimum == 0)
81  return;
82  size = largePageMinimum();
83  if (size == 0 || (size & (size - 1)) != 0)
84  return;
85  g_LargePageSize = size;
86 #endif
87 }
88 
89 void *BigAlloc(size_t size) {
90  if (size == 0)
91  return 0;
92 #ifdef _SZ_ALLOC_DEBUG
93  fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);
94 #endif
95 
96 #ifdef _7ZIP_LARGE_PAGES
97  if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18)) {
98  void *res = VirtualAlloc(
99  0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
100  if (res != 0)
101  return res;
102  }
103 #endif
104  return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
105 }
106 
107 void BigFree(void *address) {
108 #ifdef _SZ_ALLOC_DEBUG
109  if (address != 0)
110  fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
111 #endif
112 
113  if (address == 0)
114  return;
115  VirtualFree(address, 0, MEM_RELEASE);
116 }
117 
118 #endif
void * MyAlloc(size_t size)
Definition: Alloc.cc:23
#define MidAlloc(size)
Definition: Alloc.h:27
void MyFree(void *address)
Definition: Alloc.cc:37
#define BigAlloc(size)
Definition: Alloc.h:29
#define MidFree(address)
Definition: Alloc.h:28
#define BigFree(address)
Definition: Alloc.h:30
tuple size
Write out results.