1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include <stdio.h>
23 #include <alloc.h>
24
25 char *wsalloc (mptr,nbytes)
26
27 struct allocstr *mptr;
28 unsigned nbytes;
29 {
30 HEADER *morecore();
31 register HEADER *p, *q;
32 register int nunits;
33
34 nunits = 1+(nbytes+sizeof(HEADER) -1)/sizeof (HEADER);
35 if ((q = mptr->m_allocp) == NULL) {
36 mptr->m_base.s.ptr = mptr->m_allocp =q = &mptr->m_base;
37 mptr->m_base.s.size = 0;
38 }
39
40 for (p=q->s.ptr; ;q=p, p=p->s.ptr) {
41 if (p->s.size >= nunits) {
42 if (p->s.size == nunits)
43 q->s.ptr = p->s.ptr;
44 else {
45 p->s.size -= nunits;
46 p += p->s.size;
47 p->s.size = nunits;
48 }
49 mptr->m_allocp = q;
50 return ((char *) (p+1));
51 }
52 if ( p == mptr->m_allocp)
53 if ((p = morecore (mptr,nunits)) == NULL)
54 return (NULL);
55 }
56 }
57
58 wsfree (mptr,ap)
59
60 struct allocstr *mptr;
61 char *ap;
62 {
63 register HEADER *p, *q;
64
65 p = (HEADER *) ap -1;
66 for (q=mptr->m_allocp; !(p > q && p < q->s.ptr); q=q->s.ptr)
67 if (q >= q->s.ptr && (p > q || p < q->s.ptr))
68 break;
69
70 if (p+p->s.size == q->s.ptr) {
71 p->s.size += q->s.ptr->s.size;
72 p->s.ptr = q->s.ptr->s.ptr;
73 }
74 else
75 p->s.ptr = q->s.ptr;
76 if (q+q->s.size == p) {
77 q->s.size += p->s.size;
78 q->s.ptr = p->s.ptr;
79 }
80 else
81 q->s.ptr = p;
82 mptr->m_allocp = q;
83 }
84
85 HEADER *morecore (mptr,nu)
86
87 struct allocstr *mptr;
88 unsigned nu;
89 {
90 register HEADER *up;
91 char *cp;
92
93 if (mptr->memory_used == 1)
94 return(NULL);
95 cp = mptr->memory;
96 mptr->memory_used = 1;
97 up = (HEADER *)cp;
98 up->s.size = mptr->memory_size / sizeof (HEADER);
99 wsfree (mptr,(char *) (up+1));
100 return (mptr->m_allocp);
101 }
102 ^Z