diff options
| author | Albert van der Linde <alinde@google.com> | 2020-07-14 07:47:26 +0000 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-07-14 12:20:37 +0200 |
| commit | 6f4580264a29fa73097e96b436141a8594b97610 (patch) | |
| tree | 3e184241f624d90f04cffc9d546eee4e03099216 /prog/alloc.go | |
| parent | 230553f68fcaa90508b724edd0dfc806669c1f22 (diff) | |
prog/alloc: align address allocation for aligned[addr]
Calls to alloc didn't respect the alignment attribute. Now
Type.Alignment() is used to ensure each type is correctly
aligned. Existing descriptions with [align[X]] don't have an
issue as they align to small blocks and default align is to
64 bytes. This commits adds support for [align[X]] for an X
larger than 64.
Diffstat (limited to 'prog/alloc.go')
| -rw-r--r-- | prog/alloc.go | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/prog/alloc.go b/prog/alloc.go index 344ec7a0e..a4b7b7f1f 100644 --- a/prog/alloc.go +++ b/prog/alloc.go @@ -51,13 +51,18 @@ func (ma *memAlloc) noteAlloc(addr0, size0 uint64) { } } -func (ma *memAlloc) alloc(r *randGen, size0 uint64) uint64 { +// alloc returns the next free address of size0 with respect to the given alignment. +func (ma *memAlloc) alloc(r *randGen, size0, alignment0 uint64) uint64 { if size0 == 0 { size0 = 1 } + if alignment0 == 0 { + alignment0 = 1 + } size := (size0 + memAllocGranule - 1) / memAllocGranule + alignment := (alignment0 + memAllocGranule - 1) / memAllocGranule end := ma.size - size - for start := uint64(0); start <= end; start++ { + for start := uint64(0); start <= end; start += alignment { empty := true for i := uint64(0); i < size; i++ { if ma.get(start + i) { @@ -72,7 +77,7 @@ func (ma *memAlloc) alloc(r *randGen, size0 uint64) uint64 { } } ma.bankruptcy() - return ma.alloc(r, size0) + return ma.alloc(r, size0, alignment0) } func (ma *memAlloc) bankruptcy() { |
