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"
1819
#define BLOCKING 1024
2021
#define DEFINE_ALLOCATOR(name) \
22static unsigned int name##_allocs; \
23struct name *alloc_##name##_node(void) \
24{ \
25static int nr; \
26static struct name *block; \
27\
28if (!nr) { \
29nr = BLOCKING; \
30block = xcalloc(BLOCKING, sizeof(struct name)); \
31} \
32nr--; \
33name##_allocs++; \
34return block++; \
35}
3637
DEFINE_ALLOCATOR(blob)
38DEFINE_ALLOCATOR(tree)
39DEFINE_ALLOCATOR(commit)
40DEFINE_ALLOCATOR(tag)
4142
#define REPORT(name) \
43fprintf(stderr, "%10s: %8u (%zu kB)\n", #name, name##_allocs, name##_allocs*sizeof(struct name) >> 10)
4445
void alloc_report(void)
46{
47REPORT(blob);
48REPORT(tree);
49REPORT(commit);
50REPORT(tag);
51}