src: replace CONTAINER_OF with type-safe function

Replace the CONTAINER_OF macro with a template function that is as
type-safe as a reinterpret_cast<> of an arbitrary pointer can be made.

/cc @indutny.  This is right up your alley, I think.