Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 78 lines (70 sloc) 1.915 kb
855419f7 » Linus Torvalds
2006-06-19 Add specialized object allocator
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 (which isn't needed, since
8 * we never free an object descriptor anyway), but even more because it ends
9 * up with maximal alignment because it doesn't know what the object alignment
10 * for the new allocation is.
11 */
12 #include "cache.h"
13 #include "object.h"
14 #include "blob.h"
15 #include "tree.h"
16 #include "commit.h"
17 #include "tag.h"
18
19 #define BLOCKING 1024
20
2c1cbec1 » torvalds
2007-04-16 Use proper object allocators for unknown object nodes too
21 #define DEFINE_ALLOCATOR(name, type) \
855419f7 » Linus Torvalds
2006-06-19 Add specialized object allocator
22 static unsigned int name##_allocs; \
100c5f3b » torvalds
2007-04-16 Clean up object creation to use more common code
23 void *alloc_##name##_node(void) \
855419f7 » Linus Torvalds
2006-06-19 Add specialized object allocator
24 { \
25 static int nr; \
2c1cbec1 » torvalds
2007-04-16 Use proper object allocators for unknown object nodes too
26 static type *block; \
27 void *ret; \
855419f7 » Linus Torvalds
2006-06-19 Add specialized object allocator
28 \
29 if (!nr) { \
30 nr = BLOCKING; \
2c1cbec1 » torvalds
2007-04-16 Use proper object allocators for unknown object nodes too
31 block = xmalloc(BLOCKING * sizeof(type)); \
855419f7 » Linus Torvalds
2006-06-19 Add specialized object allocator
32 } \
33 nr--; \
34 name##_allocs++; \
2c1cbec1 » torvalds
2007-04-16 Use proper object allocators for unknown object nodes too
35 ret = block++; \
36 memset(ret, 0, sizeof(type)); \
37 return ret; \
855419f7 » Linus Torvalds
2006-06-19 Add specialized object allocator
38 }
39
2c1cbec1 » torvalds
2007-04-16 Use proper object allocators for unknown object nodes too
40 union any_object {
41 struct object object;
42 struct blob blob;
43 struct tree tree;
44 struct commit commit;
45 struct tag tag;
46 };
47
48 DEFINE_ALLOCATOR(blob, struct blob)
49 DEFINE_ALLOCATOR(tree, struct tree)
969eba63 » peff
2014-06-10 commit: push commit_index update into alloc_commit_node
50 DEFINE_ALLOCATOR(raw_commit, struct commit)
2c1cbec1 » torvalds
2007-04-16 Use proper object allocators for unknown object nodes too
51 DEFINE_ALLOCATOR(tag, struct tag)
52 DEFINE_ALLOCATOR(object, union any_object)
855419f7 » Linus Torvalds
2006-06-19 Add specialized object allocator
53
969eba63 » peff
2014-06-10 commit: push commit_index update into alloc_commit_node
54 void *alloc_commit_node(void)
55 {
56 static int commit_count;
57 struct commit *c = alloc_raw_commit_node();
58 c->index = commit_count++;
59 return c;
60 }
61
4b25d091 » felipec
2009-05-01 Fix a bunch of pointer declarations (codestyle)
62 static void report(const char *name, unsigned int count, size_t size)
579d1fbf » Ramsay Allan Jones
2006-07-30 Add NO_C99_FORMAT to support older compilers.
63 {
28bd70d8 » jrn
2011-03-16 unbreak and eliminate NO_C99_FORMAT
64 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
65 name, count, (uintmax_t) size);
579d1fbf » Ramsay Allan Jones
2006-07-30 Add NO_C99_FORMAT to support older compilers.
66 }
67
c335d74d » peff
2014-06-10 alloc: include any-object allocations in alloc_report
68 #define REPORT(name, type) \
69 report(#name, name##_allocs, name##_allocs * sizeof(type) >> 10)
855419f7 » Linus Torvalds
2006-06-19 Add specialized object allocator
70
71 void alloc_report(void)
72 {
c335d74d » peff
2014-06-10 alloc: include any-object allocations in alloc_report
73 REPORT(blob, struct blob);
74 REPORT(tree, struct tree);
969eba63 » peff
2014-06-10 commit: push commit_index update into alloc_commit_node
75 REPORT(raw_commit, struct commit);
c335d74d » peff
2014-06-10 alloc: include any-object allocations in alloc_report
76 REPORT(tag, struct tag);
77 REPORT(object, union any_object);
855419f7 » Linus Torvalds
2006-06-19 Add specialized object allocator
78 }
Something went wrong with that request. Please try again.