{"thread":{"id":"6420","subject":"[PATCH 2/2] Use fixed-size integers for .idx file I/O","startedAt":"2007-01-18T07:24:27Z","lastAt":"2007-01-18T15:30:52Z","messageCount":6,"participants":["Junio C Hamano","Morten Welinder","Simon 'corecode' Schubert","Johannes Schindelin","Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"31945","messageId":"7vd55col04.fsf@assigned-by-dhcp.cox.net","threadId":"6420","inReplyTo":null,"subject":"[PATCH 2/2] Use fixed-size integers for .idx file I/O","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-01-18T07:24:27Z","receivedAt":"2007-01-18T07:24:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This attempts to finish what Simon started in the previous commit.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n * I do not think this should have any platform dependent impact\n   as long as 1/2 is Ok, but for completeness's sake here it is.\n\n builtin-pack-objects.c |    6 +++---\n cache.h                |    2 +-\n sha1_file.c            |    6 +++---\n 3 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin-pack-objects.c b/builtin-pack-objects.c\nindex 42dd8c8..3824ee3 100644\n--- a/builtin-pack-objects.c\n+++ b/builtin-pack-objects.c\n@@ -569,7 +569,7 @@ static void write_index_file(void)\n \t\t\t\t\tsha1_to_hex(object_list_sha1), \"idx\");\n \tstruct object_entry **list = sorted_by_sha;\n \tstruct object_entry **last = list + nr_result;\n-\tunsigned int array[256];\n+\tuint32_t array[256];\n \n \t/*\n \t * Write the first-level table (the list is sorted,\n@@ -587,7 +587,7 @@ static void write_index_file(void)\n \t\tarray[i] = htonl(next - sorted_by_sha);\n \t\tlist = next;\n \t}\n-\tsha1write(f, array, 256 * sizeof(int));\n+\tsha1write(f, array, 256 * 4);\n \n \t/*\n \t * Write the actual SHA1 entries..\n@@ -595,7 +595,7 @@ static void write_index_file(void)\n \tlist = sorted_by_sha;\n \tfor (i = 0; i < nr_result; i++) {\n \t\tstruct object_entry *entry = *list++;\n-\t\tunsigned int offset = htonl(entry->offset);\n+\t\tuint32_t offset = htonl(entry->offset);\n \t\tsha1write(f, &offset, 4);\n \t\tsha1write(f, entry->sha1, 20);\n \t}\ndiff --git a/cache.h b/cache.h\nindex fda3f8e..e6e19bd 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -351,7 +351,7 @@ struct pack_window {\n extern struct packed_git {\n \tstruct packed_git *next;\n \tstruct pack_window *windows;\n-\tunsigned int *index_base;\n+\tuint32_t *index_base;\n \toff_t index_size;\n \toff_t pack_size;\n \tint pack_fd;\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 0b70545..3025440 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -435,7 +435,7 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,\n \t\t\t\tvoid **idx_map_)\n {\n \tvoid *idx_map;\n-\tunsigned int *index;\n+\tuint32_t *index;\n \tunsigned long idx_size;\n \tint nr, i;\n \tint fd = open(path, O_RDONLY);\n@@ -1351,7 +1351,7 @@ int nth_packed_object_sha1(const struct packed_git *p, int n,\n unsigned long find_pack_entry_one(const unsigned char *sha1,\n \t\t\t\t  struct packed_git *p)\n {\n-\tunsigned int *level1_ofs = p->index_base;\n+\tuint32_t *level1_ofs = p->index_base;\n \tint hi = ntohl(level1_ofs[*sha1]);\n \tint lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));\n \tvoid *index = p->index_base + 256;\n@@ -1360,7 +1360,7 @@ unsigned long find_pack_entry_one(const unsigned char *sha1,\n \t\tint mi = (lo + hi) / 2;\n \t\tint cmp = hashcmp((unsigned char *)index + (24 * mi) + 4, sha1);\n \t\tif (!cmp)\n-\t\t\treturn ntohl(*((unsigned int *) ((char *) index + (24 * mi))));\n+\t\t\treturn ntohl(*((uint32_t *)((char *)index + (24 * mi))));\n \t\tif (cmp > 0)\n \t\t\thi = mi;\n \t\telse\n-- \n1.5.0.rc1.g05cf8\n"},{"id":"31980","messageId":"118833cc0701180651w3b5ac164m4e396399f1d58cb7@mail.gmail.com","threadId":"6420","inReplyTo":"7vd55col04.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH 2/2] Use fixed-size integers for .idx file I/O","fromName":"Morten Welinder","fromEmail":"mwelinder@gmail.com","sentAt":"2007-01-18T14:51:37Z","receivedAt":"2007-01-18T14:51:37Z","isPatch":true,"sender":{"key":"mwelinder@gmail.com","avatar":null},"body":"> -                       return ntohl(*((unsigned int *) ((char *) index + (24 * mi))));\n> +                       return ntohl(*((uint32_t *)((char *)index + (24 * mi))));\n\nIs that pointer gymnastics guaranteed to work?  I.e., how do we know\nthat we can access an uint32_t (or unsigned) at such an address?\n\nM.\n"},{"id":"31982","messageId":"45AF8A86.7070101@fs.ei.tum.de","threadId":"6420","inReplyTo":"118833cc0701180651w3b5ac164m4e396399f1d58cb7@mail.gmail.com","subject":"Re: [PATCH 2/2] Use fixed-size integers for .idx file I/O","fromName":"Simon 'corecode' Schubert","fromEmail":"corecode@fs.ei.tum.de","sentAt":"2007-01-18T14:56:06Z","receivedAt":"2007-01-18T14:56:06Z","isPatch":true,"sender":{"key":"corecode@fs.ei.tum.de","avatar":"https://gravatar.com/avatar/eff9dbf0cdac0d1e6a6cd7ed0e50763edcb376b493b5253a35ff167918ad79e1?d=mp&s=160"},"body":"Morten Welinder wrote:\n>> -                       return ntohl(*((unsigned int *) ((char *) \n>> index + (24 * mi))));\n>> +                       return ntohl(*((uint32_t *)((char *)index + \n>> (24 * mi))));\n> \n> Is that pointer gymnastics guaranteed to work?  I.e., how do we know\n> that we can access an uint32_t (or unsigned) at such an address?\n\nif index is always aligned to a 4-byte boundary, this is safe.  apart from that, the problem already existed.\n\ncheers\n  simon\n\n-- \nServe - BSD     +++  RENT this banner advert  +++    ASCII Ribbon   /\"\\\nWork - Mac      +++  space for low €€€ NOW!1  +++      Campaign     \\ /\nParty Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \\\nDude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \\\n\n"},{"id":"31986","messageId":"Pine.LNX.4.63.0701181612440.22628@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"6420","inReplyTo":"45AF8A86.7070101@fs.ei.tum.de","subject":"Re: [PATCH 2/2] Use fixed-size integers for .idx file I/O","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-01-18T15:15:28Z","receivedAt":"2007-01-18T15:15:28Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Thu, 18 Jan 2007, Simon 'corecode' Schubert wrote:\n\n> Morten Welinder wrote:\n> > > -                       return ntohl(*((unsigned int *) ((char *) index +\n> > > (24 * mi))));\n> > > +                       return ntohl(*((uint32_t *)((char *)index + (24 *\n> > > mi))));\n> > \n> > Is that pointer gymnastics guaranteed to work?  I.e., how do we know\n> > that we can access an uint32_t (or unsigned) at such an address?\n> \n> if index is always aligned to a 4-byte boundary, this is safe.  apart from\n> that, the problem already existed.\n\nindex is assigned from p->index_base, which comes from \ncheck_packed_git_idx(), and there it comes from an xmmap(). AFAICT mmap() \n(and for NO_MMAP, malloc()) _always_ return aligned pointers, so there is \nno problem, as long as the alignment step divides 24.\n\nCiao,\nDscho\n"},{"id":"31988","messageId":"20070118152107.GA15428@spearce.org","threadId":"6420","inReplyTo":"45AF8A86.7070101@fs.ei.tum.de","subject":"Re: [PATCH 2/2] Use fixed-size integers for .idx file I/O","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-01-18T15:21:08Z","receivedAt":"2007-01-18T15:21:08Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Simon 'corecode' Schubert <corecode@fs.ei.tum.de> wrote:\n> Morten Welinder wrote:\n> >>-                       return ntohl(*((unsigned int *) ((char *) \n> >>index + (24 * mi))));\n> >>+                       return ntohl(*((uint32_t *)((char *)index + \n> >>(24 * mi))));\n> >\n> >Is that pointer gymnastics guaranteed to work?  I.e., how do we know\n> >that we can access an uint32_t (or unsigned) at such an address?\n> \n> if index is always aligned to a 4-byte boundary, this is safe.  apart from \n> that, the problem already existed.\n\nIts always 4-byte aligned here.  The index is mmap()'d as one huge\nchunk so the first byte of the index is page aligned.  The index\nstarts out with 256 4-byte words, then is composed of 24 byte units\n(20 byte SHA1, 4 byte offset).  So no worries with the current file\nformat, or code.\n\nYes, we're taking the leap of faith that any currently-used processor\nwill work on 32 bit unsigned integers if they are 4 byte aligned in\nmemory.  Maybe someone has a processor that this isn't true for, but\na lot of other software would probably break on that same system...\n\n-- \nShawn.\n"},{"id":"31991","messageId":"Pine.LNX.4.63.0701181630070.22628@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"6420","inReplyTo":"20070118152107.GA15428@spearce.org","subject":"Re: [PATCH 2/2] Use fixed-size integers for .idx file I/O","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-01-18T15:30:52Z","receivedAt":"2007-01-18T15:30:52Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Thu, 18 Jan 2007, Shawn O. Pearce wrote:\n\n> Simon 'corecode' Schubert <corecode@fs.ei.tum.de> wrote:\n> > Morten Welinder wrote:\n> > >>-                       return ntohl(*((unsigned int *) ((char *) \n> > >>index + (24 * mi))));\n> > >>+                       return ntohl(*((uint32_t *)((char *)index + \n> > >>(24 * mi))));\n> > >\n> > >Is that pointer gymnastics guaranteed to work?  I.e., how do we know\n> > >that we can access an uint32_t (or unsigned) at such an address?\n> > \n> > if index is always aligned to a 4-byte boundary, this is safe.  apart from \n> > that, the problem already existed.\n> \n> Its always 4-byte aligned here.  The index is mmap()'d as one huge\n> chunk so the first byte of the index is page aligned.  The index\n> starts out with 256 4-byte words, then is composed of 24 byte units\n> (20 byte SHA1, 4 byte offset).  So no worries with the current file\n> format, or code.\n> \n> Yes, we're taking the leap of faith that any currently-used processor\n> will work on 32 bit unsigned integers if they are 4 byte aligned in\n> memory.\n\nActually, it's even valid in 8 byte aligned mode. (24 is divisible by 8.)\n\nCiao,\nDscho\n"}]}