[libvirt] [PATCH 1/1] Implement variable length structure allocator
Eric Blake
eblake at redhat.com
Mon Apr 5 17:51:48 UTC 2010
On 04/05/2010 11:21 AM, David Allan wrote:
> +int virAllocVar(void *ptrptr, size_t struct_size, size_t element_size, size_t count)
Shouldn't that be void **ptrptr - that is, the caller passes in the
address of a void* that we then modify?
> +{
> + size_t alloc_size = 0;
> +
> +#if TEST_OOM
> + if (virAllocTestFail())
> + return -1;
> +#endif
> +
> + if (VIR_ALLOC_VAR_OVERSIZED(struct_size, count, element_size)) {
> + errno = ENOMEM;
> + return -1;
> + }
> +
> + alloc_size = struct_size + (element_size * count);
> + *(void **)ptrptr = calloc(1, alloc_size);
> + if (*(void **)ptrptr == NULL)
Especially since typing it correctly to begin with would avoid these
ugly type-punning casts?
> +++ b/src/util/memory.h
> @@ -48,6 +48,10 @@
> int virAlloc(void *ptrptr, size_t size) ATTRIBUTE_RETURN_CHECK;
> int virAllocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK;
> int virReallocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK;
> +int virAllocVar(void *ptrptr,
> + size_t struct_size,
> + size_t element_size,
> + size_t count) ATTRIBUTE_RETURN_CHECK;
Then again, fixing the type for your new method would imply fixing the
typing of virAlloc and friends as well.
> +#define VIR_ALLOC_VAR(ptr, type, count) virAllocVar(&(ptr), sizeof(*ptr), sizeof(type), (count))
Should that second argument be sizeof(*(ptr)) for safety? On the other
hand, the parenthesis around count are redundant, if you want to strip them.
--
Eric Blake eblake at redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 323 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20100405/b0bf7e1e/attachment-0001.sig>
More information about the libvir-list
mailing list