heap.h

00001 /*
00002  * Copyright (C) 2001,2002,2003 Philippe Gerum <rpm@xenomai.org>.
00003  *
00004  * RTAI/fusion 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  * RTAI/fusion 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 RTAI/fusion; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00017  * 02111-1307, USA.
00018  */
00019 
00020 #ifndef _RTAI_NUCLEUS_HEAP_H
00021 #define _RTAI_NUCLEUS_HEAP_H
00022 
00023 #include <nucleus/queue.h>
00024 
00025 /*
00026  * CONSTRAINTS:
00027  *
00028  * Minimum page size is 2 ** XNHEAP_MINLOG2 (must be large enough to
00029  * hold a pointer).
00030  *
00031  * Maximum page size is 2 ** XNHEAP_MAXLOG2.
00032  *
00033  * Minimum block size equals the minimum page size.
00034  *
00035  * Requested block size smaller than the minimum block size is
00036  * rounded to the minimum block size.
00037  *
00038  * Requested block size larger than 2 times the page size is rounded
00039  * to the next page boundary and obtained from the free page
00040  * list. So we need a bucket for each power of two between
00041  * XNHEAP_MINLOG2 and XNHEAP_MAXLOG2 inclusive, plus one to honor
00042  * requests ranging from the maximum page size to twice this size.
00043  */
00044 
00045 #if defined(__KERNEL__) || defined(__RTAI_UVM__) || defined(__RTAI_SIM__)
00046 
00047 #define XNHEAP_MINLOG2    3
00048 #define XNHEAP_MAXLOG2    22
00049 #define XNHEAP_MINALLOCSZ (1 << XNHEAP_MINLOG2)
00050 #define XNHEAP_MINALIGNSZ (1 << 4) /* i.e. 16 bytes */
00051 #define XNHEAP_NBUCKETS   (XNHEAP_MAXLOG2 - XNHEAP_MINLOG2 + 2)
00052 #define XNHEAP_MAXEXTSZ   (1 << 24) /* i.e. 16Mb */
00053 
00054 #define XNHEAP_PFREE   0
00055 #define XNHEAP_PCONT   1
00056 #define XNHEAP_PLIST   2
00057 
00058 typedef struct xnextent {
00059 
00060     xnholder_t link;
00061 
00062 #define link2extent(laddr) \
00063 ((xnextent_t *)(((char *)laddr) - (int)(&((xnextent_t *)0)->link)))
00064 
00065     caddr_t membase,    /* Base address of the page array */
00066             memlim,     /* Memory limit of page array */
00067             freelist;   /* Head of the free page list */
00068 
00069     u_char pagemap[1];  /* Beginning of page map */
00070 
00071 } xnextent_t;
00072 
00073 typedef struct xnheap {
00074 
00075     xnholder_t link;
00076 
00077 #define link2heap(laddr) \
00078 ((xnheap_t *)(((char *)laddr) - (int)(&((xnheap_t *)0)->link)))
00079 
00080     u_long extentsize,
00081            pagesize,
00082            pageshift,
00083            hdrsize,
00084            npages,      /* Number of pages per extent */
00085            ubytes,
00086            maxcont;
00087 
00088     xnqueue_t extents;
00089 
00090 #ifdef CONFIG_SMP
00091     xnlock_t lock;
00092 #endif /* CONFIG_SMP */
00093 
00094     caddr_t buckets[XNHEAP_NBUCKETS];
00095 
00096     xnholder_t *idleq;
00097 
00098     xnarch_heapcb_t archdep;
00099 
00100     XNARCH_DECL_DISPLAY_CONTEXT();
00101 
00102 } xnheap_t;
00103 
00104 extern xnheap_t kheap;
00105 
00106 #define xnheap_size(heap)            ((heap)->extentsize)
00107 #define xnheap_page_size(heap)       ((heap)->pagesize)
00108 #define xnheap_page_count(heap)      ((heap)->npages)
00109 #define xnheap_used_mem(heap)        ((heap)->ubytes)
00110 #define xnheap_max_contiguous(heap)  ((heap)->maxcont)
00111 #define xnheap_overhead(hsize,psize) \
00112 ((sizeof(xnextent_t) + (((hsize) - sizeof(xnextent_t)) / (psize)) + \
00113  XNHEAP_MINALIGNSZ - 1) & ~(XNHEAP_MINALIGNSZ - 1))
00114 
00115 #define xnmalloc(size)     xnheap_alloc(&kheap,size)
00116 #define xnfree(ptr)        xnheap_free(&kheap,ptr)
00117 #define xnfreesync()       xnheap_finalize_free(&kheap)
00118 #define xnfreesafe(thread,ptr,ln) \
00119 do { \
00120     if (xnpod_current_thread() == thread) \
00121         xnheap_schedule_free(&kheap,ptr,ln); \
00122     else \
00123         xnheap_free(&kheap,ptr); \
00124 } while(0)
00125 
00126 #ifdef __cplusplus
00127 extern "C" {
00128 #endif
00129 
00130 /* Private interface. */
00131 
00132 #ifdef __KERNEL__
00133 
00134 #define XNHEAP_DEV_MINOR 254
00135 
00136 int xnheap_mount(void);
00137 
00138 void xnheap_umount(void);
00139 
00140 int xnheap_init_shared(xnheap_t *heap,
00141                        u_long heapsize,
00142                        int memflags);
00143 
00144 int xnheap_destroy_shared(xnheap_t *heap);
00145 
00146 #define xnheap_shared_offset(heap,ptr) \
00147 (((caddr_t)(ptr)) - ((caddr_t)(heap)->archdep.heapbase))
00148 
00149 #define xnheap_shared_address(heap,off) \
00150 (((caddr_t)(heap)->archdep.heapbase) + (off))
00151 
00152 #endif /* __KERNEL__ */
00153 
00154 /* Public interface. */
00155 
00156 int xnheap_init(xnheap_t *heap,
00157                 void *heapaddr,
00158                 u_long heapsize,
00159                 u_long pagesize);
00160 
00161 int xnheap_destroy(xnheap_t *heap,
00162                    void (*flushfn)(xnheap_t *heap,
00163                                    void *extaddr,
00164                                    u_long extsize,
00165                                    void *cookie),
00166                    void *cookie);
00167 
00168 int xnheap_extend(xnheap_t *heap,
00169                   void *extaddr,
00170                   u_long extsize);
00171 
00172 void *xnheap_alloc(xnheap_t *heap,
00173                    u_long size);
00174 
00175 int xnheap_test_and_free(xnheap_t *heap,
00176                          void *block,
00177                          int (*ckfn)(void *block));
00178 
00179 int xnheap_free(xnheap_t *heap,
00180                 void *block);
00181 
00182 void xnheap_schedule_free(xnheap_t *heap,
00183                           void *block,
00184                           xnholder_t *link);
00185 
00186 void xnheap_finalize_free_inner(xnheap_t *heap);
00187 
00188 static inline void xnheap_finalize_free(xnheap_t *heap)
00189 {
00190     if (heap->idleq)
00191         xnheap_finalize_free_inner(heap);
00192 }
00193 
00194 #ifdef __cplusplus
00195 }
00196 #endif
00197 
00198 #endif /* __KERNEL__ || __RTAI_UVM__ || __RTAI_SIM__ */
00199 
00200 #define XNHEAP_DEV_NAME  "/dev/rtheap"
00201 
00202 #endif /* !_RTAI_NUCLEUS_HEAP_H */

Generated on Wed Jun 22 22:54:02 2005 for RTAI Fusion API by  doxygen 1.4.1