Home | History | Annotate | Line # | Download | only in aml
aml_memman.h revision 1.2.30.1
      1  1.2.30.1  pgoyette /*	$NetBSD: aml_memman.h,v 1.2.30.1 2016/11/04 14:49:26 pgoyette Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*-
      4       1.1  christos  * Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki (at) FreeBSD.org>
      5       1.1  christos  * All rights reserved.
      6       1.1  christos  *
      7       1.1  christos  * Redistribution and use in source and binary forms, with or without
      8       1.1  christos  * modification, are permitted provided that the following conditions
      9       1.1  christos  * are met:
     10       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11       1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12       1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  christos  *    documentation and/or other materials provided with the distribution.
     15       1.1  christos  *
     16       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17       1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20       1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.1  christos  * SUCH DAMAGE.
     27       1.1  christos  *
     28       1.1  christos  *	Id: aml_memman.h,v 1.9 2000/08/09 14:47:43 iwasaki Exp
     29       1.1  christos  *	$FreeBSD: src/usr.sbin/acpi/amldb/aml/aml_memman.h,v 1.1 2000/08/24 09:33:08 takawata Exp $
     30       1.1  christos  */
     31       1.1  christos 
     32       1.1  christos #ifndef _MEMMAN_H_
     33       1.1  christos #define _MEMMAN_H_
     34       1.1  christos 
     35       1.1  christos /*
     36       1.1  christos  * Generic Memory Management
     37       1.1  christos  */
     38       1.1  christos 
     39       1.1  christos #include <sys/param.h>
     40       1.1  christos #include <sys/queue.h>
     41       1.1  christos 
     42       1.1  christos /* memory block */
     43       1.1  christos struct	memman_block {
     44       1.1  christos 	LIST_ENTRY(memman_block) links;
     45       1.1  christos 	void		*block;
     46       1.1  christos 	unsigned	static_mem;	/* static memory or not */
     47       1.1  christos 	unsigned int	allocated;	/* number of allocated chunks */
     48       1.1  christos 	unsigned int	available;	/* number of available chunks */
     49       1.1  christos 	unsigned int	allocated_mem;	/* block + misc (in bytes) */
     50       1.1  christos 
     51  1.2.30.1  pgoyette };
     52       1.1  christos 
     53       1.1  christos LIST_HEAD(memman_block_list, memman_block);
     54       1.1  christos 
     55       1.1  christos /* memory node in block */
     56       1.1  christos struct	memman_node {
     57       1.1  christos 	LIST_ENTRY(memman_node)	links;
     58       1.1  christos 	void	*node;
     59       1.1  christos 	struct	memman_block *memblock;
     60  1.2.30.1  pgoyette };
     61       1.1  christos 
     62       1.1  christos LIST_HEAD(memman_node_list, memman_node);
     63       1.1  christos 
     64       1.1  christos /* memory type id */
     65       1.1  christos extern unsigned int	memid_unkown;
     66       1.1  christos 
     67       1.1  christos /* memory block manager */
     68       1.1  christos struct	memman_blockman {
     69       1.1  christos 	unsigned int	size;		/* size of chunk */
     70       1.1  christos 	unsigned	int available;	/* total # of available chunks */
     71       1.1  christos 	void		*initial_block;	/* initial memory storage */
     72       1.1  christos 	unsigned	initialized;	/* initialized or not */
     73       1.1  christos 
     74       1.1  christos 	struct	memman_block_list block_list;
     75       1.1  christos 	struct	memman_node_list free_node_list;
     76       1.1  christos 	struct	memman_node_list occupied_node_list;
     77       1.1  christos };
     78       1.1  christos 
     79       1.1  christos /* memory size histogram */
     80       1.1  christos #define MEMMAN_HISTOGRAM_SIZE	20
     81       1.1  christos struct	memman_histogram {
     82       1.2     lukem 	size_t	mem_size;
     83       1.1  christos 	int	count;
     84       1.1  christos };
     85       1.1  christos 
     86       1.1  christos /* flex size memory allocation info */
     87       1.1  christos struct	memman_flexmem_info {
     88       1.1  christos 	LIST_ENTRY(memman_flexmem_info) links;
     89       1.1  christos 	void	*addr;
     90       1.1  christos 	size_t	mem_size;
     91  1.2.30.1  pgoyette };
     92       1.1  christos 
     93       1.1  christos LIST_HEAD(memman_flexmem_info_list, memman_flexmem_info);
     94       1.1  christos 
     95       1.1  christos /* memory manager */
     96       1.1  christos struct	memman {
     97       1.1  christos 	struct	memman_blockman	*blockman;
     98       1.1  christos 	unsigned int	max_memid;	/* max number of valid memid */
     99       1.1  christos 
    100       1.1  christos 	/* fixed size memory blocks */
    101       1.1  christos 	unsigned int	alloc_called;	/* memman_alloc() calling */
    102       1.1  christos 	unsigned int	free_called;	/* memman_free() calling */
    103       1.1  christos 	unsigned int	salloc_called;	/* malloc() calling */
    104       1.1  christos 	unsigned int	sfree_called;	/* free() calling */
    105       1.1  christos 	size_t		required_mem;	/* total required memory (in bytes) */
    106       1.1  christos 	size_t		allocated_mem;	/* total malloc()ed memory */
    107       1.1  christos 	size_t		reclaimed_mem;	/* total free()ed memory */
    108       1.1  christos 	/* flex size memory blocks */
    109       1.1  christos 	unsigned int	flex_alloc_called; /* memman_alloc_flexsize() calling */
    110       1.1  christos 	unsigned int	flex_free_called;  /* memman_free_flexsize() calling */
    111       1.1  christos 	unsigned int	flex_salloc_called;/* malloc() calling */
    112       1.1  christos 	unsigned int	flex_sfree_called; /* free() calling */
    113       1.1  christos 	size_t		flex_required_mem; /* total required memory (in bytes) */
    114       1.1  christos 	size_t		flex_allocated_mem;/* total malloc()ed memory */
    115       1.1  christos 	size_t		flex_reclaimed_mem;/* total free()ed memory */
    116       1.1  christos 	size_t		flex_mem_size_min; /* min size of allocated memory */
    117       1.1  christos 	size_t		flex_mem_size_max; /* max size of allocated memory */
    118       1.1  christos 	size_t		flex_peak_mem_usage;/* memory usage at a peak period */
    119       1.1  christos 
    120       1.1  christos 	/* stuff for more detailed statistical information */
    121       1.1  christos 	struct	memman_histogram *flex_mem_histogram;
    122       1.1  christos 	unsigned int	flex_mem_histogram_ptr;
    123       1.1  christos 	int		flex_mem_histogram_initial_tolerance;
    124       1.1  christos 	unsigned	flex_mem_initialized;
    125       1.1  christos 	struct	memman_flexmem_info_list flexmem_info_list;
    126       1.1  christos };
    127       1.1  christos 
    128       1.1  christos #define MEMMAN_BLOCKNODE_SIZE(entries) sizeof(struct memman_block) + \
    129       1.1  christos     sizeof(struct memman_node) * entries
    130       1.1  christos 
    131       1.1  christos #ifndef ROUNDUP_UNIT
    132       1.1  christos #define ROUNDUP_UNIT	4
    133       1.1  christos #endif
    134       1.1  christos 
    135       1.1  christos #if !defined(MEMMAN_INITIAL_SIZE) || MEMMAN_INITIAL_SIZE < 2048
    136       1.1  christos #define MEMMAN_INITIAL_SIZE	2048
    137       1.1  christos #endif
    138       1.1  christos 
    139       1.1  christos #if !defined(MEMMAN_INCR_SIZE) || MEMMAN_INCR_SIZE < 512
    140       1.1  christos #define MEMMAN_INCR_SIZE	512
    141       1.1  christos #endif
    142       1.1  christos 
    143       1.1  christos #define MEMMAN_INITIALSTORAGE_DESC(type, name) \
    144       1.1  christos static struct { \
    145       1.1  christos 	char	blocknodes[MEMMAN_BLOCKNODE_SIZE(MEMMAN_INITIAL_SIZE)]; \
    146       1.1  christos 	type	realblock[MEMMAN_INITIAL_SIZE]; \
    147       1.1  christos } name
    148       1.1  christos 
    149       1.1  christos #define MEMMAN_MEMBLOCK_DESC(siz, initial_storage) \
    150       1.1  christos { \
    151       1.1  christos 	.size = siz, \
    152       1.1  christos 	.available = MEMMAN_INITIAL_SIZE, \
    153       1.1  christos 	.initial_block = &initial_storage, \
    154       1.1  christos }
    155       1.1  christos 
    156       1.1  christos #define MEMMAN_MEMMANAGER_DESC(block_man, maxmemid, histogram, tolerance) \
    157       1.1  christos { \
    158       1.1  christos 	.blockman = block_man, \
    159       1.1  christos 	.max_memid = maxmemid, \
    160       1.1  christos 	.flex_mem_histogram = histogram, \
    161       1.1  christos 	.flex_mem_histogram_initial_tolerance = tolerance, \
    162       1.1  christos }
    163       1.1  christos 
    164       1.1  christos void	*memman_alloc(struct memman *, unsigned int);
    165       1.1  christos void	*memman_alloc_flexsize(struct memman *, size_t);
    166       1.1  christos void	 memman_free(struct memman *, unsigned int, void *);
    167       1.1  christos void	 memman_free_flexsize(struct memman *, void *);
    168       1.1  christos void	 memman_freeall(struct memman *);
    169       1.1  christos void	 memman_statistics(struct memman *);
    170       1.1  christos size_t	 memman_memid2size(struct memman *, unsigned int);
    171       1.1  christos 
    172       1.1  christos #ifdef _KERNEL
    173       1.1  christos #define MEMMAN_SYSMALLOC(size)	malloc(size, M_MEMMAN, M_WAITOK)
    174       1.1  christos #define MEMMAN_SYSFREE(ptr)	free(ptr, M_MEMMAN)
    175       1.1  christos #define MEMMAN_SYSABORT()	/* no abort in kernel */
    176       1.1  christos #else /* !_KERNEL */
    177       1.1  christos #define MEMMAN_SYSMALLOC(size)	malloc(size)
    178       1.1  christos #define MEMMAN_SYSFREE(ptr)	free(ptr)
    179       1.1  christos #define MEMMAN_SYSABORT()	abort()
    180       1.1  christos #endif /* _KERNEL */
    181       1.1  christos 
    182       1.1  christos #endif /* !_MEMMAN_H_ */
    183