summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/awk/zmalloc.c
blob: 1a38d9c50e66885a8aff85ac1f5f2befca99969c (plain)
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

/********************************************
zmalloc.c
copyright 1991, Michael D. Brennan

This is a source file for mawk, an implementation of
the AWK programming language.

Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/

/*$Log: zmalloc.c,v $
/*Revision 1.2  1993/07/02 23:58:03  jtc
/*Updated to mawk 1.1.4
/*
 * Revision 5.1.1.1  1993/02/06  11:12:19  mike
 * fix bug in reuse of parser table memory
 * for most users ifdef the mess out
 *
 * Revision 5.1  1991/12/05  07:56:35  brennan
 * 1.1 pre-release
 *
*/

/*  zmalloc.c  */
#include  "mawk.h"
#include  "zmalloc.h"

void PROTO( mawk_exit, (int) ) ;

extern  struct yacc_mem  *yacc_memp ;

/*
  zmalloc() gets mem from malloc() in CHUNKS of 2048 bytes
  and cuts these blocks into smaller pieces that are multiples
  of eight bytes.  When a piece is returned via zfree(), it goes
  on a linked linear list indexed by its size.  The lists are
  an array, pool[].

  E.g., if you ask for 22 bytes with p = zmalloc(22), you actually get
  a piece of size 24.  When you free it with zfree(p,22) , it is added
  to the list at pool[2].
*/

#define POOLSZ      16

#define  CHUNK          256    
        /* number of blocks to get from malloc */

static PTR  PROTO( emalloc, (unsigned) ) ;
void PROTO( errmsg, (int , char *, ...) ) ;

static PTR emalloc(size)
  unsigned size ;
{ PTR p ;
  static char out[] = "out of memory" ;

  if( !(p = (PTR) malloc(SIZE_T(size))) )
	if ( mawk_state == EXECUTION ) rt_error(out) ;
	else /* I don't think this will ever happen */
	{ compile_error(out) ; mawk_exit(1) ; }
  return p ;
}


typedef  union  zblock {
char dummy[ZBLOCKSZ] ;
union zblock *link ;
}  ZBLOCK  ;

/* ZBLOCKS of sizes 1, 2, ... 16
   which is bytes of sizes 8, 16, ... , 128
   are stored on the linked linear lists in
   pool[0], pool[1], ... , pool[15]
*/

static  ZBLOCK  *pool[POOLSZ] ;

PTR   bmalloc( blocks )
  register unsigned blocks ;
{ 
  register ZBLOCK *p ;
  static  unsigned amt_avail ;
  static  ZBLOCK  *avail ;

  if ( blocks > POOLSZ )  return emalloc(blocks<<ZSHIFT) ;

  if ( p = pool[blocks-1] )
  { pool[blocks-1] = p->link ; return (PTR) p ; }

  if ( blocks > amt_avail )
  { 
    if ( amt_avail != 0 ) /* free avail */
    { 
      avail->link = pool[--amt_avail] ;
      pool[amt_avail] = avail ;
    }

#if MSDOS || HAVE_SMALL_MEMORY
/* this hack is dangerous (I've blown it twice), not portable,
   and counts on byacc not changing, but it is a big win on
   DOS.  On paged vmem systems it is a nop so ifdef it out.
*/
    /* use parser tables first */
    if ( yacc_memp->zblocks > blocks )
    { 
      avail = (ZBLOCK *) yacc_memp->mem ;
      amt_avail = yacc_memp++ -> zblocks ;
      /* make sure its -- aligned */
      {
	int k = (int) avail & 7 ;
        if ( k )
        { 
	  avail = (ZBLOCK*)((char *)avail + (8-k)) ;
	  amt_avail-- ;
	}
      }
    }
    else
#endif

    if ( !(avail = (ZBLOCK *) malloc(SIZE_T(CHUNK*ZBLOCKSZ))) )
    { /* if we get here, almost out of memory */
        amt_avail = 0 ;   
	return  emalloc(blocks << ZSHIFT) ;
    }
    else  amt_avail = CHUNK ;
  }
  
  /* get p from the avail pile */
  p = avail ; avail += blocks ; amt_avail -= blocks ; 
  return (PTR) p ;
}

void  bfree( p, blocks)
  register PTR p ;  
  register unsigned blocks ;
{ 

  if ( blocks > POOLSZ )  free(p) ;
  else
  {
    ((ZBLOCK *)p)->link = pool[--blocks] ;
    pool[blocks] = (ZBLOCK *) p ;
  }
}

PTR  zrealloc( p, old_size, new_size )
  register PTR  p ;
  unsigned old_size, new_size ;
{ register PTR q ;

  (void) memcpy(q = zmalloc(new_size), p, 
                SIZE_T(old_size < new_size ? old_size : new_size)) ;
 
  zfree(p, old_size) ;
  return q ;
}