Skip to content
Snippets Groups Projects
malloc.go 70 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	l.next, l.mapped = base, base
    	l.end = base + size
    
    func (l *linearAlloc) alloc(size, align uintptr, sysStat *sysMemStat, vmaName string) unsafe.Pointer {
    
    	if p+size > l.end {
    		return nil
    	}
    	l.next = p + size
    
    	if pEnd := alignUp(l.next-1, physPageSize); pEnd > l.mapped {
    
    		if l.mapMemory {
    			// Transition from Reserved to Prepared to Ready.
    
    			sysMap(unsafe.Pointer(l.mapped), n, sysStat, vmaName)
    
    			sysUsed(unsafe.Pointer(l.mapped), n, n)
    
    		l.mapped = pEnd
    	}
    	return unsafe.Pointer(p)
    }
    
    
    // notInHeap is off-heap memory allocated by a lower-level allocator
    // like sysAlloc or persistentAlloc.
    //
    
    // In general, it's better to use real types which embed
    
    // internal/runtime/sys.NotInHeap, but this serves as a generic type
    
    // for situations where that isn't possible (like in the allocators).
    
    //
    // TODO: Use this as the return type of sysAlloc, persistentAlloc, etc?
    
    type notInHeap struct{ _ sys.NotInHeap }
    
    
    func (p *notInHeap) add(bytes uintptr) *notInHeap {
    	return (*notInHeap)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + bytes))
    }
    
    // redZoneSize computes the size of the redzone for a given allocation.
    
    // Refer to the implementation of the compiler-rt.
    
    func redZoneSize(userSize uintptr) uintptr {
    
    	switch {
    	case userSize <= (64 - 16):
    		return 16 << 0
    	case userSize <= (128 - 32):
    		return 16 << 1
    	case userSize <= (512 - 64):
    		return 16 << 2
    	case userSize <= (4096 - 128):
    		return 16 << 3
    	case userSize <= (1<<14)-256:
    		return 16 << 4
    	case userSize <= (1<<15)-512:
    		return 16 << 5
    	case userSize <= (1<<16)-1024:
    		return 16 << 6
    	default:
    		return 16 << 7
    	}
    }