Use always XCNEW instead of XNEW

jacob navia jacob@jacob.remcomp.fr
Fri Sep 15 11:17:20 GMT 2023
Hi
Reflecting on the causes of the usage of XNEW (see my last mails) I think that as a general rule it would be much better if we always use XCNEW, that lease to calloc, instead of using XNEW that leads to malloc.

Look for instance at this code: (function riscv_set_arch file gas/config/tc-riscv.c)
	if (riscv_rps_as.subset_list == NULL) {
		riscv_rps_as.subset_list = XNEW(riscv_subset_list_t);
		riscv_rps_as.subset_list->head = NULL;
		riscv_rps_as.subset_list->tail = NULL;
		riscv_rps_as.subset_list->arch_str = NULL;
	}
All that could be replaced with:
	if (riscv_rps_as.subset_list == NULL) {
		riscv_rps_as.subset_list = XNEW(riscv_subset_list_t);
	}

Besides, if you add a new field to the structure you have to modify all calls to the constructor adding a new field to set to NULL. In the second form all that is done automatically.

I think it would be a good idea to change then all usages of XNEW in calls to XCNEW.

Jacob

P.S. Yes, I know, there will be a performance loss of some microseconds in the case where you initialize fields anyway but I think that has absolutely NO IMPORTANCE.


More information about the Binutils mailing list