Use g_memdup2() where available and add fallback for older GLib versions
Alloc size is based on existing allocations and struct sizes.
g_memdup() is deprecated since GLib 2.68 and we want to avoid deprecation warnings with recent versions of GLib.
Edited by Tim-Philipp Müller