mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-01 00:31:21 +00:00
967937151d
* Fixes for compress, and first test * Add decompressor and memory mapping * Add decompressor and memory mapping * Fix for windows * Fix lint * Fix compile for windows * More on decompressor * Fix lint * Decompress * Fix lint * Use decompressor in tests, fixes * Introduce Index for RecSplit * Fix compilation on Windows * close index file on failure * Fixes to the tests * Add single Elias Fano, fix recsplit fuzz test * Fix elias fano * Add two layer index * Add two level index to the tests Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local> Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
//go:build !windows
|
|
// +build !windows
|
|
|
|
/*
|
|
Copyright 2021 Erigon contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package mmap
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
const MaxMapSize = 0xFFFFFFFFFFFF
|
|
|
|
// mmap memory maps a DB's data file.
|
|
func Mmap(f *os.File, size int) ([]byte, *[MaxMapSize]byte, error) {
|
|
// Map the data file to memory.
|
|
mmapHandle1, err := unix.Mmap(int(f.Fd()), 0, size, syscall.PROT_READ, syscall.MAP_SHARED)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// Advise the kernel that the mmap is accessed randomly.
|
|
err = unix.Madvise(mmapHandle1, syscall.MADV_RANDOM)
|
|
if err != nil && err != syscall.ENOSYS {
|
|
// Ignore not implemented error in kernel because it still works.
|
|
return nil, nil, fmt.Errorf("madvise: %s", err)
|
|
}
|
|
mmapHandle2 := (*[MaxMapSize]byte)(unsafe.Pointer(&mmapHandle1[0]))
|
|
return mmapHandle1, mmapHandle2, nil
|
|
}
|
|
|
|
// munmap unmaps a DB's data file from memory.
|
|
func Munmap(mmapHandle1 []byte, _ *[MaxMapSize]byte) error {
|
|
// Ignore the unmap if we have no mapped data.
|
|
if mmapHandle1 == nil {
|
|
return nil
|
|
}
|
|
// Unmap using the original byte slice.
|
|
err := unix.Munmap(mmapHandle1)
|
|
return err
|
|
}
|