Wrap common allocate-and-swap code in a macro.
refs #638