-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfreemap.c
1496 lines (1277 loc) · 48.4 KB
/
freemap.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* freemap.c implements a reasonable sorted-list freemap to allocate disk space
* for vectors.
*
* The freemap has undergone a number of redesign phases in order to address
* performance issues (it accumulates a large number of entries during
* updates). The current design is to have one large linked list with all free
* entries on it, with a partial red-black tree index keyed on location to allow
* quick reallocation/freeing with merging of contiguous entries. We also have
* a seperate set of linked lists that list free entries in a log range, which
* allows us to malloc entries with a size requirement relatively quickly.
*
* written nml 2003-04-28
*
*/
#include "firstinclude.h"
#include "freemap.h"
#include "bit.h"
#include "rbtree.h"
#include "def.h"
#include "str.h"
#include "lcrand.h"
#include "objalloc.h"
#include "zstdint.h"
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
enum freerec_flags {
FREEREC_INDEXED = (1 << 0) /* whether this record is in the
* red-black index tree */
};
/* struct to hold a single free record */
struct freerec {
struct freerec *next; /* next in linked list */
struct freerec *prev; /* previous in linked list */
struct freerec *next_size; /* next in size-based linked list */
struct freerec *prev_size; /* previous in size-based list */
unsigned int fileno; /* what file space occurs in */
unsigned long int offset; /* what offset it occurs at */
unsigned int size; /* how big it is */
enum freerec_flags flags; /* flags associated with this record
* (see enum above) */
};
/* number of lists we index */
#define SIZELISTS (sizeof(int) * 8)
struct freemap {
struct rbtree *index; /* red-black tree storing some of the
* entries in the linked list, to
* provide fast ordered access to the
* linked list entries */
struct freerec *sizeindex[SIZELISTS]; /* pointers to linked list of free
* entries where each entry is of
* size 2^index - 2^(index + 1) */
struct freerec *sizetail[SIZELISTS]; /* pointers to tails of size-based
* linked lists */
struct freerec *first; /* sorted linked list of free
* records */
struct freerec *unused; /* linked list of unused free
* records */
unsigned int entries; /* number of entries in linked list */
int err; /* last occuring error */
double space; /* amount of space in map */
double waste; /* wasted space */
unsigned int append; /* how much space we're allowed to
* append to keep the number of
* entries down */
unsigned int files; /* how many files we have
* instanciated */
enum freemap_strategies strategy; /* allocation strategy */
unsigned int index_mark; /* integer between 0 and RAND_MAX that
* indicates what percentage of items
* to index */
struct lcrand *rand; /* random number sequence */
struct objalloc *alloc; /* node allocator */
void *opaque; /* opaque pointer for newfile fn */
int (*newfile)(void *opaque, /* what to call to find out size */
unsigned int file, /* limit on a new file */
unsigned int *maxsize);
};
/* internal function to help qsort a set of freerecs (assuming that they can't
* overlap) - sorts into reverse order */
static int freerec_cmp(const void *one, const void *two) {
const struct freerec *node1 = one,
*node2 = two;
/* have to do it the slow way (not subtraction) because of the danger of
* overflow (think *very* hard before you change this - what happens when
* you compare UINT_MAX with 0?) */
if (node1->fileno == node2->fileno) {
if (node1->offset < node2->offset) {
return -1;
} else if (node1->offset > node2->offset) {
return 1;
} else {
return 0;
}
} else {
if (node1->fileno < node2->fileno) {
return -1;
} else if (node1->fileno > node2->fileno) {
return 1;
} else {
return 0;
}
}
}
/* internal function to return the size of a given size-indexed list */
static unsigned int sizelist_size(const struct freerec *rec,
unsigned int *bytes) {
unsigned int size = 0,
lbytes = 0;
while (rec) {
size++;
lbytes += rec->size;
rec = rec->next_size;
}
if (bytes) {
*bytes = lbytes;
}
return size;
}
/* internal function to ensure that the freemap is sane */
static int freemap_invariant(struct freemap *map) {
if (DEAR_DEBUG) {
struct freerec *curr,
*prev;
unsigned int count = 0,
count2,
count3,
count4,
i;
int cmp;
count = 0;
for ((prev = NULL), curr = map->first; curr;
(prev = curr), (curr = curr->next), count++) {
/* check that linked lists are linked correctly */
if ((curr->prev != prev) || (curr->next == map->first)) {
assert(!CRASH);
return 0;
}
/* check for overlap and zero-length and incorrect sorting
* in entries */
if (prev) {
if (!curr->size
|| ((curr->fileno == prev->fileno)
&& ((curr->offset <= prev->offset + prev->size)
|| (curr->offset <= prev->offset)
|| (prev->offset + prev->size <= prev->offset)))) {
assert(!CRASH);
return 0;
}
}
}
for ((curr = prev), prev = NULL; curr;
(prev = curr), (curr = curr->prev)) {
/* check that linked lists are linked correctly */
if (curr->next != prev) {
assert(!CRASH);
return 0;
}
}
count4 = 0;
for (i = 0; i < SIZELISTS; i++) {
if (map->sizeindex[i] && map->sizeindex[i]->prev_size) {
assert(!CRASH);
return 0;
}
for ((prev = NULL), curr = map->sizeindex[i]; curr;
(prev = curr), (curr = curr->next_size), count4++) {
/* check that linked lists are linked correctly */
if ((curr->prev_size != prev)
/* try to check for loops in lists */
|| (curr->next_size == map->sizeindex[i])
|| (curr->next_size
&& (curr->next_size == curr->prev_size))) {
assert(!CRASH);
return 0;
}
/* check that the entry is of the correct size */
if (bit_log2(curr->size) != i) {
assert(!CRASH);
return 0;
}
}
assert(map->sizetail[i] == prev);
for ((curr = prev), prev = NULL; curr;
(prev = curr), (curr = curr->prev_size)) {
/* check that linked lists are linked correctly */
if (curr->next_size != prev) {
assert(!CRASH);
return 0;
}
}
}
count3 = 0;
for ((prev = NULL), curr = map->unused; curr;
(prev = curr), (curr = curr->next), count3++) {
/* check that linked lists are linked correctly */
if (curr->prev != prev) {
assert(!CRASH);
return 0;
}
/* unused entries shouldn't be indexed */
if (curr->flags) {
assert(!CRASH);
return 0;
}
/* unused entries shouldn't be sizeindexed */
if (curr->prev_size || curr->next_size) {
assert(!CRASH);
return 0;
}
/* check for overlap and incorrect sorting in entries */
if (prev) {
if ((curr->fileno == prev->fileno)
&& ((curr->offset <= prev->offset + prev->size)
|| (curr->offset <= prev->offset))) {
assert(!CRASH);
return 0;
}
}
}
for ((curr = prev), prev = NULL; curr;
(prev = curr), (curr = curr->prev)) {
/* check that linked lists are linked correctly */
if (curr->next != prev) {
assert(!CRASH);
return 0;
}
}
/* check entries count is consistent */
if (map->entries != count + count3) {
assert(!CRASH);
return 0;
}
/* check that all entries are in sizeindex */
if (count4 != count) {
assert(!CRASH);
return 0;
}
/* check that free and unused lists don't overlap */
curr = map->first;
prev = map->unused;
while (curr && prev) {
cmp = freerec_cmp(curr, prev);
if (!cmp) {
assert(!CRASH);
return 0;
} else if (cmp < 0) {
if ((curr->fileno == prev->fileno)
&& (curr->offset + curr->size > prev->offset)) {
assert(!CRASH);
return 0;
}
curr = curr->next;
} else if (cmp > 0) {
if ((curr->fileno == prev->fileno)
&& (prev->offset + prev->size > curr->offset)) {
freemap_print(map, stdout);
assert(!CRASH);
return 0;
}
prev = prev->next;
}
}
/* check that unused list has only one entry per file */
for ((prev = NULL), curr = map->unused; curr;
(prev = curr), curr = curr->next) {
if (prev && (prev->fileno == curr->fileno)) {
assert(!CRASH);
return 0;
}
}
/* count number of entries without links to size-based list, and ensure
* that its the same as the number of singleton entries in sizeindex */
count = 0;
count2 = 0;
for (curr = map->first; curr; curr = curr->next) {
if (!curr->next_size && !curr->prev_size) {
count++;
}
}
for (i = 0; i < SIZELISTS; i++) {
if (map->sizeindex[i] && !map->sizeindex[i]->next_size) {
count2++;
}
}
if (count != count2) {
assert(!CRASH);
return 0;
}
/* count number of entries in sizeindex and ensure that its the
* same as number of entries in freelist */
count = 0;
count2 = 0;
for (curr = map->first; curr; curr = curr->next) {
count++;
}
for (i = 0; i < SIZELISTS; i++) {
for (curr = map->sizeindex[i]; curr; curr = curr->next_size) {
count2++;
}
}
if (count != count2) {
assert(!CRASH);
return 0;
}
}
return 1;
}
struct freemap *freemap_new(enum freemap_strategies strategy,
unsigned int append, void *opaque,
int (*addfile)(void *opaque, unsigned int file, unsigned int *maxsize)) {
struct freemap *map;
unsigned int i;
if ((strategy != FREEMAP_STRATEGY_FIRST)
&& (strategy != FREEMAP_STRATEGY_BEST)
&& (strategy != FREEMAP_STRATEGY_WORST)
&& (strategy != FREEMAP_STRATEGY_CLOSE)) {
return NULL;
}
if ((map = malloc(sizeof(*map)))
&& (map->index = rbtree_ptr_new(freerec_cmp))
&& (map->rand = lcrand_new(time(NULL)))
&& (map->alloc
/* XXX: should allow setting of chunksize and underlying allocator for
* object alloctor from outside interface */
= objalloc_new(sizeof(struct freerec), 0, !!DEAR_DEBUG, 1024, NULL))) {
map->first = map->unused = NULL;
map->strategy = strategy;
map->entries = 0;
map->err = 0;
map->space = map->waste = 0.0;
map->append = append;
map->opaque = opaque;
map->newfile = addfile;
map->files = 0;
/* FIXME: set via interface */
map->index_mark = ((unsigned int) 0.2 * LCRAND_MAX);
for (i = 0; i < SIZELISTS; i++) {
map->sizeindex[i] = NULL;
map->sizetail[i] = NULL;
}
if (!freemap_invariant(map)) {
freemap_delete(map);
map = NULL;
}
} else {
if (map) {
if (map->index) {
if (map->rand) {
lcrand_delete(map->rand);
map->rand = NULL;
}
rbtree_delete(map->index);
map->index = NULL;
}
free(map);
map = NULL;
}
}
return map;
}
void freemap_delete(struct freemap *map) {
assert(freemap_invariant(map));
rbtree_delete(map->index);
lcrand_delete(map->rand);
objalloc_clear(map->alloc);
objalloc_delete(map->alloc);
free(map);
return;
}
/* macros to alter the sizeindex for individual records easily */
#define SIZEINDEX(map, rec) \
do { \
unsigned int sizeindex_index; \
\
sizeindex_index = bit_log2(rec->size); \
assert(!rec->next_size); \
assert(!rec->prev_size); \
rec->prev_size = map->sizetail[sizeindex_index]; \
rec->next_size = NULL; \
if (rec->prev_size) { \
assert(!rec->prev_size->next_size); \
rec->prev_size->next_size = rec; \
} else { \
assert(!map->sizeindex[sizeindex_index]); \
map->sizeindex[sizeindex_index] = rec; \
} \
map->sizetail[sizeindex_index] = rec; \
} while (0)
#define SIZEUNINDEX(map, rec, oldsize) \
do { \
unsigned int sizeindex_index = bit_log2(oldsize); \
\
/* remove from old location */ \
if (rec->prev_size) { \
rec->prev_size->next_size = rec->next_size; \
} else { \
assert(rec == map->sizeindex[sizeindex_index]); \
map->sizeindex[sizeindex_index] = rec->next_size; \
} \
if (rec->next_size) { \
rec->next_size->prev_size = rec->prev_size; \
} else { \
assert(rec == map->sizetail[sizeindex_index]); \
map->sizetail[sizeindex_index] = rec->prev_size; \
} \
rec->prev_size = NULL; \
rec->next_size = NULL; \
} while (0)
#define SIZEREINDEX(map, rec, oldsize) \
SIZEUNINDEX(map, rec, oldsize); SIZEINDEX(map, rec)
/* note that at various times during the next few routines we alter records that
* may be inserted in the index. This would generally be a problem, since we
* are altering the key of an item in a sorted data structure, but since this
* change will not change the sorting order of the index, we should be ok */
static int freemap_malloc_unused(struct freemap *map, unsigned int fileno,
unsigned int offset, unsigned int *size, int options,
struct freerec *unused) {
struct freerec *rec,
*prev;
void **find;
void *findkey;
unsigned int tmp;
if (unused->offset != offset) {
/* we're either going to allocate from the middle or the end, either
* way we need to insert the start into the free list */
struct freerec key;
key.offset = offset;
key.fileno = fileno;
/* search the red-black tree for the location nearest
* specified location */
if (rbtree_ptr_ptr_find_near(map->index, &key, &findkey,
&find) == RBTREE_OK) {
rec = *find;
prev = rec->prev;
} else {
rec = map->first;
prev = NULL;
}
/* search forward for correct place */
while (rec && freerec_cmp(rec, &key) < 0) {
prev = rec;
rec = rec->next;
}
if (prev && (prev->fileno == unused->fileno)
&& (prev->offset + prev->size == unused->offset)) {
/* can coalesce first half of unused record into free
* record */
tmp = prev->size;
prev->size += offset - unused->offset;
SIZEREINDEX(map, prev, tmp);
unused->size -= offset - unused->offset;
unused->offset = offset;
} else {
/* need to create a new rec to split the current record */
struct freerec *newrec;
if ((newrec = objalloc_malloc(map->alloc, sizeof(*newrec)))) {
newrec->fileno = unused->fileno;
newrec->offset = unused->offset;
newrec->size = offset - newrec->offset;
unused->size -= newrec->size;
unused->offset = offset;
/* insert first half into free records, to ensure
* that our assumption is maintained */
newrec->next = rec;
newrec->prev = prev;
newrec->next_size = newrec->prev_size = NULL;
if (newrec->prev) {
newrec->prev->next = newrec;
} else {
map->first = newrec;
}
if (newrec->next) {
newrec->next->prev = newrec;
}
SIZEINDEX(map, newrec);
if ((lcrand(map->rand) <= map->index_mark)
/* randomly decided to index this record */
&& (rbtree_ptr_ptr_insert(map->index, newrec, newrec)
== RBTREE_OK)) {
newrec->flags = FREEREC_INDEXED;
} else {
newrec->flags = 0;
}
map->entries++;
} else {
/* indicate out of memory error */
map->err = ENOMEM;
assert(freemap_invariant(map));
return 0;
}
}
}
assert(unused->offset == offset);
if ((unused->size == *size) || (!(options & FREEMAP_OPT_EXACT)
&& (unused->size <= *size + map->append))) {
/* unused gets consumed by allocation */
*size = unused->size;
if (unused->prev) {
unused->prev->next = unused->next;
} else {
map->unused = unused->next;
}
if (unused->next) {
unused->next->prev = unused->prev;
}
assert(!unused->flags);
objalloc_free(map->alloc, unused);
map->entries--;
} else {
/* tail of unused remains, the rest gets allocated */
unused->offset += *size;
unused->size -= *size;
}
assert(freemap_invariant(map));
return 1;
}
/* internal function to handle allocation from a specific location */
static int freemap_malloc_location(struct freemap *map, unsigned int fileno,
unsigned int offset, unsigned int *size, int options) {
struct freerec key,
*rec,
*prev,
*unused;
void **find;
void *findkey;
unsigned int tmp;
assert(freemap_invariant(map));
if (!*size) {
assert(freemap_invariant(map));
return 1;
}
/* note that we make the assumption that unused elements can only occur at
* the end of the file. This assumption is localised, because allocating a
* specific location is the only way we can break that assumption. If we
* move all unused space that has allocations after it to the previously
* used list, then everything works */
/* search the red-black tree for the location nearest specified location */
key.fileno = fileno;
key.offset = offset;
if (rbtree_ptr_ptr_find_near(map->index, &key, &findkey, &find)
== RBTREE_OK) {
rec = *find;
} else {
rec = map->first;
}
/* iterate until we hit the first free block that could be it */
while (rec && ((rec->fileno < fileno) || ((rec->fileno == fileno)
&& (rec->offset + rec->size) <= offset))) {
rec = rec->next;
}
if (rec && (rec->fileno == fileno) && (rec->offset <= offset)
&& (rec->offset + rec->size >= offset + *size)) {
if (rec->offset != offset) {
if ((rec->size - (offset - rec->offset) == *size)
|| (!(options & FREEMAP_OPT_EXACT)
&& (rec->size - (offset - rec->offset)
<= *size + map->append))) {
/* only the start of the record will remain, just shorten it */
tmp = rec->size;
rec->size = offset - rec->offset;
SIZEREINDEX(map, rec, tmp);
} else {
/* need to create a new rec to split record */
struct freerec *newrec;
if ((newrec = objalloc_malloc(map->alloc, sizeof(*newrec)))) {
newrec->offset = offset + *size;
newrec->fileno = fileno;
newrec->size = rec->offset + rec->size - newrec->offset;
tmp = rec->size;
rec->size = offset - rec->offset;
SIZEREINDEX(map, rec, tmp);
newrec->next = rec->next;
newrec->prev = rec;
newrec->next_size = newrec->prev_size = NULL;
rec->next = newrec;
if (newrec->next) {
newrec->next->prev = newrec;
}
SIZEINDEX(map, newrec);
if ((lcrand(map->rand) <= map->index_mark)
/* randomly decided to index this record */
&& (rbtree_ptr_ptr_insert(map->index, newrec, newrec)
== RBTREE_OK)) {
newrec->flags = FREEREC_INDEXED;
} else {
newrec->flags = 0;
}
map->entries++;
} else {
/* indicate out of memory error */
map->err = ENOMEM;
assert(freemap_invariant(map));
return 0;
}
}
} else if ((rec->size == *size) || (!(options & FREEMAP_OPT_EXACT)
&& (rec->size <= *size + map->append))) {
/* rec gets consumed by allocation */
*size = rec->size;
SIZEUNINDEX(map, rec, rec->size);
if (rec->prev) {
rec->prev->next = rec->next;
} else {
map->first = rec->next;
}
if (rec->next) {
rec->next->prev = rec->prev;
}
if ((rec->flags & FREEREC_INDEXED)
&& (rbtree_ptr_ptr_remove(map->index, rec, &findkey)
!= RBTREE_OK)) {
assert(!CRASH);
objalloc_free(map->alloc, rec);
/* indicate failure (and just lost memory) */
map->err = EINVAL;
assert(freemap_invariant(map));
return 0;
}
objalloc_free(map->alloc, rec);
map->entries--;
} else {
tmp = rec->size;
rec->offset += *size;
rec->size -= *size;
SIZEREINDEX(map, rec, tmp);
}
assert(freemap_invariant(map));
return 1;
}
/* couldn't cover whole allocation with one used block. rec points to the
* only free block that can be involved in the allocation, need to find
* unused blocks that are involved */
unused = map->unused;
prev = NULL;
while (unused && ((unused->fileno < fileno) || ((unused->fileno == fileno)
&& (unused->offset + unused->size) <= offset))) {
prev = unused;
unused = unused->next;
}
if (unused && (unused->fileno == fileno) && (unused->offset <= offset)
&& (unused->offset + unused->size >= offset + *size)) {
/* allocate from unused */
return freemap_malloc_unused(map, fileno, offset, size, options,
unused);
}
/* if we have a candidate from both, and they're both in the correct
* file ... */
if (unused && rec && (unused->fileno == fileno)
&& (unused->fileno == rec->fileno)
/* ... and they're contiguous with rec first (has to be first under our
* unused-at-the-end assumption) */
&& (unused->offset == rec->offset + rec->size)) {
unsigned int start = offset - rec->offset;
unsigned int end = unused->size + rec->size - start - *size;
/* start is the offset from the start of rec to the start of the
* allocation. end is the amount of space left in unused after
* allocation */
if (!end || (!(options & FREEMAP_OPT_EXACT) && (end <= map->append))) {
/* unused is consumed by the allocation, remove it */
/* calculate size of allocated block, note that we need to do this
* before we fiddle with the rec record using start value below */
*size = rec->size + unused->size - start;
if (unused->prev) {
unused->prev->next = unused->next;
} else {
map->unused = unused->next;
}
if (unused->next) {
unused->next->prev = unused->prev;
}
assert(!unused->flags);
objalloc_free(map->alloc, unused);
map->entries--;
} else {
unused->offset = unused->offset + unused->size - end;
unused->size = end;
}
if (!start) {
/* rec is consumed by the allocation, remove it */
assert(rec->offset + rec->size < offset + *size);
SIZEUNINDEX(map, rec, rec->size);
if (rec->prev) {
rec->prev->next = rec->next;
} else {
map->first = rec->next;
}
if (rec->next) {
rec->next->prev = rec->prev;
}
if ((rec->flags & FREEREC_INDEXED)
&& (rbtree_ptr_ptr_remove(map->index, rec, &findkey)
!= RBTREE_OK)) {
assert(!CRASH);
objalloc_free(map->alloc, rec);
/* indicate failure (and just lost memory) */
map->err = EINVAL;
assert(freemap_invariant(map));
return 0;
}
objalloc_free(map->alloc, rec);
map->entries--;
} else {
assert(rec->offset < offset);
tmp = rec->size;
rec->size = start;
SIZEREINDEX(map, rec, tmp);
}
assert(freemap_invariant(map));
return 1;
}
/* get allocations from allocating function if necessary */
while ((!unused || (fileno > unused->fileno)) && (fileno >= map->files)) {
if ((unused = objalloc_malloc(map->alloc, sizeof(*unused)))
/* get a new allocation from the providing function */
&& (map->newfile(map->opaque, map->files, &unused->size))) {
map->space += unused->size;
unused->fileno = map->files++;
unused->offset = 0;
unused->prev_size = unused->next_size = NULL;
unused->prev = prev;
unused->next = NULL;
if (prev) {
assert(prev->next == NULL);
prev->next = unused;
} else {
map->unused = unused;
}
unused->flags = 0;
prev = unused;
map->entries++;
} else {
/* indicate failure */
map->err = ENOMEM;
if (unused) {
objalloc_free(map->alloc, unused);
}
assert(freemap_invariant(map));
return 0;
}
}
if (unused && (unused->fileno == fileno) && (unused->offset <= offset)
&& (unused->offset + unused->size >= offset + *size)) {
/* allocate from unused */
return freemap_malloc_unused(map, fileno, offset, size, options,
unused);
}
return 0;
}
int freemap_malloc(struct freemap *map, unsigned int *fileno,
unsigned long int *offset, unsigned int *size, int options, ...) {
va_list varargs;
struct freerec *rec = NULL;
unsigned int space,
tmp;
unsigned int lsize = *size, /* local copy of size */
index,
i;
void *findkey;
assert(freemap_invariant(map));
va_start(varargs, options);
if (options & FREEMAP_OPT_LOCATION) {
/* deal with LOCATION in a different function, too hard otherwise */
unsigned int fileno = (unsigned int) va_arg(varargs, unsigned int);
unsigned long int offset
= (unsigned int) va_arg(varargs, unsigned long int);
va_end(varargs);
return freemap_malloc_location(map, fileno, offset, size, options);
}
va_end(varargs);
if (!lsize) {
*fileno = 0;
*offset = 0;
return 1;
}
index = bit_log2(lsize);
switch (map->strategy) {
case FREEMAP_STRATEGY_FIRST:
/* first-fit along location-based list */
for (rec = map->first; rec && (rec->size < lsize); rec = rec->next) ;
break;
case FREEMAP_STRATEGY_CLOSE:
/* search, ascending up to larger sizeindex lists, for an entry to fit
* this size */
rec = NULL;
i = index;
do {
for (rec = map->sizeindex[i]; rec && (rec->size < lsize);
rec = rec->next_size) ;
} while (!rec && (++i < SIZELISTS));
break;
case FREEMAP_STRATEGY_BEST:
/* search, ascending up to larger sizeindex lists, for the best entry in
* this list (which will be the best fit overall) to fit this size */
i = index;
do {
struct freerec *best;
/* find first acceptable chunk */
for (best = map->sizeindex[i]; best && (best->size < lsize);
best = best->next_size) ;
/* have found first fit, now process the rest to find best fit */
if (best) {
for (rec = best->next_size; rec; rec = rec->next_size) {
if ((rec->size >= lsize) && (rec->size < best->size)) {
best = rec;
}
}
}
rec = best;
} while (!rec && (++i < SIZELISTS));
break;
case FREEMAP_STRATEGY_WORST:
/* search, descending down from larger sizeindex lists, for the worst
* entry in this list (which will be the worst fit overall) to fit
* this size */
i = SIZELISTS - 1;
do {
struct freerec *worst;
/* find first acceptable chunk */
for (worst = map->sizeindex[i]; worst && (worst->size < lsize);
worst = worst->next_size) ;
/* have found first fit, now process the rest to find worst fit */
if (worst) {
for (rec = worst->next_size; rec; rec = rec->next_size) {
if ((rec->size >= lsize) && (rec->size > worst->size)) {
worst = rec;
}
}
}
rec = worst;
} while (!rec && (i-- + 1 > index));
break;
default:
assert("can't get here" && 0);
break;
}
/* if we've selected a record it should be suitable... */
assert(!rec || (rec->size >= lsize));
/* ...but we'll check anyway */
if (rec && (rec->size >= lsize)) {
/* allocate from rec and return it */
*fileno = rec->fileno;
*offset = rec->offset;
if ((rec->size == lsize)
|| (!(options & FREEMAP_OPT_EXACT)
&& (rec->size <= lsize + map->append))) {
/* its a close fit, just allocate all of it */
*size = rec->size;
SIZEUNINDEX(map, rec, rec->size);
if (rec->prev) {
rec->prev->next = rec->next;
} else {
map->first = rec->next;
}
if (rec->next) {
rec->next->prev = rec->prev;
}
if ((rec->flags & FREEREC_INDEXED)
&& (rbtree_ptr_ptr_remove(map->index, rec, &findkey)
!= RBTREE_OK)) {
assert(!CRASH);