blob: 143d324a65bda5f3fc4f3dd72a3949dd79f735f8 (
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
|
/* simplified linux malloc.h
Copyright (C) 1995 Joel N. Weber II
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <malloc.h>
typedef struct __malloc_struct malloc_struct;
typedef union __malloc_union
{
char *c;
malloc_struct *m;
} malloc_union;
struct __malloc_struct
{
unsigned char status;
#define ALLOC 0x53
#define FREE 0x55
#define END_OF_WORLD 0x57
malloc_union next;
} *malloc_start;
extern int __STACKSIZE;
/* WARNING: this init will only work if there is a hard limit on the
amount of RAM that can be allocated.
*/
#ifdef __AS386_16__
#asm
loc 1 ! Make sure the pointer is in the correct segment
auto_func: ! Label for bcc -M to work.
.word _malloc_init ! Pointer to the autorun function
.word no_op ! Space filler cause segs are padded to 4 bytes.
.text ! So the function after is also in the correct seg.
#endasm
#endif
malloc_init()
{
extern unsigned int sbrk();
unsigned int ptr, sz, count;
malloc_start = (malloc_struct*) ((sbrk(16)+1)&~1);
malloc_start->status = FREE;
count=254;
for(sz=16384; sz>64; )
{
ptr= sbrk(sz);
if( ptr == (unsigned)-1 ) sz>>=1;
else count+=sz;
}
if( __STACKSIZE > count || __STACKSIZE <= 0 ) __STACKSIZE = ((count>>1)&-2);
ptr = sbrk(-__STACKSIZE);
malloc_start->next.m = ((malloc_struct*)ptr) - 1;
malloc_start->next.m->status = END_OF_WORLD;
}
char *malloc(size)
size_t size;
{
register malloc_union tmp, tmp2;
/* Make sure we don't lose the alignment */
size = (size+sizeof(malloc_struct)-1)/sizeof(malloc_struct);
tmp.m = malloc_start;
while ( ( tmp.m->next.m - tmp.m - 2 ) < size
|| ( tmp.m->status == ALLOC ))
tmp.m = tmp.m->next.m;
if (tmp.m->status == FREE){
tmp2.m = size + tmp.m + 1;
tmp2.m->status = FREE;
tmp2.m->next.c = tmp.m->next.c;
tmp.m->status = ALLOC;
tmp.m->next.c = tmp2.c;
}
else return 0;
tmp.m++;
return tmp.c;
}
__malloc_cleanup() /* finds consecutive free blocks and joins them */
{
malloc_struct *tmp;
tmp = malloc_start;
while ((tmp->status != END_OF_WORLD)&&(tmp->next.m->status != END_OF_WORLD)){
if ((tmp->status==FREE)&&(tmp->next.m->status==FREE))
tmp->next.m = tmp->next.m->next.m;
else tmp = tmp->next.m;
}
}
free(what)
char *what;
{
malloc_union tmp;
tmp.c = what; tmp.m--;
if( tmp.m->status == ALLOC )
{
tmp.m->status = FREE;
__malloc_cleanup;
}
}
|