Re: [kvm-unit-tests PATCH v2] alloc: Add memalign error checks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Nov 11, 2019 at 11:12:41AM +0100, Janosch Frank wrote:
> On 11/4/19 12:29 PM, Paolo Bonzini wrote:
> > On 04/11/19 11:54, Andrew Jones wrote:
> >>
> >> diff --git a/lib/alloc.c b/lib/alloc.c
> >> index ecdbbc44dbf9..ed8f5f94c9b0 100644
> >> --- a/lib/alloc.c
> >> +++ b/lib/alloc.c
> >> @@ -46,15 +46,17 @@ void *memalign(size_t alignment, size_t size)
> >>  	uintptr_t blkalign;
> >>  	uintptr_t mem;
> >>  
> >> +	if (!size)
> >> +		return NULL;
> >> +
> >> +	assert(alignment >= sizeof(void *) && is_power_of_2(alignment));
> >>  	assert(alloc_ops && alloc_ops->memalign);
> >> -	if (alignment <= sizeof(uintptr_t))
> >> -		alignment = sizeof(uintptr_t);
> >> -	else
> >> -		size += alignment - 1;
> >>  
> >> +	size += alignment - 1;
> >>  	blkalign = MAX(alignment, alloc_ops->align_min);
> >>  	size = ALIGN(size + METADATA_EXTRA, alloc_ops->align_min);
> >>  	p = alloc_ops->memalign(blkalign, size);
> >> +	assert(p);
> 
> I had some more time to think about and test this and I think returning
> NULL here is more useful. My usecase is a limit test where I allocate
> until I get a NULL and then free everything afterwards.

I think I prefer the assert here, since most users of this will not be
expecting to run out of memory, and therefore not checking for it. I
think you may want to just write your own allocator in your unit test
that's based on alloc_pages().

Thanks,
drew





[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux