include/nucleus/heap.h

00001 /*
00002  * @note Copyright (C) 2001,2002,2003 Philippe Gerum <rpm@xenomai.org>.
00003  *
00004  * Xenomai is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published
00006  * by the Free Software Foundation; either version 2 of the License,
00007  * or (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
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00017  * 02111-1307, USA.
00018  *
00019  * \ingroup heap
00020  */
00021 
00022 #ifndef _XENO_NUCLEUS_HEAP_H
00023 #define _XENO_NUCLEUS_HEAP_H
00024 
00025 #include <nucleus/queue.h>
00026 
00027 /*
00028  * CONSTRAINTS:
00029  *
00030  * Minimum page size is 2 ** XNHEAP_MINLOG2 (must be large enough to
00031  * hold a pointer).
00032  *
00033  * Maximum page size is 2 ** XNHEAP_MAXLOG2.
00034  *
00035  * Minimum block size equals the minimum page size.
00036  *
00037  * Requested block size smaller than the minimum block size is
00038  * rounded to the minimum block size.
00039  *
00040  * Requested block size larger than 2 times the page size is rounded
00041  * to the next page boundary and obtained from the free page
00042  * list. So we need a bucket for each power of two between
00043  * XNHEAP_MINLOG2 and XNHEAP_MAXLOG2 inclusive, plus one to honor
00044  * requests ranging from the maximum page size to twice this size.
00045  */
00046 
00047 #if defined(__KERNEL__) || defined(__XENO_SIM__)
00048 
00049 #define XNHEAP_MINLOG2    3
00050 #define XNHEAP_MAXLOG2    22    /* Must hold pagemap::bcount objects */
00051 #define XNHEAP_MINALLOCSZ (1 << XNHEAP_MINLOG2)
00052 #define XNHEAP_MINALIGNSZ (1 << 4) /* i.e. 16 bytes */
00053 #define XNHEAP_NBUCKETS   (XNHEAP_MAXLOG2 - XNHEAP_MINLOG2 + 2)
00054 #define XNHEAP_MAXEXTSZ   (1 << 31) /* i.e. 2Gb */
00055 
00056 #define XNHEAP_PFREE   0
00057 #define XNHEAP_PCONT   1
00058 #define XNHEAP_PLIST   2
00059 
00060 typedef struct xnextent {
00061 
00062         xnholder_t link;
00063 
00064 #define link2extent(ln) container_of(ln, xnextent_t, link)
00065 
00066         caddr_t membase,        /* Base address of the page array */
00067                 memlim,         /* Memory limit of page array */
00068                 freelist;       /* Head of the free page list */
00069 
00070         struct xnpagemap {      /* Beginning of page map */
00071                 unsigned int type : 8;    /* PFREE, PCONT, PLIST or log2 */
00072                 unsigned int bcount : 24; /* Number of active blocks. */
00073         } pagemap[1];
00074 
00075 } xnextent_t;
00076 
00077 typedef struct xnheap {
00078 
00079         xnholder_t link;
00080 
00081 #define link2heap(ln)           container_of(ln, xnheap_t, link)
00082 
00083         u_long extentsize,
00084                 pagesize,
00085                 pageshift,
00086                 hdrsize,
00087                 npages,         /* Number of pages per extent */
00088                 ubytes,
00089                 maxcont;
00090 
00091         xnqueue_t extents;
00092 
00093         DECLARE_XNLOCK(lock);
00094 
00095         struct xnbucket {
00096                 caddr_t freelist;
00097                 int fcount;
00098         } buckets[XNHEAP_NBUCKETS];
00099 
00100         xnholder_t *idleq;
00101 
00102         xnarch_heapcb_t archdep;
00103 
00104         XNARCH_DECL_DISPLAY_CONTEXT();
00105 
00106 } xnheap_t;
00107 
00108 extern xnheap_t kheap;
00109 
00110 #define xnheap_extentsize(heap)         ((heap)->extentsize)
00111 #define xnheap_page_size(heap)          ((heap)->pagesize)
00112 #define xnheap_page_count(heap)         ((heap)->npages)
00113 #define xnheap_usable_mem(heap)         ((heap)->maxcont * countq(&(heap)->extents))
00114 #define xnheap_used_mem(heap)           ((heap)->ubytes)
00115 #define xnheap_max_contiguous(heap)     ((heap)->maxcont)
00116 
00117 static inline size_t xnheap_align(size_t size, size_t al)
00118 {
00119         /* The alignment value must be a power of 2 */
00120         return ((size+al-1)&(~(al-1)));
00121 }
00122 
00123 static inline size_t xnheap_overhead(size_t hsize, size_t psize)
00124 {
00125         size_t m = psize / sizeof(struct xnpagemap);
00126         size_t q = (size_t)xnarch_llimd(hsize - sizeof(xnextent_t), m, m + 1);
00127         return xnheap_align(hsize - q, XNHEAP_MINALIGNSZ);
00128 }
00129 
00130 #define xnmalloc(size)     xnheap_alloc(&kheap,size)
00131 #define xnfree(ptr)        xnheap_free(&kheap,ptr)
00132 #define xnfreesync()       xnheap_finalize_free(&kheap)
00133 #define xnfreesafe(thread,ptr,ln)               \
00134     do {                                        \
00135     if (xnpod_current_p(thread))                \
00136         xnheap_schedule_free(&kheap,ptr,ln);    \
00137     else                                        \
00138         xnheap_free(&kheap,ptr);                \
00139 } while(0)
00140 
00141 static inline size_t xnheap_rounded_size(size_t hsize, size_t psize)
00142 {
00143         /*
00144          * Account for the minimum heap size (i.e. 2 * page size) plus
00145          * overhead so that the actual heap space is large enough to
00146          * match the requested size. Using a small page size for large
00147          * single-block heaps might reserve a lot of useless page map
00148          * memory, but this should never get pathological anyway,
00149          * since we only consume 4 bytes per page.
00150          */
00151         if (hsize < 2 * psize)
00152                 hsize = 2 * psize;
00153         hsize += xnheap_overhead(hsize, psize);
00154         return xnheap_align(hsize, psize);
00155 }
00156 
00157 #ifdef __cplusplus
00158 extern "C" {
00159 #endif
00160 
00161 /* Private interface. */
00162 
00163 #ifdef __KERNEL__
00164 
00165 #define XNHEAP_DEV_MINOR 254
00166 
00167 int xnheap_mount(void);
00168 
00169 void xnheap_umount(void);
00170 
00171 int xnheap_init_mapped(xnheap_t *heap,
00172                        u_long heapsize,
00173                        int memflags);
00174 
00175 int xnheap_destroy_mapped(xnheap_t *heap);
00176 
00177 #define xnheap_mapped_offset(heap,ptr) \
00178 (((caddr_t)(ptr)) - ((caddr_t)(heap)->archdep.heapbase))
00179 
00180 #define xnheap_mapped_address(heap,off) \
00181 (((caddr_t)(heap)->archdep.heapbase) + (off))
00182 
00183 #define xnheap_mapped_p(heap) \
00184 ((heap)->archdep.heapbase != NULL)
00185 
00186 #endif /* __KERNEL__ */
00187 
00188 /* Public interface. */
00189 
00190 int xnheap_init(xnheap_t *heap,
00191                 void *heapaddr,
00192                 u_long heapsize,
00193                 u_long pagesize);
00194 
00195 int xnheap_destroy(xnheap_t *heap,
00196                    void (*flushfn)(xnheap_t *heap,
00197                                    void *extaddr,
00198                                    u_long extsize,
00199                                    void *cookie),
00200                    void *cookie);
00201 
00202 int xnheap_extend(xnheap_t *heap,
00203                   void *extaddr,
00204                   u_long extsize);
00205 
00206 void *xnheap_alloc(xnheap_t *heap,
00207                    u_long size);
00208 
00209 int xnheap_test_and_free(xnheap_t *heap,
00210                          void *block,
00211                          int (*ckfn)(void *block));
00212 
00213 int xnheap_free(xnheap_t *heap,
00214                 void *block);
00215 
00216 void xnheap_schedule_free(xnheap_t *heap,
00217                           void *block,
00218                           xnholder_t *link);
00219 
00220 void xnheap_finalize_free_inner(xnheap_t *heap);
00221 
00222 static inline void xnheap_finalize_free(xnheap_t *heap)
00223 {
00224     if (heap->idleq)
00225         xnheap_finalize_free_inner(heap);
00226 }
00227 
00228 int xnheap_check_block(xnheap_t *heap,
00229                        void *block);
00230 
00231 #ifdef __cplusplus
00232 }
00233 #endif
00234 
00235 #endif /* __KERNEL__ || __XENO_SIM__ */
00236 
00237 #define XNHEAP_DEV_NAME  "/dev/rtheap"
00238 
00239 #endif /* !_XENO_NUCLEUS_HEAP_H */

Generated on Mon Feb 11 12:33:52 2008 for Xenomai API by  doxygen 1.5.3