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
42class 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 int myMPI_Win_allocate_shared(MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, void *baseptr, MPI_Win *win);
69 int myMPI_Win_shared_query(MPI_Win win, int rank, MPI_Aint *size, int *disp_unit, void *baseptr);
70
71 inline double getAllocatedBytesInMB(void) { return AllocatedBytes * TO_MBYTE_FAC; }
72
73 template <typename T>
74 inline T *alloc(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
75 {
76 return static_cast<T *>(mymalloc_movable_fullinfo(&ptr, varname, n * sizeof(T), func, file, linenr, 0, 0, NULL));
77 }
78
79 template <typename T>
80 inline T *alloc_movable(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
81 {
82 return static_cast<T *>(mymalloc_movable_fullinfo(&ptr, varname, n * sizeof(T), func, file, linenr, 1, 0, NULL));
83 }
84
85 template <typename T>
86 inline T *realloc(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
87 {
88 return static_cast<T *>(myrealloc_movable_fullinfo(&ptr, n * sizeof(T), func, file, linenr, 0));
89 }
90
91 template <typename T>
92 inline T *realloc_movable(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
93 {
94 return static_cast<T *>(myrealloc_movable_fullinfo(&ptr, n * sizeof(T), func, file, linenr, 1));
95 }
96
97 void dealloc(void *ptr, const char *func, const char *file, int linenr) { myfree_movable_fullinfo(ptr, func, file, linenr, 0); }
98
99 void dealloc_movable(void *ptr, const char *func, const char *file, int linenr)
100 {
101 myfree_movable_fullinfo(ptr, func, file, linenr, 1);
102 }
103
104 void dump_memory_table(void);
105
106 private:
107 size_t AllocatedBytesGeneric;
108
109 size_t HighMarkBytes;
110 size_t HighMarkBytesWithoutGeneric;
111
112 double OldGlobHighMarkMB;
113 double OldGlobHighMarkMBWithoutGeneric;
114
115 FILE *FdMemory;
117 size_t TotBytes;
118 int Nblocks;
120 char **Table;
121 size_t *BlockSize;
122 char *MovableFlag;
123 char *GenericFlag;
124 char ***BasePointers;
125 char *VarName;
126 char *FunctionName;
127 char *ParentFileName;
128 char *FileName;
129 int *LineNumber;
130 char *HighMarkTabBuf;
132 char *HighMarkTabBufWithoutGeneric;
134 enum restart_options RestartFlag;
135
136 int dump_memory_table_buffer(char *p);
137
138 void report_memory_usage(int rank, char *tabbuf);
139};
140
141extern memory Mem;
142
143#endif
double getAllocatedBytesInMB(void)
Definition: mymalloc.h:71
memory()
Definition: mymalloc.h:45
T * alloc_movable(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
Definition: mymalloc.h:80
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:591
T * realloc(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
Definition: mymalloc.h:86
size_t FreeBytes
Definition: mymalloc.h:48
int myMPI_Win_allocate_shared(MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, void *baseptr, MPI_Win *win)
Definition: mymalloc.cc:177
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:402
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:490
char * Base
Definition: mymalloc.h:49
void * myfree_query_last_block(void)
Definition: mymalloc.cc:473
void dump_memory_table(void)
Dump the buffer where the memory information is stored to the standard output.
Definition: mymalloc.cc:355
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:311
T * alloc(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
Definition: mymalloc.h:74
void dealloc_movable(void *ptr, const char *func, const char *file, int linenr)
Definition: mymalloc.h:99
void check_maxmemsize_setting(int maxmemsize)
Definition: mymalloc.cc:684
int myMPI_Win_shared_query(MPI_Win win, int rank, MPI_Aint *size, int *disp_unit, void *baseptr)
Definition: mymalloc.cc:250
size_t AllocatedBytes
Definition: mymalloc.h:47
void mymalloc_init(int maxmemsize, enum restart_options restartflag)
Initialize memory manager.
Definition: mymalloc.cc:58
size_t roundup_to_multiple_of_cacheline_size(size_t n)
Definition: mymalloc.cc:465
void dealloc(void *ptr, const char *func, const char *file, int linenr)
Definition: mymalloc.h:97
T * realloc_movable(T *&ptr, const char *varname, size_t n, const char *func, const char *file, int linenr)
Definition: mymalloc.h:92
#define TO_MBYTE_FAC
Definition: constants.h:59
restart_options
Definition: dtypes.h:312
memory Mem
Definition: main.cc:44