This is a lot of work here. Can't we just call free and then allocate? Perhaps with temporarily disabling free callbacks because it isn't actually freed? Not sure if that would be more or less elegant.