alloc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /*
  87. * The returned count is to be used as an index into commit slabs,
  88. * that are *NOT* maintained per repository, and that is why a single
  89. * global counter is used.
  90. */
  91. static unsigned int alloc_commit_index(void)
  92. {
  93. static unsigned int parsed_commits_count;
  94. return parsed_commits_count++;
  95. }
  96. void init_commit_node(struct commit *c)
  97. {
  98. c->object.type = OBJ_COMMIT;
  99. c->index = alloc_commit_index();
  100. }
  101. void *alloc_commit_node(struct repository *r)
  102. {
  103. struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
  104. init_commit_node(c);
  105. return c;
  106. }
  107. static void report(const char *name, unsigned int count, size_t size)
  108. {
  109. fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
  110. name, count, (uintmax_t) size);
  111. }
  112. #define REPORT(name, type) \
  113. report(#name, r->parsed_objects->name##_state->count, \
  114. r->parsed_objects->name##_state->count * sizeof(type) >> 10)
  115. void alloc_report(struct repository *r)
  116. {
  117. REPORT(blob, struct blob);
  118. REPORT(tree, struct tree);
  119. REPORT(commit, struct commit);
  120. REPORT(tag, struct tag);
  121. REPORT(object, union any_object);
  122. }