mem: fix pointer pointer aliasing violations

This uses explicit memory copying to read and write pointer to pointers
of arbitrary object types. This works provided that the architecture
uses the same representation for all pointer types (the previous code
made that assumption already anyway).

Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
