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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
 * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
 * You may copy, distribute, and use this software as long as this
 * copyright statement is not removed.
 */
#include <stdio.h>
#include <fcntl.h>
#include "malloc.h"
#include "tostring.h"

/*
 * Function:    malloc()
 *
 * Purpose: memory allocator
 *
 * Arguments:   size    - size of data area needed
 *
 * Returns: pointer to allocated area, or NULL if unable
 *      to allocate addtional data.
 *
 * Narrative:
 *
 */
#ifndef lint
static
char rcs_hdr[] = "$Id: malloc.c,v 1.2 2006-07-25 10:08:36 rt Exp $";
#endif

extern int    malloc_checking;
char        * malloc_data_start;
char        * malloc_data_end;
struct mlist    * malloc_end;
int       malloc_errfd = 2;
int       malloc_errno;
int       malloc_fatal_level = M_HANDLE_CORE;
struct mlist      malloc_start;
int       malloc_warn_level;
void          malloc_memset();

char *
malloc(size)
    unsigned int      size;
{
    char        * func = "malloc";
    char        * getenv();
    void          malloc_fatal();
    void          malloc_init();
    void          malloc_split();
    void          malloc_warning();
    unsigned int      need;
    struct mlist    * oldptr;
    struct mlist    * ptr;
    char        * sbrk();

    /*
     * If this is the first call to malloc...
     */
    if( malloc_data_start == (char *) 0 )
    {
        malloc_init();
    }

    /*
     * If malloc chain checking is on, go do it.
     */
    if( malloc_checking )
    {
        (void) malloc_chain_check(1);
    }

    /*
     * always make sure there is at least on extra byte in the malloc
     * area so that we can verify that the user does not overrun the
     * data area.
     */
    size++;

    /*
     * Now look for a free area of memory of size bytes...
     */
    oldptr = NULL;
    for(ptr = &malloc_start; ; ptr = ptr->next)
    {
        /*
         * Since the malloc chain is a forward only chain, any
         * pointer that we get should always be positioned in
         * memory following the previous pointer.  If this is not
         * so, we must have a corrupted chain.
         */
        if( ptr )
        {
            if( ptr<oldptr )
            {
                malloc_errno = M_CODE_CHAIN_BROKE;
                malloc_fatal(func);
                return(NULL);
            }
            oldptr = ptr;
        }
        else if( oldptr != malloc_end )
        {
            /*
             * This should never happen.  If it does, then
             * we got a real problem.
             */
            malloc_errno = M_CODE_NO_END;
            malloc_fatal(func);
            return(NULL);
        }


        /*
         * if this element is already in use...
         */
        if( ptr && ((ptr->flag & M_INUSE) != 0) )
        {
            continue;
        }

        /*
         * if there isn't room for this block..
         */
        if( ptr && (ptr->s.size < size) )
        {
            continue;
        }

        /*
         * If ptr is null, we have run out of memory and must sbrk more
         */
        if( ptr == NULL )
        {
            need = (size + M_SIZE) * (size > 10*1024 ? 1:2);
            if( need < M_BLOCKSIZE )
            {
                need = M_BLOCKSIZE;
            }
            else if( need & (M_BLOCKSIZE-1) )
            {
                need &= ~(M_BLOCKSIZE-1);
                need += M_BLOCKSIZE;
            }
            ptr = (struct mlist *) sbrk((int)need);
            if( ptr == (struct mlist *) -1 )
            {
                malloc_errno = M_CODE_NOMORE_MEM;
                malloc_fatal(func);
            }
            malloc_data_end = sbrk((int)0);

            ptr->prev   = oldptr;
            ptr->next   = (struct mlist *) 0;
            ptr->s.size = need - M_SIZE;
            ptr->flag  = M_MAGIC;

            oldptr->next = ptr;
            malloc_end = ptr;


        } /* if( ptr ==... */

        /*
          * Now ptr points to a memory location that can store
         * this data, so lets go to work.
         */

        ptr->r_size = size;     /* save requested size  */
        ptr->flag |= M_INUSE;

        /*
          * split off unneeded data area in this block, if possible...
         */
        malloc_split(ptr);

        /*
         * re-adjust the requested size so that it is what the user
         * actually requested...
         */

        ptr->r_size--;

        /*
         * just to make sure that noone is misusing malloced
          * memory without initializing it, lets set it to
         * all '\01's.  We call local_memset() because memset()
         * may be checking for malloc'd ptrs and this isn't
         * a malloc'd ptr yet.
         */
        malloc_memset(ptr->data,M_FILL,(int)ptr->s.size);

        return( ptr->data);

    } /* for(... */

} /* malloc(... */

/*
 * Function:    malloc_split()
 *
 * Purpose: to split a malloc segment if there is enough room at the
 *      end of the segment that isn't being used
 *
 * Arguments:   ptr - pointer to segment to split
 *
 * Returns: nothing of any use.
 *
 * Narrative:
 *      get the needed size of the module
 *      round the size up to appropriat boundry
 *      calculate amount of left over space
 *      if there is enough left over space
 *          create new malloc block out of remainder
 *          if next block is free
 *          join the two blocks together
 *          fill new empty block with free space filler
 *          re-adjust pointers and size of current malloc block
 *
 *
 *
 * Mod History:
 *   90/01/27   cpcahil     Initial revision.
 */
void
malloc_split(ptr)
    struct mlist        * ptr;
{
    extern struct mlist * malloc_end;
    void              malloc_join();
    int           rest;
    int           size;
    struct mlist        * tptr;

    size = ptr->r_size;

    /*
     * roundup size to the appropriate boundry
     */

    M_ROUNDUP(size);

    /*
     * figure out how much room is left in the array.
     * if there is enough room, create a new mlist
     *     structure there.
     */

    if( ptr->s.size > size )
    {
        rest = ptr->s.size - size;
    }
    else
    {
        rest = 0;
    }

    if( rest > (M_SIZE+M_RND) )
    {
        tptr = (struct mlist *) (ptr->data+size);
        tptr->prev = ptr;
        tptr->next = ptr->next;
        tptr->flag = M_MAGIC;
        tptr->s.size = rest - M_SIZE;

        /*
         * If possible, join this segment with the next one
         */

        malloc_join(tptr, tptr->next,0,0);

        if( tptr->next )
        {
            tptr->next->prev = tptr;
        }

        malloc_memset(tptr->data,M_FREE_FILL, (int)tptr->s.size);

        ptr->next = tptr;
        ptr->s.size = size;

        if( malloc_end == ptr )
        {
            malloc_end = tptr;
        }
    }

} /* malloc_split(... */

/*
 * Function:    malloc_join()
 *
 * Purpose: to join two malloc segments together (if possible)
 *
 * Arguments:   ptr - pointer to segment to join to.
 *      nextptr - pointer to next segment to join to ptr.
 *
 * Returns: nothing of any values.
 *
 * Narrative:
 *
 * Mod History:
 *   90/01/27   cpcahil     Initial revision.
 */
void
malloc_join(ptr,nextptr, inuse_override, fill_flag)
    struct mlist    * ptr;
    struct mlist    * nextptr;
    int       inuse_override;
    int       fill_flag;
{
    unsigned int      newsize;<--- The scope of the variable 'newsize' can be reduced

    if(     ptr     && ! (inuse_override || (ptr->flag & M_INUSE)) &&
        nextptr && ! (nextptr->flag & M_INUSE) &&
        ((ptr->data+ptr->s.size) == (char *) nextptr) )
    {
        if( malloc_end == nextptr )
        {
            malloc_end = ptr;
        }
        ptr->next = nextptr->next;
        newsize = nextptr->s.size + M_SIZE;

        /*
         * if we are to fill and this segment is in use,
         *   fill in with M_FILL newly added space...
          */

        if(fill_flag && (ptr->flag & M_INUSE) )
        {
            malloc_memset(ptr->data+ptr->s.size,
                      M_FILL, (int)(nextptr->s.size + M_SIZE));
        }

        ptr->s.size += newsize;
        if( ptr->next )
        {
            ptr->next->prev = ptr;
        }
    }

} /* malloc_join(... */


/*
 * The following mess is just to ensure that the versions of these functions in
 * the current library are included (to make sure that we don't accidentaly get
 * the libc versions. (This is the lazy man's -u ld directive)
 */

void free();
int strcmp();
int memcmp();
char    * realloc();

void        (*malloc_void_funcs[])() =
{
    free,
};

int     (*malloc_int_funcs[])() =
{
    strcmp,
    memcmp,
};

char        * (*malloc_char_star_funcs[])() =
{
    realloc,
};

/*
 * This is malloc's own memset which is used without checking the parameters.
 */

void
malloc_memset(ptr,byte,len)
    char        * ptr;
    char          byte;
    int       len;
{

    while(len-- > 0)
    {
        *ptr++ = byte;
    }

} /* malloc_memset(... */

/*
 * Function:    malloc_fatal()
 *
 * Purpose: to display fatal error message and take approrpriate action
 *
 * Arguments:   funcname - name of function calling this routine
 *
 * Returns: nothing of any value
 *
 * Narrative:
 *
 * Notes:   This routine does not make use of any libc functions to build
 *      and/or disply the error message.  This is due to the fact that
 *      we are probably at a point where malloc is having a real problem
 *      and we don't want to call any function that may use malloc.
 */
void
malloc_fatal(funcname)
    char        * funcname;
{
    char          errbuf[128];
    void          exit();
    void          malloc_err_handler();
    extern char * malloc_err_strings[];
    extern int    malloc_errno;
    extern int    malloc_fatal_level;
    char        * s;
    char        * t;

    s = errbuf;
    t = "Fatal error: ";
    while( *s = *t++)
    {
        s++;
    }
    t = funcname;
    while( *s = *t++)
    {
        s++;
    }

    t = "(): ";
    while( *s = *t++)
    {
        s++;
    }

    t = malloc_err_strings[malloc_errno];
    while( *s = *t++)
    {
        s++;
    }

    *(s++) = '\n';

    if( write(malloc_errfd,errbuf,(unsigned)(s-errbuf)) != (s-errbuf))
    {
        (void) write(2,"I/O error to error file\n",(unsigned)24);
        exit(110);
    }
    malloc_err_handler(malloc_fatal_level);

} /* malloc_fatal(... */

/*
 * Function:    malloc_warning()
 *
 * Purpose: to display warning error message and take approrpriate action
 *
 * Arguments:   funcname - name of function calling this routine
 *
 * Returns: nothing of any value
 *
 * Narrative:
 *
 * Notes:   This routine does not make use of any libc functions to build
 *      and/or disply the error message.  This is due to the fact that
 *      we are probably at a point where malloc is having a real problem
 *      and we don't want to call any function that may use malloc.
 */
void
malloc_warning(funcname)
    char        * funcname;
{
    char          errbuf[128];
    void          exit();
    void          malloc_err_handler();
    extern char * malloc_err_strings[];
    extern int    malloc_errno;
    extern int    malloc_warn_level;
    char        * s;
    char        * t;

    s = errbuf;
    t = "Warning: ";
    while( *s = *t++)
    {
        s++;
    }
    t = funcname;
    while( *s = *t++)
    {
        s++;
    }

    t = "(): ";
    while( *s = *t++)
    {
        s++;
    }

    t = malloc_err_strings[malloc_errno];
    while( *s = *t++)
    {
        s++;
    }

    *(s++) = '\n';

    if( write(malloc_errfd,errbuf,(unsigned)(s-errbuf)) != (s-errbuf))
    {
        (void) write(2,"I/O error to error file\n",(unsigned)24);
        exit(110);
    }

    malloc_err_handler(malloc_warn_level);

} /* malloc_warning(... */

/*
 * Function:    malloc_err_handler()
 *
 * Purpose: to take the appropriate action for warning and/or fatal
 *      error conditions.
 *
 * Arguments:   level - error handling level
 *
 * Returns: nothing of any value
 *
 * Narrative:
 *
 * Notes:   This routine does not make use of any libc functions to build
 *      and/or disply the error message.  This is due to the fact that
 *      we are probably at a point where malloc is having a real problem
 *      and we don't want to call any function that may use malloc.
 */
void
malloc_err_handler(level)
{
    void          exit();
    void          malloc_dump();
    extern int    malloc_errfd;

    if( level & M_HANDLE_DUMP )
    {
        malloc_dump(malloc_errfd);
    }

    switch( level & ~M_HANDLE_DUMP )
    {
        /*
         * If we are to drop a core file and exit
         */
        case M_HANDLE_ABORT:
            (void) abort();
            break;

        /*
         * If we are to exit..
         */
        case M_HANDLE_EXIT:
            exit(200);
            break;

#ifndef __MSDOS__
        /*
         * If we are to dump a core, but keep going on our merry way
         */
        case M_HANDLE_CORE:
            {
                int   pid;

                /*
                 * fork so child can abort (and dump core)
                 */
                if( (pid = fork()) == 0 )
                {
                    (void) write(2,"Child dumping core\n",
                            (unsigned)9);
                    (void) abort();
                }

                /*
                  * wait for child to finish dumping core
                 */
                while( wait((int *)0) != pid)
                {
                }

                /*
                 * Move core file to core.pid.cnt so
                 * multiple cores don't overwrite each
                 * other.
                 */
                if( access("core",0) == 0 )
                {
                    static int    corecnt;
                    char          filenam[32];
                    filenam[0] = 'c';
                    filenam[1] = 'o';
                    filenam[2] = 'r';
                    filenam[3] = 'e';
                    filenam[4] = '.';
                    (void)tostring(filenam+5,getpid(),
                        5, B_DEC, '0');
                    filenam[10] = '.';
                    (void)tostring(filenam+11,corecnt++,
                        3, B_DEC, '0');
                    filenam[14] = '\0';
                    (void) unlink(filenam);
                    if( link("core",filenam) == 0)
                    {
                        (void) unlink("core");
                    }
                }
            }
#endif


        /*
         * If we are to just ignore the error and keep on processing
         */
        case M_HANDLE_IGNORE:
            break;

    } /* switch(... */

} /* malloc_err_handler(... */