Use g_memdup2() where available and add fallback for older GLib versions