This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use typed alloc for large objects too (#27)
- Loading branch information
Showing
8 changed files
with
404 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright wasilibs authors | ||
// SPDX-License-Identifier: MIT | ||
|
||
package nottinygc | ||
|
||
import "unsafe" | ||
|
||
// CPP_WORDSZ is a simple integer constant representing the word size | ||
const cppWordsz = unsafe.Sizeof(uintptr(0)) * 8 | ||
|
||
type gcBitmap struct { | ||
words []uintptr | ||
} | ||
|
||
func newBitmap(size uintptr) gcBitmap { | ||
bmSize := gcBitmapSize(size) | ||
wordsArr := cmalloc(bmSize * unsafe.Sizeof(uintptr(0))) | ||
words := unsafe.Slice((*uintptr)(wordsArr), bmSize) | ||
for i := 0; i < len(words); i++ { | ||
words[i] = 0 | ||
} | ||
return gcBitmap{words: words} | ||
} | ||
|
||
func (b gcBitmap) set(idx uintptr) { | ||
b.words[idx/cppWordsz] |= 1 << (idx % cppWordsz) | ||
} | ||
|
||
func (b gcBitmap) get(idx uintptr) uintptr { | ||
return (b.words[idx/cppWordsz] >> (idx % cppWordsz)) & 1 | ||
} | ||
|
||
func gcBitmapSize(size uintptr) uintptr { | ||
return (size + cppWordsz - 1) / cppWordsz | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright wasilibs authors | ||
// SPDX-License-Identifier: MIT | ||
|
||
package nottinygc | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestBitmap32Bits(t *testing.T) { | ||
tests := []uintptr{ | ||
0, | ||
0b1, | ||
0b101, | ||
0b111, | ||
0b0001, | ||
0b1000001, | ||
0xFFFFFFFF, | ||
0x11111111, | ||
0x01010101, | ||
0x0F0F0F0F, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) { | ||
bm := newBitmap(32) | ||
if len(bm.words) != 1 { | ||
t.Fatalf("expected 1 word, got %v", len(bm.words)) | ||
} | ||
for i := 0; i < 32; i++ { | ||
if tc&(1<<i) != 0 { | ||
bm.set(uintptr(i)) | ||
} | ||
} | ||
|
||
for i := 0; i < 32; i++ { | ||
got := bm.get(uintptr(i)) | ||
if tc&(1<<i) != 0 { | ||
if got == 0 { | ||
t.Fatalf("expected bit %v to be set", i) | ||
} | ||
} else { | ||
if got != 0 { | ||
t.Fatalf("expected bit %v to be unset", i) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Test for multiple words, we pick larger than 64-bits to have more than one word on Go | ||
// as well. We don't actually run CI with Go but it can be helpful for development. | ||
func TestBitmap128Bits(t *testing.T) { | ||
// We'll just repeat these. | ||
tests := []uintptr{ | ||
0, | ||
0b1, | ||
0b101, | ||
0b111, | ||
0b0001, | ||
0b1000001, | ||
0xFFFFFFFF, | ||
0x11111111, | ||
0x01010101, | ||
0x0F0F0F0F, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) { | ||
bm := newBitmap(128) | ||
if cppWordsz == 32 && len(bm.words) != 4 || cppWordsz == 64 && len(bm.words) != 2 { | ||
t.Fatalf("got %v words", len(bm.words)) | ||
} | ||
for j := 0; j < 4; j++ { | ||
for i := 0; i < 32; i++ { | ||
if tc&(1<<(32*j+i)) != 0 { | ||
bm.set(uintptr(i)) | ||
} | ||
} | ||
} | ||
|
||
for j := 0; j < 4; j++ { | ||
for i := 0; i < 32; i++ { | ||
got := bm.get(uintptr(32*j + i)) | ||
if tc&(1<<(32*j+i)) != 0 { | ||
if got == 0 { | ||
t.Fatalf("expected bit %v to be set", i) | ||
} | ||
} else { | ||
if got != 0 { | ||
t.Fatalf("expected bit %v to be unset", i) | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.