Skip to content

Commit

Permalink
malloc: fix macos build (#21250)
Browse files Browse the repository at this point in the history
fix build in macos

Approved by: @sukki37
  • Loading branch information
reusee authored Jan 16, 2025
1 parent 3559153 commit 4477346
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
12 changes: 3 additions & 9 deletions pkg/common/malloc/c_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,9 @@ import (

/*
#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
}

type CAllocator struct {
deallocatorPool *ClosureDeallocatorPool[cDeallocatorArgs, *cDeallocatorArgs]
bytesFree atomic.Uint64
Expand Down Expand Up @@ -61,7 +54,7 @@ func NewCAllocator() (ret *CAllocator) {
if n > cMallocReturnToOSThreshold {
ret.bytesFree.Store(0)
// return memory to OS
C.malloc_trim(0)
returnMallocMemoryToOS()
}

},
Expand All @@ -79,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)
}

0 comments on commit 4477346

Please sign in to comment.