Skip to content

Commit

Permalink
Clean up object creation to use more common code
Browse files Browse the repository at this point in the history
This replaces the fairly odd "created_object()" function that did _most_
of the object setup with a more complete "create_object()" function that
also has a more natural calling convention.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
torvalds authored and Junio C Hamano committed Apr 17, 2007
1 parent 2c1cbec commit 100c5f3
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 43 deletions.
2 changes: 1 addition & 1 deletion alloc.c
Expand Up @@ -20,7 +20,7 @@


#define DEFINE_ALLOCATOR(name, type) \ #define DEFINE_ALLOCATOR(name, type) \
static unsigned int name##_allocs; \ static unsigned int name##_allocs; \
struct name *alloc_##name##_node(void) \ void *alloc_##name##_node(void) \
{ \ { \
static int nr; \ static int nr; \
static type *block; \ static type *block; \
Expand Down
8 changes: 2 additions & 6 deletions blob.c
Expand Up @@ -6,12 +6,8 @@ const char *blob_type = "blob";
struct blob *lookup_blob(const unsigned char *sha1) struct blob *lookup_blob(const unsigned char *sha1)
{ {
struct object *obj = lookup_object(sha1); struct object *obj = lookup_object(sha1);
if (!obj) { if (!obj)
struct blob *ret = alloc_blob_node(); return create_object(sha1, OBJ_BLOB, alloc_blob_node());
created_object(sha1, &ret->object);
ret->object.type = OBJ_BLOB;
return ret;
}
if (!obj->type) if (!obj->type)
obj->type = OBJ_BLOB; obj->type = OBJ_BLOB;
if (obj->type != OBJ_BLOB) { if (obj->type != OBJ_BLOB) {
Expand Down
14 changes: 5 additions & 9 deletions cache.h
Expand Up @@ -476,15 +476,11 @@ int decode_85(char *dst, const char *line, int linelen);
void encode_85(char *buf, const unsigned char *data, int bytes); void encode_85(char *buf, const unsigned char *data, int bytes);


/* alloc.c */ /* alloc.c */
struct blob; extern void *alloc_blob_node(void);
struct tree; extern void *alloc_tree_node(void);
struct commit; extern void *alloc_commit_node(void);
struct tag; extern void *alloc_tag_node(void);
extern struct blob *alloc_blob_node(void); extern void *alloc_object_node(void);
extern struct tree *alloc_tree_node(void);
extern struct commit *alloc_commit_node(void);
extern struct tag *alloc_tag_node(void);
extern struct object *alloc_object_node(void);
extern void alloc_report(void); extern void alloc_report(void);


/* trace.c */ /* trace.c */
Expand Down
8 changes: 2 additions & 6 deletions commit.c
Expand Up @@ -98,12 +98,8 @@ struct commit *lookup_commit_reference(const unsigned char *sha1)
struct commit *lookup_commit(const unsigned char *sha1) struct commit *lookup_commit(const unsigned char *sha1)
{ {
struct object *obj = lookup_object(sha1); struct object *obj = lookup_object(sha1);
if (!obj) { if (!obj)
struct commit *ret = alloc_commit_node(); return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
created_object(sha1, &ret->object);
ret->object.type = OBJ_COMMIT;
return ret;
}
if (!obj->type) if (!obj->type)
obj->type = OBJ_COMMIT; obj->type = OBJ_COMMIT;
return check_commit(obj, sha1, 0); return check_commit(obj, sha1, 0);
Expand Down
14 changes: 7 additions & 7 deletions object.c
Expand Up @@ -105,11 +105,13 @@ static void grow_object_hash(void)
obj_hash_size = new_hash_size; obj_hash_size = new_hash_size;
} }


void created_object(const unsigned char *sha1, struct object *obj) void *create_object(const unsigned char *sha1, int type, void *o)
{ {
struct object *obj = o;

obj->parsed = 0; obj->parsed = 0;
obj->used = 0; obj->used = 0;
obj->type = OBJ_NONE; obj->type = type;
obj->flags = 0; obj->flags = 0;
hashcpy(obj->sha1, sha1); hashcpy(obj->sha1, sha1);


Expand All @@ -118,16 +120,14 @@ void created_object(const unsigned char *sha1, struct object *obj)


insert_obj_hash(obj, obj_hash, obj_hash_size); insert_obj_hash(obj, obj_hash, obj_hash_size);
nr_objs++; nr_objs++;
return obj;
} }


struct object *lookup_unknown_object(const unsigned char *sha1) struct object *lookup_unknown_object(const unsigned char *sha1)
{ {
struct object *obj = lookup_object(sha1); struct object *obj = lookup_object(sha1);
if (!obj) { if (!obj)
obj = alloc_object_node(); obj = create_object(sha1, OBJ_NONE, alloc_object_node());
created_object(sha1, obj);
obj->type = OBJ_NONE;
}
return obj; return obj;
} }


Expand Down
2 changes: 1 addition & 1 deletion object.h
Expand Up @@ -46,7 +46,7 @@ extern struct object_refs *lookup_object_refs(struct object *);
/** Internal only **/ /** Internal only **/
struct object *lookup_object(const unsigned char *sha1); struct object *lookup_object(const unsigned char *sha1);


void created_object(const unsigned char *sha1, struct object *obj); extern void *create_object(const unsigned char *sha1, int type, void *obj);


/** Returns the object, having parsed it to find out what it is. **/ /** Returns the object, having parsed it to find out what it is. **/
struct object *parse_object(const unsigned char *sha1); struct object *parse_object(const unsigned char *sha1);
Expand Down
10 changes: 3 additions & 7 deletions tag.c
Expand Up @@ -20,13 +20,9 @@ struct object *deref_tag(struct object *o, const char *warn, int warnlen)


struct tag *lookup_tag(const unsigned char *sha1) struct tag *lookup_tag(const unsigned char *sha1)
{ {
struct object *obj = lookup_object(sha1); struct object *obj = lookup_object(sha1);
if (!obj) { if (!obj)
struct tag *ret = alloc_tag_node(); return create_object(sha1, OBJ_TAG, alloc_tag_node());
created_object(sha1, &ret->object);
ret->object.type = OBJ_TAG;
return ret;
}
if (!obj->type) if (!obj->type)
obj->type = OBJ_TAG; obj->type = OBJ_TAG;
if (obj->type != OBJ_TAG) { if (obj->type != OBJ_TAG) {
Expand Down
8 changes: 2 additions & 6 deletions tree.c
Expand Up @@ -127,12 +127,8 @@ int read_tree(struct tree *tree, int stage, const char **match)
struct tree *lookup_tree(const unsigned char *sha1) struct tree *lookup_tree(const unsigned char *sha1)
{ {
struct object *obj = lookup_object(sha1); struct object *obj = lookup_object(sha1);
if (!obj) { if (!obj)
struct tree *ret = alloc_tree_node(); return create_object(sha1, OBJ_TREE, alloc_tree_node());
created_object(sha1, &ret->object);
ret->object.type = OBJ_TREE;
return ret;
}
if (!obj->type) if (!obj->type)
obj->type = OBJ_TREE; obj->type = OBJ_TREE;
if (obj->type != OBJ_TREE) { if (obj->type != OBJ_TREE) {
Expand Down

0 comments on commit 100c5f3

Please sign in to comment.