Revision 914

trunk/src/tools99/mem.cpp (revision 914)
1
/*
2
 * $Id$
3
 *
4
 * Author: Walter Bright
5
 *
6
 * This in an early version of Walter Bright's MEM package originally
7
 * published in the March-April 1990 edition of Micro Cornucopia magazine.
8
 * The MEM package was freely distributed with the Zortech compiler.
9
 * Bright contributed an updated version of the MEM package to the
10
 * Public Domain. It can be downladed from http://snippets.snippets.org/
11
 */
12
#include <stdio.h>
13
#include  <stdlib.h>
14
#ifndef EXIT_FAILURE
15
  #define EXIT_FAILURE 1
16
#endif
17

  
18
//#if defined(__NDPX__) || defined (__GNUDOS__) || defined(DOS386) || defined(__MSVC32__)
19
  #define NEAR
20
//#else
21
//  #define NEAR near
22
//#endif
23

  
24
#if defined (__WAT32__)
25
  #include <io.h>
26
#endif
27

  
28
//#else
29
  //extern void *malloc();
30
  //extern void *calloc();
31
  //extern void *realloc();
32
//#endif
33

  
34
#ifndef SAFE_MEM_H
35
  #include  "safe_mem.h"
36
#endif
37

  
38
#ifndef assert
39
  #include  <assert.h>
40
#endif
41

  
42
#if defined(__MSVC32__)
43
  #include  <dos.h>
44
#endif
45

  
46
#ifndef VAX11C
47
  #ifdef BSDUNIX
48
    #include <adstrings.h>
49
  #else
50
    #include <string.h>
51
  #endif
52
#else
53
  extern char *strcpy(),*memcpy();
54
  extern int strlen();
55
#endif  /* VAX11C */
56

  
57
int mem_inited = 0;    /* != 0 if initialized      */
58

  
59
static int mem_behavior = MEM_ABORTMSG;
60
static int (*fp)(void) = NULL;  /* out-of-memory handler  */
61
static int mem_count;    /* # of allocs that haven't been free'd  */
62
static int mem_scount;    /* # of sallocs that haven't been free'd */
63

  
64
/* Determine where to send error messages  */
65
#if defined (MSDOS)
66
  #define ferr  stdout  /* stderr can't be redirected with MS-DOS  */
67
#else
68
  #define ferr  stderr
69
#endif
70

71
/*******************************/
72

  
73
void mem_setexception(
74
  #if __ZTC__ && __cplusplus
75
    enum MEM_E
76
  #else
77
    int
78
  #endif
79
  flag,
80
  int (*handler_fp)(void))
81
{
82
    mem_behavior = flag;
83
    fp = (mem_behavior == MEM_CALLFP) ? handler_fp : 0;
84
#if MEM_DEBUG
85
    assert(0 <= flag && flag <= MEM_RETRY);
86
#endif
87
}
88

  
89
/*************************
90
 * This is called when we're out of memory.
91
 * Returns:
92
 *  1:  try again to allocate the memory
93
 *  0:  give up and return NULL
94
 */
95

  
96
static int NEAR mem_exception()
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff