• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef ALLOCATE_H
2 #define ALLOCATE_H
3 
4 #include "compat.h"
5 
6 struct allocation_blob {
7 	struct allocation_blob *next;
8 	unsigned int left, offset;
9 	unsigned char data[];
10 };
11 
12 struct allocator_struct {
13 	const char *name;
14 	struct allocation_blob *blobs;
15 	unsigned int alignment;
16 	unsigned int chunking;
17 	void *freelist;
18 	/* statistics */
19 	unsigned int allocations;
20 	unsigned long total_bytes, useful_bytes;
21 };
22 
23 struct allocator_stats {
24 	const char *name;
25 	unsigned int allocations;
26 	unsigned long total_bytes, useful_bytes;
27 };
28 
29 extern void protect_allocations(struct allocator_struct *desc);
30 extern void drop_all_allocations(struct allocator_struct *desc);
31 extern void *allocate(struct allocator_struct *desc, unsigned int size);
32 extern void free_one_entry(struct allocator_struct *desc, void *entry);
33 extern void show_allocations(struct allocator_struct *);
34 extern void get_allocator_stats(struct allocator_struct *, struct allocator_stats *);
35 extern void show_allocation_stats(void);
36 
37 #define __DECLARE_ALLOCATOR(type, x)		\
38 	extern type *__alloc_##x(int);		\
39 	extern void __free_##x(type *);		\
40 	extern void show_##x##_alloc(void);	\
41 	extern void get_##x##_stats(struct allocator_stats *);		\
42 	extern void clear_##x##_alloc(void);	\
43 	extern void protect_##x##_alloc(void);
44 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
45 
46 #define __DO_ALLOCATOR(type, objsize, objalign, objname, x)	\
47 	static struct allocator_struct x##_allocator = {	\
48 		.name = objname,				\
49 		.alignment = objalign,				\
50 		.chunking = CHUNK };				\
51 	type *__alloc_##x(int extra)				\
52 	{							\
53 		return allocate(&x##_allocator, objsize+extra);	\
54 	}							\
55 	void __free_##x(type *entry)				\
56 	{							\
57 		free_one_entry(&x##_allocator, entry);		\
58 	}							\
59 	void show_##x##_alloc(void)				\
60 	{							\
61 		show_allocations(&x##_allocator);		\
62 	}							\
63 	void get_##x##_stats(struct allocator_stats *s)		\
64 	{							\
65 		get_allocator_stats(&x##_allocator, s);		\
66 	}							\
67 	void clear_##x##_alloc(void)				\
68 	{							\
69 		drop_all_allocations(&x##_allocator);		\
70 	}							\
71 	void protect_##x##_alloc(void)				\
72 	{							\
73 		protect_allocations(&x##_allocator);		\
74 	}
75 
76 #define __ALLOCATOR(t, n, x) 					\
77 	__DO_ALLOCATOR(t, sizeof(t), __alignof__(t), n, x)
78 
79 #define ALLOCATOR(x, n) __ALLOCATOR(struct x, n, x)
80 
81 DECLARE_ALLOCATOR(ident);
82 DECLARE_ALLOCATOR(token);
83 DECLARE_ALLOCATOR(context);
84 DECLARE_ALLOCATOR(symbol);
85 DECLARE_ALLOCATOR(asm_operand);
86 DECLARE_ALLOCATOR(expression);
87 DECLARE_ALLOCATOR(statement);
88 DECLARE_ALLOCATOR(string);
89 DECLARE_ALLOCATOR(scope);
90 __DECLARE_ALLOCATOR(void, bytes);
91 DECLARE_ALLOCATOR(basic_block);
92 DECLARE_ALLOCATOR(entrypoint);
93 DECLARE_ALLOCATOR(instruction);
94 DECLARE_ALLOCATOR(multijmp);
95 DECLARE_ALLOCATOR(pseudo);
96 
97 #endif
98