From: Derrick Stolee Date: Thu, 04 Aug 2022 14:50:57 GMT Subject: Re: [PATCH v2 05/10] log-tree: use ref_namespaces instead of if/else-if Message-ID: <96cf691b-5190-5ba7-d612-e8473055c45c@github.com> In-Reply-To: <2d24b539-a5b8-ed7d-2320-97588854eb5b@github.com> On 8/4/2022 10:34 AM, Derrick Stolee wrote: > On 8/4/2022 9:31 AM, Derrick Stolee wrote: >> On 8/3/2022 2:31 AM, Junio C Hamano wrote: >>> Very nice. The double-dash in the NAMESPACE__COUNT constant somehow >>> looks strange. As we scan through ref_namespace[] array densely, >>> >>> for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) >>> ... >>> >>> without having to use the constant would probably be more in line >>> with the way how the rest of the codebase works. >> >> Ah, I did not know about that trick. Thanks! > > ...except that it doesn't work because the array is declared as > 'extern' so we don't know its size outside of refs.c. > > This motivates the use of NAMESPACE__COUNT (and the double > underscores differentiates the "COUNT" from other namespaces, so > there is no confusion about "COUNT" being a namespace). If there > is another way around this, then I would love to hear it! My current workaround is to define the size of the array in the header file: --- enum ref_namespace { NAMESPACE_HEAD, NAMESPACE_BRANCHES, NAMESPACE_TAGS, NAMESPACE_REMOTE_REFS, NAMESPACE_STASH, NAMESPACE_REPLACE, NAMESPACE_NOTES, NAMESPACE_PREFETCH, NAMESPACE_REWRITTEN, /* Must be last */ NAMESPACE__COUNT }; /* See refs.c for the contents of this array. */ extern struct ref_namespace_info ref_namespaces[NAMESPACE__COUNT]; --- Then ARRAY_SIZE(ref_namespaces) works properly. Thanks, -Stolee