-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallocate.c
executable file
·186 lines (154 loc) · 4.57 KB
/
allocate.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* SRDISK - Disk memory allocation
* Copyright (C) 1992-1996, 2005 Marko Kohtala
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "srdisk.h"
#include <stdio.h>
#include "max.h"
/******************** allocation calculations ************************/
#ifdef __cplusplus
#define LINKAGE inline
#else
#define LINKAGE static
#endif
LINKAGE dword adjust(dword a, word u)
{
word mod = a % u;
return mod ? u - mod + a : a;
}
#if 0
LINKAGE dword maxa(dword a, dword b, word u)
{
return adjust(max(a,b), u);
}
#endif
LINKAGE dword mina(dword a, dword b, word u)
{
return adjust(min(a,b), u);
}
void calc_alloc(void)
{
int i;
long to_alloc;
int method = 0; /* Method to collect memory */
do {
/* Adjust drivers allocation not to exceed the maxK without making any
drivers memory larger. This is assumed safe, since we expect memory
can always be freed without fear of failure. */
to_alloc = newf.size;
for (i = 0; i < f.chain_len; i++) {
newf.subconf[i].size =
mina(min(f.subconf[i].size, newf.subconf[i].maxK),
to_alloc,
f.subconf[i].conf->allocblock);
to_alloc -= newf.subconf[i].size;
}
/* If memory needs are not filled by now, then see where space can be
safely allocated more */
if (to_alloc > 0) {
for (i = 0; i < f.chain_len && to_alloc > 0; i++) {
if (!f.subconf[i].alloc_best) {
/* This is to avoid double calls due to min() definition */
dword free = method == 0 ? safe_size(f.subconf[i].conf)
: max_size(f.subconf[i].conf);
free = min(free, newf.subconf[i].maxK);
if (newf.subconf[i].size < newf.subconf[i].maxK
&& newf.subconf[i].size < free)
{
dword alloc = mina(to_alloc, free - f.subconf[i].size,
f.subconf[i].conf->allocblock);
newf.subconf[i].size += alloc;
to_alloc -= alloc;
}
}
}
}
method++;
} while(to_alloc > 0 && method < 2);
if (to_alloc < 0) {
/* If too much memory allocated, see if some part has small enough
allocation unit to be freed to allocate only so much memory as is
needed. */
int shrunk;
do {
shrunk = 0;
for (i = 0; i < f.chain_len; i++) {
if (f.subconf[i].conf->allocblock < -to_alloc) {
dword shrink = -to_alloc / f.subconf[i].conf->allocblock;
newf.subconf[i].size -= shrink;
to_alloc += shrink;
shrunk = 1;
}
}
} while(shrunk);
}
newf.current_size = newf.size - to_alloc;
}
/****************** The allocation itself *********************/
extern void ConfigMaxAlloc(void);
dword AllocMem(int i)
{
dword lastalloc;
lastalloc = disk_alloc(f.subconf[i].conf, newf.subconf[i].size);
if (f.subconf[i].size != lastalloc)
disk_bad = 1;
f.current_size += lastalloc - f.subconf[i].size;
f.subconf[i].size = newf.subconf[i].size = lastalloc;
return lastalloc;
}
void set_sectors(void)
{
struct config_s far *subconf;
for(subconf = conf; subconf; subconf = conf_ptr(subconf->next)) {
subconf->sectors = subconf->size * 1024 / f.bps;
}
}
void DiskAllocate(void)
/* Error: Can not allocate enough -> fatal
*/
{
long lastalloc;
long Kleft = 0;
int i;
int alloc_tries;
enum disk_repair_e old_disk_repair = disk_repair;
ConfigMaxAlloc();
if (disk_repair > dr_clear)
disk_repair = dr_clear; /* In case of error, try to make a clear disk */
for(alloc_tries = f.chain_len; alloc_tries; alloc_tries--) {
calc_alloc(); /* Calculate newf.subconf[].size */
if (newf.current_size < newf.size)
fatal("Not enough memory available");
data_on_disk = 0;
Kleft = newf.size;
for(i = 0; i < f.chain_len; i++) {
dword origsize = f.subconf[i].size;
dword destsize = newf.subconf[i].size;
if (origsize != destsize) {
lastalloc = AllocMem(i);
if (lastalloc != origsize) {
disk_bad = 1;
}
if (lastalloc != destsize) {
f.subconf[i].alloc_best = 1;
goto new_try;
}
Kleft -= lastalloc;
}
else {
Kleft -= f.subconf[i].size;
}
}
break;
new_try:;
}
if (Kleft > 0) { /* If not enough memory could be allocated */
fatal("Failed to allocate memory");
}
else if (Kleft < 0 && verbose > 2)
printf("%ld Kbytes extra allocated, "
"perhaps you should make your disk that much larger.\n"
,-Kleft);
set_sectors();
disk_repair = old_disk_repair;
}