File: memalloc.c

package info (click to toggle)
csound 1%3A4.23f12-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,132 kB
  • ctags: 17,345
  • sloc: ansic: 101,063; cpp: 7,730; perl: 335; makefile: 318; tcl: 82
file content (201 lines) | stat: -rw-r--r-- 4,995 bytes parent folder | download | duplicates (2)
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
/*  
    memalloc.c:

    Copyright (C) 1991 Barry Vercoe, John ffitch, Richard Dobson

    This file is part of Csound.

    The Csound Library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    Csound is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with Csound; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
*/

#include "cs.h"                         /*              MEMALLOC.C      */
/*RWD 9:2000 for pvocex support */
#include "pvfileio.h"
/*RWD 9:2000 fix memory leak in strings array */
extern void free_strings(void);
/* global here so reachable by all standalones */
MYFLT sstrcod = (MYFLT)SSTRCOD;
extern void rlsmemfiles(void);

/*  #define MEMDEBUG */

#if defined (_WIN32) && !defined(__GNUC__)                           /*RWD want this for console version too */
#include <crtdbg.h>
extern void DisplayMsg(char *, ...);
#endif
typedef struct {
    void *p;
    long  n;
} MEMREC;
static int apsize = 0;

#ifdef MEMDEBUG
extern void *memfiles;
static MEMREC* all = NULL;
static int ap=0;
void all_free(void)
{
    if (!all) return;
/*     DisplayMsg("%d blocks to free", ap); */
    rlsmemfiles();
    while (--ap>=0) {
/*        DisplayMsg("freeing %p(%d)", all[ap].p, all[ap].n); */
        if (all[ap].p != NULL) free(all[ap].p);
        all[ap].p = NULL;
    }
    free(all);
    all = NULL;                 /* For safety */
    ap = 0;
    apsize = 0;

    /*RWD 9:2000 not terribly vital, but good to do this somewhere... */
    pvsys_release();
    free_strings();
    memfiles = NULL;
    return;
}
#endif

void memRESET(void)
{
#ifdef MEMDEBUG
    all_free();
    ap = 0;
    all = NULL;
#endif
    apsize = 0;
}

static void memdie(long nbytes)
{
    err_printf(Str(X_989,"memory allocate failure for %d\n"), nbytes);
#ifdef mills_macintosh
    err_printf(Str(X_1297,"try increasing preferred size setting for the Perf Application\n"));
#endif
    longjmp(cglob.exitjmp,1);
}

 void *
mcalloc(long nbytes)            /* allocate new memory space, cleared to 0 */
{
    void *p;
#ifdef _DEBUG
    if (!_CrtCheckMemory()) {
      printf("Memory error\n");
    }
#endif
    if ((p = calloc((size_t)nbytes, (size_t)1)) == NULL) {
      if (nbytes==0) return NULL;
      else memdie(nbytes);
    }
#ifdef MEMDEBUG
    if (ap >= apsize) {
      MEMREC *new_all = (MEMREC*)realloc(all, sizeof(MEMREC)*(apsize += 1020));
      if (new_all == NULL) err_printf( "Too many allocs\n"), longjmp(cglob.exitjmp,1);
      all = new_all;
    }
    all[ap].n = nbytes;
    all[ap++].p = p;
#endif
    return(p);
}

 void *
mmalloc(long nbytes)         /* allocate new memory space, NOT cleared to 0 */
{
    void *p;

#ifdef _DEBUG
    if (!_CrtCheckMemory()) {
      printf("Memory error\n");
    }
#endif
    if ((p = malloc((size_t)nbytes)) == NULL)
      memdie(nbytes);
#ifdef MEMDEBUG
    if (ap >= apsize) {
      MEMREC *new_all = (MEMREC *)realloc(all, sizeof(MEMREC)*(apsize += 1020));
      if (new_all == NULL) err_printf( "Too many allocs\n"), longjmp(cglob.exitjmp,1);
      all = new_all;
    }
    all[ap].n = nbytes;
    all[ap++].p = p;
#endif
    return(p);
}

 void *
mrealloc(void *old, long nbytes)                /* Packaged realloc */
{
    void *p;

#ifdef _DEBUG
    if (!_CrtCheckMemory()) {
      printf("Memory error\n");
    }
#endif
    if ((p = realloc(old, (size_t)nbytes)) == NULL)
      memdie(nbytes);
#ifdef MEMDEBUG
    if (old != NULL) {
      int oldall = 0;
      while (all[oldall].p != old) {
        oldall++;
        if (oldall > ap) memdie(-nbytes);
      }
/*        err_printf("Changing %p(%d) to %p(%d)\n", old, all[oldall].n, p, nbytes); */
      all[oldall].n = nbytes;
      all[oldall].p = p;
    }
    else {
      if (ap >= apsize) {
        MEMREC *new_all = (MEMREC*)realloc(all, sizeof(MEMREC)*(apsize += 1020));
        if (new_all == NULL) err_printf( "Too many allocs\n"), longjmp(cglob.exitjmp,1);
        all = new_all;
      }
      all[ap].n = nbytes;
      all[ap++].p = p;
    }
#endif
    return(p);
}

void mfree(void *ptr)
{
#ifdef MEMDEBUG
    int i = 0;
    if (!ptr) return;
    /**
     * Fixed bug - used to throw an exception if all was null.
     */
    if (all == NULL) {
      return;
    }
    while (all[i].p != ptr) {
      i++;
      if (i>ap) memdie(0);
    }
    /*      err_printf("Freeing %d bytes\n", all[i].n); */
    all[i].p = NULL;
#endif
#ifdef _DEBUG
    if (!_CrtCheckMemory()) {
      printf("Memory error\n");
    }
#endif
    free(ptr);
}