1: /* @(#)malloc.c 2.1 SCCS id keyword */
2: #ifdef debug
3: #define ASSERT(p) if(!(p))botch("p");else
4: botch(s)
5: char *s;
6: {
7: printf("assertion botched: %s\n",s);
8: abort();
9: }
10: #else
11: #define ASSERT(p)
12: #endif
13:
14: /* avoid break bug */
15: #ifdef pdp11
16: #define GRANULE 64
17: #else
18: #define GRANULE 0
19: #endif
20: /* C storage allocator
21: * circular first-fit strategy
22: * works with noncontiguous, but monotonically linked, arena
23: * each block is preceded by a ptr to the (pointer of)
24: * the next following block
25: * blocks are exact number of words long
26: * aligned to the data type requirements of ALIGN
27: * pointers to blocks must have BUSY bit 0
28: * bit in ptr is 1 for busy, 0 for idle
29: * gaps in arena are merely noted as busy blocks
30: * last block of arena (pointed to by alloct) is empty and
31: * has a pointer to first
32: * idle blocks are coalesced during space search
33: *
34: * a different implementation may need to redefine
35: * ALIGN, NALIGN, BLOCK, BUSY, INT
36: * where INT is integer type to which a pointer can be cast
37: */
38: #define INT int
39: #define ALIGN int
40: #define NALIGN 1
41: #define WORD sizeof(union store)
42: #define BLOCK 1024 /* a multiple of WORD*/
43: #define BUSY 1
44: #define NULL 0
45: #define testbusy(p) ((INT)(p)&BUSY)
46: #define setbusy(p) (union store *)((INT)(p)|BUSY)
47: #define clearbusy(p) (union store *)((INT)(p)&~BUSY)
48:
49: union store { union store *ptr;
50: ALIGN dummy[NALIGN];
51: int calloc; /*calloc clears an array of integers*/
52: };
53:
54: static union store allocs[2]; /*initial arena*/
55: static union store *allocp; /*search ptr*/
56: static union store *alloct; /*arena top*/
57: static union store *allocx; /*for benefit of realloc*/
58: char *sbrk();
59:
60: char *
61: malloc(nbytes)
62: unsigned nbytes;
63: {
64: register union store *p, *q;
65: register nw;
66: static temp; /*coroutines assume no auto*/
67:
68: if(allocs[0].ptr==0) { /*first time*/
69: allocs[0].ptr = setbusy(&allocs[1]);
70: allocs[1].ptr = setbusy(&allocs[0]);
71: alloct = &allocs[1];
72: allocp = &allocs[0];
73: }
74: nw = (nbytes+WORD+WORD-1)/WORD;
75: ASSERT(allocp>=allocs && allocp<=alloct);
76: ASSERT(allock());
77: for(p=allocp; ; ) {
78: for(temp=0; ; ) {
79: if(!testbusy(p->ptr)) {
80: while(!testbusy((q=p->ptr)->ptr)) {
81: ASSERT(q>p&&q<alloct);
82: p->ptr = q->ptr;
83: }
84: if(q>=p+nw && p+nw>=p)
85: goto found;
86: }
87: q = p;
88: p = clearbusy(p->ptr);
89: if(p>q)
90: ASSERT(p<=alloct);
91: else if(q!=alloct || p!=allocs) {
92: ASSERT(q==alloct&&p==allocs);
93: return(NULL);
94: } else if(++temp>1)
95: break;
96: }
97: temp = ((nw+BLOCK/WORD)/(BLOCK/WORD))*(BLOCK/WORD);
98: q = (union store *)sbrk(0);
99: if(q+temp+GRANULE < q) {
100: return(NULL);
101: }
102: q = (union store *)sbrk(temp*WORD);
103: if((INT)q == -1) {
104: return(NULL);
105: }
106: ASSERT(q>alloct);
107: alloct->ptr = q;
108: if(q!=alloct+1)
109: alloct->ptr = setbusy(alloct->ptr);
110: alloct = q->ptr = q+temp-1;
111: alloct->ptr = setbusy(allocs);
112: }
113: found:
114: allocp = p + nw;
115: ASSERT(allocp<=alloct);
116: if(q>allocp) {
117: allocx = allocp->ptr;
118: allocp->ptr = p->ptr;
119: }
120: p->ptr = setbusy(allocp);
121: return((char *)(p+1));
122: }
123:
124: /* freeing strategy tuned for LIFO allocation
125: */
126: free(ap)
127: register char *ap;
128: {
129: register union store *p = (union store *)ap;
130:
131: ASSERT(p>clearbusy(allocs[1].ptr)&&p<=alloct);
132: ASSERT(allock());
133: allocp = --p;
134: ASSERT(testbusy(p->ptr));
135: p->ptr = clearbusy(p->ptr);
136: ASSERT(p->ptr > allocp && p->ptr <= alloct);
137: }
138:
139: /* realloc(p, nbytes) reallocates a block obtained from malloc()
140: * and freed since last call of malloc()
141: * to have new size nbytes, and old content
142: * returns new location, or 0 on failure
143: */
144:
145: char *
146: realloc(p, nbytes)
147: register union store *p;
148: unsigned nbytes;
149: {
150: register union store *q;
151: union store *s, *t;
152: register unsigned nw;
153: unsigned onw;
154:
155: if(testbusy(p[-1].ptr))
156: free((char *)p);
157: onw = p[-1].ptr - p;
158: q = (union store *)malloc(nbytes);
159: if(q==NULL || q==p)
160: return((char *)q);
161: s = p;
162: t = q;
163: nw = (nbytes+WORD-1)/WORD;
164: if(nw<onw)
165: onw = nw;
166: while(onw--!=0)
167: *t++ = *s++;
168: if(q<p && q+nw>=p)
169: (q+(q+nw-p))->ptr = allocx;
170: return((char *)q);
171: }
172:
173: #ifdef debug
174: allock()
175: {
176: #ifdef longdebug
177: register union store *p;
178: int x;
179: x = 0;
180: for(p= &allocs[0]; clearbusy(p->ptr) > p; p=clearbusy(p->ptr)) {
181: if(p==allocp)
182: x++;
183: }
184: ASSERT(p==alloct);
185: return(x==1|p==allocp);
186: #else
187: return(1);
188: #endif
189: }
190: #endif
Defined functions
botch
defined in line
4; used 1 times
free
defined in line
126; used 111 times
- in line 156
- in /usr/include/ape.h line
11-13(2)
- in /usr/include/macros.h line
64
- in /usr/include/mp.h line
6-11(3)
- in /usr/src/cmd/awk/tran.c line
63,
230
- in /usr/src/cmd/col.c line
214,
302
- in /usr/src/cmd/cron.c line
157,
169
- in /usr/src/cmd/cu.3451A.c line
1006
- in /usr/src/cmd/cu.c line
869
- in /usr/src/cmd/dc/dc.c line
1759,
1792,
1871-1872(2),
1891
- in /usr/src/cmd/dcheck.c line
128
- in /usr/src/cmd/diff/diff.c line
189-190(2)
- in /usr/src/cmd/diff/diffreg.c line
139-145(4),
465
- in /usr/src/cmd/ed.c line
747
- in /usr/src/cmd/icheck.c line
193,
206
- in /usr/src/cmd/ls/symlnk_ucbls.c line
987
- in /usr/src/cmd/ls/ucbls.c line
956
- in /usr/src/cmd/m4/m4.c line
497,
517-519(3)
- in /usr/src/cmd/nm.c line
248
- in /usr/src/cmd/ps/ps.c line
773
- in /usr/src/cmd/pstat.c line
197,
262,
339,
574
- in /usr/src/cmd/ratfor/r1.c line
260
- in /usr/src/cmd/ratfor/rlook.c line
54
- in /usr/src/cmd/refer/refer/glue1.c line
51-54(2)
- in /usr/src/cmd/refer/util/glue1.c line
121-124(2)
- in /usr/src/cmd/refer/util/hunt2.c line
232-233(2)
- in /usr/src/cmd/struct/0.alloc.c line
31,
82
- in /usr/src/cmd/struct/0.parts.c line
108
- in /usr/src/cmd/struct/beauty.y line
77-90(5),
98,
168,
175-185(3),
195,
202-208(3),
227-228(2),
408-410(2)
- in /usr/src/cmd/struct/tree.c line
35-36(2),
74,
88
- in /usr/src/cmd/uucp/LIBNDIR/closedir.c line
18
- in /usr/src/cmd/uucp/anlwrk.c line
322
- in /usr/src/cmd/uucp/gnsys.c line
83
- in /usr/src/cmd/uucp/ulockf.c line
114
- in /usr/src/lib/ape/ape.h line
11-13(2)
- in /usr/src/lib/ape/mout.c line
58
- in /usr/src/lib/c/gen/calloc.c line
32
- in /usr/src/lib/c/stdio/flsbuf.c line
122
- in /usr/src/lib/c/stdio/setbuf.c line
8
- in /usr/src/lib/libI77/close.c line
43
- in /usr/src/lib/libI77/lread.c line
328
- in /usr/src/lib/libI77/old_io.c line
430
- in /usr/src/lib/mp/mout.c line
76
- in /usr/src/ucb/libndir/closedir.c line
16
- in /usr/src/ucb/lpr/src/lpd.c line
206
- in /usr/src/ucb/lpr/src/ulf.c line
318-319(2)
- in /usr/src/ucb/pascal/pi/case.c line
154
- in /usr/src/ucb/pascal/pi/nl.c line
353
- in /usr/src/ucb/pascal/pi/tree.c line
161
- in /usr/src/ucb/pascal/pxp/nl.c line
333
- in /usr/src/ucb/pascal/pxp/tree.c line
158
- in /usr/src/ucb/pascal/pxp/yyget.c line
293
- in /usr/src/ucb/pwhash/src/cmd/ls.c line
893
- in /usr/src/ucb/sendmail/lib/libndir/closedir.c line
18
- in /usr/src/ucb/sendmail/src/clock.c line
107,
160
- in /usr/src/ucb/sendmail/src/deliver.c line
448,
950
- in /usr/src/ucb/sendmail/src/headers.c line
129
- in /usr/src/ucb/sendmail/src/queue.c line
290-291(2),
328-329(2)
malloc
defined in line
60; used 217 times
- in line 158
- in /usr/include/macros.h line
65
- in /usr/include/sys/systm.h line
84
- in /usr/src/cmd/awk/awk.def line
50
- in /usr/src/cmd/awk/b.c line
275,
473
- in /usr/src/cmd/awk/main.c line
116,
134
- in /usr/src/cmd/awk/parse.c line
6
- in /usr/src/cmd/awk/run.c line
319,
515
- in /usr/src/cmd/awk/tran.c line
42,
85,
220,
233-237(3)
- in /usr/src/cmd/cc.c line
490,
498
- in /usr/src/cmd/col.c line
210-215(2)
- in /usr/src/cmd/cron.c line
21,
160
- in /usr/src/cmd/dc/dc.c line
886,
1675-1678(2),
1692-1695(2),
1873,
1933-1935(2)
- in /usr/src/cmd/dc/dc.h line
114
- in /usr/src/cmd/dcheck.c line
35,
97
- in /usr/src/cmd/diff/diff.c line
143,
177,
191
- in /usr/src/cmd/diff/diff.h line
81
- in /usr/src/cmd/diff/diffdir.c line
119,
188
- in /usr/src/cmd/diff/diffh.c line
26,
43
- in /usr/src/cmd/diff/diffreg.c line
109
- in /usr/src/cmd/diffh.c line
24,
41
- in /usr/src/cmd/ed.c line
97,
145
- in /usr/src/cmd/eqn/lex.c line
189-192(2)
- in /usr/src/cmd/eqn/lookup.c line
184,
199
- in /usr/src/cmd/expr.y line
70,
141,
197,
211,
222-224(2)
- in /usr/src/cmd/icheck.c line
55,
162
- in /usr/src/cmd/learn/learn.c line
12
- in /usr/src/cmd/ls/ls.c line
331,
338
- in /usr/src/cmd/ls/symlnk_ls.c line
340,
349,
461
- in /usr/src/cmd/ls/symlnk_ucbls.c line
735,
1471
- in /usr/src/cmd/ls/ucbls.c line
717
- in /usr/src/cmd/m4/m4.c line
103,
486,
527
- in /usr/src/cmd/neqn/lex.c line
189-192(2)
- in /usr/src/cmd/neqn/lookup.c line
184,
199
- in /usr/src/cmd/nm.c line
35,
197-199(2)
- in /usr/src/cmd/ps/ps.c line
86,
579
- in /usr/src/cmd/quot.c line
47,
235
- in /usr/src/cmd/ratfor/r.h line
61
- in /usr/src/cmd/ratfor/r1.c line
184,
237
- in /usr/src/cmd/ratfor/rlook.c line
14,
46,
64
- in /usr/src/cmd/savecore.c line
56,
122,
265
- in /usr/src/cmd/struct/0.alloc.c line
17,
93
- in /usr/src/cmd/struct/beauty.y line
304
- in /usr/src/cmd/struct/tree.c line
12-16(2),
75,
89,
95
- in /usr/src/cmd/tar.c line
82,
527
- in /usr/src/cmd/tsort.c line
32,
61,
120-121(2)
- in /usr/src/cmd/uucp/4.2/pk1.c line
2,
45
- in /usr/src/cmd/uucp/4.2/uucp.h line
267
- in /usr/src/cmd/uucp/DIALUP/uucp.h line
269
- in /usr/src/cmd/uucp/LIBNDIR/opendir.c line
24
- in /usr/src/cmd/uucp/anlwrk.c line
323
- in /usr/src/cmd/uucp/chkpth.c line
118
- in /usr/src/cmd/uucp/pk0.c line
5
- in /usr/src/cmd/uucp/pk1.c line
5,
48
- in /usr/src/cmd/uucp/uucp.h line
268
- in /usr/src/cmd/uucp/uuxqt.c line
116
- in /usr/src/cmd/xsend/xsend.c line
28
- in /usr/src/games/snake/snscore.c line
7,
49
- in /usr/src/lib/ape/mout.c line
13,
30
- in /usr/src/lib/ape/util.c line
23-25(2)
- in /usr/src/lib/c/gen/calloc.c line
12-17(2)
- in /usr/src/lib/c/stdio/filbuf.c line
3,
21
- in /usr/src/lib/c/stdio/flsbuf.c line
3,
55
- in /usr/src/lib/curses/newwin.c line
10
- in /usr/src/lib/libI77/lread.c line
330
- in /usr/src/lib/mp/mout.c line
64
- in /usr/src/lib/mp/util.c line
3,
23
- in /usr/src/local/seqn/lex.c line
191-194(2)
- in /usr/src/local/seqn/lookup.c line
278,
293
- in /usr/src/sys/autoconfig/attach.c line
119,
134
- in /usr/src/sys/autoconfig/main.c line
79
- in /usr/src/sys/autoconfig/read_dtab.c line
91,
139
- in /usr/src/ucb/checknr.c line
131,
159-161(2),
503
- in /usr/src/ucb/chfn.c line
403-406(2)
- in /usr/src/ucb/ctags.c line
229-233(2),
701
- in /usr/src/ucb/delivermail/util.c line
63-65(2)
- in /usr/src/ucb/finger.c line
124,
222,
236,
268,
287,
294,
316-325(3),
360,
374,
441,
457,
498-505(4),
878,
892,
898,
904,
919,
941,
950,
971,
1364-1367(2)
- in /usr/src/ucb/last.c line
171
- in /usr/src/ucb/libndir/opendir.c line
18
- in /usr/src/ucb/lpr/src/lpd.c line
74,
147
- in /usr/src/ucb/lpr/src/lpq.c line
148
- in /usr/src/ucb/lpr/src/lpr.c line
58,
512
- in /usr/src/ucb/lpr/src/lprm.c line
50-53(2),
204
- in /usr/src/ucb/lpr/src/ulf.c line
34,
234-239(2)
- in /usr/src/ucb/pascal/pi/case.c line
88
- in /usr/src/ucb/pascal/pi/nl.c line
619-622(2)
- in /usr/src/ucb/pascal/pi/osubr.c line
119
- in /usr/src/ucb/pascal/pi/string.c line
77
- in /usr/src/ucb/pascal/pi/subr.c line
119
- in /usr/src/ucb/pascal/pi/tree.c line
120
- in /usr/src/ucb/pascal/pxp/nl.c line
694
- in /usr/src/ucb/pwhash/src/cmd/ls.c line
704
- in /usr/src/ucb/sendmail/aux/vacation.c line
349
- in /usr/src/ucb/sendmail/include/useful.h line
51
- in /usr/src/ucb/sendmail/lib/libndir/opendir.c line
20
- in /usr/src/ucb/sendmail/src/util.c line
135
- in /usr/src/ucb/users.c line
4,
42
- in /usr/src/ucb/vgrind/regexp.c line
97,
110
- in /usr/src/ucb/vsh/cmdload.c line
35,
76-80(2),
91
- in /usr/src/ucb/w.c line
447
realloc
defined in line
145; used 31 times
- in /usr/src/cmd/cron.c line
22,
158,
170
- in /usr/src/cmd/dc/dc.c line
1760-1764(3),
1793-1797(3),
1875
- in /usr/src/cmd/dc/dc.h line
116
- in /usr/src/cmd/diff/diff.c line
188-192(2)
- in /usr/src/cmd/ed.c line
98,
748
- in /usr/src/cmd/nm.c line
36,
203-205(2)
- in /usr/src/cmd/ps/ps.c line
86,
580
- in /usr/src/cmd/struct/0.parts.c line
107
- in /usr/src/lib/ape/shift.c line
17,
24,
62,
74
- in /usr/src/lib/libI77/lread.c line
341
- in /usr/src/ucb/lpr/src/lpd.c line
74,
159
- in /usr/src/ucb/lpr/src/lpq.c line
160
- in /usr/src/ucb/lpr/src/lprm.c line
51,
219
Defined variables
allocp
defined in line
55; used 14 times
allocs
defined in line
54; used 13 times
alloct
defined in line
56; used 17 times
Defined union's
store
defined in line
49; used 30 times
Defined macros
ALIGN
defined in line
39; used 1 times
ASSERT
defined in line
11; used 12 times
BLOCK
defined in line
42; used 3 times
BUSY
defined in line
43; used 3 times
INT
defined in line
38; used 4 times
NULL
defined in line
44; used 4 times
WORD
defined in line
41; used 9 times