xref: /netbsd-src/usr.sbin/acpitools/aml/aml_memman.h (revision 5936bc8d108c43c0c42ea717cb87a1314f82c084)
1*5936bc8dSjoerg /*	$NetBSD: aml_memman.h,v 1.3 2016/10/04 22:22:16 joerg Exp $	*/
253e202c1Schristos 
353e202c1Schristos /*-
453e202c1Schristos  * Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
553e202c1Schristos  * All rights reserved.
653e202c1Schristos  *
753e202c1Schristos  * Redistribution and use in source and binary forms, with or without
853e202c1Schristos  * modification, are permitted provided that the following conditions
953e202c1Schristos  * are met:
1053e202c1Schristos  * 1. Redistributions of source code must retain the above copyright
1153e202c1Schristos  *    notice, this list of conditions and the following disclaimer.
1253e202c1Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1353e202c1Schristos  *    notice, this list of conditions and the following disclaimer in the
1453e202c1Schristos  *    documentation and/or other materials provided with the distribution.
1553e202c1Schristos  *
1653e202c1Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1753e202c1Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1853e202c1Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1953e202c1Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2053e202c1Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2153e202c1Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2253e202c1Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2353e202c1Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2453e202c1Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2553e202c1Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2653e202c1Schristos  * SUCH DAMAGE.
2753e202c1Schristos  *
2853e202c1Schristos  *	Id: aml_memman.h,v 1.9 2000/08/09 14:47:43 iwasaki Exp
2953e202c1Schristos  *	$FreeBSD: src/usr.sbin/acpi/amldb/aml/aml_memman.h,v 1.1 2000/08/24 09:33:08 takawata Exp $
3053e202c1Schristos  */
3153e202c1Schristos 
3253e202c1Schristos #ifndef _MEMMAN_H_
3353e202c1Schristos #define _MEMMAN_H_
3453e202c1Schristos 
3553e202c1Schristos /*
3653e202c1Schristos  * Generic Memory Management
3753e202c1Schristos  */
3853e202c1Schristos 
3953e202c1Schristos #include <sys/param.h>
4053e202c1Schristos #include <sys/queue.h>
4153e202c1Schristos 
4253e202c1Schristos /* memory block */
4353e202c1Schristos struct	memman_block {
4453e202c1Schristos 	LIST_ENTRY(memman_block) links;
4553e202c1Schristos 	void		*block;
4653e202c1Schristos 	unsigned	static_mem;	/* static memory or not */
4753e202c1Schristos 	unsigned int	allocated;	/* number of allocated chunks */
4853e202c1Schristos 	unsigned int	available;	/* number of available chunks */
4953e202c1Schristos 	unsigned int	allocated_mem;	/* block + misc (in bytes) */
5053e202c1Schristos 
51*5936bc8dSjoerg };
5253e202c1Schristos 
5353e202c1Schristos LIST_HEAD(memman_block_list, memman_block);
5453e202c1Schristos 
5553e202c1Schristos /* memory node in block */
5653e202c1Schristos struct	memman_node {
5753e202c1Schristos 	LIST_ENTRY(memman_node)	links;
5853e202c1Schristos 	void	*node;
5953e202c1Schristos 	struct	memman_block *memblock;
60*5936bc8dSjoerg };
6153e202c1Schristos 
6253e202c1Schristos LIST_HEAD(memman_node_list, memman_node);
6353e202c1Schristos 
6453e202c1Schristos /* memory type id */
6553e202c1Schristos extern unsigned int	memid_unkown;
6653e202c1Schristos 
6753e202c1Schristos /* memory block manager */
6853e202c1Schristos struct	memman_blockman {
6953e202c1Schristos 	unsigned int	size;		/* size of chunk */
7053e202c1Schristos 	unsigned	int available;	/* total # of available chunks */
7153e202c1Schristos 	void		*initial_block;	/* initial memory storage */
7253e202c1Schristos 	unsigned	initialized;	/* initialized or not */
7353e202c1Schristos 
7453e202c1Schristos 	struct	memman_block_list block_list;
7553e202c1Schristos 	struct	memman_node_list free_node_list;
7653e202c1Schristos 	struct	memman_node_list occupied_node_list;
7753e202c1Schristos };
7853e202c1Schristos 
7953e202c1Schristos /* memory size histogram */
8053e202c1Schristos #define MEMMAN_HISTOGRAM_SIZE	20
8153e202c1Schristos struct	memman_histogram {
82e7dd2c75Slukem 	size_t	mem_size;
8353e202c1Schristos 	int	count;
8453e202c1Schristos };
8553e202c1Schristos 
8653e202c1Schristos /* flex size memory allocation info */
8753e202c1Schristos struct	memman_flexmem_info {
8853e202c1Schristos 	LIST_ENTRY(memman_flexmem_info) links;
8953e202c1Schristos 	void	*addr;
9053e202c1Schristos 	size_t	mem_size;
91*5936bc8dSjoerg };
9253e202c1Schristos 
9353e202c1Schristos LIST_HEAD(memman_flexmem_info_list, memman_flexmem_info);
9453e202c1Schristos 
9553e202c1Schristos /* memory manager */
9653e202c1Schristos struct	memman {
9753e202c1Schristos 	struct	memman_blockman	*blockman;
9853e202c1Schristos 	unsigned int	max_memid;	/* max number of valid memid */
9953e202c1Schristos 
10053e202c1Schristos 	/* fixed size memory blocks */
10153e202c1Schristos 	unsigned int	alloc_called;	/* memman_alloc() calling */
10253e202c1Schristos 	unsigned int	free_called;	/* memman_free() calling */
10353e202c1Schristos 	unsigned int	salloc_called;	/* malloc() calling */
10453e202c1Schristos 	unsigned int	sfree_called;	/* free() calling */
10553e202c1Schristos 	size_t		required_mem;	/* total required memory (in bytes) */
10653e202c1Schristos 	size_t		allocated_mem;	/* total malloc()ed memory */
10753e202c1Schristos 	size_t		reclaimed_mem;	/* total free()ed memory */
10853e202c1Schristos 	/* flex size memory blocks */
10953e202c1Schristos 	unsigned int	flex_alloc_called; /* memman_alloc_flexsize() calling */
11053e202c1Schristos 	unsigned int	flex_free_called;  /* memman_free_flexsize() calling */
11153e202c1Schristos 	unsigned int	flex_salloc_called;/* malloc() calling */
11253e202c1Schristos 	unsigned int	flex_sfree_called; /* free() calling */
11353e202c1Schristos 	size_t		flex_required_mem; /* total required memory (in bytes) */
11453e202c1Schristos 	size_t		flex_allocated_mem;/* total malloc()ed memory */
11553e202c1Schristos 	size_t		flex_reclaimed_mem;/* total free()ed memory */
11653e202c1Schristos 	size_t		flex_mem_size_min; /* min size of allocated memory */
11753e202c1Schristos 	size_t		flex_mem_size_max; /* max size of allocated memory */
11853e202c1Schristos 	size_t		flex_peak_mem_usage;/* memory usage at a peak period */
11953e202c1Schristos 
12053e202c1Schristos 	/* stuff for more detailed statistical information */
12153e202c1Schristos 	struct	memman_histogram *flex_mem_histogram;
12253e202c1Schristos 	unsigned int	flex_mem_histogram_ptr;
12353e202c1Schristos 	int		flex_mem_histogram_initial_tolerance;
12453e202c1Schristos 	unsigned	flex_mem_initialized;
12553e202c1Schristos 	struct	memman_flexmem_info_list flexmem_info_list;
12653e202c1Schristos };
12753e202c1Schristos 
12853e202c1Schristos #define MEMMAN_BLOCKNODE_SIZE(entries) sizeof(struct memman_block) + \
12953e202c1Schristos     sizeof(struct memman_node) * entries
13053e202c1Schristos 
13153e202c1Schristos #ifndef ROUNDUP_UNIT
13253e202c1Schristos #define ROUNDUP_UNIT	4
13353e202c1Schristos #endif
13453e202c1Schristos 
13553e202c1Schristos #if !defined(MEMMAN_INITIAL_SIZE) || MEMMAN_INITIAL_SIZE < 2048
13653e202c1Schristos #define MEMMAN_INITIAL_SIZE	2048
13753e202c1Schristos #endif
13853e202c1Schristos 
13953e202c1Schristos #if !defined(MEMMAN_INCR_SIZE) || MEMMAN_INCR_SIZE < 512
14053e202c1Schristos #define MEMMAN_INCR_SIZE	512
14153e202c1Schristos #endif
14253e202c1Schristos 
14353e202c1Schristos #define MEMMAN_INITIALSTORAGE_DESC(type, name) \
14453e202c1Schristos static struct { \
14553e202c1Schristos 	char	blocknodes[MEMMAN_BLOCKNODE_SIZE(MEMMAN_INITIAL_SIZE)]; \
14653e202c1Schristos 	type	realblock[MEMMAN_INITIAL_SIZE]; \
14753e202c1Schristos } name
14853e202c1Schristos 
14953e202c1Schristos #define MEMMAN_MEMBLOCK_DESC(siz, initial_storage) \
15053e202c1Schristos { \
15153e202c1Schristos 	.size = siz, \
15253e202c1Schristos 	.available = MEMMAN_INITIAL_SIZE, \
15353e202c1Schristos 	.initial_block = &initial_storage, \
15453e202c1Schristos }
15553e202c1Schristos 
15653e202c1Schristos #define MEMMAN_MEMMANAGER_DESC(block_man, maxmemid, histogram, tolerance) \
15753e202c1Schristos { \
15853e202c1Schristos 	.blockman = block_man, \
15953e202c1Schristos 	.max_memid = maxmemid, \
16053e202c1Schristos 	.flex_mem_histogram = histogram, \
16153e202c1Schristos 	.flex_mem_histogram_initial_tolerance = tolerance, \
16253e202c1Schristos }
16353e202c1Schristos 
16453e202c1Schristos void	*memman_alloc(struct memman *, unsigned int);
16553e202c1Schristos void	*memman_alloc_flexsize(struct memman *, size_t);
16653e202c1Schristos void	 memman_free(struct memman *, unsigned int, void *);
16753e202c1Schristos void	 memman_free_flexsize(struct memman *, void *);
16853e202c1Schristos void	 memman_freeall(struct memman *);
16953e202c1Schristos void	 memman_statistics(struct memman *);
17053e202c1Schristos size_t	 memman_memid2size(struct memman *, unsigned int);
17153e202c1Schristos 
17253e202c1Schristos #ifdef _KERNEL
17353e202c1Schristos #define MEMMAN_SYSMALLOC(size)	malloc(size, M_MEMMAN, M_WAITOK)
17453e202c1Schristos #define MEMMAN_SYSFREE(ptr)	free(ptr, M_MEMMAN)
17553e202c1Schristos #define MEMMAN_SYSABORT()	/* no abort in kernel */
17653e202c1Schristos #else /* !_KERNEL */
17753e202c1Schristos #define MEMMAN_SYSMALLOC(size)	malloc(size)
17853e202c1Schristos #define MEMMAN_SYSFREE(ptr)	free(ptr)
17953e202c1Schristos #define MEMMAN_SYSABORT()	abort()
18053e202c1Schristos #endif /* _KERNEL */
18153e202c1Schristos 
18253e202c1Schristos #endif /* !_MEMMAN_H_ */
183