-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocation.c
192 lines (181 loc) · 6.14 KB
/
allocation.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
186
187
188
189
190
191
/*
Copyright 2009 Enno Ruijters
This program is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General
Public License as published by the Free Software Foundation.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <malloc.h>
#include <string.h>
#include <obstack.h>
#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
#include "e2defrag.h"
struct allocation *copy_allocation(struct allocation *old)
{
struct allocation *ret;
size_t size;
size = sizeof(struct allocation);
size += old->extent_count * sizeof(struct data_extent);
ret = malloc(size);
if (!ret)
return NULL;
memcpy(ret, old, size);
return ret;
}
/* Will never free or realloc the old allocation. Never call this with an
* allocation already on the trees, as we will overwrite the old extent
* without updating the trees if we can merge.
*/
void alloc_move_extent(struct allocation *alloc, struct data_extent *extent,
blk64_t new_start)
{
e2_blkcnt_t nblocks;
nblocks = extent->end_block - extent->start_block + 1;
extent->start_block = new_start;
extent->end_block = new_start + nblocks - 1;
if (extent != alloc->extents) {
struct data_extent *prev_extent = extent - 1;
if (new_start == prev_extent->end_block + 1
&& extent->uninit == prev_extent->uninit)
{
size_t count, nbytes;
prev_extent->end_block = extent->end_block;
/* count = nr. of following extents in the array */
count = alloc->extent_count - (extent - alloc->extents);
count -= 1;
nbytes = count * sizeof(struct data_extent);
memmove(extent, extent + 1, nbytes);
extent = extent - 1;
alloc->extent_count--;
}
}
if (extent - alloc->extents != alloc->extent_count - 1) {
struct data_extent *next_extent = extent + 1;
if (extent->end_block + 1 == next_extent->start_block
&& extent->uninit == next_extent->uninit)
{
size_t count, nbytes;
extent->end_block = next_extent->end_block;
/* count = nr. of following extents in the array */
count = alloc->extent_count - (extent - alloc->extents);
nbytes = count * sizeof(struct data_extent);
memmove(next_extent, next_extent + 1, nbytes);
alloc->extent_count--;
}
}
}
int used_in_alloc(struct allocation *alloc, blk64_t range_start,
e2_blkcnt_t range_size)
{
int i;
blk64_t range_end = range_start + range_size - 1;
for (i = 0; i < alloc->extent_count; i++) {
if (range_end <= alloc->extents[i].end_block
&& range_start >= alloc->extents[i].start_block)
{
return 1;
}
}
return 0;
}
static void alloc_subtract_range(struct data_extent *range,
struct allocation *alloc,
struct obstack *mempool)
{
int i;
if (range->end_block < range->start_block)
return;
for (i = 0; i < alloc->extent_count; i++) {
if (range->start_block <= alloc->extents[i].end_block
&& (range->end_block >= alloc->extents[i].start_block))
{
/* Preceding range */
if (range->start_block < alloc->extents[i].start_block)
{
struct data_extent new;
new = *range;
new.end_block = alloc->extents[i].start_block;
new.end_block -= 1;
alloc_subtract_range(&new, alloc, mempool);
}
/* Following range */
if (range->end_block > alloc->extents[i].end_block) {
struct data_extent new;
new = *range;
new.start_block = alloc->extents[i].end_block;
new.start_block += 1;
alloc_subtract_range(&new, alloc, mempool);
}
return;
}
}
obstack_grow(mempool, range, sizeof(*range));
}
struct allocation *alloc_subtract(struct allocation *from,
struct allocation *data)
{
struct obstack mempool;
struct allocation *ret;
long size, i;
obstack_init(&mempool);
obstack_blank(&mempool, sizeof(struct allocation));
for (i = 0; i < from->extent_count; i++) {
struct data_extent *extent;
extent = &from->extents[i];
alloc_subtract_range(extent, data, &mempool);
}
size = obstack_object_size(&mempool);
ret = malloc(size);
if (!ret) {
obstack_free(&mempool, NULL);
return NULL;
}
memcpy(ret, obstack_finish(&mempool), size);
obstack_free(&mempool, NULL);
size -= sizeof(struct allocation);
size /= sizeof(struct data_extent);
ret->extent_count = size;
ret->block_count = 0;
for (i = 0; i < size; i++) {
struct data_extent *extent = &ret->extents[i];
ret->block_count += extent->end_block - extent->start_block + 1;
}
return ret;
}
/* This function splits and extent in two. The first extent will contain the
* blocks from the start of the given extent up to new_end_block. The second
* extent will contain the blocks from new_end_block+1 up to the end of the
* given extent. The relative locations of the new extents in the allocation
* will be that of the old extent.
* The extents in the old allocation will be removed from the trees, realloced
* and the new extents placed in the trees.
*/
struct allocation *split_extent(struct allocation *alloc,
struct data_extent *extent,
blk64_t new_end_block,
blk64_t new_start_logical)
{
size_t nbytes;
int extent_nr;
extent_nr = extent - alloc->extents;
alloc->extent_count += 1;
nbytes = sizeof(struct allocation);
nbytes += alloc->extent_count * sizeof(struct data_extent);
alloc = realloc(alloc, nbytes);
if (!alloc)
return NULL;
memmove(&alloc->extents[extent_nr + 1], &alloc->extents[extent_nr],
(alloc->extent_count - extent_nr - 1)
* sizeof(struct data_extent));
alloc->extents[extent_nr + 1].start_block = new_end_block + 1;
alloc->extents[extent_nr + 1].start_logical = new_start_logical;
alloc->extents[extent_nr].end_block = new_end_block;
return alloc;
}