xref: /illumos-gate/usr/src/tools/smatch/src/allocate.h (revision c85f09cc)
11f5207b7SJohn Levon #ifndef ALLOCATE_H
21f5207b7SJohn Levon #define ALLOCATE_H
31f5207b7SJohn Levon 
4*c85f09ccSJohn Levon #include "compat.h"
5*c85f09ccSJohn Levon 
61f5207b7SJohn Levon struct allocation_blob {
71f5207b7SJohn Levon 	struct allocation_blob *next;
81f5207b7SJohn Levon 	unsigned int left, offset;
91f5207b7SJohn Levon 	unsigned char data[];
101f5207b7SJohn Levon };
111f5207b7SJohn Levon 
121f5207b7SJohn Levon struct allocator_struct {
131f5207b7SJohn Levon 	const char *name;
141f5207b7SJohn Levon 	struct allocation_blob *blobs;
151f5207b7SJohn Levon 	unsigned int alignment;
161f5207b7SJohn Levon 	unsigned int chunking;
171f5207b7SJohn Levon 	void *freelist;
181f5207b7SJohn Levon 	/* statistics */
191f5207b7SJohn Levon 	unsigned long allocations, total_bytes, useful_bytes;
201f5207b7SJohn Levon };
211f5207b7SJohn Levon 
221f5207b7SJohn Levon struct allocator_stats {
231f5207b7SJohn Levon 	const char *name;
241f5207b7SJohn Levon 	unsigned int allocations;
251f5207b7SJohn Levon 	unsigned long total_bytes, useful_bytes;
261f5207b7SJohn Levon };
271f5207b7SJohn Levon 
281f5207b7SJohn Levon extern void protect_allocations(struct allocator_struct *desc);
291f5207b7SJohn Levon extern void drop_all_allocations(struct allocator_struct *desc);
301f5207b7SJohn Levon extern void *allocate(struct allocator_struct *desc, unsigned int size);
311f5207b7SJohn Levon extern void free_one_entry(struct allocator_struct *desc, void *entry);
321f5207b7SJohn Levon extern void show_allocations(struct allocator_struct *);
331f5207b7SJohn Levon extern void get_allocator_stats(struct allocator_struct *, struct allocator_stats *);
341f5207b7SJohn Levon extern void show_allocation_stats(void);
351f5207b7SJohn Levon 
361f5207b7SJohn Levon #define __DECLARE_ALLOCATOR(type, x)		\
371f5207b7SJohn Levon 	extern type *__alloc_##x(int);		\
381f5207b7SJohn Levon 	extern void __free_##x(type *);		\
391f5207b7SJohn Levon 	extern void show_##x##_alloc(void);	\
401f5207b7SJohn Levon 	extern void get_##x##_stats(struct allocator_stats *);		\
411f5207b7SJohn Levon 	extern void clear_##x##_alloc(void);	\
421f5207b7SJohn Levon 	extern void protect_##x##_alloc(void);
431f5207b7SJohn Levon #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
441f5207b7SJohn Levon 
451f5207b7SJohn Levon #define __DO_ALLOCATOR(type, objsize, objalign, objname, x)	\
461f5207b7SJohn Levon 	static struct allocator_struct x##_allocator = {	\
471f5207b7SJohn Levon 		.name = objname,				\
481f5207b7SJohn Levon 		.alignment = objalign,				\
491f5207b7SJohn Levon 		.chunking = CHUNK };				\
501f5207b7SJohn Levon 	type *__alloc_##x(int extra)				\
511f5207b7SJohn Levon 	{							\
521f5207b7SJohn Levon 		return allocate(&x##_allocator, objsize+extra);	\
531f5207b7SJohn Levon 	}							\
541f5207b7SJohn Levon 	void __free_##x(type *entry)				\
551f5207b7SJohn Levon 	{							\
561f5207b7SJohn Levon 		free_one_entry(&x##_allocator, entry);		\
571f5207b7SJohn Levon 	}							\
581f5207b7SJohn Levon 	void show_##x##_alloc(void)				\
591f5207b7SJohn Levon 	{							\
601f5207b7SJohn Levon 		show_allocations(&x##_allocator);		\
611f5207b7SJohn Levon 	}							\
621f5207b7SJohn Levon 	void get_##x##_stats(struct allocator_stats *s)		\
631f5207b7SJohn Levon 	{							\
641f5207b7SJohn Levon 		get_allocator_stats(&x##_allocator, s);		\
651f5207b7SJohn Levon 	}							\
661f5207b7SJohn Levon 	void clear_##x##_alloc(void)				\
671f5207b7SJohn Levon 	{							\
681f5207b7SJohn Levon 		drop_all_allocations(&x##_allocator);		\
691f5207b7SJohn Levon 	}							\
701f5207b7SJohn Levon 	void protect_##x##_alloc(void)				\
711f5207b7SJohn Levon 	{							\
721f5207b7SJohn Levon 		protect_allocations(&x##_allocator);		\
731f5207b7SJohn Levon 	}
741f5207b7SJohn Levon 
751f5207b7SJohn Levon #define __ALLOCATOR(t, n, x) 					\
761f5207b7SJohn Levon 	__DO_ALLOCATOR(t, sizeof(t), __alignof__(t), n, x)
771f5207b7SJohn Levon 
781f5207b7SJohn Levon #define ALLOCATOR(x, n) __ALLOCATOR(struct x, n, x)
791f5207b7SJohn Levon 
801f5207b7SJohn Levon DECLARE_ALLOCATOR(ident);
811f5207b7SJohn Levon DECLARE_ALLOCATOR(token);
821f5207b7SJohn Levon DECLARE_ALLOCATOR(context);
831f5207b7SJohn Levon DECLARE_ALLOCATOR(symbol);
841f5207b7SJohn Levon DECLARE_ALLOCATOR(expression);
851f5207b7SJohn Levon DECLARE_ALLOCATOR(statement);
861f5207b7SJohn Levon DECLARE_ALLOCATOR(string);
871f5207b7SJohn Levon DECLARE_ALLOCATOR(scope);
881f5207b7SJohn Levon __DECLARE_ALLOCATOR(void, bytes);
891f5207b7SJohn Levon DECLARE_ALLOCATOR(basic_block);
901f5207b7SJohn Levon DECLARE_ALLOCATOR(entrypoint);
911f5207b7SJohn Levon DECLARE_ALLOCATOR(instruction);
921f5207b7SJohn Levon DECLARE_ALLOCATOR(multijmp);
931f5207b7SJohn Levon DECLARE_ALLOCATOR(pseudo);
941f5207b7SJohn Levon DECLARE_ALLOCATOR(attribute);
951f5207b7SJohn Levon 
961f5207b7SJohn Levon #endif
97