source: PSPA/madxPSPA/src/mad_mem.c @ 446

Last change on this file since 446 was 430, checked in by touze, 11 years ago

import madx-5.01.00

File size: 3.8 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "mad_extrn_f.h"
6#include "mad_mem.h"
7#include "mad_err.h"
8
9// private constant
10
11#define FREECODE 380226     /* check-code to avoid multiple "free" */
12#define MTABLE_SIZE 1000000
13
14// private globals
15
16static char* myfree_caller = "none";
17
18#ifdef _MEMLEAKS
19static int item_no=-1;
20static int* mtable[MTABLE_SIZE];
21#endif
22
23// public interface
24
25void
26mad_mem_handler(int sig)
27{
28  (void)sig;
29 
30  if (strcmp(myfree_caller, "none") == 0)
31    puts("+++ memory access outside program range, fatal +++");
32  else
33    printf("+++ illegal call to free memory from routine: %s +++\n", myfree_caller);
34
35  puts("good bye");
36  exit(EXIT_FAILURE);
37}
38
39void*
40mymalloc(char* caller, size_t size)
41#ifdef _MEMLEAKS
42{
43  /* calls malloc, checks for memory granted */
44  void* p;
45  int* i_p;
46  size_t l_size = size + sizeof(double);
47  if ((p = malloc(l_size)) == NULL)
48  {
49    fatal_error("memory overflow, called from routine:", caller);
50  } 
51  i_p = (int*) p;
52  mtable[-item_no]=i_p;
53  *i_p++ = item_no;
54  *i_p = l_size;
55  fprintf(stderr,"ALLOCATE called by %s \n",caller);
56  fprintf(stderr,"[Allocated item %i (size %i)] \n",item_no,l_size);
57  if (item_no == -MTABLE_SIZE)
58  {
59    fatal_error("Too many allocs!!!", "MTABLE_SIZE");
60  }
61  item_no = item_no - 1;
62  return (void *)((char*)p+sizeof(double));
63}
64#endif
65#ifndef _MEMLEAKS
66{
67  /* calls malloc, checks for memory granted */
68  void* p;
69  int* i_p;
70  size_t l_size = size + sizeof(double)+2;
71/*  printf("xxxx %d xxxx\n",l_size);*/
72  if ((p = malloc(l_size)) == NULL)
73  {
74    fatal_error("memory overflow, called from routine:", caller);
75  } 
76  i_p = (int*) p; *i_p = FREECODE;
77  return (void *)((char*)p+sizeof(double));
78}
79#endif
80
81void*
82mycalloc(char* caller, size_t nelem, size_t size)
83#ifdef _MEMLEAKS
84{
85  /* calls calloc, checks for memory granted */
86  void* p;
87  int* i_p;
88  size_t l_size = nelem*size + sizeof(double);
89  if ((p = calloc(1, l_size)) == NULL)
90  {
91    fatal_error("memory overflow, called from routine:", caller);
92  } 
93  mtable[-item_no]=p;
94  i_p = (int*) p;
95  *i_p++ = item_no;
96  *i_p = l_size;
97  fprintf(stderr,"ALLOCATE called by %s \n",caller);
98  fprintf(stderr,"[Allocated item %i (size %i)] \n",item_no,l_size);
99  if (item_no == -MTABLE_SIZE)
100  {
101    fatal_error("Too many allocs!!!", "MTABLE_SIZE");
102  }
103  item_no = item_no - 1;
104  return (void *)((char*)p+sizeof(double));
105}
106#endif
107#ifndef _MEMLEAKS
108{
109  /* calls calloc, checks for memory granted */
110  void* p;
111  int* i_p;
112  size_t l_size = nelem*size + sizeof(double);
113  if ((p = calloc(1, l_size)) == NULL)
114  {
115    fatal_error("memory overflow, called from routine:", caller);
116  } 
117  i_p = (int*) p; *i_p = FREECODE;
118  return ((char*)p+sizeof(double));
119}
120#endif
121
122void
123myfree(char* rout_name, void* p)
124#ifdef _MEMLEAKS
125{
126  int my_size,my_item_no,myend,old_item_no;
127  char* l_p = (char*)p - sizeof(double);
128  int* i_p = (int*) l_p;
129  myfree_caller = rout_name;
130/* Look for the integer address (backwards) in mtable */
131  myend=-item_no-1;
132  old_item_no=0;
133  while (myend > 0)
134  {
135    if (mtable[myend] == i_p)
136    {
137      old_item_no=-myend;
138      mtable[myend]=NULL;
139      break;
140    }
141    else
142    {
143      myend=myend-1;
144    }
145  }
146  if ( old_item_no == 0)
147  {
148    fatal_error("Free memory error!!!, called from routine:", myfree_caller);
149  }
150  my_item_no = *i_p++;
151  my_size = *i_p;
152  if (my_item_no != old_item_no)
153  {
154    fatal_error("Memory item number discrepancy!!!:", myfree_caller);
155  }
156  fprintf(stderr,"DEALLOCATE called by %s \n",myfree_caller);
157  fprintf(stderr,"[Deallocated item %i (size %i)] \n",my_item_no,my_size);
158  free(l_p);
159  myfree_caller = "none";
160}
161#endif
162#ifndef _MEMLEAKS
163{
164  char* l_p = (char*)p - sizeof(double);
165  int* i_p = (int*) l_p;
166  myfree_caller = rout_name;
167  if (*i_p == FREECODE)
168  {
169    *i_p = 0; free(l_p);
170  }
171  myfree_caller = "none";
172}
173#endif
174
Note: See TracBrowser for help on using the repository browser.