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