~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to tools/vmalloc/malloc.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include        "vmhdr.h"
 
2
 
 
3
#if _std_malloc || _BLD_INSTRUMENT_ || cray
 
4
int     _STUB_malloc;
 
5
#else
 
6
 
 
7
/*      malloc compatibility functions.
 
8
**      These are aware of debugging/profiling and driven by the environment variables:
 
9
**      VMETHOD: select an allocation method by name.
 
10
**
 
11
**      VMPROFILE: if is a file name, write profile data to it.
 
12
**      VMTRACE: if is a file name, write trace data to it.
 
13
**      The pattern %p in a file name will be replaced by the process ID.
 
14
**
 
15
**      VMDEBUG:
 
16
**              a:                      abort on any warning
 
17
**              [decimal]:              period to check arena.
 
18
**              0x[hexadecimal]:        address to watch.
 
19
**
 
20
**      Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
 
21
*/
 
22
 
 
23
#if _hdr_stat
 
24
#include        <stat.h>
 
25
#else
 
26
#if _sys_stat
 
27
#include        <sys/stat.h>
 
28
#endif
 
29
#endif
 
30
 
 
31
#ifdef S_IRUSR
 
32
#define CREAT_MODE      (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
 
33
#else
 
34
#define CREAT_MODE      0644
 
35
#endif
 
36
 
 
37
#undef malloc
 
38
#undef free
 
39
#undef realloc
 
40
#undef calloc
 
41
#undef cfree
 
42
#undef memalign
 
43
#undef valloc
 
44
 
 
45
#if __STD_C
 
46
static Vmulong_t atou(char** sp)
 
47
#else
 
48
static Vmulong_t atou(sp)
 
49
char**  sp;
 
50
#endif
 
51
{
 
52
        char*           s = *sp;
 
53
        Vmulong_t       v = 0;
 
54
 
 
55
        if(s[0] == '0' && (s[1] == 'x' || s[1] == 'X') )
 
56
        {       for(s += 2; *s; ++s)
 
57
                {       if(*s >= '0' && *s <= '9')
 
58
                                v = (v << 4) + (*s - '0');
 
59
                        else if(*s >= 'a' && *s <= 'f')
 
60
                                v = (v << 4) + (*s - 'a') + 10;
 
61
                        else if(*s >= 'A' && *s <= 'F')
 
62
                                v = (v << 4) + (*s - 'A') + 10;
 
63
                        else break;
 
64
                }
 
65
        }
 
66
        else
 
67
        {       for(; *s; ++s)
 
68
                {       if(*s >= '0' && *s <= '9')
 
69
                                v = v*10 + (*s - '0');
 
70
                        else break;
 
71
                }
 
72
        }
 
73
 
 
74
        *sp = s;
 
75
        return v;
 
76
}
 
77
 
 
78
static int              _Vmflinit = 0;
 
79
static Vmulong_t        _Vmdbcheck = 0;
 
80
static Vmulong_t        _Vmdbtime = 0;
 
81
static int              _Vmpffd = -1;
 
82
#define VMFLINIT() \
 
83
        { if(!_Vmflinit)        vmflinit(); \
 
84
          if(_Vmdbcheck && (++_Vmdbtime % _Vmdbcheck) == 0 && \
 
85
             Vmregion->meth.meth == VM_MTDEBUG) \
 
86
                vmdbcheck(Vmregion); \
 
87
        }
 
88
 
 
89
#if __STD_C
 
90
static char* insertpid(char* begs, char* ends)
 
91
#else
 
92
static char* insertpid(begs,ends)
 
93
char*   begs;
 
94
char*   ends;
 
95
#endif
 
96
{       int     pid;
 
97
        char*   s;
 
98
 
 
99
        if((pid = getpid()) < 0)
 
100
                return NIL(char*);
 
101
 
 
102
        s = ends;
 
103
        do
 
104
        {       if(s == begs)
 
105
                        return NIL(char*);
 
106
                *--s = '0' + pid%10;
 
107
        } while((pid /= 10) > 0);
 
108
        while(s < ends)
 
109
                *begs++ = *s++;
 
110
 
 
111
        return begs;
 
112
}
 
113
 
 
114
#if __STD_C
 
115
static int createfile(char* file)
 
116
#else
 
117
static int createfile(file)
 
118
char*   file;
 
119
#endif
 
120
{
 
121
        char    buf[1024];
 
122
        char    *next, *endb;
 
123
 
 
124
        next = buf;
 
125
        endb = buf + sizeof(buf);
 
126
        while(*file)
 
127
        {       if(*file == '%')
 
128
                {       switch(file[1])
 
129
                        {
 
130
                        case 'p' :
 
131
                                if(!(next = insertpid(next,endb)) )
 
132
                                        return -1;
 
133
                                file += 2;
 
134
                                break;
 
135
                        default :
 
136
                                goto copy;
 
137
                        }
 
138
                }
 
139
                else
 
140
                { copy:
 
141
                        *next++ = *file++;
 
142
                }
 
143
 
 
144
                if(next >= endb)
 
145
                        return -1;
 
146
        }
 
147
 
 
148
        *next = '\0';
 
149
#if _PACKAGE_ast
 
150
        return open(buf,O_WRONLY|O_CREAT|O_TRUNC,CREAT_MODE);
 
151
#else
 
152
        return creat(buf,CREAT_MODE);
 
153
#endif
 
154
}
 
155
 
 
156
#if __STD_C
 
157
static void pfprint(void)
 
158
#else
 
159
static void pfprint()
 
160
#endif
 
161
{
 
162
        if(Vmregion->meth.meth == VM_MTPROFILE)
 
163
                vmprofile(Vmregion,_Vmpffd);
 
164
}
 
165
 
 
166
#if __STD_C
 
167
static int vmflinit(void)
 
168
#else
 
169
static int vmflinit()
 
170
#endif
 
171
{
 
172
        char*           env;
 
173
        Vmalloc_t*      vm;
 
174
        int             fd;
 
175
        Vmulong_t       addr;
 
176
        char*           file;
 
177
        int             line;
 
178
 
 
179
        /* this must be done now to avoid any inadvertent recursion (more below) */
 
180
        _Vmflinit = 1;
 
181
        VMFILELINE(Vmregion,file,line);
 
182
 
 
183
        /* if getenv() calls malloc(), this may not be caught by the eventual region */
 
184
        vm = NIL(Vmalloc_t*);
 
185
        if((env = getenv("VMETHOD")) )
 
186
        {       if(strcmp(env,"Vmdebug") == 0 || strcmp(env,"vmdebug") == 0)
 
187
                        vm = vmopen(Vmdcsbrk,Vmdebug,0);
 
188
                else if(strcmp(env,"Vmprofile") == 0 || strcmp(env,"vmprofile") == 0 )
 
189
                        vm = vmopen(Vmdcsbrk,Vmprofile,0);
 
190
                else if(strcmp(env,"Vmlast") == 0 || strcmp(env,"vmlast") == 0 )
 
191
                        vm = vmopen(Vmdcsbrk,Vmlast,0);
 
192
                else if(strcmp(env,"Vmpool") == 0 || strcmp(env,"vmpool") == 0 )
 
193
                        vm = vmopen(Vmdcsbrk,Vmpool,0);
 
194
                else if(strcmp(env,"Vmbest") == 0 || strcmp(env,"vmbest") == 0 )
 
195
                        vm = Vmheap;
 
196
        }
 
197
 
 
198
        if((!vm || vm->meth.meth == VM_MTDEBUG) &&
 
199
           (env = getenv("VMDEBUG")) && env[0] )
 
200
        {       if(vm || (vm = vmopen(Vmdcsbrk,Vmdebug,0)) )
 
201
                {       reg int setcheck = 0;
 
202
 
 
203
                        while(*env)
 
204
                        {       if(*env == 'a')
 
205
                                        vmset(vm,VM_DBABORT,1);
 
206
 
 
207
                                if(*env < '0' || *env > '9')
 
208
                                        env += 1;
 
209
                                else if(env[0] == '0' &&
 
210
                                        (env[1] == 'x' || env[1] == 'X') )
 
211
                                {       if((addr = atou(&env)) != 0)
 
212
                                                vmdbwatch((Void_t*)addr);
 
213
                                }
 
214
                                else
 
215
                                {       _Vmdbcheck = atou(&env);
 
216
                                        setcheck = 1;
 
217
                                }
 
218
                        }
 
219
                        if(!setcheck)
 
220
                                _Vmdbcheck = 1;
 
221
                }
 
222
        }
 
223
 
 
224
        if((!vm || vm->meth.meth == VM_MTPROFILE) &&
 
225
           (env = getenv("VMPROFILE")) && env[0] )
 
226
        {       _Vmpffd = createfile(env);
 
227
                if(!vm)
 
228
                        vm = vmopen(Vmdcsbrk,Vmprofile,0);
 
229
        }
 
230
 
 
231
        /* slip in the new region now so that malloc() will work fine */
 
232
        if(vm)
 
233
                Vmregion = vm;
 
234
 
 
235
        /* turn on tracing if requested */
 
236
        if((env = getenv("VMTRACE")) && env[0] && (fd = createfile(env)) >= 0)
 
237
        {       vmset(Vmregion,VM_TRACE,1);
 
238
                vmtrace(fd);
 
239
        }
 
240
 
 
241
        /* make sure that profile data is output upon exiting */
 
242
        if(vm && vm->meth.meth == VM_MTPROFILE)
 
243
        {       if(_Vmpffd < 0)
 
244
                        _Vmpffd = 2;
 
245
                /* this may wind up calling malloc(), but region is ok now */
 
246
                atexit(pfprint);
 
247
        }
 
248
        else if(_Vmpffd >= 0)
 
249
        {       close(_Vmpffd);
 
250
                _Vmpffd = -1;
 
251
        }
 
252
 
 
253
        /* reset file and line number to correct values for the call */
 
254
        Vmregion->file = file;
 
255
        Vmregion->line = line;
 
256
 
 
257
        return 0;
 
258
}
 
259
 
 
260
#if __STD_C
 
261
Void_t* malloc(reg size_t size)
 
262
#else
 
263
Void_t* malloc(size)
 
264
reg size_t      size;
 
265
#endif
 
266
{
 
267
        VMFLINIT();
 
268
        return (*Vmregion->meth.allocf)(Vmregion,size);
 
269
}
 
270
 
 
271
#if __STD_C
 
272
Void_t* realloc(reg Void_t* data, reg size_t size)
 
273
#else
 
274
Void_t* realloc(data,size)
 
275
reg Void_t*     data;   /* block to be reallocated      */
 
276
reg size_t      size;   /* new size                     */
 
277
#endif
 
278
{
 
279
        VMFLINIT();
 
280
        return (*Vmregion->meth.resizef)(Vmregion,data,size,VM_RSCOPY|VM_RSMOVE);
 
281
}
 
282
 
 
283
#if __STD_C
 
284
void free(reg Void_t* data)
 
285
#else
 
286
void free(data)
 
287
reg Void_t*     data;
 
288
#endif
 
289
{
 
290
        VMFLINIT();
 
291
        (void)(*Vmregion->meth.freef)(Vmregion,data);
 
292
}
 
293
 
 
294
#if __STD_C
 
295
Void_t* calloc(reg size_t n_obj, reg size_t s_obj)
 
296
#else
 
297
Void_t* calloc(n_obj, s_obj)
 
298
reg size_t      n_obj;
 
299
reg size_t      s_obj;
 
300
#endif
 
301
{
 
302
        VMFLINIT();
 
303
        return (*Vmregion->meth.resizef)(Vmregion,NIL(Void_t*),n_obj*s_obj,VM_RSZERO);
 
304
}
 
305
 
 
306
#if __STD_C
 
307
void cfree(reg Void_t* data)
 
308
#else
 
309
void cfree(data)
 
310
reg Void_t*     data;
 
311
#endif
 
312
{
 
313
        VMFLINIT();
 
314
        (void)(*Vmregion->meth.freef)(Vmregion,data);
 
315
}
 
316
 
 
317
#if __STD_C
 
318
Void_t* memalign(reg size_t align, reg size_t size)
 
319
#else
 
320
Void_t* memalign(align, size)
 
321
reg size_t      align;
 
322
reg size_t      size;
 
323
#endif
 
324
{
 
325
        VMFLINIT();
 
326
        return (*Vmregion->meth.alignf)(Vmregion,size,align);
 
327
}
 
328
 
 
329
#if __STD_C
 
330
Void_t* valloc(reg size_t size)
 
331
#else
 
332
Void_t* valloc(size)
 
333
reg size_t      size;
 
334
#endif
 
335
{
 
336
        VMFLINIT();
 
337
        GETPAGESIZE(_Vmpagesize);
 
338
        return (*Vmregion->meth.alignf)(Vmregion,size,_Vmpagesize);
 
339
}
 
340
 
 
341
#if _hdr_malloc
 
342
 
 
343
#define calloc  ______calloc
 
344
#define free    ______free
 
345
#define malloc  ______malloc
 
346
#define realloc ______realloc
 
347
 
 
348
#include        <malloc.h>
 
349
 
 
350
#if _lib_mallopt
 
351
#if __STD_C
 
352
int mallopt(int cmd, int value)
 
353
#else
 
354
int mallopt(cmd, value)
 
355
int     cmd;
 
356
int     value;
 
357
#endif
 
358
{
 
359
        VMFLINIT();
 
360
        return 0;
 
361
}
 
362
#endif
 
363
 
 
364
#if _lib_mallinfo
 
365
#if __STD_C
 
366
struct mallinfo mallinfo(void)
 
367
#else
 
368
struct mallinfo mallinfo()
 
369
#endif
 
370
{
 
371
        Vmstat_t        sb;
 
372
        struct mallinfo mi;
 
373
 
 
374
        VMFLINIT();
 
375
        memset(&mi,0,sizeof(mi));
 
376
        if(vmstat(Vmregion,&sb) >= 0)
 
377
        {       mi.arena = sb.extent;
 
378
                mi.ordblks = sb.n_busy+sb.n_free;
 
379
                mi.uordblks = sb.s_busy;
 
380
                mi.fordblks = sb.s_free;
 
381
        }
 
382
        return mi;
 
383
}
 
384
#endif
 
385
 
 
386
#if _lib_mstats
 
387
#if __STD_C
 
388
struct mstats mstats(void)
 
389
#else
 
390
struct mstats mstats()
 
391
#endif
 
392
{
 
393
        Vmstat_t        sb;
 
394
        struct mstats   ms;
 
395
 
 
396
        VMFLINIT();
 
397
        memset(&ms,0,sizeof(ms));
 
398
        if(vmstat(Vmregion,&sb) >= 0)
 
399
        {       ms.bytes_total = sb.extent;
 
400
                ms.chunks_used = sb.n_busy;
 
401
                ms.bytes_used = sb.s_busy;
 
402
                ms.chunks_free = sb.n_free;
 
403
                ms.bytes_free = sb.s_free;
 
404
        }
 
405
        return ms;
 
406
}
 
407
#endif
 
408
 
 
409
#endif/*_hdr_malloc*/
 
410
 
 
411
#if !_lib_alloca || _mal_alloca
 
412
#ifndef _stk_down
 
413
#define _stk_down       0
 
414
#endif
 
415
typedef struct _alloca_s        Alloca_t;
 
416
union _alloca_u
 
417
{       struct
 
418
        {       char*           addr;
 
419
                Alloca_t*       next;
 
420
        } head;
 
421
        char    array[ALIGN];
 
422
};
 
423
struct _alloca_s
 
424
{       union _alloca_u head;
 
425
        Vmuchar_t       data[1];
 
426
};
 
427
 
 
428
#if __STD_C
 
429
Void_t* alloca(size_t size)
 
430
#else
 
431
Void_t* alloca(size)
 
432
size_t  size;
 
433
#endif
 
434
{       char            array[ALIGN];
 
435
        char*           file;
 
436
        int             line;
 
437
        reg Alloca_t*   f;
 
438
        static Alloca_t* Frame;
 
439
 
 
440
        VMFLINIT();
 
441
        VMFILELINE(Vmregion,file,line);
 
442
        while(Frame)
 
443
        {       if(( _stk_down && &array[0] > Frame->head.head.addr) ||
 
444
                   (!_stk_down && &array[0] < Frame->head.head.addr) )
 
445
                {       f = Frame;
 
446
                        Frame = f->head.head.next;
 
447
                        (void)(*Vmregion->meth.freef)(Vmregion,f);
 
448
                }
 
449
                else    break;
 
450
        }
 
451
 
 
452
        Vmregion->file = file;
 
453
        Vmregion->line = line;
 
454
        f = (Alloca_t*)(*Vmregion->meth.allocf)(Vmregion,size+sizeof(Alloca_t)-1);
 
455
 
 
456
        f->head.head.addr = &array[0];
 
457
        f->head.head.next = Frame;
 
458
        Frame = f;
 
459
 
 
460
        return (Void_t*)f->data;
 
461
}
 
462
#endif /*!_lib_alloca || _mal_alloca*/
 
463
 
 
464
#endif /*_std_malloc || _BLD_INSTRUMENT_ || cray*/