Backport from trunk r9871

Fixed compilation when using system malloc for pkg memory