Debian Codegraph @

all_test.go

raw symbols SCIP
1 // Copyright 2017 The Memory Authors. 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 package memory // import "modernc.org/memory"
6
7 import (
8 "bytes"
9 "fmt"
10 "math"
11 "os"
12 "path"
13 "runtime"
14 "strings"
15 "testing"
16 "unsafe"
17
18 "modernc.org/mathutil"
19 )
20
21 func caller(s string, va ...interface{}) {
22 if s == "" {
23 s = strings.Repeat("%v ", len(va))
24 }
25 _, fn, fl, _ := runtime.Caller(2)
26 fmt.Fprintf(os.Stderr, "# caller: %s:%d: ", path.Base(fn), fl)
27 fmt.Fprintf(os.Stderr, s, va...)
28 fmt.Fprintln(os.Stderr)
29 _, fn, fl, _ = runtime.Caller(1)
30 fmt.Fprintf(os.Stderr, "# \tcallee: %s:%d: ", path.Base(fn), fl)
31 fmt.Fprintln(os.Stderr)
32 os.Stderr.Sync()
33 }
34
35 func dbg(s string, va ...interface{}) {
36 if s == "" {
37 s = strings.Repeat("%v ", len(va))
38 }
39 _, fn, fl, _ := runtime.Caller(1)
40 fmt.Fprintf(os.Stderr, "# dbg %s:%d: ", path.Base(fn), fl)
41 fmt.Fprintf(os.Stderr, s, va...)
42 fmt.Fprintln(os.Stderr)
43 os.Stderr.Sync()
44 }
45
46 func TODO(...interface{}) string { //TODOOK
47 _, fn, fl, _ := runtime.Caller(1)
48 return fmt.Sprintf("# TODO: %s:%d:\n", path.Base(fn), fl) //TODOOK
49 }
50
51 func use(...interface{}) {}
52
53 func init() {
54 use(caller, dbg, TODO) //TODOOK
55 }
56
57 // ============================================================================
58
59 const quota = 128 << 20
60
61 var (
62 max = 2 * osPageSize
63 bigMax = 2 * pageSize
64 )
65
66 type block struct {
67 p uintptr
68 size int
69 }
70
71 func test1u(t *testing.T, max int) {
72 var alloc Allocator
73
74 defer alloc.Close()
75
76 rem := quota
77 var a []block
78 srng, err := mathutil.NewFC32(0, math.MaxInt32, true)
79 if err != nil {
80 t.Fatal(err)
81 }
82
83 vrng, err := mathutil.NewFC32(0, math.MaxInt32, true)
84 if err != nil {
85 t.Fatal(err)
86 }
87
88 // Allocate
89 for rem > 0 {
90 size := srng.Next()%max + 1
91 rem -= size
92 p, err := alloc.UintptrMalloc(size)
93 if err != nil {
94 t.Fatal(err)
95 }
96
97 a = append(a, block{p, size})
98 for i := 0; i < size; i++ {
99 *(*byte)(unsafe.Pointer(p + uintptr(i))) = byte(vrng.Next())
100 }
101 }
102 if counters {
103 t.Logf("allocs %v, mmaps %v, bytes %v, overhead %v (%.2f%%).", alloc.Allocs, alloc.Mmaps, alloc.Bytes, alloc.Bytes-quota, 100*float64(alloc.Bytes-quota)/quota)
104 }
105 srng.Seek(0)
106 vrng.Seek(0)
107 // Verify
108 for i, b := range a {
109 if g, e := b.size, srng.Next()%max+1; g != e {
110 t.Fatal(i, g, e)
111 }
112
113 if a, b := b.size, UintptrUsableSize(b.p); a > b {
114 t.Fatal(i, a, b)
115 }
116
117 for j := 0; j < b.size; j++ {
118 g := *(*byte)(unsafe.Pointer(b.p + uintptr(j)))
119 if e := byte(vrng.Next()); g != e {
120 t.Fatalf("%v,%v %#x: %#02x %#02x", i, j, b.p+uintptr(j), g, e)
121 }
122
123 *(*byte)(unsafe.Pointer(b.p + uintptr(j))) = 0
124 }
125 }
126 // Shuffle
127 for i := range a {
128 j := srng.Next() % len(a)
129 a[i], a[j] = a[j], a[i]
130 }
131 // Free
132 for _, b := range a {
133 if err := alloc.UintptrFree(b.p); err != nil {
134 t.Fatal(err)
135 }
136 }
137 if err := alloc.Trim(); err != nil {
138 t.Fatal(err)
139 }
140
141 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
142 t.Fatalf("%+v", alloc)
143 }
144 }
145
146 func Test1USmall(t *testing.T) { test1u(t, max) }
147 func Test1UBig(t *testing.T) { test1u(t, bigMax) }
148
149 func test2u(t *testing.T, max int) {
150 var alloc Allocator
151
152 defer alloc.Close()
153
154 rem := quota
155 var a []block
156 srng, err := mathutil.NewFC32(0, math.MaxInt32, true)
157 if err != nil {
158 t.Fatal(err)
159 }
160
161 vrng, err := mathutil.NewFC32(0, math.MaxInt32, true)
162 if err != nil {
163 t.Fatal(err)
164 }
165
166 // Allocate
167 for rem > 0 {
168 size := srng.Next()%max + 1
169 rem -= size
170 p, err := alloc.UintptrMalloc(size)
171 if err != nil {
172 t.Fatal(err)
173 }
174
175 a = append(a, block{p, size})
176 for i := 0; i < size; i++ {
177 *(*byte)(unsafe.Pointer(p + uintptr(i))) = byte(vrng.Next())
178 }
179 }
180 if counters {
181 t.Logf("allocs %v, mmaps %v, bytes %v, overhead %v (%.2f%%).", alloc.Allocs, alloc.Mmaps, alloc.Bytes, alloc.Bytes-quota, 100*float64(alloc.Bytes-quota)/quota)
182 }
183 srng.Seek(0)
184 vrng.Seek(0)
185 // Verify & free
186 for i, b := range a {
187 if g, e := b.size, srng.Next()%max+1; g != e {
188 t.Fatal(i, g, e)
189 }
190
191 if a, b := b.size, UintptrUsableSize(b.p); a > b {
192 t.Fatal(i, a, b)
193 }
194
195 for j := 0; j < b.size; j++ {
196 g := *(*byte)(unsafe.Pointer(b.p + uintptr(j)))
197 if e := byte(vrng.Next()); g != e {
198 t.Fatalf("%v,%v %#x: %#02x %#02x", i, j, b.p+uintptr(j), g, e)
199 }
200
201 *(*byte)(unsafe.Pointer(b.p + uintptr(j))) = 0
202 }
203 if err := alloc.UintptrFree(b.p); err != nil {
204 t.Fatal(err)
205 }
206 }
207 if err := alloc.Trim(); err != nil {
208 t.Fatal(err)
209 }
210
211 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
212 t.Fatalf("%+v", alloc)
213 }
214 }
215
216 func Test2USmall(t *testing.T) { test2u(t, max) }
217 func Test2UBig(t *testing.T) { test2u(t, bigMax) }
218
219 func test3u(t *testing.T, max int) {
220 var alloc Allocator
221
222 defer alloc.Close()
223
224 rem := quota
225 m := map[block][]byte{}
226 srng, err := mathutil.NewFC32(1, max, true)
227 if err != nil {
228 t.Fatal(err)
229 }
230
231 vrng, err := mathutil.NewFC32(1, max, true)
232 if err != nil {
233 t.Fatal(err)
234 }
235
236 for rem > 0 {
237 switch srng.Next() % 3 {
238 case 0, 1: // 2/3 allocate
239 size := srng.Next()
240 rem -= size
241 p, err := alloc.UintptrMalloc(size)
242 if err != nil {
243 t.Fatal(err)
244 }
245
246 b := make([]byte, size)
247 for i := range b {
248 b[i] = byte(vrng.Next())
249 *(*byte)(unsafe.Pointer(p + uintptr(i))) = b[i]
250 }
251 m[block{p, size}] = append([]byte(nil), b...)
252 default: // 1/3 free
253 for b, v := range m {
254 for i, v := range v {
255 if *(*byte)(unsafe.Pointer(b.p + uintptr(i))) != v {
256 t.Fatal("corrupted heap")
257 }
258 }
259
260 if a, b := b.size, UintptrUsableSize(b.p); a > b {
261 t.Fatal(a, b)
262 }
263
264 for j := 0; j < b.size; j++ {
265 *(*byte)(unsafe.Pointer(b.p + uintptr(j))) = 0
266 }
267 rem += b.size
268 alloc.UintptrFree(b.p)
269 delete(m, b)
270 break
271 }
272 }
273 }
274 if counters {
275 t.Logf("allocs %v, mmaps %v, bytes %v, overhead %v (%.2f%%).", alloc.Allocs, alloc.Mmaps, alloc.Bytes, alloc.Bytes-quota, 100*float64(alloc.Bytes-quota)/quota)
276 }
277 for b, v := range m {
278 for i, v := range v {
279 if *(*byte)(unsafe.Pointer(b.p + uintptr(i))) != v {
280 t.Fatal("corrupted heap")
281 }
282 }
283
284 if a, b := b.size, UintptrUsableSize(b.p); a > b {
285 t.Fatal(a, b)
286 }
287
288 for j := 0; j < b.size; j++ {
289 *(*byte)(unsafe.Pointer(b.p + uintptr(j))) = 0
290 }
291 alloc.UintptrFree(b.p)
292 }
293 if err := alloc.Trim(); err != nil {
294 t.Fatal(err)
295 }
296
297 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
298 t.Fatalf("%+v", alloc)
299 }
300 }
301
302 func Test3USmall(t *testing.T) { test3u(t, max) }
303 func Test3UBig(t *testing.T) { test3u(t, bigMax) }
304
305 func TestUFree(t *testing.T) {
306 var alloc Allocator
307
308 defer alloc.Close()
309
310 p, err := alloc.UintptrMalloc(1)
311 if err != nil {
312 t.Fatal(err)
313 }
314
315 if err := alloc.UintptrFree(p); err != nil {
316 t.Fatal(err)
317 }
318
319 if err := alloc.Trim(); err != nil {
320 t.Fatal(err)
321 }
322
323 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
324 t.Fatalf("%+v", alloc)
325 }
326 }
327
328 func TestUMalloc(t *testing.T) {
329 var alloc Allocator
330
331 defer alloc.Close()
332
333 p, err := alloc.UintptrMalloc(maxSlotSize)
334 if err != nil {
335 t.Fatal(err)
336 }
337
338 pg := (*page)(unsafe.Pointer(p &^ uintptr(osPageMask)))
339 if 1<<pg.log > maxSlotSize {
340 t.Fatal(1<<pg.log, maxSlotSize)
341 }
342
343 if err := alloc.UintptrFree(p); err != nil {
344 t.Fatal(err)
345 }
346
347 if err := alloc.Trim(); err != nil {
348 t.Fatal(err)
349 }
350
351 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
352 t.Fatalf("%+v", alloc)
353 }
354 }
355
356 func TestUMallocSizeOverflow(t *testing.T) {
357 var alloc Allocator
358
359 defer alloc.Close()
360
361 p, err := alloc.UintptrMalloc(int(^uint(0) >> 1))
362 if err == nil {
363 t.Fatalf("UintptrMalloc returned %#x, want an error", p)
364 }
365 if p != 0 {
366 t.Fatalf("UintptrMalloc returned %#x with error %v", p, err)
367 }
368 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
369 t.Fatalf("%+v", alloc)
370 }
371 }
372
373 func test1(t *testing.T, max int) {
374 var alloc Allocator
375
376 defer alloc.Close()
377
378 rem := quota
379 var a [][]byte
380 srng, err := mathutil.NewFC32(0, math.MaxInt32, true)
381 if err != nil {
382 t.Fatal(err)
383 }
384
385 vrng, err := mathutil.NewFC32(0, math.MaxInt32, true)
386 if err != nil {
387 t.Fatal(err)
388 }
389
390 // Allocate
391 for rem > 0 {
392 size := srng.Next()%max + 1
393 rem -= size
394 b, err := alloc.Malloc(size)
395 if err != nil {
396 t.Fatal(err)
397 }
398
399 a = append(a, b)
400 for i := range b {
401 b[i] = byte(vrng.Next())
402 }
403 }
404 if counters {
405 t.Logf("allocs %v, mmaps %v, bytes %v, overhead %v (%.2f%%).", alloc.Allocs, alloc.Mmaps, alloc.Bytes, alloc.Bytes-quota, 100*float64(alloc.Bytes-quota)/quota)
406 }
407 srng.Seek(0)
408 vrng.Seek(0)
409 // Verify
410 for i, b := range a {
411 if g, e := len(b), srng.Next()%max+1; g != e {
412 t.Fatal(i, g, e)
413 }
414
415 if a, b := len(b), UsableSize(&b[0]); a > b {
416 t.Fatal(i, a, b)
417 }
418
419 for i, g := range b {
420 if e := byte(vrng.Next()); g != e {
421 t.Fatalf("%v %p: %#02x %#02x", i, &b[i], g, e)
422 }
423
424 b[i] = 0
425 }
426 }
427 // Shuffle
428 for i := range a {
429 j := srng.Next() % len(a)
430 a[i], a[j] = a[j], a[i]
431 }
432 // Free
433 for _, b := range a {
434 if err := alloc.Free(b); err != nil {
435 t.Fatal(err)
436 }
437 }
438 if err := alloc.Trim(); err != nil {
439 t.Fatal(err)
440 }
441
442 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
443 t.Fatalf("%+v", alloc)
444 }
445 }
446
447 func Test1Small(t *testing.T) { test1(t, max) }
448 func Test1Big(t *testing.T) { test1(t, bigMax) }
449
450 func test2(t *testing.T, max int) {
451 var alloc Allocator
452
453 defer alloc.Close()
454
455 rem := quota
456 var a [][]byte
457 srng, err := mathutil.NewFC32(0, math.MaxInt32, true)
458 if err != nil {
459 t.Fatal(err)
460 }
461
462 vrng, err := mathutil.NewFC32(0, math.MaxInt32, true)
463 if err != nil {
464 t.Fatal(err)
465 }
466
467 // Allocate
468 for rem > 0 {
469 size := srng.Next()%max + 1
470 rem -= size
471 b, err := alloc.Malloc(size)
472 if err != nil {
473 t.Fatal(err)
474 }
475
476 a = append(a, b)
477 for i := range b {
478 b[i] = byte(vrng.Next())
479 }
480 }
481 if counters {
482 t.Logf("allocs %v, mmaps %v, bytes %v, overhead %v (%.2f%%).", alloc.Allocs, alloc.Mmaps, alloc.Bytes, alloc.Bytes-quota, 100*float64(alloc.Bytes-quota)/quota)
483 }
484 srng.Seek(0)
485 vrng.Seek(0)
486 // Verify & free
487 for i, b := range a {
488 if g, e := len(b), srng.Next()%max+1; g != e {
489 t.Fatal(i, g, e)
490 }
491
492 if a, b := len(b), UsableSize(&b[0]); a > b {
493 t.Fatal(i, a, b)
494 }
495
496 for i, g := range b {
497 if e := byte(vrng.Next()); g != e {
498 t.Fatalf("%v %p: %#02x %#02x", i, &b[i], g, e)
499 }
500
501 b[i] = 0
502 }
503 if err := alloc.Free(b); err != nil {
504 t.Fatal(err)
505 }
506 }
507 if err := alloc.Trim(); err != nil {
508 t.Fatal(err)
509 }
510
511 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
512 t.Fatalf("%+v", alloc)
513 }
514 }
515
516 func Test2Small(t *testing.T) { test2(t, max) }
517 func Test2Big(t *testing.T) { test2(t, bigMax) }
518
519 func test3(t *testing.T, max int) {
520 var alloc Allocator
521
522 defer alloc.Close()
523
524 rem := quota
525 m := map[*[]byte][]byte{}
526 srng, err := mathutil.NewFC32(1, max, true)
527 if err != nil {
528 t.Fatal(err)
529 }
530
531 vrng, err := mathutil.NewFC32(1, max, true)
532 if err != nil {
533 t.Fatal(err)
534 }
535
536 for rem > 0 {
537 switch srng.Next() % 3 {
538 case 0, 1: // 2/3 allocate
539 size := srng.Next()
540 rem -= size
541 b, err := alloc.Malloc(size)
542 if err != nil {
543 t.Fatal(err)
544 }
545
546 for i := range b {
547 b[i] = byte(vrng.Next())
548 }
549 m[&b] = append([]byte(nil), b...)
550 default: // 1/3 free
551 for k, v := range m {
552 b := *k
553 if !bytes.Equal(b, v) {
554 t.Fatal("corrupted heap")
555 }
556
557 if a, b := len(b), UsableSize(&b[0]); a > b {
558 t.Fatal(a, b)
559 }
560
561 for i := range b {
562 b[i] = 0
563 }
564 rem += len(b)
565 alloc.Free(b)
566 delete(m, k)
567 break
568 }
569 }
570 }
571 if counters {
572 t.Logf("allocs %v, mmaps %v, bytes %v, overhead %v (%.2f%%).", alloc.Allocs, alloc.Mmaps, alloc.Bytes, alloc.Bytes-quota, 100*float64(alloc.Bytes-quota)/quota)
573 }
574 for k, v := range m {
575 b := *k
576 if !bytes.Equal(b, v) {
577 t.Fatal("corrupted heap")
578 }
579
580 if a, b := len(b), UsableSize(&b[0]); a > b {
581 t.Fatal(a, b)
582 }
583
584 for i := range b {
585 b[i] = 0
586 }
587 alloc.Free(b)
588 }
589 if err := alloc.Trim(); err != nil {
590 t.Fatal(err)
591 }
592
593 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
594 t.Fatalf("%+v", alloc)
595 }
596 }
597
598 func Test3Small(t *testing.T) { test3(t, max) }
599 func Test3Big(t *testing.T) { test3(t, bigMax) }
600
601 func TestFree(t *testing.T) {
602 var alloc Allocator
603
604 defer alloc.Close()
605
606 b, err := alloc.Malloc(1)
607 if err != nil {
608 t.Fatal(err)
609 }
610
611 if err := alloc.Free(b[:0]); err != nil {
612 t.Fatal(err)
613 }
614
615 if err := alloc.Trim(); err != nil {
616 t.Fatal(err)
617 }
618
619 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
620 t.Fatalf("%+v", alloc)
621 }
622 }
623
624 func TestMalloc(t *testing.T) {
625 var alloc Allocator
626
627 defer alloc.Close()
628
629 b, err := alloc.Malloc(maxSlotSize)
630 if err != nil {
631 t.Fatal(err)
632 }
633
634 p := (*page)(unsafe.Pointer(uintptr(unsafe.Pointer(&b[0])) &^ uintptr(osPageMask)))
635 if 1<<p.log > maxSlotSize {
636 t.Fatal(1<<p.log, maxSlotSize)
637 }
638
639 if err := alloc.Free(b[:0]); err != nil {
640 t.Fatal(err)
641 }
642
643 if err := alloc.Trim(); err != nil {
644 t.Fatal(err)
645 }
646
647 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
648 t.Fatalf("%+v", alloc)
649 }
650 }
651
652 func TestTrim(t *testing.T) {
653 var alloc Allocator
654
655 defer alloc.Close()
656
657 // Draining a size class retains its page instead of unmapping it.
658 p, err := alloc.UintptrMalloc(1)
659 if err != nil {
660 t.Fatal(err)
661 }
662
663 regs := len(alloc.regs)
664 mmaps := alloc.Mmaps
665 if err := alloc.UintptrFree(p); err != nil {
666 t.Fatal(err)
667 }
668
669 if g, e := len(alloc.regs), regs; g != e {
670 t.Fatalf("mapped pages %v, expected %v", g, e)
671 }
672
673 // Cycling the class reuses the retained page instead of acquiring new ones.
674 for i := 0; i < 1<<14; i++ {
675 if p, err = alloc.UintptrMalloc(1); err != nil {
676 t.Fatal(err)
677 }
678
679 if err = alloc.UintptrFree(p); err != nil {
680 t.Fatal(err)
681 }
682 }
683 if g, e := len(alloc.regs), regs; g != e {
684 t.Fatalf("mapped pages %v, expected %v", g, e)
685 }
686
687 if counters && alloc.Mmaps != mmaps {
688 t.Fatalf("mmaps %v, expected %v", alloc.Mmaps, mmaps)
689 }
690
691 // A page with a live slot is not released by Trim.
692 if p, err = alloc.UintptrMalloc(1); err != nil {
693 t.Fatal(err)
694 }
695
696 if err := alloc.Trim(); err != nil {
697 t.Fatal(err)
698 }
699
700 if g, e := len(alloc.regs), 1; g != e {
701 t.Fatalf("mapped pages %v, expected %v", g, e)
702 }
703
704 // Trim releases the page once it is empty again.
705 if err := alloc.UintptrFree(p); err != nil {
706 t.Fatal(err)
707 }
708
709 if err := alloc.Trim(); err != nil {
710 t.Fatal(err)
711 }
712
713 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
714 t.Fatalf("%+v", alloc)
715 }
716 }
717
718 func TestTrimClasses(t *testing.T) {
719 var alloc Allocator
720
721 defer alloc.Close()
722
723 // Retention is per size class and bounded by one page each.
724 var a []uintptr
725 var classes int
726 for log := uint(0); log <= maxSlotSizeLog; log++ {
727 p, err := alloc.UintptrMalloc(1 << log)
728 if err != nil {
729 t.Fatal(err)
730 }
731
732 a = append(a, p)
733 }
734 for _, p := range a {
735 if err := alloc.UintptrFree(p); err != nil {
736 t.Fatal(err)
737 }
738 }
739 for log := range alloc.pages {
740 if alloc.pages[log] != 0 {
741 classes++
742 }
743 }
744 minLog := 0
745 for 1<<minLog < int(mallocAllign) {
746 minLog++
747 }
748 if g, e := classes, int(maxSlotSizeLog)-minLog+1; g != e {
749 t.Fatalf("retained classes %v, expected %v", g, e)
750 }
751
752 e := classes
753 if canCarve {
754 // Slab pages are acquired slabBatch at a time; the classes
755 // carve their pages from whole batches.
756 e = roundup(classes, slabBatch)
757 }
758 if g := len(alloc.regs); g != e {
759 t.Fatalf("mapped pages %v, expected %v (retained classes %v)", g, e, classes)
760 }
761
762 if err := alloc.Trim(); err != nil {
763 t.Fatal(err)
764 }
765
766 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
767 t.Fatalf("%+v", alloc)
768 }
769 }
770
771 func TestTrimBigPage(t *testing.T) {
772 var alloc Allocator
773
774 defer alloc.Close()
775
776 // A page backing a single big allocation is retained for reuse;
777 // Trim returns it.
778 p, err := alloc.UintptrMalloc(maxSlotSize + 1)
779 if err != nil {
780 t.Fatal(err)
781 }
782
783 if (*page)(unsafe.Pointer(p&^uintptr(pageMask))).log != 0 {
784 t.Fatal("not a dedicated page")
785 }
786
787 regs := len(alloc.regs)
788 if err := alloc.UintptrFree(p); err != nil {
789 t.Fatal(err)
790 }
791
792 if g, e := len(alloc.regs), regs; g != e {
793 t.Fatalf("regs %v, want %v: freed region was unmapped, not retained", g, e)
794 }
795
796 if err := alloc.Trim(); err != nil {
797 t.Fatal(err)
798 }
799
800 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
801 t.Fatalf("%+v", alloc)
802 }
803 }
804
805 func benchmarkFree(b *testing.B, size int) {
806 var alloc Allocator
807
808 defer alloc.Close()
809
810 a := make([][]byte, b.N)
811 for i := range a {
812 p, err := alloc.Malloc(size)
813 if err != nil {
814 b.Fatal(err)
815 }
816
817 a[i] = p
818 }
819 b.ResetTimer()
820 for _, b := range a {
821 alloc.Free(b)
822 }
823 b.StopTimer()
824 if err := alloc.Trim(); err != nil {
825 b.Fatal(err)
826 }
827
828 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
829 b.Fatalf("%+v", alloc)
830 }
831 }
832
833 func BenchmarkFree16(b *testing.B) { benchmarkFree(b, 1<<4) }
834 func BenchmarkFree32(b *testing.B) { benchmarkFree(b, 1<<5) }
835 func BenchmarkFree64(b *testing.B) { benchmarkFree(b, 1<<6) }
836
837 func benchmarkCalloc(b *testing.B, size int) {
838 var alloc Allocator
839
840 defer alloc.Close()
841
842 a := make([][]byte, b.N)
843 b.ResetTimer()
844 for i := range a {
845 p, err := alloc.Calloc(size)
846 if err != nil {
847 b.Fatal(err)
848 }
849
850 a[i] = p
851 }
852 b.StopTimer()
853 for _, b := range a {
854 alloc.Free(b)
855 }
856 if err := alloc.Trim(); err != nil {
857 b.Fatal(err)
858 }
859
860 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
861 b.Fatalf("%+v", alloc)
862 }
863 }
864
865 func BenchmarkCalloc16(b *testing.B) { benchmarkCalloc(b, 1<<4) }
866 func BenchmarkCalloc32(b *testing.B) { benchmarkCalloc(b, 1<<5) }
867 func BenchmarkCalloc64(b *testing.B) { benchmarkCalloc(b, 1<<6) }
868
869 func benchmarkGoCalloc(b *testing.B, size int) {
870 a := make([][]byte, b.N)
871 b.ResetTimer()
872 for i := range a {
873 a[i] = make([]byte, size)
874 }
875 b.StopTimer()
876 use(a)
877 }
878
879 func BenchmarkGoCalloc16(b *testing.B) { benchmarkGoCalloc(b, 1<<4) }
880 func BenchmarkGoCalloc32(b *testing.B) { benchmarkGoCalloc(b, 1<<5) }
881 func BenchmarkGoCalloc64(b *testing.B) { benchmarkGoCalloc(b, 1<<6) }
882
883 func benchmarkMalloc(b *testing.B, size int) {
884 var alloc Allocator
885
886 defer alloc.Close()
887
888 a := make([][]byte, b.N)
889 b.ResetTimer()
890 for i := range a {
891 p, err := alloc.Malloc(size)
892 if err != nil {
893 b.Fatal(err)
894 }
895
896 a[i] = p
897 }
898 b.StopTimer()
899 for _, b := range a {
900 alloc.Free(b)
901 }
902 if err := alloc.Trim(); err != nil {
903 b.Fatal(err)
904 }
905
906 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
907 b.Fatalf("%+v", alloc)
908 }
909 }
910
911 func BenchmarkMalloc16(b *testing.B) { benchmarkMalloc(b, 1<<4) }
912 func BenchmarkMalloc32(b *testing.B) { benchmarkMalloc(b, 1<<5) }
913 func BenchmarkMalloc64(b *testing.B) { benchmarkMalloc(b, 1<<6) }
914
915 func benchmarkUintptrFree(b *testing.B, size int) {
916 var alloc Allocator
917
918 defer alloc.Close()
919
920 a := make([]uintptr, b.N)
921 for i := range a {
922 p, err := alloc.UintptrMalloc(size)
923 if err != nil {
924 b.Fatal(err)
925 }
926
927 a[i] = p
928 }
929 b.ResetTimer()
930 for _, p := range a {
931 alloc.UintptrFree(p)
932 }
933 b.StopTimer()
934 if err := alloc.Trim(); err != nil {
935 b.Fatal(err)
936 }
937
938 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
939 b.Fatalf("%+v", alloc)
940 }
941 }
942
943 func BenchmarkUintptrFree16(b *testing.B) { benchmarkUintptrFree(b, 1<<4) }
944 func BenchmarkUintptrFree32(b *testing.B) { benchmarkUintptrFree(b, 1<<5) }
945 func BenchmarkUintptrFree64(b *testing.B) { benchmarkUintptrFree(b, 1<<6) }
946
947 func benchmarkUintptrCalloc(b *testing.B, size int) {
948 var alloc Allocator
949
950 defer alloc.Close()
951
952 a := make([]uintptr, b.N)
953 b.ResetTimer()
954 for i := range a {
955 p, err := alloc.UintptrCalloc(size)
956 if err != nil {
957 b.Fatal(err)
958 }
959
960 a[i] = p
961 }
962 b.StopTimer()
963 for _, p := range a {
964 alloc.UintptrFree(p)
965 }
966 if err := alloc.Trim(); err != nil {
967 b.Fatal(err)
968 }
969
970 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
971 b.Fatalf("%+v", alloc)
972 }
973 }
974
975 func BenchmarkUintptrCalloc16(b *testing.B) { benchmarkUintptrCalloc(b, 1<<4) }
976 func BenchmarkUintptrCalloc32(b *testing.B) { benchmarkUintptrCalloc(b, 1<<5) }
977 func BenchmarkUintptrCalloc64(b *testing.B) { benchmarkUintptrCalloc(b, 1<<6) }
978
979 func benchmarkUintptrMalloc(b *testing.B, size int) {
980 var alloc Allocator
981
982 defer alloc.Close()
983
984 a := make([]uintptr, b.N)
985 b.ResetTimer()
986 for i := range a {
987 p, err := alloc.UintptrMalloc(size)
988 if err != nil {
989 b.Fatal(err)
990 }
991
992 a[i] = p
993 }
994 b.StopTimer()
995 for _, p := range a {
996 alloc.UintptrFree(p)
997 }
998 if err := alloc.Trim(); err != nil {
999 b.Fatal(err)
1000 }
1001
1002 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
1003 b.Fatalf("%+v", alloc)
1004 }
1005 }
1006
1007 func BenchmarkUintptrMalloc16(b *testing.B) { benchmarkUintptrMalloc(b, 1<<4) }
1008 func BenchmarkUintptrMalloc32(b *testing.B) { benchmarkUintptrMalloc(b, 1<<5) }
1009 func BenchmarkUintptrMalloc64(b *testing.B) { benchmarkUintptrMalloc(b, 1<<6) }
1010
1011 func TestFreedPool(t *testing.T) {
1012 var alloc Allocator
1013
1014 defer alloc.Close()
1015
1016 p, err := alloc.UintptrMalloc(1 << 20)
1017 if err != nil {
1018 t.Fatal(err)
1019 }
1020
1021 mmaps := alloc.Mmaps
1022 regs := len(alloc.regs)
1023 if err := alloc.UintptrFree(p); err != nil {
1024 t.Fatal(err)
1025 }
1026
1027 if g, e := len(alloc.regs), regs; g != e {
1028 t.Fatalf("regs %v, want %v: freed region was unmapped, not retained", g, e)
1029 }
1030
1031 q, err := alloc.UintptrMalloc(1 << 20)
1032 if err != nil {
1033 t.Fatal(err)
1034 }
1035
1036 if q != p {
1037 t.Fatalf("got %#x, want %#x: allocation did not reuse the retained region", q, p)
1038 }
1039
1040 if g, e := alloc.Mmaps, mmaps; g != e {
1041 t.Fatalf("Mmaps %v, want %v", g, e)
1042 }
1043
1044 if err := alloc.UintptrFree(q); err != nil {
1045 t.Fatal(err)
1046 }
1047
1048 if err := alloc.Trim(); err != nil {
1049 t.Fatal(err)
1050 }
1051
1052 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
1053 t.Fatalf("%+v", alloc)
1054 }
1055 }
1056
1057 func TestSlabBatch(t *testing.T) {
1058 if !canCarve {
1059 t.Skip("carving not supported on this platform")
1060 }
1061
1062 var alloc Allocator
1063
1064 defer alloc.Close()
1065
1066 p, err := alloc.UintptrMalloc(1)
1067 if err != nil {
1068 t.Fatal(err)
1069 }
1070
1071 if g, e := len(alloc.regs), slabBatch; g != e {
1072 t.Fatalf("regs %v, want %v: pageSize regions were not acquired in a batch", g, e)
1073 }
1074
1075 if err := alloc.UintptrFree(p); err != nil {
1076 t.Fatal(err)
1077 }
1078
1079 if err := alloc.Trim(); err != nil {
1080 t.Fatal(err)
1081 }
1082
1083 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
1084 t.Fatalf("%+v", alloc)
1085 }
1086 }
1087
1088 func TestSlabBatchFreedPoolBound(t *testing.T) {
1089 if !canCarve {
1090 t.Skip("carving not supported on this platform")
1091 }
1092
1093 var alloc Allocator
1094
1095 defer alloc.Close()
1096
1097 for _, size := range []int{16 << 20, 4 << 20} {
1098 p, err := alloc.UintptrMalloc(size - int(headerSize))
1099 if err != nil {
1100 t.Fatal(err)
1101 }
1102 if err := alloc.UintptrFree(p); err != nil {
1103 t.Fatal(err)
1104 }
1105 }
1106
1107 if room := alloc.maxFreedSize() - alloc.freedSize; room >= (slabBatch-1)*pageSize {
1108 t.Fatalf("test setup left %v bytes in pool, enough for a slab batch", room)
1109 }
1110
1111 p, err := alloc.UintptrMalloc(1)
1112 if err != nil {
1113 t.Fatal(err)
1114 }
1115 if alloc.freedSize > alloc.maxFreedSize() {
1116 t.Fatalf("freed pool size %v exceeds bound %v", alloc.freedSize, alloc.maxFreedSize())
1117 }
1118
1119 if err := alloc.UintptrFree(p); err != nil {
1120 t.Fatal(err)
1121 }
1122 if err := alloc.Trim(); err != nil {
1123 t.Fatal(err)
1124 }
1125 if alloc.Allocs != 0 || alloc.Mmaps != 0 || alloc.Bytes != 0 || len(alloc.regs) != 0 {
1126 t.Fatalf("%+v", alloc)
1127 }
1128 }