read-cache.c: fix index memory allocation

estimate_cache_size() tries to guess how much memory is needed for the
in-memory representation of an index file.  It does that by using the
file size, the number of entries and the difference of the sizes of the
on-disk and in-memory structs -- without having to check the length of
the name of each entry, which varies for each entry, but their sums are
the same no matter the representation.

Except there can be a difference.  First of all, the size is really
calculated by ce_size and ondisk_ce_size based on offsetof(..., name),
not sizeof, which can be different.  And entries are padded with 1 to 8
NULs at the end (after the variable name) to make their total length a
multiple of eight.

So in order to allocate enough memory to hold the index, change the
delta calculation to be based on offsetof(..., name) and round up to
the next multiple of eight.

On a 32-bit Linux, this delta was used before:

	sizeof(struct cache_entry)        == 72
	sizeof(struct ondisk_cache_entry) == 64
	                                    ---
	                                      8

The actual difference for an entry with a filename length of one was,
however (find the definitions are in cache.h):

	offsetof(struct cache_entry, name)        == 72
	offsetof(struct ondisk_cache_entry, name) == 62

	ce_size        == (72 + 1 + 8) & ~7 == 80
	ondisk_ce_size == (62 + 1 + 8) & ~7 == 64
	                                      ---
	                                       16

So eight bytes less had been allocated for such entries.  The new
formula yields the correct delta:

	(72 - 62 + 7) & ~7 == 16

Reported-by: John Hsing <tsyj2007@gmail.com>
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2 files changed
tree: 422eccf364dfd61fb9ea14447a0e3a274dd728b7
  1. block-sha1/
  2. builtin/
  3. compat/
  4. contrib/
  5. Documentation/
  6. git-gui/
  7. git_remote_helpers/
  8. gitk-git/
  9. gitweb/
  10. mergetools/
  11. perl/
  12. po/
  13. ppc/
  14. t/
  15. templates/
  16. vcs-svn/
  17. xdiff/
  18. .gitattributes
  19. .gitignore
  20. .mailmap
  21. abspath.c
  22. aclocal.m4
  23. advice.c
  24. advice.h
  25. alias.c
  26. alloc.c
  27. archive-tar.c
  28. archive-zip.c
  29. archive.c
  30. archive.h
  31. attr.c
  32. attr.h
  33. base85.c
  34. bisect.c
  35. bisect.h
  36. blob.c
  37. blob.h
  38. branch.c
  39. branch.h
  40. builtin.h
  41. bundle.c
  42. bundle.h
  43. cache-tree.c
  44. cache-tree.h
  45. cache.h
  46. check-builtins.sh
  47. check-racy.c
  48. check_bindir
  49. color.c
  50. color.h
  51. combine-diff.c
  52. command-list.txt
  53. commit.c
  54. commit.h
  55. config.c
  56. config.mak.in
  57. configure.ac
  58. connect.c
  59. convert.c
  60. convert.h
  61. copy.c
  62. COPYING
  63. csum-file.c
  64. csum-file.h
  65. ctype.c
  66. daemon.c
  67. date.c
  68. decorate.c
  69. decorate.h
  70. delta.h
  71. diff-delta.c
  72. diff-lib.c
  73. diff-no-index.c
  74. diff.c
  75. diff.h
  76. diffcore-break.c
  77. diffcore-delta.c
  78. diffcore-order.c
  79. diffcore-pickaxe.c
  80. diffcore-rename.c
  81. diffcore.h
  82. dir.c
  83. dir.h
  84. editor.c
  85. entry.c
  86. environment.c
  87. exec_cmd.c
  88. exec_cmd.h
  89. fast-import.c
  90. fetch-pack.h
  91. fixup-builtins
  92. fsck.c
  93. fsck.h
  94. generate-cmdlist.sh
  95. gettext.c
  96. gettext.h
  97. git-add--interactive.perl
  98. git-am.sh
  99. git-archimport.perl
  100. git-bisect.sh
  101. git-compat-util.h
  102. git-cvsexportcommit.perl
  103. git-cvsimport.perl
  104. git-cvsserver.perl
  105. git-difftool--helper.sh
  106. git-difftool.perl
  107. git-filter-branch.sh
  108. git-instaweb.sh
  109. git-lost-found.sh
  110. git-merge-octopus.sh
  111. git-merge-one-file.sh
  112. git-merge-resolve.sh
  113. git-mergetool--lib.sh
  114. git-mergetool.sh
  115. git-parse-remote.sh
  116. git-pull.sh
  117. git-quiltimport.sh
  118. git-rebase--am.sh
  119. git-rebase--interactive.sh
  120. git-rebase--merge.sh
  121. git-rebase.sh
  122. git-relink.perl
  123. git-remote-testgit.py
  124. git-repack.sh
  125. git-request-pull.sh
  126. git-send-email.perl
  127. git-sh-i18n.sh
  128. git-sh-setup.sh
  129. git-stash.sh
  130. git-submodule.sh
  131. git-svn.perl
  132. GIT-VERSION-GEN
  133. git-web--browse.sh
  134. git.c
  135. git.spec.in
  136. graph.c
  137. graph.h
  138. grep.c
  139. grep.h
  140. hash.c
  141. hash.h
  142. help.c
  143. help.h
  144. hex.c
  145. http-backend.c
  146. http-fetch.c
  147. http-push.c
  148. http-walker.c
  149. http.c
  150. http.h
  151. ident.c
  152. imap-send.c
  153. INSTALL
  154. kwset.c
  155. kwset.h
  156. levenshtein.c
  157. levenshtein.h
  158. LGPL-2.1
  159. list-objects.c
  160. list-objects.h
  161. ll-merge.c
  162. ll-merge.h
  163. lockfile.c
  164. log-tree.c
  165. log-tree.h
  166. mailmap.c
  167. mailmap.h
  168. Makefile
  169. match-trees.c
  170. merge-file.c
  171. merge-file.h
  172. merge-recursive.c
  173. merge-recursive.h
  174. name-hash.c
  175. notes-cache.c
  176. notes-cache.h
  177. notes-merge.c
  178. notes-merge.h
  179. notes.c
  180. notes.h
  181. object.c
  182. object.h
  183. pack-check.c
  184. pack-refs.c
  185. pack-refs.h
  186. pack-revindex.c
  187. pack-revindex.h
  188. pack-write.c
  189. pack.h
  190. pager.c
  191. parse-options-cb.c
  192. parse-options.c
  193. parse-options.h
  194. patch-delta.c
  195. patch-ids.c
  196. patch-ids.h
  197. path.c
  198. pkt-line.c
  199. pkt-line.h
  200. preload-index.c
  201. pretty.c
  202. progress.c
  203. progress.h
  204. quote.c
  205. quote.h
  206. reachable.c
  207. reachable.h
  208. read-cache.c
  209. README
  210. reflog-walk.c
  211. reflog-walk.h
  212. refs.c
  213. refs.h
  214. remote-curl.c
  215. remote.c
  216. remote.h
  217. replace_object.c
  218. rerere.c
  219. rerere.h
  220. resolve-undo.c
  221. resolve-undo.h
  222. revision.c
  223. revision.h
  224. run-command.c
  225. run-command.h
  226. send-pack.h
  227. server-info.c
  228. setup.c
  229. sh-i18n--envsubst.c
  230. sha1-array.c
  231. sha1-array.h
  232. sha1-lookup.c
  233. sha1-lookup.h
  234. sha1_file.c
  235. sha1_name.c
  236. shallow.c
  237. shell.c
  238. shortlog.h
  239. show-index.c
  240. sideband.c
  241. sideband.h
  242. sigchain.c
  243. sigchain.h
  244. strbuf.c
  245. strbuf.h
  246. streaming.c
  247. streaming.h
  248. string-list.c
  249. string-list.h
  250. submodule.c
  251. submodule.h
  252. symlinks.c
  253. tag.c
  254. tag.h
  255. tar.h
  256. test-chmtime.c
  257. test-ctype.c
  258. test-date.c
  259. test-delta.c
  260. test-dump-cache-tree.c
  261. test-genrandom.c
  262. test-index-version.c
  263. test-line-buffer.c
  264. test-match-trees.c
  265. test-mktemp.c
  266. test-obj-pool.c
  267. test-parse-options.c
  268. test-path-utils.c
  269. test-run-command.c
  270. test-sha1.c
  271. test-sha1.sh
  272. test-sigchain.c
  273. test-string-pool.c
  274. test-subprocess.c
  275. test-svn-fe.c
  276. test-treap.c
  277. thread-utils.c
  278. thread-utils.h
  279. trace.c
  280. transport-helper.c
  281. transport.c
  282. transport.h
  283. tree-diff.c
  284. tree-walk.c
  285. tree-walk.h
  286. tree.c
  287. tree.h
  288. unimplemented.sh
  289. unpack-trees.c
  290. unpack-trees.h
  291. upload-pack.c
  292. url.c
  293. url.h
  294. usage.c
  295. userdiff.c
  296. userdiff.h
  297. utf8.c
  298. utf8.h
  299. walker.c
  300. walker.h
  301. wrap-for-bin.sh
  302. wrapper.c
  303. write_or_die.c
  304. ws.c
  305. wt-status.c
  306. wt-status.h
  307. xdiff-interface.c
  308. xdiff-interface.h
  309. zlib.c