Skip to content

Commit ef87690

Browse files
committed
Merge branch 'rs/allocate-cache-entry-individually'
* rs/allocate-cache-entry-individually: cache.h: put single NUL at end of struct cache_entry read-cache.c: allocate index entries individually Conflicts: read-cache.c
2 parents 51f737e + ee7825b commit ef87690

File tree

2 files changed

+33
-53
lines changed

2 files changed

+33
-53
lines changed

cache.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static inline unsigned int canon_mode(unsigned int mode)
306306
}
307307

308308
#define flexible_size(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
309-
#define cache_entry_size(len) flexible_size(cache_entry,len)
309+
#define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
310310
#define ondisk_cache_entry_size(len) flexible_size(ondisk_cache_entry,len)
311311
#define ondisk_cache_entry_extended_size(len) flexible_size(ondisk_cache_entry_extended,len)
312312

@@ -316,7 +316,6 @@ struct index_state {
316316
struct string_list *resolve_undo;
317317
struct cache_tree *cache_tree;
318318
struct cache_time timestamp;
319-
void *alloc;
320319
unsigned name_hash_initialized : 1,
321320
initialized : 1;
322321
struct hash_table name_hash;

read-cache.c

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,71 +1224,61 @@ int read_index(struct index_state *istate)
12241224
return read_index_from(istate, get_index_file());
12251225
}
12261226

1227-
static void convert_from_disk(struct ondisk_cache_entry *ondisk, struct cache_entry *ce)
1227+
static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk)
12281228
{
1229+
struct cache_entry *ce;
12291230
size_t len;
12301231
const char *name;
1232+
unsigned int flags;
12311233

1232-
ce->ce_ctime.sec = ntohl(ondisk->ctime.sec);
1233-
ce->ce_mtime.sec = ntohl(ondisk->mtime.sec);
1234-
ce->ce_ctime.nsec = ntohl(ondisk->ctime.nsec);
1235-
ce->ce_mtime.nsec = ntohl(ondisk->mtime.nsec);
1236-
ce->ce_dev = ntohl(ondisk->dev);
1237-
ce->ce_ino = ntohl(ondisk->ino);
1238-
ce->ce_mode = ntohl(ondisk->mode);
1239-
ce->ce_uid = ntohl(ondisk->uid);
1240-
ce->ce_gid = ntohl(ondisk->gid);
1241-
ce->ce_size = ntohl(ondisk->size);
12421234
/* On-disk flags are just 16 bits */
1243-
ce->ce_flags = ntohs(ondisk->flags);
1244-
1245-
hashcpy(ce->sha1, ondisk->sha1);
1235+
flags = ntohs(ondisk->flags);
1236+
len = flags & CE_NAMEMASK;
12461237

1247-
len = ce->ce_flags & CE_NAMEMASK;
1248-
1249-
if (ce->ce_flags & CE_EXTENDED) {
1238+
if (flags & CE_EXTENDED) {
12501239
struct ondisk_cache_entry_extended *ondisk2;
12511240
int extended_flags;
12521241
ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;
12531242
extended_flags = ntohs(ondisk2->flags2) << 16;
12541243
/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
12551244
if (extended_flags & ~CE_EXTENDED_FLAGS)
12561245
die("Unknown index entry format %08x", extended_flags);
1257-
ce->ce_flags |= extended_flags;
1246+
flags |= extended_flags;
12581247
name = ondisk2->name;
12591248
}
12601249
else
12611250
name = ondisk->name;
12621251

12631252
if (len == CE_NAMEMASK)
12641253
len = strlen(name);
1265-
/*
1266-
* NEEDSWORK: If the original index is crafted, this copy could
1267-
* go unchecked.
1268-
*/
1269-
memcpy(ce->name, name, len + 1);
1270-
}
12711254

1272-
static inline size_t estimate_cache_size(size_t ondisk_size, unsigned int entries)
1273-
{
1274-
size_t fix_size_mem = offsetof(struct cache_entry, name);
1275-
size_t fix_size_dsk = offsetof(struct ondisk_cache_entry, name);
1276-
long per_entry = (fix_size_mem - fix_size_dsk + 7) & ~7;
1255+
ce = xmalloc(cache_entry_size(len));
12771256

1278-
/*
1279-
* Alignment can cause differences. This should be "alignof", but
1280-
* since that's a gcc'ism, just use the size of a pointer.
1281-
*/
1282-
per_entry += sizeof(void *);
1283-
return ondisk_size + entries*per_entry;
1257+
ce->ce_ctime.sec = ntohl(ondisk->ctime.sec);
1258+
ce->ce_mtime.sec = ntohl(ondisk->mtime.sec);
1259+
ce->ce_ctime.nsec = ntohl(ondisk->ctime.nsec);
1260+
ce->ce_mtime.nsec = ntohl(ondisk->mtime.nsec);
1261+
ce->ce_dev = ntohl(ondisk->dev);
1262+
ce->ce_ino = ntohl(ondisk->ino);
1263+
ce->ce_mode = ntohl(ondisk->mode);
1264+
ce->ce_uid = ntohl(ondisk->uid);
1265+
ce->ce_gid = ntohl(ondisk->gid);
1266+
ce->ce_size = ntohl(ondisk->size);
1267+
ce->ce_flags = flags;
1268+
1269+
hashcpy(ce->sha1, ondisk->sha1);
1270+
1271+
memcpy(ce->name, name, len);
1272+
ce->name[len] = '\0';
1273+
return ce;
12841274
}
12851275

12861276
/* remember to discard_cache() before reading a different cache! */
12871277
int read_index_from(struct index_state *istate, const char *path)
12881278
{
12891279
int fd, i;
12901280
struct stat st;
1291-
unsigned long src_offset, dst_offset;
1281+
unsigned long src_offset;
12921282
struct cache_header *hdr;
12931283
void *mmap;
12941284
size_t mmap_size;
@@ -1327,29 +1317,18 @@ int read_index_from(struct index_state *istate, const char *path)
13271317
istate->cache_nr = ntohl(hdr->hdr_entries);
13281318
istate->cache_alloc = alloc_nr(istate->cache_nr);
13291319
istate->cache = xcalloc(istate->cache_alloc, sizeof(struct cache_entry *));
1330-
1331-
/*
1332-
* The disk format is actually larger than the in-memory format,
1333-
* due to space for nsec etc, so even though the in-memory one
1334-
* has room for a few more flags, we can allocate using the same
1335-
* index size
1336-
*/
1337-
istate->alloc = xmalloc(estimate_cache_size(mmap_size, istate->cache_nr));
13381320
istate->initialized = 1;
13391321

13401322
src_offset = sizeof(*hdr);
1341-
dst_offset = 0;
13421323
for (i = 0; i < istate->cache_nr; i++) {
13431324
struct ondisk_cache_entry *disk_ce;
13441325
struct cache_entry *ce;
13451326

13461327
disk_ce = (struct ondisk_cache_entry *)((char *)mmap + src_offset);
1347-
ce = (struct cache_entry *)((char *)istate->alloc + dst_offset);
1348-
convert_from_disk(disk_ce, ce);
1328+
ce = create_from_disk(disk_ce);
13491329
set_index_entry(istate, i, ce);
13501330

13511331
src_offset += ondisk_ce_size(ce);
1352-
dst_offset += ce_size(ce);
13531332
}
13541333
istate->timestamp.sec = st.st_mtime;
13551334
istate->timestamp.nsec = ST_MTIME_NSEC(st);
@@ -1383,11 +1362,15 @@ int read_index_from(struct index_state *istate, const char *path)
13831362

13841363
int is_index_unborn(struct index_state *istate)
13851364
{
1386-
return (!istate->cache_nr && !istate->alloc && !istate->timestamp.sec);
1365+
return (!istate->cache_nr && !istate->timestamp.sec);
13871366
}
13881367

13891368
int discard_index(struct index_state *istate)
13901369
{
1370+
int i;
1371+
1372+
for (i = 0; i < istate->cache_nr; i++)
1373+
free(istate->cache[i]);
13911374
resolve_undo_clear_index(istate);
13921375
istate->cache_nr = 0;
13931376
istate->cache_changed = 0;
@@ -1396,8 +1379,6 @@ int discard_index(struct index_state *istate)
13961379
istate->name_hash_initialized = 0;
13971380
free_hash(&istate->name_hash);
13981381
cache_tree_free(&(istate->cache_tree));
1399-
free(istate->alloc);
1400-
istate->alloc = NULL;
14011382
istate->initialized = 0;
14021383

14031384
/* no need to throw away allocated active_cache */

0 commit comments

Comments
 (0)