本文整理汇总了Golang中runtime.Alloc函数的典型用法代码示例。如果您正苦于以下问题:Golang Alloc函数的具体用法?Golang Alloc怎么用?Golang Alloc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Alloc函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: prime
// Prime the data structures by allocating one of
// each block in order. After this, there should be
// little reason to ask for more memory from the OS.
func prime() {
for i := 0; i < 16; i++ {
b := runtime.Alloc(1 << uint(i))
runtime.Free(b)
}
for i := uintptr(0); i < 256; i++ {
b := runtime.Alloc(i << 12)
runtime.Free(b)
}
}
示例2: main
func main() {
flag.Parse()
// prime();
var blocks [1]struct {
base *byte
siz uintptr
}
for i := 0; i < 1<<10; i++ {
if i%(1<<10) == 0 && *chatty {
println(i)
}
b := rand.Int() % len(blocks)
if blocks[b].base != nil {
// println("Free", blocks[b].siz, blocks[b].base);
runtime.Free(blocks[b].base)
blocks[b].base = nil
allocated -= uint64(blocks[b].siz)
continue
}
siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20))
base := runtime.Alloc(siz)
// ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2);
// obj, size, ref, ok := allocator.find(ptr);
// if obj != base || *ref != 0 || !ok {
// panicln("find", siz, obj, ref, ok);
// }
blocks[b].base = base
blocks[b].siz = siz
allocated += uint64(siz)
// println("Alloc", siz, base);
memset(base, 0xbb, siz)
bigger()
}
}
示例3: main
func main() {
runtime.Free(runtime.Alloc(1))
runtime.UpdateMemStats()
if *chatty {
fmt.Printf("%+v %v\n", runtime.MemStats, uint64(0))
}
}
示例4: main
func main() {
flag.Parse()
runtime.MemStats.Alloc = 0 // ignore stacks
for i := 0; i < 1<<7; i++ {
for j := 1; j <= 1<<22; j <<= 1 {
if i == 0 && *chatty {
println("First alloc:", j)
}
if a := runtime.MemStats.Alloc; a != 0 {
panicln("no allocations but stats report", a, "bytes allocated")
}
b := runtime.Alloc(uintptr(j))
during := runtime.MemStats.Alloc
runtime.Free(b)
if a := runtime.MemStats.Alloc; a != 0 {
panic("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
}
bigger()
}
if i%(1<<10) == 0 && *chatty {
println(i)
}
if i == 0 {
if *chatty {
println("Primed", i)
}
// runtime.frozen = true;
}
}
}
示例5: main
func main() {
runtime.GC() // clean up garbage from init
runtime.MemProfileRate = 0 // disable profiler
runtime.MemStats.Alloc = 0 // ignore stacks
flag.Parse()
for i := 0; i < 1<<7; i++ {
for j := 1; j <= 1<<22; j <<= 1 {
if i == 0 && *chatty {
println("First alloc:", j)
}
if a := runtime.MemStats.Alloc; a != 0 {
println("no allocations but stats report", a, "bytes allocated")
panic("fail")
}
b := runtime.Alloc(uintptr(j))
during := runtime.MemStats.Alloc
runtime.Free(b)
if a := runtime.MemStats.Alloc; a != 0 {
println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
panic("fail")
}
bigger()
}
if i%(1<<10) == 0 && *chatty {
println(i)
}
if i == 0 {
if *chatty {
println("Primed", i)
}
// runtime.frozen = true
}
}
}
示例6: AllocAndFree
func AllocAndFree(size, count int) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
if *chatty {
fmt.Printf("size=%d count=%d ...\n", size, count)
}
runtime.ReadMemStats(stats)
n1 := stats.Alloc
for i := 0; i < count; i++ {
b[i] = runtime.Alloc(uintptr(size))
base, n := runtime.Lookup(b[i])
if base != b[i] || !OkAmount(uintptr(size), n) {
println("lookup failed: got", base, n, "for", b[i])
panic("fail")
}
runtime.ReadMemStats(stats)
if stats.Sys > 1e9 {
println("too much memory allocated")
panic("fail")
}
}
runtime.ReadMemStats(stats)
n2 := stats.Alloc
if *chatty {
fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
}
n3 := stats.Alloc
for j := 0; j < count; j++ {
i := j
if *reverse {
i = count - 1 - j
}
alloc := uintptr(stats.Alloc)
base, n := runtime.Lookup(b[i])
if base != b[i] || !OkAmount(uintptr(size), n) {
println("lookup failed: got", base, n, "for", b[i])
panic("fail")
}
runtime.Free(b[i])
runtime.ReadMemStats(stats)
if stats.Alloc != uint64(alloc-n) {
println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n)
panic("fail")
}
if stats.Sys > 1e9 {
println("too much memory allocated")
panic("fail")
}
}
runtime.ReadMemStats(stats)
n4 := stats.Alloc
if *chatty {
fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
}
if n2-n1 != n3-n4 {
println("wrong alloc count: ", n2-n1, n3-n4)
panic("fail")
}
}
示例7: main
func main() {
memstats := new(runtime.MemStats)
runtime.Free(runtime.Alloc(1))
runtime.ReadMemStats(memstats)
if *chatty {
fmt.Printf("%+v %v\n", memstats, uint64(0))
}
}
示例8: newBagKV
func newBagKV(size uint8) *bagKV {
asize := sizeofSub*uintptr(size) + sizeofBagKV
b := (*bagKV)(unsafe.Pointer(runtime.Alloc(asize)))
b.occupied_ = size
return b
}
示例9: newSpanKV
func newSpanKV(size uint16) *spanKV {
asize := sizeofSub*uintptr(size) + sizeofSpanKV
s := (*spanKV)(unsafe.Pointer(runtime.Alloc(asize)))
s.size = size
return s
}