THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.2KB

  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
  7. * it maintains all the allocation infrastructure, but even more because it ends
  8. * up with maximal alignment because it doesn't know what the object alignment
  9. * for the new allocation is.
  10. */
  11. #include "cache.h"
  12. #include "object.h"
  13. #include "blob.h"
  14. #include "tree.h"
  15. #include "commit.h"
  16. #include "tag.h"
  17. #include "alloc.h"
  18. #define BLOCKING 1024
  19. union any_object {
  20. struct object object;
  21. struct blob blob;
  22. struct tree tree;
  23. struct commit commit;
  24. struct tag tag;
  25. };
  26. struct alloc_state {
  27. int count; /* total number of nodes allocated */
  28. int nr; /* number of nodes left in current allocation */
  29. void *p; /* first free node in current allocation */
  30. /* bookkeeping of allocations */
  31. void **slabs;
  32. int slab_nr, slab_alloc;
  33. };
  34. struct alloc_state *allocate_alloc_state(void)
  35. {
  36. return xcalloc(1, sizeof(struct alloc_state));
  37. }
  38. void clear_alloc_state(struct alloc_state *s)
  39. {
  40. while (s->slab_nr > 0) {
  41. s->slab_nr--;
  42. free(s->slabs[s->slab_nr]);
  43. }
  44. FREE_AND_NULL(s->slabs);
  45. }
  46. static inline void *alloc_node(struct alloc_state *s, size_t node_size)
  47. {
  48. void *ret;
  49. if (!s->nr) {
  50. s->nr = BLOCKING;
  51. s->p = xmalloc(BLOCKING * node_size);
  52. ALLOC_GROW(s->slabs, s->slab_nr + 1, s->slab_alloc);
  53. s->slabs[s->slab_nr++] = s->p;
  54. }
  55. s->nr--;
  56. s->count++;
  57. ret = s->p;
  58. s->p = (char *)s->p + node_size;
  59. memset(ret, 0, node_size);
  60. return ret;
  61. }
  62. void *alloc_blob_node(struct repository *r)
  63. {
  64. struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob));
  65. b->object.type = OBJ_BLOB;
  66. return b;
  67. }
  68. void *alloc_tree_node(struct repository *r)
  69. {
  70. struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree));
  71. t->object.type = OBJ_TREE;
  72. return t;
  73. }
  74. void *alloc_tag_node(struct repository *r)
  75. {
  76. struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag));
  77. t->object.type = OBJ_TAG;
  78. return t;
  79. }
  80. void *alloc_object_node(struct repository *r)
  81. {
  82. struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object));
  83. obj->type = OBJ_NONE;
  84. return obj;
  85. }
  86. static unsigned int alloc_commit_index(struct repository *r)
  87. {
  88. return r->parsed_objects->commit_count++;
  89. }
  90. void init_commit_node(struct repository *r, struct commit *c)
  91. {
  92. c->object.type = OBJ_COMMIT;
  93. c->index = alloc_commit_index(r);
  94. c->graph_pos = COMMIT_NOT_FROM_GRAPH;
  95. c->generation = GENERATION_NUMBER_INFINITY;
  96. }
  97. void *alloc_commit_node(struct repository *r)
  98. {
  99. struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
  100. init_commit_node(r, c);
  101. return c;
  102. }
  103. static void report(const char *name, unsigned int count, size_t size)
  104. {
  105. fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
  106. name, count, (uintmax_t) size);
  107. }
  108. #define REPORT(name, type) \
  109. report(#name, r->parsed_objects->name##_state->count, \
  110. r->parsed_objects->name##_state->count * sizeof(type) >> 10)
  111. void alloc_report(struct repository *r)
  112. {
  113. REPORT(blob, struct blob);
  114. REPORT(tree, struct tree);
  115. REPORT(commit, struct commit);
  116. REPORT(tag, struct tag);
  117. REPORT(object, union any_object);
  118. }