Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hjmalloc.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <stdio.h>
3 #include <time.h>
4 #include "tls.hh"
5 #include "tpmalloc/mymalloc.h"
6 
7 
10 
12 G4ThreadLocal long int freecount = 0;
13 
14 G4ThreadLocal double alloctime = 0.0;
15 G4ThreadLocal double freetime = 0.0;
16 
17 #define HJMALLOCDEBUG 0
18 #define HJMALLOCPROFILE 0
19 
20 //#define TLMSSIZE 512
21 #define TLMSSIZE 256
22 //#define TLMSSIZE 128
23 //#define TLMSSIZE 32
24 //#define TLMSSIZE 16
25 
28 int tpmallocflag() {return mallocswitch;}
29 
30 void *malloc (size_t __size)
31 {
32  #if HJMALLOCPROFILE
33  alloccount++;
34  #endif
35 
36  #if HJMALLOCDEBUG
37  std::cout << "in malloc of mymalloc" << std::endl;
38  #endif
39 
40  if (mallocswitch == 0)
41  return mymalloc(__size);
42  else
43  {
44  #if HJMALLOCDEBUG
45  std::cout << "mspace in malloc of mymalloc" << std::endl;
46  #endif
47  if (tlms == 0) tlms = create_mspace(TLMSSIZE * 1024 * 1024, 0);
48 
49  #if HJMALLOCPROFILE
50  double timed;
51  struct timespec ts1, ts2;
52  clock_gettime(CLOCK_REALTIME, &ts1);
53  #endif
54 
55  void *tempmalloc = mspace_malloc(tlms, __size);
56 
57  #if HJMALLOCPROFILE
58  clock_gettime(CLOCK_REALTIME, &ts2);
59  timed = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
60  alloctime = alloctime + timed;
61  #endif
62 
63  return tempmalloc;
64  }
65 }
66 
67 void *calloc (size_t __nmemb, size_t __size)
68 {
69  #if HJMALLOCPROFILE
70  alloccount++;
71  #endif
72 
73  #if HJMALLOCDEBUG
74  std::cout << "in calloc of mymalloc" << std::endl;
75  #endif
76  if (mallocswitch == 0)
77  return mycalloc(__nmemb, __size);
78  else
79  {
80  #if HJMALLOCDEBUG
81  std::cout << "mspace in alloc of mymalloc" << std::endl;
82  #endif
83  if (tlms == 0) tlms = create_mspace(TLMSSIZE * 1024 * 1024, 0);
84 
85  #if HJMALLOCPROFILE
86  double timed;
87  struct timespec ts1, ts2;
88  clock_gettime(CLOCK_REALTIME, &ts1);
89  #endif
90 
91  void *tempmalloc = mspace_calloc(tlms, __nmemb, __size);
92 
93  #if HJMALLOCPROFILE
94  clock_gettime(CLOCK_REALTIME, &ts2);
95  timed = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
96  alloctime = alloctime + timed;
97  #endif
98 
99  return tempmalloc;
100  }
101 }
102 
103 void *realloc (void *__ptr, size_t __size)
104 {
105  #if HJMALLOCPROFILE
106  alloccount++;
107  freecount++;
108  #endif
109 
110  #if HJMALLOCDEBUG
111  std::cout << "in realloc of mymalloc" << std::endl;
112  #endif
113  if (mallocswitch == 0)
114  return myrealloc(__ptr, __size);
115  else
116  {
117  #if HJMALLOCDEBUG
118  std::cout << "mspace in realloc of mymalloc" << std::endl;
119  #endif
120  if (tlms == 0) tlms = create_mspace(TLMSSIZE * 1024 * 1024, 0);
121 
122  #if HJMALLOCPROFILE
123  double timed;
124  struct timespec ts1, ts2;
125  clock_gettime(CLOCK_REALTIME, &ts1);
126  #endif
127 
128  void *tempmalloc = mspace_realloc(tlms, __ptr, __size);
129 
130  #if HJMALLOCPROFILE
131  clock_gettime(CLOCK_REALTIME, &ts2);
132  timed = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
133  alloctime = alloctime + timed;
134  #endif
135 
136  return tempmalloc;
137  }
138 }
139 
140 void free (void *__ptr)
141 {
142  #if HJMALLOCPROFILE
143  freecount++;
144  #endif
145 
146  #if HJMALLOCDEBUG
147  std::cout << "in free of mymalloc" << std::endl;
148  #endif
149  if (mallocswitch == 0)
150  myfree(__ptr);
151  else
152  {
153  #if HJMALLOCDEBUG
154  std::cout << "mspace in free of mymalloc" << std::endl;
155  #endif
156 
157  #if HJMALLOCPROFILE
158  double timed;
159  struct timespec ts1, ts2;
160  clock_gettime(CLOCK_REALTIME, &ts1);
161  #endif
162 
163  mspace_free(tlms, __ptr);
164 
165  #if HJMALLOCPROFILE
166  clock_gettime(CLOCK_REALTIME, &ts2);
167  timed = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
168  freetime = freetime + timed;
169  #endif
170 
171  }
172 }
173 
174 void cfree (void *__ptr)
175 {
176  #if HJMALLOCPROFILE
177  freecount++;
178  #endif
179 
180  #if HJMALLOCDEBUG
181  std::cout << "in cfree of mymalloc" << std::endl;
182  #endif
183  if (mallocswitch == 0)
184  cfree(__ptr);
185  else
186  {
187  #if HJMALLOCDEBUG
188  std::cout << "mspace in cfree of mymalloc" << std::endl;
189  #endif
190 
191  #if HJMALLOCPROFILE
192  double timed;
193  struct timespec ts1, ts2;
194  clock_gettime(CLOCK_REALTIME, &ts1);
195  #endif
196 
197  mspace_free(tlms, __ptr);
198 
199  #if HJMALLOCPROFILE
200  clock_gettime(CLOCK_REALTIME, &ts2);
201  timed = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
202  freetime = freetime + timed;
203  #endif
204 
205  }
206 }
207 
208 void *valloc (size_t __size)
209 {
210  #if HJMALLOCPROFILE
211  alloccount++;
212  #endif
213 
214  #if HJMALLOCDEBUG
215  std::cout << "in valloc of mymalloc" << std::endl;
216  #endif
217  if (mallocswitch == 0)
218  return myvalloc(__size);
219  else
220  {
221  #if HJMALLOCDEBUG
222  std::cout << "mspace in valloc of mymalloc" << std::endl;
223  #endif
224  if (tlms == 0) tlms = create_mspace(TLMSSIZE * 1024 * 1024, 0);
225 
226  #if HJMALLOCPROFILE
227  double timed;
228  struct timespec ts1, ts2;
229  clock_gettime(CLOCK_REALTIME, &ts1);
230  #endif
231 
232  void *tempmalloc = mspace_malloc(tlms, __size);
233 
234  #if HJMALLOCPROFILE
235  clock_gettime(CLOCK_REALTIME, &ts2);
236  timed = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
237  alloctime = alloctime + timed;
238  #endif
239 
240  return tempmalloc;
241  }
242 }
void * valloc(size_t __size)
Definition: hjmalloc.cc:208
G4ThreadLocal mspace tlms
Definition: hjmalloc.cc:9
void turnontpmalloc()
Definition: hjmalloc.cc:26
void * calloc(size_t __nmemb, size_t __size)
Definition: hjmalloc.cc:67
void free(void *__ptr)
Definition: hjmalloc.cc:140
void * mspace_malloc(mspace msp, size_t bytes)
Definition: mymalloc.cc:5061
#define TLMSSIZE
Definition: hjmalloc.cc:21
#define G4ThreadLocal
Definition: tls.hh:52
G4ThreadLocal double freetime
Definition: hjmalloc.cc:15
G4ThreadLocal long int freecount
Definition: hjmalloc.cc:12
G4ThreadLocal int mallocswitch
Definition: hjmalloc.cc:8
void * mspace
Definition: mymalloc.h:419
G4ThreadLocal double alloctime
Definition: hjmalloc.cc:14
void * mspace_realloc(mspace msp, void *mem, size_t newsize)
Definition: mymalloc.cc:5296
void cfree(void *__ptr)
Definition: hjmalloc.cc:174
void turnofftpmalloc()
Definition: hjmalloc.cc:27
int tpmallocflag()
Definition: hjmalloc.cc:28
mspace create_mspace(size_t capacity, int locked)
Definition: mymalloc.cc:4974
void * mspace_calloc(mspace msp, size_t n_elements, size_t elem_size)
Definition: mymalloc.cc:5276
void * realloc(void *__ptr, size_t __size)
Definition: hjmalloc.cc:103
void * malloc(size_t __size)
Definition: hjmalloc.cc:30
void mspace_free(mspace msp, void *mem)
Definition: mymalloc.cc:5175
G4ThreadLocal long int alloccount
Definition: hjmalloc.cc:11