File: mem.c

package info (click to toggle)
k2pdfopt 2.53%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,668 kB
  • sloc: ansic: 81,507; cpp: 6,400; makefile: 5
file content (491 lines) | stat: -rw-r--r-- 11,743 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
** mem.c        Memory allocation functions
**
** Part of willus.com general purpose C code library.
**
** Copyright (C) 2018  http://willus.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "willus.h"
#ifdef WIN32
#include <windows.h>
#endif

#define USEGLOBAL

#ifndef NOMEMDEBUG
/* #define DEBUG */
#ifdef DEBUG
static void willus_mem_update(char *label,char *name,int memsize,void *ptr);
#define MAXPTRS 256000
static void *ptrs[MAXPTRS];
static long  sizealloced[MAXPTRS];
static char  fname[MAXPTRS][32];
static int   n;
static char *okay[] = { "" };
static FILE *f;
static long  allocated_ptrs;
static long  totmem;
static int   willus_mem_inited=0;
#endif
#endif // NOMEMDEBUG

static void mem_warn(char *name,int size,int exitcode);


void willus_mem_init(void)

    {
#ifndef NOMEMDEBUG
#ifdef DEBUG
    int i;
    for (i=0;i<MAXPTRS;i++)
        {
        ptrs[i] = NULL;
        sizealloced[i] = 0;
        fname[i][0] = '\0';
        }
    f=fopen("allocs.dat","w");
    allocated_ptrs=0;
    totmem=0;
    n=0;
    willus_mem_inited=1;
#endif
#endif // NOMEMDEBUG
    }


void willus_mem_close(void)

    {
#ifndef NOMEMDEBUG
#ifdef DEBUG
    if (!willus_mem_inited)
        willus_mem_init();
    fclose(f);
#endif
#endif // NOMEMDEBUG
    }


int willus_mem_alloc_warn(void **ptr,int size,char *name,int exitcode)

    {
    int status;

    status = willus_mem_alloc((double **)ptr,(long)size,name);
    if (!status)
        mem_warn(name,size,exitcode);
    return(status);
    }


int willus_mem_realloc_warn(void **ptr,int newsize,char *name,int exitcode)

    {
    int status;

    status = willus_mem_realloc((double **)ptr,newsize,name);
    if (!status)
        mem_warn(name,newsize,exitcode);
    return(status);
    }


int willus_mem_realloc_robust_warn(void **ptr,int newsize,int oldsize,char *name,
                                int exitcode)

    {
    int status;

    status = willus_mem_realloc_robust((double **)ptr,newsize,oldsize,name);
    if (!status)
        mem_warn(name,newsize,exitcode);
    return(status);
    }


/*
** The reason I don't simply use malloc is because I want to allocate
** memory using type long instead of type size_t.  On some compilers,
** like gcc, these are the same, so it doesn't matter.  On other
** compilers, like Turbo C, these are different.
**
*/
int willus_mem_alloc(double **ptr,long size,char *name)

    {
#if (defined(HAVE_WIN32_API) && !defined(__DMC__))
    unsigned long memsize;
    memsize = (unsigned long)size;
#ifdef USEGLOBAL
    (*ptr) = (memsize==size) ? (double *)GlobalAlloc(GPTR,memsize) : NULL;
#else
    (*ptr) = (memsize==size) ? (double *)CoTaskMemAlloc(memsize) : NULL;
#endif
#else
    size_t  memsize;
    memsize=(size_t)size;
    (*ptr) =  (memsize==size) ? (double *)malloc(memsize) : NULL;
#endif
#ifndef NOMEMDEBUG
#ifdef DEBUG
    if (!willus_mem_inited)
        willus_mem_init();
    if ((*ptr)!=NULL)
        {
        int i;

        allocated_ptrs++;
        if (n<allocated_ptrs)
            {
            ptrs[n]=(*ptr);
            sizealloced[n]=memsize;
            strncpy(fname[n],name,31);
            fname[n][31]='\0';
            n++;
            }
        else
            {
            for (i=0;i<n && ptrs[i]!=0;i++);
            ptrs[i]=(*ptr);
            sizealloced[i]=memsize;
            strncpy(fname[i],name,31);
            fname[i][31]='\0';
            if (i>=n)
                n++;
            }
        totmem += memsize;
        willus_mem_update("MA    ",name,memsize,(*ptr));
        }
    else
        fprintf(f,"*** MEM ALLOC FAILS! *** %7ld %s\n",memsize,name);
#endif
#endif // NOMEMDEBUG
/*
{
f=fopen("mem.dat","a");
fprintf(f,"willus_mem_alloc(%d,%s)\n",size,name);
fclose(f);
}
*/
    return((*ptr)!=NULL);
    }


static void mem_warn(char *name,int size,int exitcode)

    {
    static char buf[128];

    aprintf("\n" ANSI_RED "\aCannot allocate enough memory for "
            "function %s." ANSI_NORMAL "\n",name);
    comma_print(buf,size);
    aprintf("    " ANSI_RED "(Needed %s bytes.)" ANSI_NORMAL "\n\n",buf);
    if (exitcode!=0)
        {
        aprintf("    " ANSI_RED "Program terminated." ANSI_NORMAL "\n\n");
        exit(exitcode);
        }
    }


#ifndef NOMEMDEBUG
#ifdef DEBUG
static void willus_mem_update(char *label,char *name,int memsize,void *ptr)

    {
    int i;
    static int count=0;

    if (!willus_mem_inited)
        willus_mem_init();
    for (i=0;okay[i][0]!='\0';i++)
        if (!strncmp(name,okay[i],strlen(okay[i])))
            break;
//    if (okay[i][0]=='\0' || in_string(label,"!")>=0)
    if (1)
        {
/*
        fprintf(f,"%s %5ld %7ld %9ld %10p %s\n",label,allocated_ptrs,
                                                (long)memsize,(long)totmem,ptr,name);
*/
        count++;
        if ((count % 500)==0)
            {
            int ap;
            for (ap=i=0;i<n;i++)
                if (ptrs[i]!=0)
                    ap++;
            fprintf(f,"xx %5d %d\n",ap,(int)totmem);
            // fprintf(f,"=== %d POINTERS LEFT ===\n",ap);
/*
            if ((count % 10000)==0)
            {
            for (i=0;i<n;i++)
                if (ptrs[i]!=0)
                    fprintf(f,"%-32s %10p %7d\n",fname[i],ptrs[i],(int)sizealloced[i]);
            fprintf(f,"\n");
            }
*/
            fflush(f);
            }
        }
    }

void willus_mem_debug_update(char *);
void willus_mem_debug_update(char *s)

    {
    int i,ap;
    for (ap=i=0;i<n;i++)
        if (ptrs[i]!=0)
            ap++;
    fprintf(f,"%s\nyy %5d %d\n",s,ap,(int)totmem);
    for (i=0;i<n;i++)
        if (ptrs[i]!=0)
            fprintf(f,"%-32s %10p %7d\n",fname[i],ptrs[i],(int)sizealloced[i]);
    fprintf(f,"\n");
    fflush(f);
    }
    
#endif
#endif // NOMEMDEBUG


int willus_mem_realloc(double **ptr,long newsize,char *name)

    {
#if (defined(HAVE_WIN32_API) && !defined(__DMC__))
    unsigned long memsize;
    void *newptr;
#else
    size_t  memsize;
    void *newptr;
#endif

#if (defined(HAVE_WIN32_API) && !defined(__DMC__))
    memsize=(unsigned long)newsize;
#else
    memsize=(size_t)newsize;
#endif
    if (memsize!=newsize)
        return(0);
    if ((*ptr)==NULL)
        return(willus_mem_alloc(ptr,newsize,name));
#if (defined(HAVE_WIN32_API) && !defined(__DMC__))
#ifdef USEGLOBAL
    newptr = (void *)GlobalReAlloc((void *)(*ptr),memsize,GMEM_MOVEABLE);
    if (newptr==NULL)
        {
        printf("GlobalReAlloc fails:\n    %s\n",win_lasterror());
        printf("    Function:  %s\n",name);
        printf("    Mem size requested:  %ld\n",newsize);
        }
#else
    newptr = (void *)CoTaskMemRealloc((void *)(*ptr),memsize);
#endif
#else
    newptr = realloc((void *)(*ptr),memsize);
#endif
    if (newptr==NULL && willus_mem_alloc((double **)&newptr,newsize,name))
        {
        printf("!! DIRTY REALLOC in willus_mem_realloc !!\n");
        memcpy(newptr,(*ptr),newsize);
        willus_mem_free(ptr,name);
        }
    if (newptr==NULL)
        return(0);
#ifndef NOMEMDEBUG
#ifdef DEBUG
    if (!willus_mem_inited)
        willus_mem_init();
    {
    int i;
    char label[64];
    for (i=0;i<n && ptrs[i]!=(*ptr);i++);
    if (i>=n)
        {
        sprintf(label,"!!Bad SRA!! oldptr=%p ",(*ptr));
        totmem += memsize;
        ptrs[n] = newptr;
        sizealloced[n] = memsize;
        strncpy(fname[n],name,31);
        fname[n][31]='\0';
        n++;
        allocated_ptrs++;
        willus_mem_update(label,name,memsize,newptr);
        }
    else
        {
        totmem += memsize-sizealloced[i];
        sizealloced[i] = memsize;
        ptrs[i] = newptr;
        strncpy(fname[i],name,31);
        fname[i][31]='\0';
        willus_mem_update(" SRA  ",name,memsize,newptr);
        }
    }
#endif
#endif // NOMEMDEBUG

    (*ptr) = newptr;
    return(1);
    }


int willus_mem_realloc_robust(double **ptr,long newsize,long oldsize,char *name)

    {
#if (defined(HAVE_WIN32_API) && !defined(__DMC__))
    unsigned long memsize;
    void *newptr;
#else
    size_t  memsize;
    void *newptr;
#endif

#ifndef NOMEMDEBUG
#ifdef DEBUG
    int ra=0;
#endif
#endif // NOMEMDEBUG

#if (defined(HAVE_WIN32_API) && !defined(__DMC__))
    memsize=(unsigned long)newsize;
#else
    memsize=(size_t)newsize;
#endif
    if (memsize!=newsize)
        return(0);
    if ((*ptr)==NULL || oldsize<=0)
        return(willus_mem_alloc(ptr,newsize,name));
#if (defined(HAVE_WIN32_API) && !defined(__DMC__))
#ifdef USEGLOBAL
    newptr = (void *)GlobalReAlloc((void *)(*ptr),memsize,GMEM_MOVEABLE);
#else
    newptr = (void *)CoTaskMemRealloc((void *)(*ptr),memsize);
#endif
#else
    newptr = realloc((void *)(*ptr),memsize);
#endif
    if (newptr==NULL && willus_mem_alloc((double **)&newptr,newsize,name))
        {
#ifndef NOMEMDEBUG
#ifdef DEBUG
        ra=1;
        printf("Copying %ld bytes from old pointer to new pointer.\n",oldsize);
#endif
#endif // NOMEMDEBUG
        memcpy(newptr,(*ptr),oldsize);
#ifndef NOMEMDEBUG
#ifdef DEBUG
        printf("Done.\n");
#endif
#endif // NOMEMDEBUG
        willus_mem_free(ptr,name);
        }
    if (newptr==NULL)
        return(0);
#ifndef NOMEMDEBUG
#ifdef DEBUG
    if (!willus_mem_inited)
        willus_mem_init();
    if (ra==0)
    {
    int i;
    char label[80];

    for (i=0;i<n && ptrs[i]!=(*ptr);i++);
    if (i>=n)
        {
        sprintf(label,"!!Bad RRA!! oldptr=%p,oldsize=%d ",(*ptr),(int)oldsize);
        totmem += memsize-oldsize;
        ptrs[n] = newptr;
        sizealloced[n] = memsize;
        strncpy(fname[n],name,31);
        fname[n][31]='\0';
        n++;
        allocated_ptrs++;
        willus_mem_update(label,name,memsize,newptr);
        }
        // printf("*** !! realloc can't find pointer in list !! ***\n");
    else
        {
        totmem += memsize-sizealloced[i];
        sizealloced[i] = memsize;
        ptrs[i] = newptr;
        strncpy(fname[i],name,31);
        fname[i][31]='\0';
        willus_mem_update(" RRA  ",name,memsize,newptr);
        }
    }
#endif
#endif // NOMEMDEBUG

    (*ptr) = newptr;
    return(1);
    }


void willus_mem_free(double **ptr,char *name)

    {
#ifndef NOMEMDEBUG
#ifdef DEBUG
    if (!willus_mem_inited)
        willus_mem_init();
#endif
#endif // NOMEMDEBUG
    if ((*ptr)!=NULL)
        {
#ifndef NOMEMDEBUG
#ifdef DEBUG
        int i;

        allocated_ptrs--;
        for (i=0;i<n && ptrs[i]!=(*ptr);i++);
        if (i>=n)
            willus_mem_update("  !!MF",name,0,(*ptr));
        else
            {
            ptrs[i]=0;
            totmem -= sizealloced[i];
            sizealloced[i]=0;
            for (i=MAXPTRS-1;i>=0;i--)
                if (ptrs[i]!=0)
                    break;
            n=i+1;
            willus_mem_update("    MF",name,0,(*ptr));
            }
#endif
#endif // NOMEMDEBUG
#if (defined(HAVE_WIN32_API) && !defined(__DMC__))
#ifdef USEGLOBAL
        GlobalFree((void *)(*ptr));
#else
        CoTaskMemFree((void *)(*ptr));
#endif
#else
        free((void *)(*ptr));
#endif
        (*ptr)=NULL;
        }
    }