On Sun, Mar 25, 2018 at 01:55:07AM +0530, Sukrit Bhatnagar wrote: > Hi, > > I am interested in implementing the GCC cleanup attribute for automatic > resource freeing as part of GSoC'18. I have shared a proposal for the same. > > This mail is to discuss the code design for implementing it. > > > Here are some of my ideas: > > This attribute requires a cleanup function that is called automatically > when the corresponding variable goes out of scope. There are some functions > whose logic can be reused: > > - Functions such as virCommandFree, virConfFreeList and virCgroupFree can > be directly used as cleanup functions. They have parameter and return type > valid for a cleanup function. > > - Functions such as virFileClose and virFileFclose need some additional > consideration as they return a value. I think we can set some global > variable in a separate source file (just like errno variable from errno.h). > Then the value to be returned can be accessed globally. Note we call VIR_FORCE_CLOSE and VIR_FORCE_FCLOSE in cleanup scenarios to explicitly ignore the return value. We only care about checking return value for VIR_CLOSE and VIR_FCLOSE in a tiny set of cases. > > - Functions such as virDomainEventGraphicsDispose need an entirely new > design. They are used as callbacks in object classes and passed as an > argument in virClassNew. This would require making changes to > virObjectUnref's code too. *This is the part I am not sure how to implement > cleanup logic for.* There shouldn't be any need to touch the DIspose funtions - that's internal helper code. To "free" an object variable, you just want to end up invoking the regular virObjectUnref function as the cleanup func. > Also, since the __attribute__((__cleanup__(anyfunc))) looks ugly, a macro > like autoclean (ideas for macro name welcome!) can be used instead. As > Martin pointed out in my proposal, for some types, this can be done right > after typedef declarations, so that the type itself contains this attribute. Just suffix it with "Auto", eg virDomainAutoPtr, vs virDomainPtr. > I can create new files vircleanup.{c,h} for defining cleanup functions for > types which do not have an existing cleanup/free function. This can be done > separately for each driver supported. > For example, cleanups pertaining to lxc driver will be in > src/lxc/lxc_cleanup.c. I'm not rally seeing a compelling need for this - just add it in appropriate existing files. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :| -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list