GADGET-4
mymalloc.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * \copyright This file is part of the GADGET4 N-body/SPH code developed
3  * \copyright by Volker Springel. Copyright (C) 2014-2020 by Volker Springel
4  * \copyright (vspringel@mpa-garching.mpg.de) and all contributing authors.
5  *******************************************************************************/
6 
12 #ifndef MYMALLOC_H
13 #define MYMALLOC_H
14 
15 #include <stdio.h>
16 
17 #define CACHELINESIZE 64
18 
19 #define MAXBLOCKS 5000
20 #define MAXCHARS 40
21 
22 #define LOC __FUNCTION__, __FILE__, __LINE__
23 #define MMM(x, y) (x, #x, y, __FUNCTION__, __FILE__, __LINE__)
24 #define DDD(x) (x, __FUNCTION__, __FILE__, __LINE__)
25 
26 #define mymalloc(x, y) mymalloc_movable_fullinfo(NULL, x, y, __FUNCTION__, __FILE__, __LINE__, 0, 0, NULL)
27 #define mymalloc_clear(x, y) mymalloc_movable_fullinfo(NULL, x, y, __FUNCTION__, __FILE__, __LINE__, 0, 1, NULL)
28 #define mymalloc_g(x, y) mymalloc_movable_fullinfo(NULL, x, y, __FUNCTION__, __FILE__, __LINE__, 0, 0, callorigin)
29 
30 #define mymalloc_movable(x, y, z) mymalloc_movable_fullinfo(x, y, z, __FUNCTION__, __FILE__, __LINE__, 1, 0, NULL)
31 #define mymalloc_movable_clear(x, y, z) mymalloc_movable_fullinfo(x, y, z, __FUNCTION__, __FILE__, __LINE__, 1, 1, NULL)
32 #define mymalloc_movable_g(x, y, z) mymalloc_movable_fullinfo(x, y, z, __FUNCTION__, __FILE__, __LINE__, 1, 0, callorigin)
33 
34 #define myfree(x) myfree_movable_fullinfo(x, __FUNCTION__, __FILE__, __LINE__, 0)
35 #define myfree_movable(x) myfree_movable_fullinfo(x, __FUNCTION__, __FILE__, __LINE__, 1)
36 
37 #define myrealloc(x, y) myrealloc_movable_fullinfo(x, y, __FUNCTION__, __FILE__, __LINE__, 0)
38 #define myrealloc_movable(x, y) myrealloc_movable_fullinfo(x, y, __FUNCTION__, __FILE__, __LINE__, 1)
39 
40 #include "../mpi_utils/setcomm.h"
41 
42 class memory : public setcomm
43 {
44  public:
45  memory() : setcomm("delayed init") {}
46 
48  size_t FreeBytes;
49  char *Base;
51  void mymalloc_init(int maxmemsize, enum restart_options restartflag);
52 
53  void *mymalloc_movable_fullinfo(void *ptr, const char *varname, size_t n, const char *func, const char *file, int line,
54  int movable_flag, int clear_flag, char *originflag);
55 
56  void *myrealloc_movable_fullinfo(void *p, size_t n, const char *func, const char *file, int line, int movable_flag);
57 
58  void myfree_movable_fullinfo(void *p, const char *func, const char *file, int line, int movable_flag);
59 
60  void *myfree_query_last_block(void);
61 
63 
65 
66  void check_maxmemsize_setting(int maxmemsize);
67 
68  inline double getAllocatedBytesInMB(void) { return AllocatedBytes * TO_MBYTE_FAC; }
69 
70  template <typename T>
71  inline T *alloc(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
72  {
73  return static_cast<T *>(mymalloc_movable_fullinfo(&ptr, varname, n * sizeof(T), func, file, linenr, 0, 0, NULL));
74  }
75 
76  template <typename T>
77  inline T *alloc_movable(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
78  {
79  return static_cast<T *>(mymalloc_movable_fullinfo(&ptr, varname, n * sizeof(T), func, file, linenr, 1, 0, NULL));
80  }
81 
82  template <typename T>
83  inline T *realloc(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
84  {
85  return static_cast<T *>(myrealloc_movable_fullinfo(&ptr, n * sizeof(T), func, file, linenr, 0));
86  }
87 
88  template <typename T>
89  inline T *realloc_movable(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
90  {
91  return static_cast<T *>(myrealloc_movable_fullinfo(&ptr, n * sizeof(T), func, file, linenr, 1));
92  }
93 
94  void dealloc(void *ptr, const char *func, const char *file, int linenr) { myfree_movable_fullinfo(ptr, func, file, linenr, 0); }
95 
96  void dealloc_movable(void *ptr, const char *func, const char *file, int linenr)
97  {
98  myfree_movable_fullinfo(ptr, func, file, linenr, 1);
99  }
100 
101  void dump_memory_table(void);
102 
103  private:
104  size_t AllocatedBytesGeneric;
105 
106  size_t HighMarkBytes;
107  size_t HighMarkBytesWithoutGeneric;
108 
109  double OldGlobHighMarkMB;
110  double OldGlobHighMarkMBWithoutGeneric;
111 
112  FILE *FdMemory;
114  size_t TotBytes;
115  int Nblocks;
117  char **Table;
118  size_t *BlockSize;
119  char *MovableFlag;
120  char *GenericFlag;
121  char ***BasePointers;
122  char *VarName;
123  char *FunctionName;
124  char *ParentFileName;
125  char *FileName;
126  int *LineNumber;
127  char *HighMarkTabBuf;
129  char *HighMarkTabBufWithoutGeneric;
131  enum restart_options RestartFlag;
132 
133  int dump_memory_table_buffer(char *p);
134 
135  void report_memory_usage(int rank, char *tabbuf);
136 };
137 
138 extern memory Mem;
139 
140 #endif
double getAllocatedBytesInMB(void)
Definition: mymalloc.h:68
memory()
Definition: mymalloc.h:45
void * myrealloc_movable_fullinfo(void *p, size_t n, const char *func, const char *file, int line, int movable_flag)
Reallocate an existing movable memory block.
Definition: mymalloc.cc:500
size_t FreeBytes
Definition: mymalloc.h:48
void * mymalloc_movable_fullinfo(void *ptr, const char *varname, size_t n, const char *func, const char *file, int line, int movable_flag, int clear_flag, char *originflag)
Allocate a movable memory block and store the relative information.
Definition: mymalloc.cc:311
void myfree_movable_fullinfo(void *p, const char *func, const char *file, int line, int movable_flag)
Deallocate a movable memory block.
Definition: mymalloc.cc:399
T * alloc_movable(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
Definition: mymalloc.h:77
char * Base
Definition: mymalloc.h:49
void * myfree_query_last_block(void)
Definition: mymalloc.cc:382
void dump_memory_table(void)
Dump the buffer where the memory information is stored to the standard output.
Definition: mymalloc.cc:264
void report_detailed_memory_usage_of_largest_task(void)
Output memory usage for the task with the greatest amount of memory allocated.
Definition: mymalloc.cc:220
void dealloc_movable(void *ptr, const char *func, const char *file, int linenr)
Definition: mymalloc.h:96
T * alloc(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
Definition: mymalloc.h:71
T * realloc(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
Definition: mymalloc.h:83
void check_maxmemsize_setting(int maxmemsize)
Definition: mymalloc.cc:593
size_t AllocatedBytes
Definition: mymalloc.h:47
void mymalloc_init(int maxmemsize, enum restart_options restartflag)
Initialize memory manager.
Definition: mymalloc.cc:54
size_t roundup_to_multiple_of_cacheline_size(size_t n)
Definition: mymalloc.cc:374
void dealloc(void *ptr, const char *func, const char *file, int linenr)
Definition: mymalloc.h:94
T * realloc_movable(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
Definition: mymalloc.h:89
#define TO_MBYTE_FAC
Definition: constants.h:59
restart_options
Definition: dtypes.h:312
memory Mem
Definition: main.cc:44