Line | |
---|
1 | |
---|
2 | typedef struct header |
---|
3 | { struct header *ptr; |
---|
4 | unsigned size; |
---|
5 | } HEADER; |
---|
6 | |
---|
7 | HEADER base,*allocp,*heap; |
---|
8 | |
---|
9 | char *malloc(s) |
---|
10 | unsigned s; |
---|
11 | {HEADER *p,*q; |
---|
12 | int nunits; |
---|
13 | nunits = 1 + (s + sizeof(HEADER) - 1) / sizeof(HEADER); |
---|
14 | if ((q = allocp) == NULL) |
---|
15 | { base.ptr = allocp = q = &base; |
---|
16 | base.size = 0; |
---|
17 | } |
---|
18 | for (p = q->ptr; ; q = p,p = p->ptr) |
---|
19 | { if (p->size >= nunits) |
---|
20 | { if (p->size == nunits) |
---|
21 | q->ptr = p->ptr; |
---|
22 | else |
---|
23 | { p->size -= nunits; |
---|
24 | p += p->size; |
---|
25 | p->size = nunits; |
---|
26 | } |
---|
27 | allocp = q; |
---|
28 | clearblock(p); |
---|
29 | return ((char *)(p + 1)); |
---|
30 | } |
---|
31 | if (p == allocp) |
---|
32 | if ((p = morecore(nunits)) == NULL) |
---|
33 | return(NULL); |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | clearblock(p) |
---|
38 | HEADER *p; |
---|
39 | {char *s,*t; |
---|
40 | s = (char *)(p + 1); |
---|
41 | t = (char *)(p + p->size); |
---|
42 | while (s < t) *s++ = 0; |
---|
43 | } |
---|
44 | |
---|
45 | #define NALLOC 128 |
---|
46 | |
---|
47 | HEADER *morecore(nu) |
---|
48 | unsigned nu; |
---|
49 | {char *cp; |
---|
50 | HEADER *up; |
---|
51 | int rnu; |
---|
52 | rnu = NALLOC * ((nu + NALLOC - 1) / NALLOC); |
---|
53 | cp = sbrk(rnu * sizeof(HEADER)); |
---|
54 | if ((int)cp == -1) return NULL; |
---|
55 | up = (HEADER *) cp; |
---|
56 | up->size = rnu; |
---|
57 | mfree((char *)(up+1)); |
---|
58 | return allocp; |
---|
59 | } |
---|
60 | |
---|
61 | #asm |
---|
62 | sbrk PSHS U |
---|
63 | LEAU ,S |
---|
64 | |
---|
65 | LDD heap,Y |
---|
66 | BNE _mc0 |
---|
67 | BSR initheap |
---|
68 | _mc0 PSHS D |
---|
69 | TFR S,D |
---|
70 | SUBD ,S++ |
---|
71 | CMPD 4,U |
---|
72 | BCC _mc1 |
---|
73 | LDD #-1 |
---|
74 | LEAS ,U |
---|
75 | PULS U,PC |
---|
76 | |
---|
77 | _mc1 LDD 4,U |
---|
78 | LDX heap,Y |
---|
79 | LEAX D,X |
---|
80 | LDD heap,Y |
---|
81 | STX heap,Y |
---|
82 | LEAS ,U |
---|
83 | PULS U,PC |
---|
84 | |
---|
85 | initheap |
---|
86 | PSHS U |
---|
87 | LEAU ,S |
---|
88 | TFR Y,D |
---|
89 | ADDD #_GLOBALS |
---|
90 | STD heap,Y |
---|
91 | LEAS ,U |
---|
92 | PULS U,PC |
---|
93 | #endasm |
---|
94 | |
---|
95 | mfree(ap) |
---|
96 | char *ap; |
---|
97 | {HEADER *p,*q; |
---|
98 | p = (HEADER *)ap - 1; |
---|
99 | for (q = allocp; !(p > q && p < q->ptr); q = q->ptr) |
---|
100 | if (q >= q->ptr && (p > q || p < q->ptr)) break; |
---|
101 | if (p + p->size == q->ptr) |
---|
102 | { p->size += q->ptr->size; |
---|
103 | p->ptr = q->ptr->ptr; |
---|
104 | } |
---|
105 | else p->ptr = q->ptr; |
---|
106 | if (q + q->size == p) |
---|
107 | { q->size += p->size; |
---|
108 | q->ptr = p->ptr; |
---|
109 | } |
---|
110 | else q->ptr = p; |
---|
111 | allocp = q; |
---|
112 | } |
---|
113 | |
---|
114 | unsigned freesize() |
---|
115 | {int i; |
---|
116 | if (!heap) initheap(); |
---|
117 | return ((char *)&i - (char *)heap); |
---|
118 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.