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