github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/runtime/mfixalloc.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Fixed-size object allocator. Returned memory is not zeroed. 6 // 7 // See malloc.h for overview. 8 9 package runtime 10 11 import "unsafe" 12 13 // Initialize f to allocate objects of the given size, 14 // using the allocator to obtain chunks of memory. 15 func fixAlloc_Init(f *fixalloc, size uintptr, first func(unsafe.Pointer, unsafe.Pointer), arg unsafe.Pointer, stat *uint64) { 16 f.size = size 17 f.first = *(*unsafe.Pointer)(unsafe.Pointer(&first)) 18 f.arg = arg 19 f.list = nil 20 f.chunk = nil 21 f.nchunk = 0 22 f.inuse = 0 23 f.stat = stat 24 } 25 26 func fixAlloc_Alloc(f *fixalloc) unsafe.Pointer { 27 if f.size == 0 { 28 print("runtime: use of FixAlloc_Alloc before FixAlloc_Init\n") 29 gothrow("runtime: internal error") 30 } 31 32 if f.list != nil { 33 v := unsafe.Pointer(f.list) 34 f.list = f.list.next 35 f.inuse += f.size 36 return v 37 } 38 if uintptr(f.nchunk) < f.size { 39 f.chunk = (*uint8)(persistentalloc(_FixAllocChunk, 0, f.stat)) 40 f.nchunk = _FixAllocChunk 41 } 42 43 v := (unsafe.Pointer)(f.chunk) 44 if f.first != nil { 45 fn := *(*func(unsafe.Pointer, unsafe.Pointer))(unsafe.Pointer(&f.first)) 46 fn(f.arg, v) 47 } 48 f.chunk = (*byte)(add(unsafe.Pointer(f.chunk), f.size)) 49 f.nchunk -= uint32(f.size) 50 f.inuse += f.size 51 return v 52 } 53 54 func fixAlloc_Free(f *fixalloc, p unsafe.Pointer) { 55 f.inuse -= f.size 56 v := (*mlink)(p) 57 v.next = f.list 58 f.list = v 59 }