Annotation of researchv10no/cmd/cfront/ooptcfront/alloc.c, revision 1.1.1.1

1.1       root        1: /* -*- Mode:C++ -*- */
                      2: /*ident        "@(#)ctrans:src/alloc.c 1.1.1.8" */
                      3: /*
                      4:        $Source: /usr3/lang/benson/work/stripped_cfront/RCS/alloc.c,v $ $RCSfile: alloc.c,v $
                      5:        $Revision: 1.1 $                $Date: 89/11/20 08:50:09 $
                      6:        $Author: benson $               $Locker:  $
                      7:        $State: Exp $
                      8: 
                      9: 
                     10: */
                     11: /**************************************************************************
                     12: 
                     13:         C++ source for cfront, the C++ compiler front-end
                     14:         written in the computer science research center of Bell Labs
                     15: 
                     16:         Copyright (c) 1984 AT&T, Inc. All Rights Reserved
                     17:         THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T, INC.
                     18: 
                     19: alloc.c:
                     20: 
                     21: *****************************************************************************/
                     22: #include "cfront.h"
                     23: #include <memory.h>
                     24: #include "size.h"
                     25: 
                     26: //int Nchunk;
                     27: 
                     28: //void print_free()
                     29: //{
                     30: //     fprintf(stderr,"free store: %d bytes alloc()=%d free()=%d\n",Nfree_store,Nal//loc, Nfree);
                     31: //     fprintf(stderr,"%d chunks: %d (%d)\n",Nchunk,CHUNK,Nchunk*CHUNK);
                     32: //}
                     33: 
                     34: 
                     35: /*
                     36: #ifdef __cplusplus
                     37: #include <new.h>
                     38: #define NEW_SIZE size_t
                     39: #else
                     40: #define NEW_SIZE long
                     41: #endif
                     42: */
                     43: 
                     44: // now __HAVE_SIZE_T defined in 2.0 malloc.h c++ header file
                     45: #ifdef __HAVE_SIZE_T
                     46: #include <new.h>
                     47: #define NEW_SIZE size_t
                     48: #else
                     49: #define NEW_SIZE long
                     50: #endif
                     51: 
                     52: void* operator new(NEW_SIZE sz)        // get memory that might be freed
                     53: {
                     54:        char* p = calloc((unsigned)sz,1);
                     55: 
                     56: //fprintf(stderr,"alloc(%d)->%d\n",sz,p);
                     57:        if (p == 0) {                   // no space
                     58:                free((char*)gtbl);      // get space for error message
                     59: //             if (Nspy) print_free();
                     60:                error('i',"free store exhausted");
                     61:        }
                     62: //     Nalloc++;
                     63: //     Nfree_store += sz+sizeof(int*);
                     64:        return p;
                     65: }
                     66: //int NFn, NFtn, NFbt, NFpv, NFf, NFe, NFs, NFc;
                     67: 
                     68: void operator delete (void* p)
                     69: {
                     70:        if (p == 0) return;
                     71: 
                     72: //fprintf(stderr,"free(%d) %d\n",p,((int*)p)[-1]-(int)p-1+sizeof(int*));
                     73: 
                     74: //if (Nspy) {
                     75: //     Pname pp = (Pname) p;
                     76: //     TOK t = pp->base;
                     77: //     Nfree++;
                     78: //     Nfree_store -= ((int*)p)[-1]-(int)p-1+sizeof(int*);
                     79: //     switch (t) {    // can be fooled by character strings
                     80: //     case INT: case CHAR: case TYPE: case VOID: case SHORT: case LONG:
                     81: //     case FLOAT: case DOUBLE: case LDOUBLE: case COBJ: case EOBJ: case FIELD:
                     82: //                     NFbt++; break;
                     83: //
                     84: //     case PTR: case VEC:
                     85: //                     NFpv++; break;
                     86: //
                     87: //     case FCT:       NFf++; break;
                     88: //
                     89: //     case ICON: case CCON: case STRING: case FCON: case THIS:
                     90: //                     NFc++; break;
                     91: //     }
                     92: //}
                     93:        free((char*)p);
                     94: }
                     95: 
                     96: #ifdef OPTIMIZE
                     97: inline void mzero (register char * p, register int l)
                     98: {
                     99:     while(l>=0) { *p++ = 0; l--; }
                    100: }
                    101: #else
                    102: #define mzero(p,l) memset(p,0,l)
                    103: #endif
                    104: 
                    105: /* page chunking slows things down -- disimproved locality
                    106:    overwhelms cpu in malloc? */
                    107: #ifdef PageChunk
                    108: #define PAGESWORTH(sz) (8192/sizeof(sz))
                    109: #define PAGESIZE 8192
                    110: #endif
                    111: 
                    112: void *
                    113: expr::operator new (size_t)
                    114: {
                    115:     char * page;
                    116: 
                    117:     if(!free) {
                    118: #ifdef PageChunk
                    119:        Pexpr x;
                    120:        page = valloc(PAGESIZE);
                    121:        free = Pexpr(page);
                    122:        for(x = free; x < &free[PAGESWORTH(expr) - 1]; x++)
                    123:            x->e1 = x+1;
                    124:        x->e1 = 0;
                    125: #else
                    126:        page = malloc(sizeof(expr));
                    127:        free = Pexpr(page);
                    128:        free->e1 = 0;
                    129: #endif
                    130:     }
                    131:     page = (char *)free;
                    132:     free = free->e1;
                    133:     mzero(page, sizeof(expr));
                    134:     return (void *)page;
                    135: }
                    136: 
                    137: void *
                    138: stmt::operator new (size_t)
                    139: {
                    140:     char * page;
                    141: 
                    142:     if(!free) {
                    143: #ifdef PageChunk
                    144:        Pstmt x;
                    145:        page = valloc(PAGESIZE);
                    146:        free = Pstmt(page);
                    147:        for(x = free; x < &free[PAGESWORTH(stmt) - 1]; x++)
                    148:            x->s_list = x+1;
                    149:        x->s_list = 0;
                    150: #else
                    151:        page = malloc(sizeof(stmt));
                    152:        free = Pstmt(page);
                    153:        free->s_list = 0;
                    154: #endif
                    155:     }
                    156:     page = (char *)free;
                    157:     free = free->s_list;
                    158:     mzero(page, sizeof(stmt));
                    159:     return (void *) page;
                    160: }
                    161: 
                    162: void *
                    163: name::operator new (size_t)
                    164: {
                    165:     char * page;
                    166: 
                    167:     if(!free) {
                    168: #ifdef PageChunk
                    169:        Pname x;
                    170:        page = valloc(PAGESIZE);
                    171:        free = Pname(page);
                    172:        for(x = free; x < &free[PAGESWORTH(name) - 1]; x++)
                    173:            x->n_tbl_list = x+1;
                    174:        x->n_tbl_list = 0;
                    175: #else
                    176:        page = malloc(sizeof(name));
                    177:        free = Pname(page);
                    178:        free->n_tbl_list = 0;
                    179: #endif
                    180:     }
                    181:     page = (char *)free;
                    182:     free = free->n_tbl_list;
                    183:     mzero(page, sizeof(name));
                    184:     return (void *) page;
                    185: }
                    186: 
                    187: void
                    188: expr::operator delete (void * ve)
                    189: {
                    190:     Pexpr(ve)->permanent = 3;
                    191:     Pexpr(ve)->e1 = new_free;
                    192:     new_free = Pexpr(ve);
                    193: }
                    194: 
                    195: void
                    196: stmt::operator delete (void * vs)
                    197: {
                    198:     Pstmt(vs)->permanent = 3;
                    199:     Pstmt(vs)->s_list = new_free;
                    200:     new_free = Pstmt(vs);
                    201: }
                    202: 
                    203: void
                    204: name::operator delete (void * vn)
                    205: {
                    206:     Pname(vn)->permanent = 3;
                    207:     Pname(vn)->n_tbl_list = new_free;
                    208:     new_free = Pname(vn);
                    209: }
                    210: 
                    211: void recycle_deleted_storage ()
                    212: {
                    213:     Pexpr fce;
                    214:     if(expr::free) {
                    215:        for (fce = expr::free;fce->e1;fce = fce->e1) ; /* just chase */
                    216:        fce->e1 = expr::new_free;
                    217:     } else expr::free = expr::new_free;
                    218:     expr::new_free = 0;
                    219: 
                    220:     Pstmt sce;
                    221:     if(stmt::free) {
                    222:        for (sce = stmt::free;sce->s_list;sce = sce->s_list) ; /* just chase */
                    223:        sce->s_list = stmt::new_free;
                    224:     } else stmt::free = stmt::new_free;
                    225:     stmt::new_free = 0;
                    226: 
                    227:     Pname nce;
                    228:     if(name::free) {
                    229:        for (nce = name::free;nce->n_tbl_list;nce = nce->n_tbl_list) ; /* just chase */
                    230:        nce->n_tbl_list = name::new_free;
                    231:     } else name::free = name::new_free;
                    232:     name::new_free = 0;
                    233: }
                    234: 
                    235: static char rcsinfo[] = "$Header: /usr3/lang/benson/work/stripped_cfront/RCS/alloc.c,v 1.1 89/11/20 08:50:09 benson Exp $";
                    236: 
                    237: 
                    238: /* $Log:       alloc.c,v $
                    239:  * Revision 1.1  89/11/20  08:50:09  benson
                    240:  * Initial revision
                    241:  * 
                    242:  * Revision 1.1  89/11/20  08:49:48  benson
                    243:  * Initial revision
                    244:  * 
                    245:  * Revision 1.1  89/11/20  08:49:25  benson
                    246:  * Initial revision
                    247:  * 
                    248:  * Revision 1.1  89/11/20  08:48:50  benson
                    249:  * Initial revision
                    250:  * 
                    251:  * Revision 1.6  89/10/05  11:37:40  benson
                    252:  * change free-list management. Deleted things aren't recycled ever
                    253:  * until the start of the next top level declaration.
                    254:  * 
                    255:  * Revision 1.5  89/09/02  22:08:56  benson
                    256:  * mark things deleted.
                    257:  * 
                    258:  * Revision 1.4  89/07/21  16:47:01  benson
                    259:  * add operator new/delete to expr, stmt, name that use free lists.
                    260:  * It speeds things up enough to be worthwhile.
                    261:  * 
                    262:  * Revision 1.3  89/07/20  17:42:10  benson
                    263:  * Rip out chunk allocator.
                    264:  * 
                    265:  * Revision 1.2  89/06/28  12:56:59  benson
                    266:  * Port of changes from 2.0beta6 odi bugfixes
                    267:  * 
                    268:    end_log
                    269: 
                    270: */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.