File: clb_memory.c

package info (click to toggle)
eprover 3.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,504 kB
  • sloc: ansic: 104,396; csh: 13,135; python: 11,207; awk: 5,825; makefile: 554; sh: 400
file content (421 lines) | stat: -rw-r--r-- 11,009 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*-----------------------------------------------------------------------

  File  : clb_memory.c

  Author: Stephan Schulz

  Memory management. The implemented routines are very simple, because
  they make use of the standard memory management for reorganization.

  Copyright 1998-2017 by the author.
  This code is released under the GNU General Public Licence and
  the GNU Lesser General Public License.
  See the file COPYING in the main E directory for details..
  Run "eprover -h" for contact information.

  Changes

  Created: Thu Aug 14 10:00:35 MET DST 1997

  -----------------------------------------------------------------------*/

#ifdef USE_NEWMEM
#include "clb_newmem.c"
#else

#include "clb_memory.h"


/*-----------------------------------------------------------------------*/
/*                         Globale Variable                              */
/*-----------------------------------------------------------------------*/

/* This global variable is set whenever malloc() failed and triggered
   a free memory reorganization. User programs may examine this
   variable to take certain measures (and may reset it if they think
   that they freed significant amounts of memory). However, this
   is somewhat discouraged - do you really want your program to depend
   in pretty complex ways on uncontrolable features like the amount of
   free memory available to your process? _I_ only used to use it for
   some measurements ;-)  StS */

bool MemIsLow = false;

Mem_p free_mem_list[MEM_ARR_SIZE] = {NULL};

#ifdef CLB_MEMORY_DEBUG
long size_malloc_mem = 0;
long size_malloc_count = 0;
long size_free_mem = 0;
long size_free_count = 0;
long clb_free_count = 0;
long secure_malloc_count = 0;
long secure_malloc_mem = 0;
long secure_realloc_count = 0;
long secure_realloc_m_count = 0;
long secure_realloc_f_count = 0;
#endif


/*---------------------------------------------------------------------*/
/*                         Internal Functions                          */
/*---------------------------------------------------------------------*/


#ifdef CLB_MEMORY_DEBUG

/*-----------------------------------------------------------------------
//
// Function: free_list_size()
//
//   Return the length if the list of MemCells.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

static long free_list_size(Mem_p list)
{
   long res = 0;
   while(list)
   {
      list = list->next;
      res++;
   }
   return res;
}

#endif


/*-----------------------------------------------------------------------*/
/*                  Exportierte Funktionen                               */
/*-----------------------------------------------------------------------*/


/*-----------------------------------------------------------------------
//
// Function: MemFlushFreeList()
//
//   Returns all memory kept in free_mem_list[] to the operation
//   system. This is useful if a very different memory access pattern
//   is expected (SizeFree() never reorganizes the memory
//   automatically).
//
// Global Variables: free_mem_list[]
//
// Side Effects    : Memory operations
//
/----------------------------------------------------------------------*/

void MemFlushFreeList(void)
{
   int f;
   void* handle;

   VERBOUT("MemFlushFreeList() called for cleanup or reorganization\n");
   for(f = 0;f<MEM_ARR_SIZE;f++)
   {
      while(free_mem_list[f])
      {
         handle = (void*)free_mem_list[f];
         free_mem_list[f] = free_mem_list[f]->next;
         FREE(handle);
      }
   }
}


/*-----------------------------------------------------------------------
//
// Function: SecureMalloc()
//
//  Returns a pointer to an unused memory block sized size. If
//  possible, a fresh block is allocated, if not, the
//  reorganization of free_mem_list is triggered, if still no memory
//  is available, an error will be produced. If the first malloc
//  fails, MemIsLow will be set.
//
// Global Variables: free_mem_list, MemIsLow
//
// Side Effects    : Memory operations, possibly error
//
/----------------------------------------------------------------------*/

void* SecureMalloc(size_t size)
{
   void* handle;

#ifdef CLB_MEMORY_DEBUG
   secure_malloc_count++;
   secure_malloc_mem += size;
#endif

   handle = (void*)malloc(size);

   if(UNLIKELY(!handle))
   {    /* malloc has no memory left  */
      MemIsLow = true;
      MemFlushFreeList(); /* Return own freelist */

      handle = (void*)malloc(size);

      if(!handle)
      {   /*  Still nothing...*/
#ifdef PRINT_SOMEERRORS_STDOUT
         SetMemoryLimit(RLIM_INFINITY);
         fprintf(stdout, "# Failure: Resource limit exceeded (memory)\n");
         TSTPOUT(stdout, "ResourceOut");
         fflush(stdout);
         PrintRusage(stdout);
#endif
         Error("Out of Memory", OUT_OF_MEMORY);
      }
   }
#ifdef CLB_MEMORY_DEBUG2
   printf("\nBlock %p M: %zd\n", handle, size);
#endif
   return handle;
}

/*-------------------------------------------------------------------------
//
// Function: SecureRealloc()
//
//   Imitates realloc, but reorganizes free_mem_list to get new memory
//   if the block has to be moved and no memory is available. Will
//   terminate with OUT_OF_MEMORY if no memory is found.
//
// Global Variables: -
//
// Side Effect     : Via SecureMalloc()
//
//-----------------------------------------------------------------------*/

void* SecureRealloc(void *ptr, size_t size)
{
   void* handle;

#ifdef CLB_MEMORY_DEBUG
   secure_realloc_count++;
   if(ptr && !size)
   {
      secure_realloc_f_count++;
   }
   else if(!ptr && size)
   {
      secure_realloc_m_count++;
   }
#endif
   /* SunOS realloc() is broken, so here is a stupid workaround...*/

   handle = ptr?realloc(ptr,size):malloc(size);
   if(UNLIKELY(!handle && size!=0))
   {
      MemIsLow = true;
      MemFlushFreeList();
      handle = ptr?realloc(ptr,size):malloc(size);
      if(!handle)
      {   /*  Still nothing...*/
#ifdef PRINT_SOMEERRORS_STDOUT
         SetMemoryLimit(RLIM_INFINITY);
         fprintf(stdout, "# Failure: Resource limit exceeded (memory)\n");
         fflush(stdout);
         PrintRusage(stdout);
#endif
         Error("Out of Memory", OUT_OF_MEMORY);
      }
   }
#ifdef CLB_MEMORY_DEBUG2
   if(ptr != handle)
   {
      if(ptr)
      {
         printf("\nBlock %p F:\n", ptr);
      }
      printf("\nBlock %p R: %zd\n" ,handle, size);
   }
#endif

   return handle;
}

/*-----------------------------------------------------------------------
//
// Function: SecureStrdup()
//
//   Implements the functionality of strdup, but uses SecureMalloc()
/    for the memory handling.
//
// Global Variables: -
//
// Side Effects    : By SecureMalloc()
//
/----------------------------------------------------------------------*/

char* SecureStrdup(const char* source)
{
   char* handle;

   handle = SecureMalloc(strlen(source)+1);
   strcpy(handle,source);

   return handle;
}

/*-----------------------------------------------------------------------
//
// Function: SecureStrndup()
//
//   Implements the functionality of GNU strndup, but uses
//   SecureMalloc() for the memory handling (creates a NULL-terminated
//   copy of the string or the first n bytes of it).
//
// Global Variables: -
//
// Side Effects    : By SecureMalloc()
//
/----------------------------------------------------------------------*/

char* SecureStrndup(const char* source, size_t n)
{
   char* handle;
   size_t len;

   assert(source);
   assert(n>=0);

   len = strlen(source);

   if(len > n)
   {
      handle = SecureMalloc(n+1);
      strncpy(handle,source, n);
      handle[n]='\0';
   }
   else
   {
      handle = SecureStrdup(source);
   }
   return handle;
}




/*-----------------------------------------------------------------------
//
// Function: IntArrayAlloc()
//
//   Return a pointer to  a freshly allocated, 0-initialized block of
//   longs.
//
// Global Variables: -
//
// Side Effects    : Memory operations
//
/----------------------------------------------------------------------*/

long* IntArrayAlloc(int size)
{
   long *res;
   int i;

   res = SizeMalloc(size*sizeof(long));

   for(i=0; i<size; i++)
   {
      res[i]=0;
   }
   return res;
}




#ifdef CLB_MEMORY_DEBUG

/*-----------------------------------------------------------------------
//
// Function: MemDebugPrintStats()
//
//   Print information about allocated and deallocated memory.
//
// Global Variables: size_malloc_mem, size_malloc_count,
//                   size_free_mem, size_free_count
//
// Side Effects    : Output
//
/----------------------------------------------------------------------*/

void MemDebugPrintStats(FILE* out)
{
   fprintf(out,
           "\n# -------------------------------------------------\n");
   fprintf(out,
           "# Total SizeMalloc()ed memory: %ld Bytes (%ld requests)\n",
           size_malloc_mem, size_malloc_count);
   fprintf(out,
           "# Total SizeFree()ed   memory: %ld Bytes (%ld requests)\n",
           size_free_mem, size_free_count);
   fprintf(out,
           "# New requests: %6ld (%6ld by SecureMalloc(), %6ld by SecureRealloc())\n",
           secure_malloc_count+secure_realloc_m_count,
           secure_malloc_count, secure_realloc_m_count);
   fprintf(out,
           "# Total SecureMalloc()ed memory: %ld Bytes\n", secure_malloc_mem);
   fprintf(out,
           "# Returned:    %6ld (%6ld by FREE(),         %6ld by SecureRealloc())\n",
           clb_free_count+secure_realloc_f_count,
           clb_free_count , secure_realloc_f_count);
   fprintf(out,
           "# SecureRealloc(ptr): %6ld (%6ld Allocs, %6ld Frees, %6ld Reallocs)\n",
           secure_realloc_count, secure_realloc_m_count,
           secure_realloc_f_count,
           secure_realloc_count-(secure_realloc_m_count+secure_realloc_f_count));
   fprintf(out,
           "# -------------------------------------------------\n\n");
}


/*-----------------------------------------------------------------------
//
// Function: MemFreeListPrint()
//
//   Print the size of the free list for each size.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

void MemFreeListPrint(FILE* out)
{
   long i, sum =0, count=0, tmp;

   fprintf(out, "# MemFreeListPrint()\n");
   fprintf(out, "# ===============================\n");
   for(i=0; i< MEM_ARR_SIZE; i++)
   {
      tmp = free_list_size(free_mem_list[i]);
      if(tmp)
      {
         fprintf(out, "# %4ld: %ld\n", i, tmp);
         sum += tmp*(i);
         count += tmp;
      }
   }
   fprintf(out, "# ===============================\n");
   fprintf(out, "# %6ld blocks with %ld bytes \n", count, sum);
}


#endif
#endif

/*-----------------------------------------------------------------------*/
/*                       Ende des Files                                  */
/*-----------------------------------------------------------------------*/