Value Witness Table
As well as protocol witness table this table exists in each type, that fits protocol. It contains four methods realizations: allocate, copy, destruct, deallocate. The object life cycle is managed by these methods. Look at example:
- While creating (Point(...) as Drawable) object allocate method is called from this object's VWT. allocate method decides where the object's content has to be placed (in value buffer or on heap) and if it places on heap it makes required quantity of memory
- copy method places object content into relevant place
- After finishing the work with object the destruct method will be called and reduce all the reference counters if they exist
- After destruct the deallocate method will be called and free the allocated memory on heap if it exist
Protocol Witness Table
As I mentioned above, it contains realizations of methods, that protocol requires, for type that linked with this table
Existential Container - Answers
So we replied to two questions:
- Protocol-Witness table is kept in Existential Container of this object and can be received from it
- If array element type is protocol then any element of this array takes fixed value of 5 machine words - it's required for Existential Container. If element content can't be placed in value buffer it places on heap. If it is, the content places on value buffer. In any case we get that object size with protocol type equals 5 machine words (40 bits) so all the array elements will have same size