1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00

btree: remove resethash

This commit is contained in:
wdvxdr 2022-08-31 20:30:16 +08:00
parent 1ff5e4de12
commit 96e6397636
3 changed files with 11 additions and 21 deletions

View File

@ -40,6 +40,7 @@ func TestBtree(t *testing.T) {
"123", "123",
"We are met on a great battle-field of that war.", "We are met on a great battle-field of that war.",
"Abraham Lincoln, November 19, 1863, Gettysburg, Pennsylvania", "Abraham Lincoln, November 19, 1863, Gettysburg, Pennsylvania",
// "00", // TODO: fix this
} }
sha := make([]*byte, len(tests)) sha := make([]*byte, len(tests))
for i, tt := range tests { for i, tt := range tests {

View File

@ -1,6 +1,7 @@
package btree package btree
import ( import (
"encoding/binary"
"math/rand" "math/rand"
"unsafe" "unsafe"
) )
@ -31,9 +32,8 @@ func (d *DB) allocChunk(size int) int64 {
/* create fake size SHA-1 */ /* create fake size SHA-1 */
var sha1 [hashSize]byte var sha1 [hashSize]byte
p := unsafe.Pointer(&sha1[0]) binary.LittleEndian.PutUint32(sha1[0*4:1*4], ^uint32(0)) // *(uint32_t *) hash = -1;
*(*int32)(p) = -1 // *(uint32_t *) hash = -1; binary.LittleEndian.PutUint32(sha1[1*4:2*4], uint32(size)) // ((__be32 *) hash)[1] = to_be32(size);
*(*uint32)(unsafe.Add(p, i32s)) = uint32(size) // ((__be32 *) hash)[1] = to_be32(size);
/* find free chunk with the larger or the same size/SHA-1 */ /* find free chunk with the larger or the same size/SHA-1 */
d.inAllocator = true d.inAllocator = true
@ -41,14 +41,13 @@ func (d *DB) allocChunk(size int) int64 {
offset = d.delete(d.freeTop, &sha1[0]) offset = d.delete(d.freeTop, &sha1[0])
d.deleteLarger = false d.deleteLarger = false
if offset != 0 { if offset != 0 {
assert(*(*int32)(p) == -1) // assert(*(uint32_t *) hash == (uint32_t) -1) flen := int(binary.LittleEndian.Uint32(sha1[:4])) // size_t free_len = from_be32(((__be32 *) hash)[1])
flen := int(*(*uint32)(unsafe.Add(p, i32s))) // size_t free_len = from_be32(((__be32 *) hash)[1])
assert(power2(flen) == flen) assert(power2(flen) == flen)
assert(flen >= size) assert(flen >= size)
/* delete buddy information */ /* delete buddy information */
resethash(&sha1[0]) sha1 = [hashSize]byte{}
*(*int64)(p) = offset binary.LittleEndian.PutUint64(sha1[0*8:1*8], uint64(offset))
buddyLen := d.delete(d.freeTop, &sha1[0]) buddyLen := d.delete(d.freeTop, &sha1[0])
assert(buddyLen == int64(size)) assert(buddyLen == int64(size))
@ -98,17 +97,12 @@ func (d *DB) freeChunk(offset int64, size int) {
/* create fake offset SHA-1 for buddy allocation */ /* create fake offset SHA-1 for buddy allocation */
var sha1 [hashSize]byte var sha1 [hashSize]byte
p := unsafe.Pointer(&sha1[0])
d.inAllocator = true d.inAllocator = true
const i32s = unsafe.Sizeof(int32(0))
/* add buddy information */ /* add buddy information */
resethash(&sha1[0]) binary.LittleEndian.PutUint32(sha1[0*4:1*4], ^uint32(0)) // *(uint32_t *) hash = -1;
*(*int32)(p) = -1 // *(uint32_t *) hash = -1; binary.LittleEndian.PutUint32(sha1[1*4:2*4], uint32(size)) // ((__be32 *) hash)[1] = to_be32(size);
*(*uint32)(unsafe.Add(p, i32s)) = uint32(size) // ((__be32 *) hash)[1] = to_be32(size); binary.LittleEndian.PutUint32(sha1[2*4:3*4], rand.Uint32()) /* to make SHA-1 unique */
*(*uint32)(unsafe.Add(p, i32s*2)) = rand.Uint32() /* to make SHA-1 unique */ binary.LittleEndian.PutUint32(sha1[3*4:4*4], rand.Uint32())
*(*uint32)(unsafe.Add(p, i32s*3)) = rand.Uint32()
// insert_toplevel(btree, &btree->free_top, hash, NULL, offset); // insert_toplevel(btree, &btree->free_top, hash, NULL, offset);
_ = d.insertTopLevel(&d.freeTop, &sha1[0], nil, int(offset)) _ = d.insertTopLevel(&d.freeTop, &sha1[0], nil, int(offset))

View File

@ -37,11 +37,6 @@ func copyhash(dst *byte, src *byte) {
*(*[hashSize]byte)(pa) = *(*[hashSize]byte)(pb) *(*[hashSize]byte)(pa) = *(*[hashSize]byte)(pb)
} }
func resethash(sha1 *byte) {
p := unsafe.Pointer(sha1)
*(*[hashSize]byte)(p) = [hashSize]byte{}
}
// reading table // reading table
func read32(r io.Reader) (int32, error) { func read32(r io.Reader) (int32, error) {