Skip to content

Commit

Permalink
malloc: default to malloc
Browse files Browse the repository at this point in the history
  • Loading branch information
reusee committed Jan 16, 2025
1 parent 105b313 commit 7608e62
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
29 changes: 24 additions & 5 deletions pkg/common/malloc/c_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package malloc

import "unsafe"
import (
"sync/atomic"
"unsafe"
)

/*
#include <stdlib.h>
Expand All @@ -25,24 +28,39 @@ import "C"

type CAllocator struct {
deallocatorPool *ClosureDeallocatorPool[cDeallocatorArgs, *cDeallocatorArgs]
bytesFree atomic.Uint64
}

type cDeallocatorArgs struct {
ptr unsafe.Pointer
ptr unsafe.Pointer
size uint64
}

func (cDeallocatorArgs) As(Trait) bool {
return false
}

func NewCAllocator() *CAllocator {
return &CAllocator{
const (
cMallocReturnToOSThreshold = 64 * MB
)

func NewCAllocator() (ret *CAllocator) {
ret = &CAllocator{
deallocatorPool: NewClosureDeallocatorPool(
func(hints Hints, args *cDeallocatorArgs) {
C.free(args.ptr)

n := ret.bytesFree.Add(args.size)
if n > cMallocReturnToOSThreshold {
ret.bytesFree.Store(0)
// return memory to OS
returnMallocMemoryToOS()
}

},
),
}
return
}

var _ Allocator = new(CAllocator)
Expand All @@ -54,6 +72,7 @@ func (c *CAllocator) Allocate(size uint64, hints Hints) ([]byte, Deallocator, er
}
slice := unsafe.Slice((*byte)(ptr), size)
return slice, c.deallocatorPool.Get(cDeallocatorArgs{
ptr: ptr,
ptr: ptr,
size: size,
}), nil
}
20 changes: 20 additions & 0 deletions pkg/common/malloc/c_allocator_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 Matrix Origin
//
// 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.

//go:build cgo

package malloc

func returnMallocMemoryToOS() {
}
33 changes: 33 additions & 0 deletions pkg/common/malloc/c_allocator_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Matrix Origin
//
// 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.

//go:build cgo

package malloc

/*
#include <stdlib.h>
#include <malloc.h>
*/
import "C"

func init() {
// malloc tunings
C.mallopt(C.M_TOP_PAD, 0) // no sbrk padding
C.mallopt(C.M_MMAP_THRESHOLD, 0) // always use mmap
}

func returnMallocMemoryToOS() {
C.malloc_trim(0)
}
9 changes: 9 additions & 0 deletions pkg/common/malloc/c_allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ func FuzzCAllocator(f *testing.F) {
return NewCAllocator()
})
}

func TestCAllocatorReturnToOSThreshold(t *testing.T) {
allocator := NewCAllocator()
_, dec, err := allocator.Allocate(cMallocReturnToOSThreshold*2, NoHints)
if err != nil {
t.Fatal(err)
}
dec.Deallocate(NoHints)
}
2 changes: 1 addition & 1 deletion pkg/common/malloc/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func newDefault(delta *Config) (allocator Allocator) {
}()

if config.Allocator == nil {
config.Allocator = ptrTo("mmap")
config.Allocator = ptrTo("c")
}

switch strings.ToLower(*config.Allocator) {
Expand Down
1 change: 1 addition & 0 deletions pkg/common/malloc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package malloc

const (
KB = 1 << 10
MB = 1 << 20
GB = 1 << 30
)
Expand Down

0 comments on commit 7608e62

Please sign in to comment.