File: wisememman.dy

package info (click to toggle)
wise 2.4.1-21
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 27,140 kB
  • sloc: ansic: 276,365; makefile: 1,003; perl: 886; lex: 93; yacc: 81; sh: 24
file content (200 lines) | stat: -rwxr-xr-x 3,972 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
%{

#define CKALLOC_GUARD
#include "wisebase.h"
#undef  CKALLOC_GUARD

#define WISE_MEMORY_WATCH_MAX 4096

%}


%{
#include "wisememman.h"


#ifdef WISE_MEMORY_WATCH
 struct wise_memory_watcher {
   void * position;
   int amount;
   int has_freed;
   char * file;
   int lineno;
 };

 static struct wise_memory_watcher mempool[WISE_MEMORY_WATCH_MAX];
 static int nextpool = 0;

%func
Displays to filehandle all non freed memory
%%
void display_allocated_memory(char * tag,FILE * ofp)
{
  int i;
  for(i=0;i < nextpool && i<WISE_MEMORY_WATCH_MAX;i++)
    if( mempool[i].has_freed == FALSE ) {
      if( mempool[i].file != NULL ) 
	fprintf(ofp,"%s [%s:%d] %d of %d bytes not free\n",tag,mempool[i].file,mempool[i].lineno,mempool[i].position,mempool[i].amount);
      else
	fprintf(ofp,"%s %d of %d bytes not free\n",tag,mempool[i].position,mempool[i].amount);
      fprintf(ofp,"Bytes: %c%c\n",*((char *)mempool[i].position),*((char *)mempool[i].position +1));
    }

}


%func 
Only if compiled with WISE_MEMORY_WATCH

Allocates memory and watches the usage of it,
knowing the file and line number
%%
void * allocate_watched_memory_file(char * file,int lineno,int no_bytes)
{
  if( nextpool >= WISE_MEMORY_WATCH_MAX ) {
    warn("Memory allocation over limits for watching");
    return malloc(no_bytes);
  }

  mempool[nextpool].position = malloc(no_bytes);
  mempool[nextpool].amount = no_bytes;
  mempool[nextpool].has_freed = FALSE;
  mempool[nextpool].file = file;
  mempool[nextpool].lineno = lineno;
  

  nextpool++;

  return mempool[nextpool-1].position;
}


%func 
Only if compiled with WISE_MEMORY_WATCH

Allocates memory and watches the usage of it
%%
void * allocate_watched_memory(int no_bytes)
{
  if( nextpool >= WISE_MEMORY_WATCH_MAX ) {
    warn("Memory allocation over limits for watching");
    return malloc(no_bytes);
  }

  mempool[nextpool].position = malloc(no_bytes);
  mempool[nextpool].amount = no_bytes;
  mempool[nextpool].has_freed = FALSE;
  mempool[nextpool].file = NULL;
  mempool[nextpool].lineno = -1;

  nextpool++;

  return mempool[nextpool-1].position;
}

%func 
Only if compiled with WISE_MEMORY_WATCH

frees memory that has been watched
%%
void * free_watched_memory(void * mem)
{
  int i;
  for(i=0;i < nextpool && i<WISE_MEMORY_WATCH_MAX;i++)
    if( mempool[i].position == mem ) {
      break;
    }

  if( i == nextpool || i == WISE_MEMORY_WATCH_MAX ) {
    warn("Problem! memory position %d not watched",(int)mem);
    return NULL;
  }


  free(mempool[i].position); 
  mempool[i].has_freed = TRUE;

  return NULL;
}

#endif /* WISE_MEMORY_WATCH */

%func
Tries to alloc bytes of memory. Posts
to warn if it fails
%%
void * ckalloc(size_t bytes)
{
  register void *ret;
  extern void *calloc (size_t nelem, size_t elsize);

#ifdef WISE_MEMORY_WATCH
  /* call into the watched memory pool */
  ret = allocate_watched_memory(bytes);
  if( ret == NULL ) {
    warn("Out of memory (watched) on %d bytes\n",bytes);
    return NULL;
  } else 
    return ret;
  
#endif

  if( (ret = calloc(bytes, sizeof(char))) == NULL) {
    warn("Out of memory, on asking for %d bytes\n",bytes);
    return NULL; /*** for the moment, could fail here ***/
  } else
    return ret;	
}

%func
calloc equivalent
%%
void * ckcalloc(int len,size_t bytes)
{
  return ckalloc(len*bytes);
}

%func
realloc equivalent
%%
void * ckrealloc(void *ptr, size_t bytes)
{
  register void *ret;
  extern void *realloc (void *ptr, size_t size);

  if (ptr == NULL) {	
    warn("Bad call to ckrealloc, NULL pointer\n");
    return NULL;
  }
  else if( (ret = realloc(ptr, bytes)) == NULL) {
    warn("Out of memory, trying to realloc %d bytes\n",bytes);
    return NULL;
  }
  else
    return ret;	
}

%func
free equivalent
%%
void * ckfree(void *ptr)
{

#ifdef WISE_MEMORY_WATCH
  free_watched_memory(ptr);
  return NULL;
#endif

  if (ptr == NULL)
    warn("Bad call to ckfree - NULL pointer\n");
  else {
    free(ptr);
    ptr = NULL;
  }
  return ptr;
}