aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2015-01-28 13:03:30 -0500
committerBen Johnson <benbjohnson@yahoo.com>2015-01-28 13:03:30 -0500
commitdacc1873d170e88019c30f9dab1a364b45cc7516 (patch)
tree1af0a7c204c54eeefc587d5f7d350148aad694f2
parentAdd additional error checks in test suite. (diff)
downloaddedo-dacc1873d170e88019c30f9dab1a364b45cc7516.tar.gz
dedo-dacc1873d170e88019c30f9dab1a364b45cc7516.tar.xz
Fix mmap step & max size check.
This commit adds fixes suggested by @tv42 for the mmap step fix in 834b38e: * Check max size before calculating the new the mmap size. * Fix mmap step loop to go to 1GB instead of 512MB.
-rw-r--r--db.go22
1 files changed, 13 insertions, 9 deletions
diff --git a/db.go b/db.go
index 535e566..3198bca 100644
--- a/db.go
+++ b/db.go
@@ -171,11 +171,9 @@ func (db *DB) mmap(minsz int) error {
if size < minsz {
size = minsz
}
- size = db.mmapSize(size)
-
- // Verify the map size is not above the maximum allowed.
- if size > maxMapSize {
- return fmt.Errorf("mmap too large")
+ size, err = db.mmapSize(size)
+ if err != nil {
+ return err
}
// Dereference all mmap references before unmapping.
@@ -218,14 +216,20 @@ func (db *DB) munmap() error {
// mmapSize determines the appropriate size for the mmap given the current size
// of the database. The minimum size is 4MB and doubles until it reaches 1GB.
-func (db *DB) mmapSize(size int) int {
+// Returns an error if the new mmap size is greater than the max allowed.
+func (db *DB) mmapSize(size int) (int, error) {
// Double the size from 1MB until 1GB.
- for i := uint(20); i < 30; i++ {
+ for i := uint(20); i <= 30; i++ {
if size <= 1<<i {
- return 1 << i
+ return 1 << i, nil
}
}
+ // Verify the map size is not above the maximum allowed.
+ if size > maxMapSize-maxMmapStep {
+ return 0, fmt.Errorf("mmap too large")
+ }
+
// If larger than 1GB then grow by 1GB at a time.
size += maxMmapStep
if remainder := size % maxMmapStep; remainder > 0 {
@@ -238,7 +242,7 @@ func (db *DB) mmapSize(size int) int {
size = ((size / db.pageSize) + 1) * db.pageSize
}
- return size
+ return size, nil
}
// init creates a new database file and initializes its meta pages.