Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * alloc.c - specialized allocator for internal objects |
| 3 | * |
| 4 | * Copyright (C) 2006 Linus Torvalds |
| 5 | * |
| 6 | * The standard malloc/free wastes too much space for objects, partly because |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 7 | * it maintains all the allocation infrastructure, but even more because it ends |
Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 8 | * up with maximal alignment because it doesn't know what the object alignment |
| 9 | * for the new allocation is. |
| 10 | */ |
Elijah Newren | a64215b | 2023-02-24 00:09:30 +0000 | [diff] [blame] | 11 | #include "git-compat-util.h" |
Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 12 | #include "object.h" |
| 13 | #include "blob.h" |
| 14 | #include "tree.h" |
| 15 | #include "commit.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 16 | #include "repository.h" |
Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 17 | #include "tag.h" |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 18 | #include "alloc.h" |
Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 19 | |
| 20 | #define BLOCKING 1024 |
| 21 | |
Linus Torvalds | 2c1cbec | 2007-04-16 22:10:19 -0700 | [diff] [blame] | 22 | union any_object { |
| 23 | struct object object; |
| 24 | struct blob blob; |
| 25 | struct tree tree; |
| 26 | struct commit commit; |
| 27 | struct tag tag; |
| 28 | }; |
| 29 | |
Ramsay Jones | 225ea22 | 2014-07-13 02:41:41 -0400 | [diff] [blame] | 30 | struct alloc_state { |
Ramsay Jones | 225ea22 | 2014-07-13 02:41:41 -0400 | [diff] [blame] | 31 | int nr; /* number of nodes left in current allocation */ |
| 32 | void *p; /* first free node in current allocation */ |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 33 | |
| 34 | /* bookkeeping of allocations */ |
| 35 | void **slabs; |
| 36 | int slab_nr, slab_alloc; |
Ramsay Jones | 225ea22 | 2014-07-13 02:41:41 -0400 | [diff] [blame] | 37 | }; |
| 38 | |
Elijah Newren | 1731310 | 2018-08-15 10:54:06 -0700 | [diff] [blame] | 39 | struct alloc_state *allocate_alloc_state(void) |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 40 | { |
| 41 | return xcalloc(1, sizeof(struct alloc_state)); |
| 42 | } |
| 43 | |
| 44 | void clear_alloc_state(struct alloc_state *s) |
| 45 | { |
| 46 | while (s->slab_nr > 0) { |
| 47 | s->slab_nr--; |
| 48 | free(s->slabs[s->slab_nr]); |
| 49 | } |
| 50 | |
| 51 | FREE_AND_NULL(s->slabs); |
| 52 | } |
| 53 | |
Ramsay Jones | 225ea22 | 2014-07-13 02:41:41 -0400 | [diff] [blame] | 54 | static inline void *alloc_node(struct alloc_state *s, size_t node_size) |
| 55 | { |
| 56 | void *ret; |
| 57 | |
| 58 | if (!s->nr) { |
| 59 | s->nr = BLOCKING; |
| 60 | s->p = xmalloc(BLOCKING * node_size); |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 61 | |
| 62 | ALLOC_GROW(s->slabs, s->slab_nr + 1, s->slab_alloc); |
| 63 | s->slabs[s->slab_nr++] = s->p; |
Ramsay Jones | 225ea22 | 2014-07-13 02:41:41 -0400 | [diff] [blame] | 64 | } |
| 65 | s->nr--; |
Ramsay Jones | 225ea22 | 2014-07-13 02:41:41 -0400 | [diff] [blame] | 66 | ret = s->p; |
| 67 | s->p = (char *)s->p + node_size; |
| 68 | memset(ret, 0, node_size); |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 69 | |
Ramsay Jones | 225ea22 | 2014-07-13 02:41:41 -0400 | [diff] [blame] | 70 | return ret; |
| 71 | } |
| 72 | |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 73 | void *alloc_blob_node(struct repository *r) |
Jeff King | 600e2a6 | 2014-07-13 02:41:51 -0400 | [diff] [blame] | 74 | { |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 75 | struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob)); |
Jeff King | d36f51c | 2014-07-13 02:41:55 -0400 | [diff] [blame] | 76 | b->object.type = OBJ_BLOB; |
Jeff King | 600e2a6 | 2014-07-13 02:41:51 -0400 | [diff] [blame] | 77 | return b; |
| 78 | } |
| 79 | |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 80 | void *alloc_tree_node(struct repository *r) |
Jeff King | 600e2a6 | 2014-07-13 02:41:51 -0400 | [diff] [blame] | 81 | { |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 82 | struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree)); |
Jeff King | d36f51c | 2014-07-13 02:41:55 -0400 | [diff] [blame] | 83 | t->object.type = OBJ_TREE; |
Jeff King | 600e2a6 | 2014-07-13 02:41:51 -0400 | [diff] [blame] | 84 | return t; |
| 85 | } |
| 86 | |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 87 | void *alloc_tag_node(struct repository *r) |
Jeff King | 600e2a6 | 2014-07-13 02:41:51 -0400 | [diff] [blame] | 88 | { |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 89 | struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag)); |
Jeff King | d36f51c | 2014-07-13 02:41:55 -0400 | [diff] [blame] | 90 | t->object.type = OBJ_TAG; |
Jeff King | 600e2a6 | 2014-07-13 02:41:51 -0400 | [diff] [blame] | 91 | return t; |
| 92 | } |
| 93 | |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 94 | void *alloc_object_node(struct repository *r) |
Jeff King | 600e2a6 | 2014-07-13 02:41:51 -0400 | [diff] [blame] | 95 | { |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 96 | struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object)); |
Jeff King | d36f51c | 2014-07-13 02:41:55 -0400 | [diff] [blame] | 97 | obj->type = OBJ_NONE; |
Jeff King | 600e2a6 | 2014-07-13 02:41:51 -0400 | [diff] [blame] | 98 | return obj; |
| 99 | } |
Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 100 | |
Abhishek Kumar | 6da43d9 | 2020-06-17 14:44:08 +0530 | [diff] [blame] | 101 | /* |
| 102 | * The returned count is to be used as an index into commit slabs, |
| 103 | * that are *NOT* maintained per repository, and that is why a single |
| 104 | * global counter is used. |
| 105 | */ |
| 106 | static unsigned int alloc_commit_index(void) |
Jeff King | 94d5a22 | 2014-07-13 02:42:08 -0400 | [diff] [blame] | 107 | { |
Abhishek Kumar | 6da43d9 | 2020-06-17 14:44:08 +0530 | [diff] [blame] | 108 | static unsigned int parsed_commits_count; |
| 109 | return parsed_commits_count++; |
Jeff King | 94d5a22 | 2014-07-13 02:42:08 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Abhishek Kumar | 6da43d9 | 2020-06-17 14:44:08 +0530 | [diff] [blame] | 112 | void init_commit_node(struct commit *c) |
SZEDER Gábor | 4468d44 | 2019-01-27 14:08:32 +0100 | [diff] [blame] | 113 | { |
| 114 | c->object.type = OBJ_COMMIT; |
Abhishek Kumar | 6da43d9 | 2020-06-17 14:44:08 +0530 | [diff] [blame] | 115 | c->index = alloc_commit_index(); |
SZEDER Gábor | 4468d44 | 2019-01-27 14:08:32 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 118 | void *alloc_commit_node(struct repository *r) |
Jeff King | 969eba6 | 2014-06-10 17:39:04 -0400 | [diff] [blame] | 119 | { |
Stefan Beller | 14ba97f | 2018-05-15 14:48:42 -0700 | [diff] [blame] | 120 | struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit)); |
Abhishek Kumar | 6da43d9 | 2020-06-17 14:44:08 +0530 | [diff] [blame] | 121 | init_commit_node(c); |
Jeff King | 969eba6 | 2014-06-10 17:39:04 -0400 | [diff] [blame] | 122 | return c; |
| 123 | } |