platform: improve nmp_object_stackinit_id() for types that don't implement cmd_plobj_id_copy()

An object type that doesn't implement cmd_plobj_id_copy(), either:

- implements cmd_obj_copy(), but then we cannot copy the ID only
  to a stack instance, because that cannot track ownership.
  This is a bug in the caller. We cannot use stackinit for an
  object of a type that is not a plain old data (in C++ terms).

- fallback to plain memcpy(). That is in line with nmp_object_clone().
  and nmp_object_copy().
This commit is contained in:
Thomas Haller
2022-07-14 18:14:07 +02:00
parent 52c8ee2c9d
commit a242d41cc0

View File

@@ -840,6 +840,17 @@ nmp_object_stackinit_id(NMPObject *obj, const NMPObject *src)
_nmp_object_stackinit_from_class(obj, klass); _nmp_object_stackinit_from_class(obj, klass);
if (klass->cmd_plobj_id_copy) if (klass->cmd_plobj_id_copy)
klass->cmd_plobj_id_copy(&obj->object, &src->object); klass->cmd_plobj_id_copy(&obj->object, &src->object);
else {
/* This object must not implement cmd_obj_copy().
* If it would, it would mean that we require a deep copy
* of the data. As @obj is stack-allocated, it cannot track
* ownership. The caller must not use nmp_object_stackinit_id()
* with an object of such a type. */
nm_assert(!klass->cmd_obj_copy);
/* plain memcpy of the public part suffices. */
memcpy(&obj->object, &src->object, klass->sizeof_data);
}
return obj; return obj;
} }