Debian Codegraph @

falloc/all_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // blame: jnml, labs.nic.cz
6
7 package falloc // import "modernc.org/fileutil/falloc"
8
9 import (
10 "bytes"
11 "errors"
12 "flag"
13 "fmt"
14 "io/ioutil"
15 "log"
16 "math"
17 "os"
18 "path/filepath"
19 "runtime"
20 "testing"
21 "time"
22
23 "modernc.org/fileutil"
24 "modernc.org/fileutil/storage"
25 "modernc.org/mathutil"
26 )
27
28 var (
29 blockFlag = flag.Uint("block", 256, "block size for some of the dev tests")
30 cacheTotalFlag = flag.Int64("cachemax", 1<<25, "cache total bytes")
31 cachedFlag = flag.Bool("cache", false, "enable caching store")
32 devFlag = flag.Bool("dev", false, "enable dev tests")
33 dropFlag = flag.Bool("drop", false, "drop system file cache for some of the dev tests before measurement")
34 fadviseFlag = flag.Bool("fadvise", false, "hint kernel about random file access")
35 nFlag = flag.Int("n", 1, "parameter for some of the dev tests")
36 probeFlag = flag.Bool("probe", false, "report store probe statistics")
37 optGo = flag.Int("go", 3, "GOMAXPROCS")
38 )
39
40 func TestMain(m *testing.M) {
41 flag.Parse()
42 runtime.GOMAXPROCS(*optGo)
43 os.Exit(m.Run())
44 }
45
46 func temp() (dir, name string) {
47 dir, err := ioutil.TempDir("", "test-falloc-")
48 if err != nil {
49 panic(err)
50 }
51
52 name = filepath.Join(dir, "test.db")
53 return dir, name
54 }
55
56 type balancedAcid struct {
57 storage.Accessor
58 nesting int
59 }
60
61 func newBalancedAcid(store storage.Accessor) storage.Accessor {
62 return &balancedAcid{store, 0}
63 }
64
65 func (b *balancedAcid) BeginUpdate() error {
66 if b.nesting < 0 {
67 return errors.New("BeginUpdate with nesting < 0")
68 }
69
70 b.nesting++
71 return nil
72 }
73
74 func (b *balancedAcid) EndUpdate() error {
75 if b.nesting <= 0 {
76 return errors.New("EndUpdate with nesting <= 0")
77 }
78
79 b.nesting--
80 return nil
81 }
82
83 func (b *balancedAcid) Close() error {
84 if b.nesting != 1 {
85 return fmt.Errorf("before Close(): nesting %d %p", b.nesting, b)
86 }
87
88 if err := b.Accessor.Close(); err != nil {
89 return err
90 }
91
92 if b.nesting != 1 {
93 return fmt.Errorf("after Close(): nesting %d", b.nesting)
94 }
95
96 return nil
97 }
98
99 func fopen(fn string) (f *File, err error) {
100 var store storage.Accessor
101 if store, err = storage.OpenFile(fn, os.O_RDWR, 0666); err != nil {
102 return
103 }
104
105 var advise func(int64, int, bool)
106 if *fadviseFlag {
107 file := store.(*storage.FileAccessor).File
108 if err = fileutil.Fadvise(file, 0, 0, fileutil.POSIX_FADV_RANDOM); err != nil {
109 return
110 }
111 advise = func(off int64, len int, write bool) {
112 if err = fileutil.Fadvise(file, off, off+int64(len), fileutil.POSIX_FADV_DONTNEED); err != nil {
113 log.Fatal("advisor advise err", err)
114 }
115 }
116 }
117
118 var prob *storage.Probe
119 if *probeFlag {
120 prob = storage.NewProbe(store, nil)
121 store = prob
122 }
123 if *cachedFlag {
124 if store, err = storage.NewCache(store, *cacheTotalFlag, advise); err != nil {
125 return
126 }
127
128 if *probeFlag {
129 store = storage.NewProbe(store, prob)
130 }
131 }
132 f, err = Open(newBalancedAcid(store))
133 return
134 }
135
136 func fcreate(fn string) (f *File, err error) {
137 var store storage.Accessor
138 if store, err = storage.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666); err != nil {
139 return
140 }
141
142 var advise func(int64, int, bool)
143 if *fadviseFlag {
144 file := store.(*storage.FileAccessor).File
145 if err = fileutil.Fadvise(file, 0, 0, fileutil.POSIX_FADV_RANDOM); err != nil {
146 return
147 }
148 advise = func(off int64, len int, write bool) {
149 if err = fileutil.Fadvise(file, off, off+int64(len), fileutil.POSIX_FADV_DONTNEED); err != nil {
150 log.Fatal("advisor advise err", err)
151 }
152 }
153 }
154
155 var prob *storage.Probe
156 if *probeFlag {
157 prob = storage.NewProbe(store, nil)
158 store = prob
159 }
160 if *cachedFlag {
161 if store, err = storage.NewCache(store, *cacheTotalFlag, advise); err != nil {
162 return
163 }
164
165 if *probeFlag {
166 store = storage.NewProbe(store, prob)
167 }
168 }
169 f, err = New(newBalancedAcid(store))
170 return
171 }
172
173 func probed(t *testing.T, f *File) {
174 if f == nil {
175 return
176 }
177
178 dump := func(p *storage.Probe) {
179 t.Logf("OpsRd %d OpsWr %d BytesRd %d(avg %.1f) BytesWr %d(avg %.1f) SectorsRd %d(%d, +%d, x%.2f) SectorsWr %d(%d, +%d, x%.2f)",
180 p.OpsRd, p.OpsWr,
181 p.BytesRd, float64(p.BytesRd)/float64(p.OpsRd),
182 p.BytesWr, float64(p.BytesWr)/float64(p.OpsWr),
183 p.SectorsRd,
184 p.SectorsRd<<9,
185 p.SectorsRd<<9-p.BytesRd,
186 float64(p.SectorsRd<<9)/float64(p.BytesRd),
187 p.SectorsWr,
188 p.SectorsWr<<9,
189 p.SectorsWr<<9-p.BytesWr,
190 float64(p.SectorsWr<<9)/float64(p.BytesWr),
191 )
192 }
193
194 if ph, ok := f.Accessor().(*storage.Probe); ok {
195 dump(ph)
196 if c, ok := ph.Accessor.(*storage.Cache); ok {
197 if pl, ok := c.Accessor().(*storage.Probe); ok {
198 dump(pl)
199 }
200 }
201 }
202 }
203
204 func (f *File) audit() (usedblocks, totalblocks int64, err error) {
205 defer func() {
206 if e := recover(); e != nil {
207 err = e.(error)
208 }
209 }()
210
211 fi, err := f.f.Stat()
212 if err != nil {
213 panic(err)
214 }
215
216 freemap := map[int64]int64{}
217 fp := int64(0)
218 buf := make([]byte, 22)
219 freeblocks := int64(0)
220
221 // linear scan
222 for fp < fi.Size() {
223 totalblocks++
224 typ, size := f.getInfo(fp >> 4)
225 f.read(buf[:1], fp+size<<4-1)
226 last := buf[0]
227 switch {
228 default:
229 panic("internal error")
230 case typ == 0:
231 if last != 0 {
232 panic(fmt.Errorf("@%#x used empty, last @%#x: %#x != 0", fp, fp+size<<4-1, last))
233 }
234 case typ >= 0x1 && typ <= 0xed:
235 if last >= 0xfe {
236 panic(fmt.Errorf("@%#x used short, last @%#x: %#x > 0xfe", fp, fp+size<<4-1, last))
237 }
238 case typ >= 0xee && typ <= 0xfb:
239 if last > 1 {
240 panic(fmt.Errorf("@%#x used esc short, last @%#x: %#x > 1", fp, fp+size<<4-1, last))
241 }
242 case typ == 0xfc:
243 f.read(buf[:2], fp+1)
244 switch n := int(buf[0])<<8 + int(buf[1]); {
245 default:
246 panic(fmt.Errorf("@%#x used long, illegal content length %#x < 0xee(238)", fp, n))
247 case n >= 0xee && n <= 0xf0f0:
248 if last >= 0xfe {
249 panic(fmt.Errorf("@%#x used long, last @%#x: %#x > 0xfe", fp, fp+size<<4-1, last))
250 }
251 case n >= 0xf0f1 && n <= 0xffff:
252 if last > 1 {
253 panic(fmt.Errorf("@%#x used esc long, last @%#x: %#x > 1", fp, fp+size<<4-1, last))
254 }
255 }
256 case typ == 0xfd:
257 if last != 0 {
258 panic(fmt.Errorf("@%#x reloc, last @%#x: %#x != 0", fp, fp+size<<4-1, last))
259 }
260
261 var target int64
262 f.read(buf[:7], fp+1)
263 (*Handle)(&target).Get(buf)
264 if target >= f.atoms {
265 panic(fmt.Errorf("@%#x illegal reloc, target %#x > f.atoms(%#x)", fp, target, f.atoms))
266 }
267
268 ttyp, _ := f.getInfo(target)
269 if ttyp >= 0xfe {
270 panic(fmt.Errorf("@%#x reloc, points to unused @%#x", fp, target))
271 }
272
273 if ttyp == 0xfd {
274 panic(fmt.Errorf("@%#x reloc, points to reloc @%#x", fp, target))
275 }
276 case typ == 0xfe:
277 if size < 2 {
278 panic(fmt.Errorf("@%#x illegal free block, atoms %d < 2", fp, size))
279 }
280
281 if fp>>4 < f.canfree {
282 panic(fmt.Errorf("@%#x illegal free block @ < f.canfree", fp))
283 }
284
285 f.read(buf[:22], fp)
286 var prev, next, sz int64
287 (*Handle)(&prev).Get(buf[1:])
288 (*Handle)(&next).Get(buf[8:])
289 f.checkPrevNext(fp, prev, next)
290 f.read(buf[:7], fp+size<<4-8)
291 (*Handle)(&sz).Get(buf)
292 if sz != size {
293 panic(fmt.Errorf("@%#x mismatch size, %d != %d", fp, sz, size))
294 }
295
296 if last != 0xfe {
297 panic(fmt.Errorf("@%#x free atom, last @%#x: %#x != 0xff", fp, fp+size<<4-1, last))
298 }
299 freemap[fp>>4] = size
300 freeblocks++
301 case typ == 0xff:
302 f.read(buf[:14], fp+1)
303 var prev, next int64
304 (*Handle)(&prev).Get(buf)
305 (*Handle)(&next).Get(buf[7:])
306 f.checkPrevNext(fp, prev, next)
307 if last != 0xff {
308 panic(fmt.Errorf("@%#x free atom, last @%#x: %#x != 0xff", fp, fp+size<<4-1, last))
309 }
310 freemap[fp>>4] = size
311 freeblocks++
312 }
313 fp += size << 4
314 }
315 usedblocks = totalblocks - freeblocks
316
317 // check free table
318 for size := len(f.freetab) - 1; size > 0; size-- {
319 var prev, next, fprev int64
320 this := f.freetab[size]
321 for this != 0 {
322 sz, ok := freemap[this]
323 if !ok {
324 panic(fmt.Errorf("bad freetab[%d] item @%#x", size, this))
325 }
326
327 delete(freemap, this)
328
329 if sz < int64(size) {
330 panic(fmt.Errorf("bad freetab[%d] item size @%#x %d", size, this, sz))
331 }
332
333 if sz == 1 {
334 f.read(buf[:15], this<<4)
335 (*Handle)(&fprev).Get(buf[1:])
336 if fprev != prev {
337 panic(fmt.Errorf("bad fprev %#x, exp %#x", fprev, prev))
338 }
339
340 (*Handle)(&next).Get(buf[8:])
341 } else {
342 f.read(buf, this<<4)
343 (*Handle)(&fprev).Get(buf[1:])
344 if fprev != prev {
345 panic(fmt.Errorf("bad fprev %#x, exp %#x", fprev, prev))
346 }
347 var fsz int64
348 (*Handle)(&fsz).Get(buf[15:])
349 if fsz != sz {
350 panic(fmt.Errorf("bad fsz %d @%#x, exp %#x", fsz, this<<4, sz))
351 }
352
353 (*Handle)(&next).Get(buf[8:])
354 }
355
356 prev, this = this, next
357 }
358 }
359
360 if n := len(freemap); n != 0 {
361 for h, s := range freemap {
362 panic(fmt.Errorf("%d lost free blocks in freemap, e.g. %d free atoms @%#x", n, s, h))
363 }
364 }
365
366 return
367
368 }
369
370 func (f *File) checkPrevNext(fp, prev, next int64) {
371 if prev != 0 && prev < f.canfree {
372 panic(fmt.Errorf("@%#x illegal free atom, prev %#x < f.canfree(%#x)", fp, prev, f.canfree))
373 }
374
375 if prev >= f.atoms {
376 panic(fmt.Errorf("@%#x illegal free atom, prev %#x > f.atoms", fp, prev))
377 }
378
379 if next != 0 && next < f.canfree {
380 panic(fmt.Errorf("@%#x illegal free atom, next %#x < f.canfree(%#x)", fp, next, f.canfree))
381 }
382
383 if next >= f.atoms {
384 panic(fmt.Errorf("@%#x illegal free atom, next %#x > f.atoms", fp, next))
385 }
386 }
387
388 func reaudit(t *testing.T, f *File, fn string) (of *File) {
389 var err error
390 if _, _, err := f.audit(); err != nil {
391 t.Fatal(err)
392 }
393
394 if err := f.Close(); err != nil {
395 t.Fatal(err)
396 }
397
398 f = nil
399 runtime.GC()
400 if of, err = fopen(fn); err != nil {
401 t.Fatal(err)
402 }
403
404 if _, _, err := of.audit(); err != nil {
405 t.Fatal(err)
406 }
407
408 return
409 }
410
411 func TestCreate(t *testing.T) {
412 dir, name := temp()
413 defer os.RemoveAll(dir)
414
415 f, err := fcreate(name)
416 if err != nil {
417 t.Fatal(err)
418 }
419
420 defer func() {
421 err := os.Remove(name)
422 if err != nil {
423 t.Fatal(err)
424 }
425 }()
426
427 f.Accessor().Sync()
428 probed(t, f)
429 if err = f.Close(); err != nil {
430 t.Log(f.f.(*balancedAcid).nesting)
431 t.Fatal(err)
432 }
433
434 b, err := ioutil.ReadFile(name)
435 if err != nil {
436 t.Fatal(err)
437 }
438
439 x := b[:16]
440 if !bytes.Equal(x, hdr) {
441 t.Fatalf("\n% x\n% x", x, hdr)
442 }
443
444 x = b[16:32]
445 if !bytes.Equal(x, empty) {
446 t.Fatalf("\n% x\n% x", x, hdr)
447 }
448 }
449
450 func TestOpen(t *testing.T) {
451 dir, name := temp()
452 defer os.RemoveAll(dir)
453
454 f, err := fcreate(name)
455 if err != nil {
456 t.Fatal(err)
457 }
458
459 defer func() {
460 probed(t, f)
461 ec := f.Close()
462 er := os.Remove(name)
463 if ec != nil {
464 t.Fatal(ec)
465 }
466
467 if er != nil {
468 t.Fatal(er)
469 }
470 }()
471
472 if err := f.Close(); err != nil {
473 t.Fatal(err)
474 }
475
476 if f, err = fopen(name); err != nil {
477 t.Fatal(err)
478 }
479
480 for i, p := range f.freetab {
481 if p != 0 {
482 t.Fatal(i+1, p)
483 }
484 }
485 }
486
487 func alloc(f *File, b []byte) (y int64) {
488 if h, err := f.Alloc(b); err != nil {
489 panic(err)
490 } else {
491 y = int64(h)
492 }
493 return
494 }
495
496 func realloc(f *File, atom int64, b []byte, keepHandle bool) (y int64) {
497 if h, err := f.Realloc(Handle(atom), b, keepHandle); err != nil {
498 panic(err)
499 } else {
500 y = int64(h)
501 }
502 return
503 }
504
505 func testContentEncodingDecoding(t *testing.T, min, max int) {
506 dir, name := temp()
507 defer os.RemoveAll(dir)
508
509 f, err := fcreate(name)
510 if err != nil {
511 t.Fatal(err)
512 }
513
514 defer func() {
515 ec := f.Close()
516 er := os.Remove(name)
517 if ec != nil {
518 t.Fatal(ec)
519 }
520
521 if er != nil {
522 t.Fatal(er)
523 }
524 }()
525
526 b := make([]byte, max)
527 r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true)
528 if err != nil {
529 t.Fatal(err)
530 }
531
532 blocks := int64(3)
533 a := make([]int64, 0, 4*(max-min+1))
534 for cl := min; cl <= max; cl++ {
535 src := b[:cl]
536 for i := range src {
537 b[i] = byte(r.Next())
538 }
539 a = append(a, alloc(f, src))
540 blocks++
541 if cl == 0 {
542 continue
543 }
544
545 for i := range src {
546 b[i] = byte(r.Next())
547 }
548 src[cl-1] = 0xfd
549 a = append(a, alloc(f, src))
550 blocks++
551 for i := range src {
552 b[i] = byte(r.Next())
553 }
554 src[cl-1] = 0xfe
555 a = append(a, alloc(f, src))
556 blocks++
557 for i := range src {
558 b[i] = byte(r.Next())
559 }
560 src[cl-1] = 0xff
561 a = append(a, alloc(f, src))
562 blocks++
563 }
564
565 f.Accessor().Sync()
566 probed(t, f)
567 if err := f.Close(); err != nil {
568 t.Fatal(err)
569 }
570
571 f = nil
572 runtime.GC()
573 if f, err = fopen(name); err != nil {
574 t.Fatal(err)
575 }
576
577 r.Seek(0)
578 ai := 0
579 for cl := min; cl <= max; cl++ {
580 h := a[ai]
581 ai++
582 src := b[:cl]
583 for i := range src {
584 b[i] = byte(r.Next())
585 }
586 got, _ := f.readUsed(h)
587 if !bytes.Equal(src, got) {
588 t.Fatalf("cl %d atom %#x\nexp % x\ngot % x", cl, h, src, got)
589 }
590 if cl == 0 {
591 continue
592 }
593
594 for i := range src {
595 b[i] = byte(r.Next())
596 }
597 src[cl-1] = 0xfd
598 h = a[ai]
599 ai++
600 got, _ = f.readUsed(h)
601 if !bytes.Equal(src, got) {
602 t.Fatalf("cl %d atom %#x\nexp % x\ngot % x", cl, h, src, got)
603 }
604
605 for i := range src {
606 b[i] = byte(r.Next())
607 }
608 src[cl-1] = 0xfe
609 h = a[ai]
610 ai++
611 got, _ = f.readUsed(h)
612 if !bytes.Equal(src, got) {
613 t.Fatalf("cl %d atom %#x\nexp % x\ngot % x", cl, h, src, got)
614 }
615
616 for i := range src {
617 b[i] = byte(r.Next())
618 }
619 src[cl-1] = 0xff
620 h = a[ai]
621 ai++
622 got, _ = f.readUsed(h)
623 if !bytes.Equal(src, got) {
624 t.Fatalf("cl %d atom %#x\nexp % x\ngot % x", cl, h, src, got)
625 }
626 }
627
628 auditblocks, _, err := f.audit()
629 if err != nil {
630 t.Fatal(err)
631 }
632
633 if auditblocks != blocks {
634 t.Fatal(auditblocks, blocks)
635 }
636
637 if f = reaudit(t, f, name); err != nil {
638 t.Fatal(err)
639 }
640 }
641
642 func TestContentEncodingDecoding(t *testing.T) {
643 testContentEncodingDecoding(t, 0, 1024)
644 testContentEncodingDecoding(t, 61680-17, 61680)
645 }
646
647 type freeItem struct {
648 size int64
649 head int64
650 }
651
652 func (f *File) reportFree() (report []freeItem) {
653 for size, head := range f.freetab {
654 if size != 0 && head != 0 {
655 report = append(report, freeItem{int64(size), head})
656 }
657 }
658 return
659 }
660
661 func free(f *File, h int64) {
662 if err := f.Free(Handle(h)); err != nil {
663 panic(err)
664 }
665 }
666
667 func testFreeTail(t *testing.T, b []byte) {
668 dir, name := temp()
669 defer os.RemoveAll(dir)
670
671 f, err := fcreate(name)
672 if err != nil {
673 t.Fatal(err)
674 }
675
676 defer func() {
677 ec := f.Close()
678 er := os.Remove(name)
679 if ec != nil {
680 t.Fatal(ec)
681 }
682
683 if er != nil {
684 t.Fatal(er)
685 }
686 }()
687
688 fs0 := f.atoms
689 used0, total0, err := f.audit()
690 if err != nil {
691 panic(err)
692 }
693
694 if used0 != total0 {
695 t.Fatal(used0, total0)
696 }
697
698 handle := alloc(f, b)
699 free(f, handle)
700 if fs1 := f.atoms; fs1 != fs0 {
701 t.Fatal(fs1, fs0)
702 }
703
704 if rep := f.reportFree(); len(rep) != 0 {
705 t.Fatal(rep)
706 }
707
708 if err := f.Close(); err != nil {
709 t.Fatal(err)
710 }
711
712 f = nil
713 runtime.GC()
714 if f, err = fopen(name); err != nil {
715 t.Fatal(err)
716 }
717
718 used, total, err := f.audit()
719 if err != nil {
720 panic(err)
721 }
722
723 if used != used0 {
724 t.Fatal(used, used0)
725 }
726
727 if total != total0 {
728 t.Fatal(total, total0)
729 }
730 }
731
732 func TestFreeTail(t *testing.T) {
733 b := make([]byte, 61680)
734 for n := 0; n <= 253+16; n++ {
735 data := b[:n]
736 testFreeTail(t, data)
737 if n == 0 {
738 continue
739 }
740
741 data[n-1] = 0xff
742 testFreeTail(t, data)
743 data[n-1] = 0
744 }
745
746 for n := 61680 - 16; n <= 61680; n++ {
747 data := b[:n]
748 testFreeTail(t, data)
749 data[n-1] = 0xff
750 testFreeTail(t, data)
751 data[n-1] = 0
752 }
753 }
754
755 func testFreeTail2(t *testing.T, b []byte) {
756 dir, name := temp()
757 defer os.RemoveAll(dir)
758
759 f, err := fcreate(name)
760 if err != nil {
761 t.Fatal(err)
762 }
763
764 defer func() {
765 ec := f.Close()
766 er := os.Remove(name)
767 if ec != nil {
768 t.Fatal(ec)
769 }
770
771 if er != nil {
772 t.Fatal(er)
773 }
774 }()
775
776 fs0 := f.atoms
777 used0, total0, err := f.audit()
778 if err != nil {
779 panic(err)
780 }
781
782 if used0 != total0 {
783 t.Fatal(used0, total0)
784 }
785
786 handle := alloc(f, b)
787 handle2 := alloc(f, b)
788 free(f, handle)
789 free(f, handle2)
790 if fs1 := f.atoms; fs1 != fs0 {
791 t.Fatal(fs1, fs0)
792 }
793
794 if rep := f.reportFree(); len(rep) != 0 {
795 t.Fatal(rep)
796 }
797
798 if err := f.Close(); err != nil {
799 t.Fatal(err)
800 }
801
802 f = nil
803 runtime.GC()
804 if f, err = fopen(name); err != nil {
805 t.Fatal(err)
806 }
807
808 used, total, err := f.audit()
809 if err != nil {
810 panic(err)
811 }
812
813 if used != used0 {
814 t.Fatal(used, used0)
815 }
816
817 if total != total0 {
818 t.Fatal(total, total0)
819 }
820 }
821
822 func TestFreeTail2(t *testing.T) {
823 b := make([]byte, 61680)
824 for n := 0; n <= 253+16; n++ {
825 data := b[:n]
826 testFreeTail2(t, data)
827 if n == 0 {
828 continue
829 }
830
831 data[n-1] = 0xff
832 testFreeTail2(t, data)
833 data[n-1] = 0
834 }
835
836 for n := 61680 - 16; n <= 61680; n++ {
837 data := b[:n]
838 testFreeTail2(t, data)
839 data[n-1] = 0xff
840 testFreeTail2(t, data)
841 data[n-1] = 0
842 }
843 }
844
845 func testFreeIsolated(t *testing.T, b []byte) {
846 dir, name := temp()
847 defer os.RemoveAll(dir)
848
849 f, err := fcreate(name)
850 if err != nil {
851 t.Fatal(err)
852 }
853
854 defer func() {
855 ec := f.Close()
856 er := os.Remove(name)
857 if ec != nil {
858 t.Fatal(ec)
859 }
860
861 if er != nil {
862 t.Fatal(er)
863 }
864 }()
865
866 rqAtoms := rq2Atoms(len(b))
867 left := alloc(f, nil)
868 handle := alloc(f, b)
869 right := alloc(f, nil)
870
871 fs0 := f.atoms
872 used0, total0, err := f.audit()
873 if err != nil {
874 panic(err)
875 }
876
877 if used0 != total0 {
878 t.Fatal(used0, total0)
879 }
880
881 free(f, handle)
882 if fs1 := f.atoms; fs1 != fs0 {
883 t.Fatal(fs1, fs0)
884 }
885
886 rep := f.reportFree()
887 if len(rep) != 1 {
888 t.Fatal(rep)
889 }
890
891 if x := rep[0]; x.size != rqAtoms || x.head != handle {
892 t.Fatal(x)
893 }
894
895 used, total, err := f.audit()
896 if err != nil {
897 panic(err)
898 }
899
900 if n, free := f.getSize(left); n != 1 || free {
901 t.Fatal(n, free)
902 }
903
904 if n, free := f.getSize(right); n != 1 || free {
905 t.Fatal(n, free)
906 }
907
908 if used != used0-1 {
909 t.Fatal(used, used0)
910 }
911
912 if total != total0 {
913 t.Fatal(total, total0)
914 }
915
916 if free := total - used; free != 1 {
917 t.Fatal(free)
918 }
919
920 // verify persisted file correct
921 if err := f.Close(); err != nil {
922 t.Fatal(err)
923 }
924
925 f = nil
926 runtime.GC()
927 if f, err = fopen(name); err != nil {
928 t.Fatal(err)
929 }
930
931 if fs1 := f.atoms; fs1 != fs0 {
932 t.Fatal(fs1, fs0)
933 }
934
935 rep = f.reportFree()
936 if len(rep) != 1 {
937 t.Fatal(rep)
938 }
939
940 if x := rep[0]; x.size != rqAtoms || x.head != handle {
941 t.Fatal(x)
942 }
943
944 used, total, err = f.audit()
945 if err != nil {
946 panic(err)
947 }
948
949 if n, free := f.getSize(left); n != 1 || free {
950 t.Fatal(n, free)
951 }
952
953 if n, free := f.getSize(right); n != 1 || free {
954 t.Fatal(n, free)
955 }
956
957 if used != used0-1 {
958 t.Fatal(used, used0)
959 }
960
961 if total != total0 {
962 t.Fatal(total, total0)
963 }
964
965 if free := total - used; free != 1 {
966 t.Fatal(free)
967 }
968
969 }
970
971 func TestFreeIsolated(t *testing.T) {
972 b := make([]byte, 61680)
973 for n := 0; n <= 253+16; n++ {
974 data := b[:n]
975 testFreeIsolated(t, data)
976 }
977
978 for n := 61680 - 16; n <= 61680; n++ {
979 data := b[:n]
980 testFreeIsolated(t, data)
981 }
982 }
983
984 func testFreeBlockList(t *testing.T, a, b int) {
985 var h [2]int64
986
987 t.Log(a, b)
988 dir, name := temp()
989 defer os.RemoveAll(dir)
990
991 f, err := fcreate(name)
992 if err != nil {
993 t.Fatal(err)
994 }
995
996 defer func() {
997 if f != nil {
998 if err := f.Close(); err != nil {
999 t.Fatal(err)
1000 }
1001 }
1002
1003 f = nil
1004 runtime.GC()
1005 os.Remove(name)
1006 }()
1007
1008 used0, total0, err := f.audit()
1009 if err != nil {
1010 t.Fatal(err)
1011 }
1012
1013 alloc(f, nil)
1014 h[0] = alloc(f, nil)
1015 alloc(f, nil)
1016 h[1] = alloc(f, nil)
1017 alloc(f, nil)
1018
1019 if err := f.Close(); err != nil {
1020 t.Fatal(err)
1021 }
1022
1023 f = nil
1024 runtime.GC()
1025 if f, err = fopen(name); err != nil {
1026 t.Fatal(err)
1027 }
1028
1029 used, total, err := f.audit()
1030 if err != nil {
1031 t.Fatal(err)
1032 }
1033
1034 if used-used0 != 5 || total-total0 != 5 || used != total {
1035 t.Fatal(used0, total0, used, total)
1036 }
1037
1038 free(f, h[a])
1039 free(f, h[b])
1040
1041 used, total, err = f.audit()
1042 if err != nil {
1043 t.Fatal(err)
1044 }
1045
1046 if used-used0 != 3 || total-total0 != 5 || total-used != 2 {
1047 t.Fatal(used0, total0, used, total)
1048 }
1049
1050 if err := f.Close(); err != nil {
1051 t.Fatal(err)
1052 }
1053
1054 f = nil
1055 runtime.GC()
1056 if f, err = fopen(name); err != nil {
1057 t.Fatal(err)
1058 }
1059
1060 used, total, err = f.audit()
1061 if err != nil {
1062 t.Fatal(err)
1063 }
1064
1065 if used-used0 != 3 || total-total0 != 5 || total-used != 2 {
1066 t.Fatal(used0, total0, used, total)
1067 }
1068 }
1069
1070 func TestFreeBlockList(t *testing.T) {
1071 testFreeBlockList(t, 0, 1)
1072 testFreeBlockList(t, 1, 0)
1073 }
1074
1075 func testFreeBlockList2(t *testing.T, a, b, c int) {
1076 var h [3]int64
1077
1078 dir, name := temp()
1079 defer os.RemoveAll(dir)
1080
1081 f, err := fcreate(name)
1082 if err != nil {
1083 t.Fatal(err)
1084 }
1085
1086 defer func() {
1087 if f != nil {
1088 if err := f.Close(); err != nil {
1089 t.Fatal(err)
1090 }
1091 }
1092
1093 f = nil
1094 runtime.GC()
1095 os.Remove(name)
1096 }()
1097
1098 used0, total0, err := f.audit()
1099 if err != nil {
1100 t.Fatal(err)
1101 }
1102
1103 alloc(f, nil)
1104 h[0] = alloc(f, nil)
1105 alloc(f, nil)
1106 h[1] = alloc(f, nil)
1107 alloc(f, nil)
1108 h[2] = alloc(f, nil)
1109 alloc(f, nil)
1110
1111 if err := f.Close(); err != nil {
1112 t.Fatal(err)
1113 }
1114
1115 f = nil
1116 runtime.GC()
1117 if f, err = fopen(name); err != nil {
1118 t.Fatal(err)
1119 }
1120
1121 used, total, err := f.audit()
1122 if err != nil {
1123 t.Fatal(err)
1124 }
1125
1126 if used-used0 != 7 || total-total0 != 7 || used != total {
1127 t.Fatal(used0, total0, used, total)
1128 }
1129
1130 free(f, h[a])
1131 free(f, h[b])
1132 free(f, h[c])
1133
1134 used, total, err = f.audit()
1135 if err != nil {
1136 t.Fatal(err)
1137 }
1138
1139 if used-used0 != 4 || total-total0 != 7 || total-used != 3 {
1140 t.Fatal(used0, total0, used, total)
1141 }
1142
1143 if err := f.Close(); err != nil {
1144 t.Fatal(err)
1145 }
1146
1147 f = nil
1148 runtime.GC()
1149 if f, err = fopen(name); err != nil {
1150 t.Fatal(err)
1151 }
1152
1153 used, total, err = f.audit()
1154 if err != nil {
1155 t.Fatal(err)
1156 }
1157
1158 if used-used0 != 4 || total-total0 != 7 || total-used != 3 {
1159 t.Fatal(used0, total0, used, total)
1160 }
1161 }
1162
1163 func TestFreeBlockList2(t *testing.T) {
1164 testFreeBlockList2(t, 0, 1, 2)
1165 testFreeBlockList2(t, 0, 2, 1)
1166 testFreeBlockList2(t, 1, 0, 2)
1167 testFreeBlockList2(t, 1, 2, 0)
1168 testFreeBlockList2(t, 2, 0, 1)
1169 testFreeBlockList2(t, 2, 1, 0)
1170 }
1171
1172 var crng *mathutil.FC32
1173
1174 func init() {
1175 var err error
1176 if crng, err = mathutil.NewFC32(0, math.MaxInt32, true); err != nil {
1177 panic(err)
1178 }
1179 }
1180
1181 func content(b []byte, h int64) (c []byte) {
1182 crng.Seed(h)
1183 crng.Seek(0)
1184 c = b[:crng.Next()%61681]
1185 for i := range c {
1186 c[i] = byte(crng.Next())
1187 }
1188 return
1189 }
1190
1191 func testFreeBlockList3(t *testing.T, n, mod int) {
1192 rng, err := mathutil.NewFC32(0, n-1, true)
1193 if err != nil {
1194 t.Fatal(err)
1195 }
1196
1197 dir, name := temp()
1198 defer os.RemoveAll(dir)
1199
1200 f, err := fcreate(name)
1201 if err != nil {
1202 t.Fatal(err)
1203 }
1204
1205 defer func() {
1206 if f != nil {
1207 if err := f.Close(); err != nil {
1208 t.Fatal(err)
1209 }
1210 }
1211
1212 f = nil
1213 runtime.GC()
1214 os.Remove(name)
1215 }()
1216
1217 ha := make([]int64, n)
1218 b := make([]byte, 61680)
1219 for i := range ha {
1220 h := f.atoms
1221 ha[i] = h
1222 c := content(b, h)
1223 if alloc(f, c) != h {
1224 t.Fatal(h)
1225 }
1226 }
1227 f = reaudit(t, f, name)
1228 del := map[int64]bool{}
1229 for _ = range ha {
1230 i := rng.Next()
1231 if i%mod != 0 {
1232 h := ha[i]
1233 free(f, h)
1234 del[h] = true
1235 }
1236 }
1237 f = reaudit(t, f, name)
1238 for _, h := range ha {
1239 if !del[h] {
1240 exp := content(b, h)
1241 got, _ := f.readUsed(h)
1242 if !bytes.Equal(exp, got) {
1243 t.Fatal(len(got), len(exp))
1244 }
1245 }
1246 }
1247 }
1248
1249 func TestFreeBlockList3(t *testing.T) {
1250 testFreeBlockList3(t, 111, 1)
1251 testFreeBlockList3(t, 151, 2)
1252 testFreeBlockList3(t, 170, 3)
1253 testFreeBlockList3(t, 170, 4)
1254 }
1255
1256 func TestRealloc1(t *testing.T) {
1257 dir, name := temp()
1258 defer os.RemoveAll(dir)
1259
1260 f, err := fcreate(name)
1261 if err != nil {
1262 t.Fatal(err)
1263 }
1264
1265 defer func() {
1266 if f != nil {
1267 if err := f.Close(); err != nil {
1268 t.Fatal(err)
1269 }
1270 }
1271
1272 f = nil
1273 runtime.GC()
1274 os.Remove(name)
1275 }()
1276
1277 b := make([]byte, 61680)
1278 c := content(b, 10)
1279
1280 h10 := alloc(f, nil)
1281 h20 := alloc(f, nil)
1282
1283 used0, total0, err := f.audit()
1284 if err != nil {
1285 t.Fatal(err)
1286 }
1287
1288 exp := c[:15]
1289 if handle := realloc(f, h10, exp, false); handle != h10 {
1290 t.Fatal(handle, h10)
1291 }
1292
1293 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
1294 t.Fatal(len(got), len(exp))
1295 }
1296
1297 if got, _ := f.readUsed(h20); len(got) != 0 {
1298 t.Fatal(len(got), 0)
1299 }
1300
1301 f = reaudit(t, f, name)
1302
1303 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
1304 t.Fatal(len(got), len(exp))
1305 }
1306
1307 if got, _ := f.readUsed(h20); len(got) != 0 {
1308 t.Fatal(len(got), 0)
1309 }
1310
1311 used, total, err := f.audit()
1312 if err != nil {
1313 t.Fatal(err)
1314 }
1315
1316 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 0 || free != 0 {
1317 t.Fatal(difused, diftotal, free)
1318 }
1319 }
1320
1321 func TestRealloc1Keep(t *testing.T) {
1322 dir, name := temp()
1323 defer os.RemoveAll(dir)
1324
1325 f, err := fcreate(name)
1326 if err != nil {
1327 t.Fatal(err)
1328 }
1329
1330 defer func() {
1331 if f != nil {
1332 if err := f.Close(); err != nil {
1333 t.Fatal(err)
1334 }
1335 }
1336
1337 f = nil
1338 runtime.GC()
1339 os.Remove(name)
1340 }()
1341
1342 b := make([]byte, 61680)
1343 c := content(b, 10)
1344
1345 h10 := alloc(f, nil)
1346 h20 := alloc(f, nil)
1347
1348 used0, total0, err := f.audit()
1349 if err != nil {
1350 t.Fatal(err)
1351 }
1352
1353 exp := c[:15]
1354 if handle := realloc(f, h10, exp, true); handle != h10 {
1355 t.Fatal(handle, h10)
1356 }
1357
1358 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
1359 t.Fatal(len(got), len(exp))
1360 }
1361
1362 if got, _ := f.readUsed(h20); len(got) != 0 {
1363 t.Fatal(len(got), 0)
1364 }
1365
1366 f = reaudit(t, f, name)
1367
1368 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
1369 t.Fatal(len(got), len(exp))
1370 }
1371
1372 if got, _ := f.readUsed(h20); len(got) != 0 {
1373 t.Fatal(len(got), 0)
1374 }
1375
1376 used, total, err := f.audit()
1377 if err != nil {
1378 t.Fatal(err)
1379 }
1380
1381 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 0 || free != 0 {
1382 t.Fatal(difused, diftotal, free)
1383 }
1384 }
1385
1386 func TestRealloc2(t *testing.T) {
1387 dir, name := temp()
1388 defer os.RemoveAll(dir)
1389
1390 f, err := fcreate(name)
1391 if err != nil {
1392 t.Fatal(err)
1393 }
1394
1395 defer func() {
1396 if f != nil {
1397 if err := f.Close(); err != nil {
1398 t.Fatal(err)
1399 }
1400 }
1401
1402 f = nil
1403 runtime.GC()
1404 os.Remove(name)
1405 }()
1406
1407 b := make([]byte, 61680)
1408 c := content(b, 10)
1409
1410 h10 := alloc(f, c[:31])
1411 h20 := alloc(f, nil)
1412
1413 used0, total0, err := f.audit()
1414 if err != nil {
1415 t.Fatal(err)
1416 }
1417
1418 exp := c[:15]
1419 if handle := realloc(f, h10, exp, false); handle != h10 {
1420 t.Fatal(handle, h10)
1421 }
1422
1423 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
1424 t.Fatal(len(got), len(exp))
1425 }
1426
1427 if got, _ := f.readUsed(h20); len(got) != 0 {
1428 t.Fatal(len(got), 0)
1429 }
1430
1431 f = reaudit(t, f, name)
1432
1433 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
1434 t.Fatal(len(got), len(exp))
1435 }
1436
1437 if got, _ := f.readUsed(h20); len(got) != 0 {
1438 t.Fatal(len(got), 0)
1439 }
1440
1441 used, total, err := f.audit()
1442 if err != nil {
1443 t.Fatal(err)
1444 }
1445
1446 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {
1447 t.Fatal(difused, diftotal, free)
1448 }
1449 }
1450
1451 func TestRealloc2Keep(t *testing.T) {
1452 dir, name := temp()
1453 defer os.RemoveAll(dir)
1454
1455 f, err := fcreate(name)
1456 if err != nil {
1457 t.Fatal(err)
1458 }
1459
1460 defer func() {
1461 if f != nil {
1462 if err := f.Close(); err != nil {
1463 t.Fatal(err)
1464 }
1465 }
1466
1467 f = nil
1468 runtime.GC()
1469 os.Remove(name)
1470 }()
1471
1472 b := make([]byte, 61680)
1473 c := content(b, 10)
1474
1475 h10 := alloc(f, c[:31])
1476 h20 := alloc(f, nil)
1477
1478 used0, total0, err := f.audit()
1479 if err != nil {
1480 t.Fatal(err)
1481 }
1482
1483 exp := c[:15]
1484 if handle := realloc(f, h10, exp, true); handle != h10 {
1485 t.Fatal(handle, h10)
1486 }
1487
1488 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
1489 t.Fatal(len(got), len(exp))
1490 }
1491
1492 if got, _ := f.readUsed(h20); len(got) != 0 {
1493 t.Fatal(len(got), 0)
1494 }
1495
1496 f = reaudit(t, f, name)
1497
1498 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
1499 t.Fatal(len(got), len(exp))
1500 }
1501
1502 if got, _ := f.readUsed(h20); len(got) != 0 {
1503 t.Fatal(len(got), 0)
1504 }
1505
1506 used, total, err := f.audit()
1507 if err != nil {
1508 t.Fatal(err)
1509 }
1510
1511 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {
1512 t.Fatal(difused, diftotal, free)
1513 }
1514 }
1515
1516 func TestRealloc3(t *testing.T) {
1517 dir, name := temp()
1518 defer os.RemoveAll(dir)
1519
1520 f, err := fcreate(name)
1521 if err != nil {
1522 t.Fatal(err)
1523 }
1524
1525 defer func() {
1526 if f != nil {
1527 if err := f.Close(); err != nil {
1528 t.Fatal(err)
1529 }
1530 }
1531
1532 f = nil
1533 runtime.GC()
1534 os.Remove(name)
1535 }()
1536
1537 b := make([]byte, 61680)
1538 c := content(b, 10)
1539
1540 h10 := alloc(f, nil)
1541 h20 := alloc(f, nil)
1542
1543 used0, total0, err := f.audit()
1544 if err != nil {
1545 t.Fatal(err)
1546 }
1547
1548 exp := c[:31]
1549 var handle int64
1550 if handle = realloc(f, h10, exp, false); handle == h10 {
1551 t.Fatal(handle, h10)
1552 }
1553
1554 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1555 t.Fatal(len(got), len(exp))
1556 }
1557
1558 if got, _ := f.readUsed(h20); len(got) != 0 {
1559 t.Fatal(len(got), 0)
1560 }
1561
1562 f = reaudit(t, f, name)
1563
1564 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1565 t.Fatal(len(got), len(exp))
1566 }
1567
1568 if got, _ := f.readUsed(h20); len(got) != 0 {
1569 t.Fatal(len(got), 0)
1570 }
1571
1572 used, total, err := f.audit()
1573 if err != nil {
1574 t.Fatal(err)
1575 }
1576
1577 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {
1578 t.Fatal(difused, diftotal, free)
1579 }
1580 }
1581
1582 func TestRealloc3Keep(t *testing.T) {
1583 dir, name := temp()
1584 defer os.RemoveAll(dir)
1585
1586 f, err := fcreate(name)
1587 if err != nil {
1588 t.Fatal(err)
1589 }
1590
1591 defer func() {
1592 if f != nil {
1593 if err := f.Close(); err != nil {
1594 t.Fatal(err)
1595 }
1596 }
1597
1598 f = nil
1599 runtime.GC()
1600 os.Remove(name)
1601 }()
1602
1603 b := make([]byte, 61680)
1604 c := content(b, 10)
1605
1606 h10 := alloc(f, nil)
1607 h20 := alloc(f, nil)
1608
1609 used0, total0, err := f.audit()
1610 if err != nil {
1611 t.Fatal(err)
1612 }
1613
1614 exp := c[:31]
1615 var handle int64
1616 if handle = realloc(f, h10, exp, true); handle != h10 {
1617 t.Fatal(handle, h10)
1618 }
1619
1620 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1621 t.Fatal(len(got), len(exp))
1622 }
1623
1624 if got, _ := f.readUsed(h20); len(got) != 0 {
1625 t.Fatal(len(got), 0)
1626 }
1627
1628 f = reaudit(t, f, name)
1629
1630 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1631 t.Fatal(len(got), len(exp))
1632 }
1633
1634 if got, _ := f.readUsed(h20); len(got) != 0 {
1635 t.Fatal(len(got), 0)
1636 }
1637
1638 used, total, err := f.audit()
1639 if err != nil {
1640 t.Fatal(err)
1641 }
1642
1643 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 1 || diftotal != 1 || free != 0 {
1644 t.Fatal(difused, diftotal, free)
1645 }
1646 }
1647
1648 func TestRealloc4Keep(t *testing.T) {
1649 dir, name := temp()
1650 defer os.RemoveAll(dir)
1651
1652 f, err := fcreate(name)
1653 if err != nil {
1654 t.Fatal(err)
1655 }
1656
1657 defer func() {
1658 if f != nil {
1659 if err := f.Close(); err != nil {
1660 t.Fatal(err)
1661 }
1662 }
1663
1664 f = nil
1665 runtime.GC()
1666 os.Remove(name)
1667 }()
1668
1669 b := make([]byte, 61680)
1670 c := content(b, 10)
1671
1672 h10 := alloc(f, c[:31])
1673 h20 := alloc(f, nil)
1674
1675 used0, total0, err := f.audit()
1676 if err != nil {
1677 t.Fatal(err)
1678 }
1679
1680 exp := c[:47]
1681 var handle int64
1682 if handle = realloc(f, h10, exp, true); handle != h10 {
1683 t.Fatal(handle, h10)
1684 }
1685
1686 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1687 t.Fatal(len(got), len(exp))
1688 }
1689
1690 if got, _ := f.readUsed(h20); len(got) != 0 {
1691 t.Fatal(len(got), 0)
1692 }
1693
1694 f = reaudit(t, f, name)
1695
1696 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1697 t.Fatal(len(got), len(exp))
1698 }
1699
1700 if got, _ := f.readUsed(h20); len(got) != 0 {
1701 t.Fatal(len(got), 0)
1702 }
1703
1704 used, total, err := f.audit()
1705 if err != nil {
1706 t.Fatal(err)
1707 }
1708
1709 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 1 || diftotal != 2 || free != 1 {
1710 t.Fatal(difused, diftotal, free)
1711 }
1712 }
1713
1714 func TestRealloc5(t *testing.T) {
1715 dir, name := temp()
1716 defer os.RemoveAll(dir)
1717
1718 f, err := fcreate(name)
1719 if err != nil {
1720 t.Fatal(err)
1721 }
1722
1723 defer func() {
1724 if f != nil {
1725 if err := f.Close(); err != nil {
1726 t.Fatal(err)
1727 }
1728 }
1729
1730 f = nil
1731 runtime.GC()
1732 os.Remove(name)
1733 }()
1734
1735 b := make([]byte, 61680)
1736 c := content(b, 10)
1737
1738 h10 := alloc(f, nil)
1739 h15 := alloc(f, nil)
1740 h20 := alloc(f, nil)
1741
1742 used0, total0, err := f.audit()
1743 if err != nil {
1744 t.Fatal(err)
1745 }
1746
1747 free(f, h15)
1748 exp := c[:31]
1749 var handle int64
1750 if handle = realloc(f, h10, exp, false); handle != h10 {
1751 t.Fatal(handle, h10)
1752 }
1753
1754 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1755 t.Fatal(len(got), len(exp))
1756 }
1757
1758 if got, _ := f.readUsed(h20); len(got) != 0 {
1759 t.Fatal(len(got), 0)
1760 }
1761
1762 f = reaudit(t, f, name)
1763
1764 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1765 t.Fatal(len(got), len(exp))
1766 }
1767
1768 if got, _ := f.readUsed(h20); len(got) != 0 {
1769 t.Fatal(len(got), 0)
1770 }
1771
1772 used, total, err := f.audit()
1773 if err != nil {
1774 t.Fatal(err)
1775 }
1776
1777 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -1 || free != 0 {
1778 t.Fatal(difused, diftotal, free)
1779 }
1780 }
1781
1782 func TestRealloc5Keep(t *testing.T) {
1783 dir, name := temp()
1784 defer os.RemoveAll(dir)
1785
1786 f, err := fcreate(name)
1787 if err != nil {
1788 t.Fatal(err)
1789 }
1790
1791 defer func() {
1792 if f != nil {
1793 if err := f.Close(); err != nil {
1794 t.Fatal(err)
1795 }
1796 }
1797
1798 f = nil
1799 runtime.GC()
1800 os.Remove(name)
1801 }()
1802
1803 b := make([]byte, 61680)
1804 c := content(b, 10)
1805
1806 h10 := alloc(f, nil)
1807 h15 := alloc(f, nil)
1808 h20 := alloc(f, nil)
1809
1810 used0, total0, err := f.audit()
1811 if err != nil {
1812 t.Fatal(err)
1813 }
1814
1815 free(f, h15)
1816 exp := c[:31]
1817 var handle int64
1818 if handle = realloc(f, h10, exp, true); handle != h10 {
1819 t.Fatal(handle, h10)
1820 }
1821
1822 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1823 t.Fatal(len(got), len(exp))
1824 }
1825
1826 if got, _ := f.readUsed(h20); len(got) != 0 {
1827 t.Fatal(len(got), 0)
1828 }
1829
1830 f = reaudit(t, f, name)
1831
1832 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1833 t.Fatal(len(got), len(exp))
1834 }
1835
1836 if got, _ := f.readUsed(h20); len(got) != 0 {
1837 t.Fatal(len(got), 0)
1838 }
1839
1840 used, total, err := f.audit()
1841 if err != nil {
1842 t.Fatal(err)
1843 }
1844
1845 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -1 || free != 0 {
1846 t.Fatal(difused, diftotal, free)
1847 }
1848 }
1849
1850 func TestRealloc6(t *testing.T) {
1851 dir, name := temp()
1852 defer os.RemoveAll(dir)
1853
1854 f, err := fcreate(name)
1855 if err != nil {
1856 t.Fatal(err)
1857 }
1858
1859 defer func() {
1860 if f != nil {
1861 if err := f.Close(); err != nil {
1862 t.Fatal(err)
1863 }
1864 }
1865
1866 f = nil
1867 runtime.GC()
1868 os.Remove(name)
1869 }()
1870
1871 b := make([]byte, 61680)
1872 c := content(b, 10)
1873
1874 h10 := alloc(f, nil)
1875 h15 := alloc(f, c[:31])
1876 h20 := alloc(f, nil)
1877
1878 used0, total0, err := f.audit()
1879 if err != nil {
1880 t.Fatal(err)
1881 }
1882
1883 free(f, h15)
1884 exp := c[:31]
1885 var handle int64
1886 if handle = realloc(f, h10, exp, false); handle != h10 {
1887 t.Fatal(handle, h10)
1888 }
1889
1890 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1891 t.Fatal(len(got), len(exp))
1892 }
1893
1894 if got, _ := f.readUsed(h20); len(got) != 0 {
1895 t.Fatal(len(got), 0)
1896 }
1897
1898 f = reaudit(t, f, name)
1899
1900 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1901 t.Fatal(len(got), len(exp))
1902 }
1903
1904 if got, _ := f.readUsed(h20); len(got) != 0 {
1905 t.Fatal(len(got), 0)
1906 }
1907
1908 used, total, err := f.audit()
1909 if err != nil {
1910 t.Fatal(err)
1911 }
1912
1913 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != 0 || free != 1 {
1914 t.Fatal(difused, diftotal, free)
1915 }
1916 }
1917
1918 func TestRealloc6Keep(t *testing.T) {
1919 dir, name := temp()
1920 defer os.RemoveAll(dir)
1921
1922 f, err := fcreate(name)
1923 if err != nil {
1924 t.Fatal(err)
1925 }
1926
1927 defer func() {
1928 if f != nil {
1929 if err := f.Close(); err != nil {
1930 t.Fatal(err)
1931 }
1932 }
1933
1934 f = nil
1935 runtime.GC()
1936 os.Remove(name)
1937 }()
1938
1939 b := make([]byte, 61680)
1940 c := content(b, 10)
1941
1942 h10 := alloc(f, nil)
1943 h15 := alloc(f, c[:31])
1944 h20 := alloc(f, nil)
1945
1946 used0, total0, err := f.audit()
1947 if err != nil {
1948 t.Fatal(err)
1949 }
1950
1951 free(f, h15)
1952 exp := c[:31]
1953 var handle int64
1954 if handle = realloc(f, h10, exp, true); handle != h10 {
1955 t.Fatal(handle, h10)
1956 }
1957
1958 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1959 t.Fatal(len(got), len(exp))
1960 }
1961
1962 if got, _ := f.readUsed(h20); len(got) != 0 {
1963 t.Fatal(len(got), 0)
1964 }
1965
1966 f = reaudit(t, f, name)
1967
1968 if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) {
1969 t.Fatal(len(got), len(exp))
1970 }
1971
1972 if got, _ := f.readUsed(h20); len(got) != 0 {
1973 t.Fatal(len(got), 0)
1974 }
1975
1976 used, total, err := f.audit()
1977 if err != nil {
1978 t.Fatal(err)
1979 }
1980
1981 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != 0 || free != 1 {
1982 t.Fatal(difused, diftotal, free)
1983 }
1984 }
1985
1986 func TestRelocRealloc1(t *testing.T) {
1987 dir, name := temp()
1988 defer os.RemoveAll(dir)
1989
1990 f, err := fcreate(name)
1991 if err != nil {
1992 t.Fatal(err)
1993 }
1994
1995 defer func() {
1996 if f != nil {
1997 if err := f.Close(); err != nil {
1998 t.Fatal(err)
1999 }
2000 }
2001
2002 f = nil
2003 runtime.GC()
2004 os.Remove(name)
2005 }()
2006
2007 b := make([]byte, 61680)
2008
2009 h10 := alloc(f, nil)
2010 h20 := alloc(f, nil)
2011 var handle int64
2012 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2013 t.Fatal(handle, h10)
2014 }
2015
2016 used0, total0, err := f.audit() // c+3, c+3
2017 if err != nil {
2018 t.Fatal(err)
2019 }
2020
2021 c := content(b, 10)
2022 exp := c[:15]
2023 if handle = realloc(f, h10, exp, false); handle != h10 {
2024 t.Fatal(handle, h10)
2025 }
2026
2027 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2028 t.Fatal(len(got), len(exp))
2029 }
2030
2031 if got, _ := f.readUsed(h20); len(got) != 0 {
2032 t.Fatal(len(got), 0)
2033 }
2034
2035 f = reaudit(t, f, name)
2036
2037 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2038 t.Fatal(len(got), len(exp))
2039 }
2040
2041 if got, _ := f.readUsed(h20); len(got) != 0 {
2042 t.Fatal(len(got), 0)
2043 }
2044
2045 used, total, err := f.audit() // c+2, c+2
2046 if err != nil {
2047 t.Fatal(err)
2048 }
2049
2050 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -1 || free != 0 {
2051 t.Fatal(difused, diftotal, free)
2052 }
2053 }
2054
2055 func TestRelocRealloc1Keep(t *testing.T) {
2056 dir, name := temp()
2057 defer os.RemoveAll(dir)
2058
2059 f, err := fcreate(name)
2060 if err != nil {
2061 t.Fatal(err)
2062 }
2063
2064 defer func() {
2065 if f != nil {
2066 if err := f.Close(); err != nil {
2067 t.Fatal(err)
2068 }
2069 }
2070
2071 f = nil
2072 runtime.GC()
2073 os.Remove(name)
2074 }()
2075
2076 b := make([]byte, 61680)
2077
2078 h10 := alloc(f, nil)
2079 h20 := alloc(f, nil)
2080 var handle int64
2081 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2082 t.Fatal(handle, h10)
2083 }
2084
2085 used0, total0, err := f.audit() // c+3, c+3
2086 if err != nil {
2087 t.Fatal(err)
2088 }
2089
2090 c := content(b, 10)
2091 exp := c[:15]
2092 if handle = realloc(f, h10, exp, true); handle != h10 {
2093 t.Fatal(handle, h10)
2094 }
2095
2096 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2097 t.Fatal(len(got), len(exp))
2098 }
2099
2100 if got, _ := f.readUsed(h20); len(got) != 0 {
2101 t.Fatal(len(got), 0)
2102 }
2103
2104 f = reaudit(t, f, name)
2105
2106 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2107 t.Fatal(len(got), len(exp))
2108 }
2109
2110 if got, _ := f.readUsed(h20); len(got) != 0 {
2111 t.Fatal(len(got), 0)
2112 }
2113
2114 used, total, err := f.audit() // c+2, c+2
2115 if err != nil {
2116 t.Fatal(err)
2117 }
2118
2119 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -1 || free != 0 {
2120 t.Fatal(difused, diftotal, free)
2121 }
2122 }
2123
2124 func TestRelocRealloc2(t *testing.T) {
2125 dir, name := temp()
2126 defer os.RemoveAll(dir)
2127
2128 f, err := fcreate(name)
2129 if err != nil {
2130 t.Fatal(err)
2131 }
2132
2133 defer func() {
2134 if f != nil {
2135 if err := f.Close(); err != nil {
2136 t.Fatal(err)
2137 }
2138 }
2139
2140 f = nil
2141 runtime.GC()
2142 os.Remove(name)
2143 }()
2144
2145 b := make([]byte, 61680)
2146
2147 h10 := alloc(f, nil)
2148 h20 := alloc(f, nil)
2149 var handle int64
2150 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2151 t.Fatal(handle, h10)
2152 }
2153
2154 free(f, h20)
2155
2156 used0, total0, err := f.audit() // c+2, c+3
2157 if err != nil {
2158 t.Fatal(err)
2159 }
2160
2161 c := content(b, 10)
2162 exp := c[:31]
2163 if handle = realloc(f, h10, exp, false); handle != h10 {
2164 t.Fatal(handle, h10)
2165 }
2166
2167 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2168 t.Fatal(len(got), len(exp))
2169 }
2170
2171 f = reaudit(t, f, name)
2172
2173 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2174 t.Fatal(len(got), len(exp))
2175 }
2176
2177 used, total, err := f.audit() // c+1, c+1
2178 if err != nil {
2179 t.Fatal(err)
2180 }
2181
2182 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -2 || free != 0 {
2183 t.Fatal(difused, diftotal, free)
2184 }
2185 }
2186
2187 func TestRelocRealloc2Keep(t *testing.T) {
2188 dir, name := temp()
2189 defer os.RemoveAll(dir)
2190
2191 f, err := fcreate(name)
2192 if err != nil {
2193 t.Fatal(err)
2194 }
2195
2196 defer func() {
2197 if f != nil {
2198 if err := f.Close(); err != nil {
2199 t.Fatal(err)
2200 }
2201 }
2202
2203 f = nil
2204 runtime.GC()
2205 os.Remove(name)
2206 }()
2207
2208 b := make([]byte, 61680)
2209
2210 h10 := alloc(f, nil)
2211 h20 := alloc(f, nil)
2212 var handle int64
2213 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2214 t.Fatal(handle, h10)
2215 }
2216
2217 free(f, h20)
2218
2219 used0, total0, err := f.audit() // c+2, c+3
2220 if err != nil {
2221 t.Fatal(err)
2222 }
2223
2224 c := content(b, 10)
2225 exp := c[:31]
2226 if handle = realloc(f, h10, exp, true); handle != h10 {
2227 t.Fatal(handle, h10)
2228 }
2229
2230 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2231 t.Fatal(len(got), len(exp))
2232 }
2233
2234 f = reaudit(t, f, name)
2235
2236 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2237 t.Fatal(len(got), len(exp))
2238 }
2239
2240 used, total, err := f.audit() // c+1, c+1
2241 if err != nil {
2242 t.Fatal(err)
2243 }
2244
2245 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -2 || free != 0 {
2246 t.Fatal(difused, diftotal, free)
2247 }
2248 }
2249
2250 func TestRelocRealloc3(t *testing.T) {
2251 dir, name := temp()
2252 defer os.RemoveAll(dir)
2253
2254 f, err := fcreate(name)
2255 if err != nil {
2256 t.Fatal(err)
2257 }
2258
2259 defer func() {
2260 if f != nil {
2261 if err := f.Close(); err != nil {
2262 t.Fatal(err)
2263 }
2264 }
2265
2266 f = nil
2267 runtime.GC()
2268 os.Remove(name)
2269 }()
2270
2271 b := make([]byte, 61680)
2272
2273 h10 := alloc(f, nil)
2274 h20 := alloc(f, b[:31])
2275 var handle int64
2276 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2277 t.Fatal(handle, h10)
2278 }
2279
2280 free(f, h20)
2281
2282 used0, total0, err := f.audit() // c+2, c+3
2283 if err != nil {
2284 t.Fatal(err)
2285 }
2286
2287 c := content(b, 10)
2288 exp := c[:31]
2289 if handle = realloc(f, h10, exp, false); handle != h10 {
2290 t.Fatal(handle, h10)
2291 }
2292
2293 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2294 t.Fatal(len(got), len(exp))
2295 }
2296
2297 f = reaudit(t, f, name)
2298
2299 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2300 t.Fatal(len(got), len(exp))
2301 }
2302
2303 used, total, err := f.audit() // c+1, c+1
2304 if err != nil {
2305 t.Fatal(err)
2306 }
2307
2308 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -2 || free != 0 {
2309 t.Fatal(difused, diftotal, free)
2310 }
2311 }
2312
2313 func TestRelocRealloc3Keep(t *testing.T) {
2314 dir, name := temp()
2315 defer os.RemoveAll(dir)
2316
2317 f, err := fcreate(name)
2318 if err != nil {
2319 t.Fatal(err)
2320 }
2321
2322 defer func() {
2323 if f != nil {
2324 if err := f.Close(); err != nil {
2325 t.Fatal(err)
2326 }
2327 }
2328
2329 f = nil
2330 runtime.GC()
2331 os.Remove(name)
2332 }()
2333
2334 b := make([]byte, 61680)
2335
2336 h10 := alloc(f, nil)
2337 h20 := alloc(f, b[:31])
2338 var handle int64
2339 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2340 t.Fatal(handle, h10)
2341 }
2342
2343 free(f, h20)
2344
2345 used0, total0, err := f.audit() // c+2, c+3
2346 if err != nil {
2347 t.Fatal(err)
2348 }
2349
2350 c := content(b, 10)
2351 exp := c[:31]
2352 if handle = realloc(f, h10, exp, true); handle != h10 {
2353 t.Fatal(handle, h10)
2354 }
2355
2356 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2357 t.Fatal(len(got), len(exp))
2358 }
2359
2360 f = reaudit(t, f, name)
2361
2362 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2363 t.Fatal(len(got), len(exp))
2364 }
2365
2366 used, total, err := f.audit() // c+1, c+1
2367 if err != nil {
2368 t.Fatal(err)
2369 }
2370
2371 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != -2 || free != 0 {
2372 t.Fatal(difused, diftotal, free)
2373 }
2374 }
2375
2376 func TestRelocRealloc4(t *testing.T) {
2377 dir, name := temp()
2378 defer os.RemoveAll(dir)
2379
2380 f, err := fcreate(name)
2381 if err != nil {
2382 t.Fatal(err)
2383 }
2384
2385 defer func() {
2386 if f != nil {
2387 if err := f.Close(); err != nil {
2388 t.Fatal(err)
2389 }
2390 }
2391
2392 f = nil
2393 runtime.GC()
2394 os.Remove(name)
2395 }()
2396
2397 b := make([]byte, 61680)
2398
2399 h10 := alloc(f, nil)
2400 _ = alloc(f, nil)
2401 var handle int64
2402 if handle = realloc(f, h10, b[:47], true); handle != h10 {
2403 t.Fatal(handle, h10)
2404 }
2405
2406 _ = alloc(f, nil)
2407
2408 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2409 t.Fatal(handle, h10)
2410 }
2411
2412 used0, total0, err := f.audit() // c+4, c+5
2413 if err != nil {
2414 t.Fatal(err)
2415 }
2416
2417 c := content(b, 10)
2418 exp := c[:47]
2419 if handle = realloc(f, h10, exp, false); handle != h10 {
2420 t.Fatal(handle, h10)
2421 }
2422
2423 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2424 t.Fatal(len(got), len(exp))
2425 }
2426
2427 f = reaudit(t, f, name)
2428
2429 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2430 t.Fatal(len(got), len(exp))
2431 }
2432
2433 used, total, err := f.audit() // c+4, c+4
2434 if err != nil {
2435 t.Fatal(err)
2436 }
2437
2438 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != -1 || free != 0 {
2439 t.Fatal(difused, diftotal, free)
2440 }
2441 }
2442
2443 func TestRelocRealloc4Keep(t *testing.T) {
2444 dir, name := temp()
2445 defer os.RemoveAll(dir)
2446
2447 f, err := fcreate(name)
2448 if err != nil {
2449 t.Fatal(err)
2450 }
2451
2452 defer func() {
2453 if f != nil {
2454 if err := f.Close(); err != nil {
2455 t.Fatal(err)
2456 }
2457 }
2458
2459 f = nil
2460 runtime.GC()
2461 os.Remove(name)
2462 }()
2463
2464 b := make([]byte, 61680)
2465
2466 h10 := alloc(f, nil)
2467 _ = alloc(f, nil)
2468 var handle int64
2469 if handle = realloc(f, h10, b[:47], true); handle != h10 {
2470 t.Fatal(handle, h10)
2471 }
2472
2473 _ = alloc(f, nil)
2474
2475 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2476 t.Fatal(handle, h10)
2477 }
2478
2479 used0, total0, err := f.audit() // c+4, c+5
2480 if err != nil {
2481 t.Fatal(err)
2482 }
2483
2484 c := content(b, 10)
2485 exp := c[:47]
2486 if handle = realloc(f, h10, exp, true); handle != h10 {
2487 t.Fatal(handle, h10)
2488 }
2489
2490 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2491 t.Fatal(len(got), len(exp))
2492 }
2493
2494 f = reaudit(t, f, name)
2495
2496 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2497 t.Fatal(len(got), len(exp))
2498 }
2499
2500 used, total, err := f.audit() // c+4, c+4
2501 if err != nil {
2502 t.Fatal(err)
2503 }
2504
2505 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != -1 || free != 0 {
2506 t.Fatal(difused, diftotal, free)
2507 }
2508 }
2509
2510 func TestRelocRealloc5(t *testing.T) {
2511 dir, name := temp()
2512 defer os.RemoveAll(dir)
2513
2514 f, err := fcreate(name)
2515 if err != nil {
2516 t.Fatal(err)
2517 }
2518
2519 defer func() {
2520 if f != nil {
2521 if err := f.Close(); err != nil {
2522 t.Fatal(err)
2523 }
2524 }
2525
2526 f = nil
2527 runtime.GC()
2528 os.Remove(name)
2529 }()
2530
2531 b := make([]byte, 61680)
2532
2533 h10 := alloc(f, nil)
2534 _ = alloc(f, nil)
2535 var handle int64
2536 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2537 t.Fatal(handle, h10)
2538 }
2539
2540 _ = alloc(f, nil)
2541
2542 used0, total0, err := f.audit() // c+4, c+4
2543 if err != nil {
2544 t.Fatal(err)
2545 }
2546
2547 c := content(b, 10)
2548 exp := c[:47]
2549 if handle = realloc(f, h10, exp, false); handle != h10 {
2550 t.Fatal(handle, h10)
2551 }
2552
2553 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2554 t.Fatal(len(got), len(exp))
2555 }
2556
2557 f = reaudit(t, f, name)
2558
2559 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2560 t.Fatal(len(got), len(exp))
2561 }
2562
2563 used, total, err := f.audit() // c+4, c+5
2564 if err != nil {
2565 t.Fatal(err)
2566 }
2567
2568 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {
2569 t.Fatal(difused, diftotal, free)
2570 }
2571 }
2572
2573 func TestRelocRealloc5Keep(t *testing.T) {
2574 dir, name := temp()
2575 defer os.RemoveAll(dir)
2576
2577 f, err := fcreate(name)
2578 if err != nil {
2579 t.Fatal(err)
2580 }
2581
2582 defer func() {
2583 if f != nil {
2584 if err := f.Close(); err != nil {
2585 t.Fatal(err)
2586 }
2587 }
2588
2589 f = nil
2590 runtime.GC()
2591 os.Remove(name)
2592 }()
2593
2594 b := make([]byte, 61680)
2595
2596 h10 := alloc(f, nil)
2597 _ = alloc(f, nil)
2598 var handle int64
2599 if handle = realloc(f, h10, b[:31], true); handle != h10 {
2600 t.Fatal(handle, h10)
2601 }
2602
2603 _ = alloc(f, nil)
2604
2605 used0, total0, err := f.audit() // c+4, c+4
2606 if err != nil {
2607 t.Fatal(err)
2608 }
2609
2610 c := content(b, 10)
2611 exp := c[:47]
2612 if handle = realloc(f, h10, exp, true); handle != h10 {
2613 t.Fatal(handle, h10)
2614 }
2615
2616 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2617 t.Fatal(len(got), len(exp))
2618 }
2619
2620 f = reaudit(t, f, name)
2621
2622 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2623 t.Fatal(len(got), len(exp))
2624 }
2625
2626 used, total, err := f.audit() // c+4, c+5
2627 if err != nil {
2628 t.Fatal(err)
2629 }
2630
2631 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 1 || free != 1 {
2632 t.Fatal(difused, diftotal, free)
2633 }
2634 }
2635
2636 func TestRelocRealloc6(t *testing.T) {
2637 dir, name := temp()
2638 defer os.RemoveAll(dir)
2639
2640 f, err := fcreate(name)
2641 if err != nil {
2642 t.Fatal(err)
2643 }
2644
2645 defer func() {
2646 if f != nil {
2647 if err := f.Close(); err != nil {
2648 t.Fatal(err)
2649 }
2650 }
2651
2652 f = nil
2653 runtime.GC()
2654 os.Remove(name)
2655 }()
2656
2657 b := make([]byte, 61680)
2658
2659 h10 := alloc(f, b[:31])
2660 h20 := alloc(f, nil)
2661 _ = alloc(f, nil)
2662 free(f, h20)
2663
2664 used0, total0, err := f.audit() // c+2, c+3
2665 if err != nil {
2666 t.Fatal(err)
2667 }
2668
2669 c := content(b, 10)
2670 exp := c[:15]
2671 if handle := realloc(f, h10, exp, false); handle != h10 {
2672 t.Fatal(handle, h10)
2673 }
2674
2675 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2676 t.Fatal(len(got), len(exp))
2677 }
2678
2679 f = reaudit(t, f, name)
2680
2681 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2682 t.Fatal(len(got), len(exp))
2683 }
2684
2685 used, total, err := f.audit() // c+2, c+3
2686 if err != nil {
2687 t.Fatal(err)
2688 }
2689
2690 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 0 || free != 1 {
2691 t.Fatal(difused, diftotal, free)
2692 }
2693 }
2694
2695 func TestRelocRealloc6Keep(t *testing.T) {
2696 dir, name := temp()
2697 defer os.RemoveAll(dir)
2698
2699 f, err := fcreate(name)
2700 if err != nil {
2701 t.Fatal(err)
2702 }
2703
2704 defer func() {
2705 if f != nil {
2706 if err := f.Close(); err != nil {
2707 t.Fatal(err)
2708 }
2709 }
2710
2711 f = nil
2712 runtime.GC()
2713 os.Remove(name)
2714 }()
2715
2716 b := make([]byte, 61680)
2717
2718 h10 := alloc(f, b[:31])
2719 h20 := alloc(f, nil)
2720 _ = alloc(f, nil)
2721 free(f, h20)
2722
2723 used0, total0, err := f.audit() // c+2, c+3
2724 if err != nil {
2725 t.Fatal(err)
2726 }
2727
2728 c := content(b, 10)
2729 exp := c[:15]
2730 if handle := realloc(f, h10, exp, true); handle != h10 {
2731 t.Fatal(handle, h10)
2732 }
2733
2734 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2735 t.Fatal(len(got), len(exp))
2736 }
2737
2738 f = reaudit(t, f, name)
2739
2740 if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) {
2741 t.Fatal(len(got), len(exp))
2742 }
2743
2744 used, total, err := f.audit() // c+2, c+3
2745 if err != nil {
2746 t.Fatal(err)
2747 }
2748
2749 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 0 || diftotal != 0 || free != 1 {
2750 t.Fatal(difused, diftotal, free)
2751 }
2752 }
2753
2754 func TestFreespaceReuse(t *testing.T) {
2755 dir, name := temp()
2756 defer os.RemoveAll(dir)
2757
2758 f, err := fcreate(name)
2759 if err != nil {
2760 t.Fatal(err)
2761 }
2762
2763 defer func() {
2764 if f != nil {
2765 if err := f.Close(); err != nil {
2766 t.Fatal(err)
2767 }
2768 }
2769
2770 f = nil
2771 runtime.GC()
2772 os.Remove(name)
2773 }()
2774
2775 b := make([]byte, 61680)
2776 c := content(b, 10)
2777
2778 c10 := c[0 : 0+15]
2779 c20 := c[16:63]
2780 c50 := c[64 : 64+15]
2781 h10 := alloc(f, c10)
2782 h201 := alloc(f, nil)
2783 h202 := alloc(f, nil)
2784 h203 := alloc(f, nil)
2785 h50 := alloc(f, c50)
2786 free(f, h201)
2787 free(f, h202)
2788 free(f, h203)
2789 used0, total0, err := f.audit() // c+2, c+3
2790 if err != nil {
2791 t.Fatal(err)
2792 }
2793
2794 h20 := alloc(f, c20)
2795
2796 if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) {
2797 t.Fatal()
2798 }
2799
2800 if got, _ := f.readUsed(h20); !bytes.Equal(got, c20) {
2801 t.Fatal()
2802 }
2803
2804 if got, _ := f.readUsed(h50); !bytes.Equal(got, c50) {
2805 t.Fatal()
2806 }
2807
2808 f = reaudit(t, f, name)
2809
2810 if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) {
2811 t.Fatal()
2812 }
2813
2814 if got, _ := f.readUsed(h20); !bytes.Equal(got, c20) {
2815 t.Fatal()
2816 }
2817
2818 if got, _ := f.readUsed(h50); !bytes.Equal(got, c50) {
2819 t.Fatal()
2820 }
2821
2822 used, total, err := f.audit() // c+3, c+3
2823 if err != nil {
2824 t.Fatal(err)
2825 }
2826
2827 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 1 || diftotal != 0 || free != 0 {
2828 t.Fatal(difused, diftotal, free)
2829 }
2830 }
2831
2832 func TestFreespaceReuse2(t *testing.T) {
2833 dir, name := temp()
2834 defer os.RemoveAll(dir)
2835
2836 f, err := fcreate(name)
2837 if err != nil {
2838 t.Fatal(err)
2839 }
2840
2841 defer func() {
2842 if f != nil {
2843 if err := f.Close(); err != nil {
2844 t.Fatal(err)
2845 }
2846 }
2847
2848 f = nil
2849 runtime.GC()
2850 os.Remove(name)
2851 }()
2852
2853 b := make([]byte, 61680)
2854 c := content(b, 10)
2855
2856 c10 := c[0 : 0+15]
2857 c20 := c[16:47]
2858 c50 := c[64 : 64+15]
2859 h10 := alloc(f, c10)
2860 h201 := alloc(f, nil)
2861 h202 := alloc(f, nil)
2862 h203 := alloc(f, nil)
2863 h50 := alloc(f, c50)
2864 free(f, h201)
2865 free(f, h202)
2866 free(f, h203)
2867 used0, total0, err := f.audit() // c+2, c+3
2868 if err != nil {
2869 t.Fatal(err)
2870 }
2871
2872 h20 := alloc(f, c20)
2873
2874 if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) {
2875 t.Fatal()
2876 }
2877
2878 if got, _ := f.readUsed(h20); !bytes.Equal(got, c20) {
2879 t.Fatal()
2880 }
2881
2882 if got, _ := f.readUsed(h50); !bytes.Equal(got, c50) {
2883 t.Fatal()
2884 }
2885
2886 f = reaudit(t, f, name)
2887
2888 if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) {
2889 t.Fatal()
2890 }
2891
2892 if got, _ := f.readUsed(h20); !bytes.Equal(got, c20) {
2893 t.Fatal()
2894 }
2895
2896 if got, _ := f.readUsed(h50); !bytes.Equal(got, c50) {
2897 t.Fatal()
2898 }
2899
2900 used, total, err := f.audit() // c+3, c+4
2901 if err != nil {
2902 t.Fatal(err)
2903 }
2904
2905 if difused, diftotal, free := used-used0, total-total0, total-used; difused != 1 || diftotal != 1 || free != 1 {
2906 t.Fatal(difused, diftotal, free)
2907 }
2908 }
2909
2910 func testBug1(t *testing.T, swap bool) {
2911 // Free lists table item for size 3856 points to list of free blocks
2912 // NOT of size 3856 but at least 3856.
2913
2914 dir, name := temp()
2915 defer os.RemoveAll(dir)
2916
2917 f, err := fcreate(name)
2918 if err != nil {
2919 t.Fatal(err)
2920 }
2921
2922 defer func() {
2923 if f != nil {
2924 if err := f.Close(); err != nil {
2925 t.Fatal(err)
2926 }
2927 }
2928
2929 f = nil
2930 runtime.GC()
2931 os.Remove(name)
2932 }()
2933
2934 _ = alloc(f, nil)
2935 b := make([]byte, 61680)
2936 f1 := alloc(f, b)
2937 f2 := alloc(f, b)
2938 _ = alloc(f, nil)
2939
2940 used0, total0, err := f.audit() // c+4, c+4
2941 if err != nil {
2942 t.Fatal(err)
2943 }
2944
2945 if swap {
2946 f1, f2 = f2, f1
2947 }
2948 free(f, f1)
2949 free(f, f2)
2950 _ = alloc(f, nil)
2951
2952 f = reaudit(t, f, name)
2953
2954 used, total, err := f.audit() // c+3, c+4
2955 if err != nil {
2956 t.Fatal(err)
2957 }
2958
2959 if difused, diftotal, free := used-used0, total-total0, total-used; difused != -1 || diftotal != 0 || free != 1 {
2960 t.Fatal(difused, diftotal, free)
2961 }
2962 }
2963
2964 func TestBug1(t *testing.T) {
2965 testBug1(t, false)
2966 testBug1(t, true)
2967 }
2968
2969 func TestMix(t *testing.T) {
2970 if testing.Short() {
2971 t.Log("skipped")
2972 return
2973 }
2974
2975 const (
2976 n = 1 << 10
2977 )
2978
2979 if testing.Short() {
2980 t.Log("skipped")
2981 return
2982 }
2983
2984 t.Log(n)
2985 dir, name := temp()
2986 defer os.RemoveAll(dir)
2987
2988 f, err := fcreate(name)
2989 if err != nil {
2990 t.Fatal(err)
2991 }
2992
2993 defer func() {
2994 if f != nil {
2995 if err := f.Close(); err != nil {
2996 t.Fatal(err)
2997 }
2998 }
2999
3000 f = nil
3001 runtime.GC()
3002 os.Remove(name)
3003 }()
3004
3005 b := make([]byte, 61680)
3006 rng, err := mathutil.NewFC32(0, n-1, true)
3007 if err != nil {
3008 t.Fatal(err)
3009 }
3010
3011 ha := make([]int64, n)
3012 payload := 0
3013
3014 t0 := time.Now()
3015 // Alloc n block with upper half of content
3016 for _ = range ha {
3017 r := rng.Next()
3018 c := content(b, int64(r))
3019 c = c[len(c)/2:]
3020 ha[r] = alloc(f, c)
3021 payload += len(c)
3022 }
3023 dt := float64(time.Now().Sub(t0)) / 1e9
3024 t.Logf("write time A %.3g", dt)
3025
3026 // verify
3027 f = reaudit(t, f, name)
3028 t.Logf("size A %d for %d bytes (fill factor %3.1f%%)", f.atoms<<4, payload, 100*float64(payload)/float64(f.atoms<<4))
3029 t0 = time.Now()
3030 for _ = range ha {
3031 r := rng.Next()
3032 c := content(b, int64(r))
3033 c = c[len(c)/2:]
3034 if got, _ := f.readUsed(ha[r]); !bytes.Equal(got, c) {
3035 t.Fatal()
3036 }
3037 }
3038 dt = float64(time.Now().Sub(t0)) / 1e9
3039 t.Logf("read time A %.3g", dt)
3040 // free half of the blocks
3041 t0 = time.Now()
3042 for i := 0; i < n/2; i++ {
3043 free(f, ha[i])
3044 ha[i] = 0
3045 }
3046 dt = float64(time.Now().Sub(t0)) / 1e9
3047 t.Logf("free time A %.3g", dt)
3048
3049 // verify
3050 f = reaudit(t, f, name)
3051 t.Logf("size B %d (freeing half of the blocks)", f.atoms<<4)
3052 t0 = time.Now()
3053 for _ = range ha {
3054 r := rng.Next()
3055 h := ha[r]
3056 if h == 0 {
3057 continue
3058 }
3059
3060 c := content(b, int64(r))
3061 c = c[len(c)/2:]
3062 if got, _ := f.readUsed(h); !bytes.Equal(got, c) {
3063 t.Fatal()
3064 }
3065 }
3066 dt = float64(time.Now().Sub(t0)) / 1e9
3067 t.Logf("read time B %.3g", dt)
3068
3069 // reloc extend
3070 t0 = time.Now()
3071 for _ = range ha {
3072 r := rng.Next()
3073 h := ha[r]
3074 if h == 0 {
3075 continue
3076 }
3077
3078 c := content(b, int64(r))
3079 //f = reaudit(t, f, name)
3080 if h2 := realloc(f, h, c, true); h2 != h {
3081 t.Fatal()
3082 }
3083 }
3084 dt = float64(time.Now().Sub(t0)) / 1e9
3085 t.Logf("realoc time B %.3g", dt)
3086
3087 // verify
3088 f = reaudit(t, f, name)
3089 t.Logf("size C %d for %d bytes (reallocated all used blocks to double size, fill factor %3.1f%%", f.atoms<<4, payload, 100*float64(payload)/float64(f.atoms<<4))
3090
3091 t0 = time.Now()
3092 for _ = range ha {
3093 r := rng.Next()
3094 h := ha[r]
3095 if h == 0 {
3096 continue
3097 }
3098
3099 c := content(b, int64(r))
3100 if got, _ := f.readUsed(ha[r]); !bytes.Equal(got, c) {
3101 t.Fatal()
3102 }
3103 }
3104 dt = float64(time.Now().Sub(t0)) / 1e9
3105 t.Logf("read time C %.3g", dt)
3106 }