github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/mm/fallback.go (about)

     1  package mm
     2  
     3  const FallbackDef = `
     4  package xxx
     5  
     6  import "github.com/egonelbre/exp/mm"
     7  
     8  type Fallback struct {
     9  	Primary  {{ .Primary.Type  }}
    10  	Fallback {{ .Fallback.Type }}
    11  }
    12  
    13  func (m *Fallback) Alignment() int {
    14  	return {{ min .Primary.Alignment .Fallback.Alignment }}
    15  }
    16  
    17  func (m *Fallback) Alloc(size int) unsafe.Pointer {
    18  	if size == 0 {
    19  		return nil
    20  	}
    21  	p := m.Primary.Allocate(size)
    22  	if p == nil {
    23  		p = m.Fallback.Allocate(size)
    24  	}
    25  	return p
    26  }
    27  
    28  {{ if and .Primary.Owns (or .Primary.Dealloc .Fallback.Dealloc) }}
    29  func (m *Fallback) Dealloc(p unsafe.Pointer) {
    30  	if m.Primary.Owns(p) {
    31  		{{ if .Primary.Dealloc }}
    32  			return m.Primary.Dealloc(p)
    33  		{{ else }}
    34  			return false
    35  		{{ end }}
    36  	} else {
    37  		{{ if .Fallback.Dealloc }}
    38  			return m.Fallback.Dealloc(p)
    39  		{{ else }}
    40  			return false
    41  		{{ end }}
    42  	}
    43  }
    44  {{ end }}
    45  
    46  {{ if and .Primary.Empty .Fallback.Empty }}
    47  func (m *Fallback) Empty() bool {
    48  	return m.Primary.Empty() && m.Fallback.Empty()
    49  }
    50  {{ end }}
    51  `