heap.h

00001 /*
00002  * Copyright (C) 2001,2002,2003 Philippe Gerum <rpm@xenomai.org>.
00003  *
00004  * Xenomai is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * Xenomai is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with Xenomai; if not, write to the Free Software Foundation,
00016  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  * As a special exception, the RTAI project gives permission
00019  * for additional uses of the text contained in its release of
00020  * Xenomai.
00021  *
00022  * The exception is that, if you link the Xenomai libraries with other
00023  * files to produce an executable, this does not by itself cause the
00024  * resulting executable to be covered by the GNU General Public License.
00025  * Your use of that executable is in no way restricted on account of
00026  * linking the Xenomai libraries code into it.
00027  *
00028  * This exception does not however invalidate any other reasons why
00029  * the executable file might be covered by the GNU General Public
00030  * License.
00031  *
00032  * This exception applies only to the code released by the
00033  * RTAI project under the name Xenomai.  If you copy code from other
00034  * RTAI project releases into a copy of Xenomai, as the General Public
00035  * License permits, the exception does not apply to the code that you
00036  * add in this way.  To avoid misleading anyone as to the status of
00037  * such modified files, you must delete this exception notice from
00038  * them.
00039  *
00040  * If you write modifications of your own for Xenomai, it is your
00041  * choice whether to permit this exception to apply to your
00042  * modifications. If you do not wish that, delete this exception
00043  * notice.
00044  */
00045 
00046 #ifndef _RTAI_NUCLEUS_HEAP_H
00047 #define _RTAI_NUCLEUS_HEAP_H
00048 
00049 #include <nucleus/queue.h>
00050 
00051 /*
00052  * CONSTRAINTS:
00053  *
00054  * Minimum page size is 2 ** XNHEAP_MINLOG2 (must be large enough to
00055  * hold a pointer).
00056  *
00057  * Maximum page size is 2 ** XNHEAP_MAXLOG2.
00058  *
00059  * Minimum block size equals the minimum page size.
00060  *
00061  * Requested block size smaller than the minimum block size is
00062  * rounded to the minimum block size.
00063  *
00064  * Requested block size larger than 2 times the page size is rounded
00065  * to the next page boundary and obtained from the free page
00066  * list. So we need a bucket for each power of two between
00067  * XNHEAP_MINLOG2 and XNHEAP_MAXLOG2 inclusive, plus one to honor
00068  * requests ranging from the maximum page size to twice this size.
00069  */
00070 
00071 #if defined(__KERNEL__) || defined(__RTAI_UVM__) || defined(__RTAI_SIM__)
00072 
00073 #define XNHEAP_MINLOG2    3
00074 #define XNHEAP_MAXLOG2    22
00075 #define XNHEAP_MINALLOCSZ (1 << XNHEAP_MINLOG2)
00076 #define XNHEAP_MINALIGNSZ (1 << (XNHEAP_MINLOG2 + 1))
00077 #define XNHEAP_NBUCKETS   (XNHEAP_MAXLOG2 - XNHEAP_MINLOG2 + 2)
00078 #define XNHEAP_MAXEXTSZ   (1 << 24) /* i.e. 16Mb */
00079 
00080 #define XNHEAP_PFREE   0
00081 #define XNHEAP_PCONT   1
00082 #define XNHEAP_PLIST   2
00083 
00084 typedef struct xnextent {
00085 
00086     xnholder_t link;
00087 
00088 #define link2extent(laddr) \
00089 ((xnextent_t *)(((char *)laddr) - (int)(&((xnextent_t *)0)->link)))
00090 
00091     caddr_t membase,    /* Base address of the page array */
00092             memlim,     /* Memory limit of page array */
00093             freelist;   /* Head of the free page list */
00094 
00095     u_char pagemap[1];  /* Beginning of page map */
00096 
00097 } xnextent_t;
00098 
00099 typedef struct xnheap {
00100 
00101 #ifdef CONFIG_SMP
00102     xnlock_t lock;
00103 #endif /* CONFIG_SMP */
00104 
00105     xnholder_t link;
00106 
00107 #define link2heap(laddr) \
00108 ((xnheap_t *)(((char *)laddr) - (int)(&((xnheap_t *)0)->link)))
00109 
00110     u_long extentsize,
00111            pagesize,
00112            pageshift,
00113            hdrsize,
00114            npages,      /* Number of pages per extent */
00115            ubytes,
00116            maxcont;
00117 
00118     xnqueue_t extents;
00119 
00120     caddr_t buckets[XNHEAP_NBUCKETS];
00121 
00122     xnarch_heapcb_t archdep;
00123 
00124     XNARCH_DECL_DISPLAY_CONTEXT();
00125 
00126 } xnheap_t;
00127 
00128 extern xnheap_t kheap;
00129 
00130 #define xnheap_size(heap)        ((heap)->extentsize)
00131 #define xnheap_page_size(heap)   ((heap)->pagesize)
00132 #define xnheap_page_count(heap)  ((heap)->npages)
00133 #define xnheap_used_mem(heap)    ((heap)->ubytes)
00134 #define xnheap_overhead(hsize,psize) \
00135 ((sizeof(xnextent_t) + (((hsize) - sizeof(xnextent_t)) / (psize)) + \
00136  XNHEAP_MINALIGNSZ - 1) & ~(XNHEAP_MINALIGNSZ - 1))
00137 
00138 #define xnmalloc(size)  xnheap_alloc(&kheap,size)
00139 #define xnfree(ptr)     xnheap_free(&kheap,ptr)
00140 
00141 #ifdef __cplusplus
00142 extern "C" {
00143 #endif
00144 
00145 /* Private interface. */
00146 
00147 #ifdef __KERNEL__
00148 
00149 #define XNHEAP_DEV_MINOR 254
00150 
00151 int xnheap_mount(void);
00152 
00153 void xnheap_umount(void);
00154 
00155 int xnheap_init_shared(xnheap_t *heap,
00156                        u_long heapsize,
00157                        int memflags);
00158 
00159 int xnheap_destroy_shared(xnheap_t *heap);
00160 
00161 #define xnheap_shared_offset(heap,ptr) \
00162 (((caddr_t)(ptr)) - ((caddr_t)(heap)->archdep.shmbase))
00163 
00164 #define xnheap_shared_address(heap,off) \
00165 (((caddr_t)(heap)->archdep.shmbase) + (off))
00166 
00167 #endif /* __KERNEL__ */
00168 
00169 /* Public interface. */
00170 
00171 int xnheap_init(xnheap_t *heap,
00172                 void *heapaddr,
00173                 u_long heapsize,
00174                 u_long pagesize);
00175 
00176 int xnheap_destroy(xnheap_t *heap,
00177                    void (*flushfn)(xnheap_t *heap,
00178                                    void *extaddr,
00179                                    u_long extsize,
00180                                    void *cookie),
00181                    void *cookie);
00182 
00183 int xnheap_extend(xnheap_t *heap,
00184                   void *extaddr,
00185                   u_long extsize);
00186 
00187 void *xnheap_alloc(xnheap_t *heap,
00188                    u_long size);
00189 
00190 int xnheap_free(xnheap_t *heap,
00191                 void *block);
00192 
00193 #ifdef __cplusplus
00194 }
00195 #endif
00196 
00197 #endif /* __KERNEL__ || __RTAI_UVM__ || __RTAI_SIM__ */
00198 
00199 #define XNHEAP_DEV_NAME  "/dev/rtheap"
00200 
00201 #endif /* !_RTAI_NUCLEUS_HEAP_H */

Generated on Mon Dec 13 09:49:49 2004 for RTAI API by  doxygen 1.3.9.1