git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Define constants for lengths of object names
@ 2014-05-01 11:06 brian m. carlson
  2014-05-01 17:20 ` Jonathan Nieder
  0 siblings, 1 reply; 7+ messages in thread
From: brian m. carlson @ 2014-05-01 11:06 UTC (permalink / raw)
  To: git; +Cc: Duy Nguyen, Michael Haggerty, Jonathan Nieder, Junio C Hamano

Using preprocessor constants rather than hardcoded numbers is considered a
good programming practice.  Provide two constants, GIT_OID_RAWSZ and
GIT_OID_HEXSZ, which are the lengths of an SHA-1 object name in bytes and
hex digits, respectively.  These names are the same as those used by
libgit2.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 object.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/object.h b/object.h
index 6e12f2c..f1cff2d 100644
--- a/object.h
+++ b/object.h
@@ -1,6 +1,13 @@
 #ifndef OBJECT_H
 #define OBJECT_H
 
+/*
+ * The length in bytes and in hex digits of an object name (SHA-1 value).
+ * These are the same names used by libgit2.
+ */
+#define GIT_OID_RAWSZ 20
+#define GIT_OID_HEXSZ 40
+
 struct object_list {
 	struct object *item;
 	struct object_list *next;
@@ -49,7 +56,7 @@ struct object {
 	unsigned used : 1;
 	unsigned type : TYPE_BITS;
 	unsigned flags : FLAG_BITS;
-	unsigned char sha1[20];
+	unsigned char sha1[GIT_OID_RAWSZ];
 };
 
 extern const char *typename(unsigned int type);
-- 
2.0.0.rc0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] Define constants for lengths of object names
  2014-05-01 11:06 [PATCH] Define constants for lengths of object names brian m. carlson
@ 2014-05-01 17:20 ` Jonathan Nieder
  2014-05-01 18:23   ` Jonathan Nieder
  2014-05-01 23:00   ` brian m. carlson
  0 siblings, 2 replies; 7+ messages in thread
From: Jonathan Nieder @ 2014-05-01 17:20 UTC (permalink / raw)
  To: brian m. carlson; +Cc: git, Duy Nguyen, Michael Haggerty, Junio C Hamano

Hi,

brian m. carlson wrote:

> --- a/object.h
> +++ b/object.h
[...]
> @@ -49,7 +56,7 @@ struct object {
>  	unsigned used : 1;
>  	unsigned type : TYPE_BITS;
>  	unsigned flags : FLAG_BITS;
> -	unsigned char sha1[20];
> +	unsigned char sha1[GIT_OID_RAWSZ];

Maybe my brain has been damaged by reading code from too many C
projects that hard-code some constants, but I find '20' easier to read
here.

What happened to the

	struct object_id {
		unsigned char id[20];
	};

	...

	struct object {
		...
		struct object_id id;
	};

idea?

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Define constants for lengths of object names
  2014-05-01 17:20 ` Jonathan Nieder
@ 2014-05-01 18:23   ` Jonathan Nieder
  2014-05-01 23:00   ` brian m. carlson
  1 sibling, 0 replies; 7+ messages in thread
From: Jonathan Nieder @ 2014-05-01 18:23 UTC (permalink / raw)
  To: brian m. carlson; +Cc: git, Duy Nguyen, Michael Haggerty, Junio C Hamano

Jonathan Nieder wrote:
> brian m. carlson wrote:

>> --- a/object.h
>> +++ b/object.h
> [...]
>> @@ -49,7 +56,7 @@ struct object {
>>  	unsigned used : 1;
>>  	unsigned type : TYPE_BITS;
>>  	unsigned flags : FLAG_BITS;
>> -	unsigned char sha1[20];
>> +	unsigned char sha1[GIT_OID_RAWSZ];
>
> Maybe my brain has been damaged by reading code from too many C
> projects that hard-code some constants, but I find '20' easier to read
> here.

That said, RAW_SHA1_BYTES would sound okay to me.

Part of the problem was how long it takes to mentally parse oid_rawsz.

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Define constants for lengths of object names
  2014-05-01 17:20 ` Jonathan Nieder
  2014-05-01 18:23   ` Jonathan Nieder
@ 2014-05-01 23:00   ` brian m. carlson
  2014-05-01 23:05     ` Jonathan Nieder
  1 sibling, 1 reply; 7+ messages in thread
From: brian m. carlson @ 2014-05-01 23:00 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: git, Duy Nguyen, Michael Haggerty, Junio C Hamano

[-- Attachment #1: Type: text/plain, Size: 1108 bytes --]

On Thu, May 01, 2014 at 10:20:07AM -0700, Jonathan Nieder wrote:
> Hi,
> 
> brian m. carlson wrote:
> 
> > --- a/object.h
> > +++ b/object.h
> [...]
> > @@ -49,7 +56,7 @@ struct object {
> >  	unsigned used : 1;
> >  	unsigned type : TYPE_BITS;
> >  	unsigned flags : FLAG_BITS;
> > -	unsigned char sha1[20];
> > +	unsigned char sha1[GIT_OID_RAWSZ];
> 
> Maybe my brain has been damaged by reading code from too many C
> projects that hard-code some constants, but I find '20' easier to read
> here.
> 
> What happened to the
> 
> 	struct object_id {
> 		unsigned char id[20];
> 	};
> 
> 	...
> 
> 	struct object {
> 		...
> 		struct object_id id;
> 	};
> 
> idea?

There didn't seem to be a huge amount of support for it.  Also, there
were concerns that some architectures might impose alignment constraints
on it that made sizeof(struct object_id) != 20.

-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Define constants for lengths of object names
  2014-05-01 23:00   ` brian m. carlson
@ 2014-05-01 23:05     ` Jonathan Nieder
  2014-05-02  0:15       ` Duy Nguyen
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Nieder @ 2014-05-01 23:05 UTC (permalink / raw)
  To: brian m. carlson; +Cc: git, Duy Nguyen, Michael Haggerty, Junio C Hamano

brian m. carlson wrote:
> On Thu, May 01, 2014 at 10:20:07AM -0700, Jonathan Nieder wrote:

>> What happened to the
>>
>> 	struct object_id {
>> 		unsigned char id[20];
>> 	};
>>
>> 	...
>>
>> 	struct object {
>> 		...
>> 		struct object_id id;
>> 	};
>>
>> idea?
>
> There didn't seem to be a huge amount of support for it.

I can make up for it in enthuasiasm.  Please?  It's something I've
wanted for a long time but never found the time to do.

>                                                           Also, there
> were concerns that some architectures might impose alignment constraints
> on it that made sizeof(struct object_id) != 20.

Sounds awful.  What architecture?

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Define constants for lengths of object names
  2014-05-01 23:05     ` Jonathan Nieder
@ 2014-05-02  0:15       ` Duy Nguyen
  2014-05-02 22:09         ` brian m. carlson
  0 siblings, 1 reply; 7+ messages in thread
From: Duy Nguyen @ 2014-05-02  0:15 UTC (permalink / raw)
  To: Jonathan Nieder
  Cc: brian m. carlson, Git Mailing List, Michael Haggerty, Junio C Hamano

On Fri, May 2, 2014 at 6:05 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> brian m. carlson wrote:
>> On Thu, May 01, 2014 at 10:20:07AM -0700, Jonathan Nieder wrote:
>
>>> What happened to the
>>>
>>>      struct object_id {
>>>              unsigned char id[20];
>>>      };
>>>
>>>      ...
>>>
>>>      struct object {
>>>              ...
>>>              struct object_id id;
>>>      };
>>>
>>> idea?
>>
>> There didn't seem to be a huge amount of support for it.
>
> I can make up for it in enthuasiasm.  Please?  It's something I've
> wanted for a long time but never found the time to do.

It's definitely better in the sense that the compiler will catch new
"char[20]" declarations for us. It's also a lot more work.

>>                                                           Also, there
>> were concerns that some architectures might impose alignment constraints
>> on it that made sizeof(struct object_id) != 20.
>
> Sounds awful.  What architecture?

No architecture was named last time if I remember correctly. But we
could check "sizeof(struct object_id) == 20" in a test or something.
When people scream, we can pack the struct on that particular
platform?
-- 
Duy

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Define constants for lengths of object names
  2014-05-02  0:15       ` Duy Nguyen
@ 2014-05-02 22:09         ` brian m. carlson
  0 siblings, 0 replies; 7+ messages in thread
From: brian m. carlson @ 2014-05-02 22:09 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Jonathan Nieder, Git Mailing List, Michael Haggerty, Junio C Hamano

[-- Attachment #1: Type: text/plain, Size: 1353 bytes --]

On Fri, May 02, 2014 at 07:15:44AM +0700, Duy Nguyen wrote:
> On Fri, May 2, 2014 at 6:05 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> >
> > I can make up for it in enthuasiasm.  Please?  It's something I've
> > wanted for a long time but never found the time to do.
> 
> It's definitely better in the sense that the compiler will catch new
> "char[20]" declarations for us. It's also a lot more work.

It is.  I'm going to start with a patch that introduces struct object_id
and the fixed constants.  Then I'm going to get a patch that compiles
with lots of warnings, and then I'm going to fix all those warnings.
Otherwise, the patch will simply be too enormous to review.

I'm willing to hear other suggestions for going about this, though.

> No architecture was named last time if I remember correctly. But we
> could check "sizeof(struct object_id) == 20" in a test or something.
> When people scream, we can pack the struct on that particular
> platform?

Sounds like a plan.  I am not aware of any architecture that has this
limitation; I've worked with x86(-64)?, 32-bit PowerPC, UltraSPARC, and
ARM.

-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-05-02 22:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-01 11:06 [PATCH] Define constants for lengths of object names brian m. carlson
2014-05-01 17:20 ` Jonathan Nieder
2014-05-01 18:23   ` Jonathan Nieder
2014-05-01 23:00   ` brian m. carlson
2014-05-01 23:05     ` Jonathan Nieder
2014-05-02  0:15       ` Duy Nguyen
2014-05-02 22:09         ` brian m. carlson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).