rtai-core/include/rtai_malloc.h

00001 /*
00002  * Copyright (C) 2001,2002,2003,2004 Philippe Gerum <rpm@xenomai.org>.
00003  *
00004  * RTAI 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  * RTAI is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00011  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
00012  * License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with RTAI; if not, write to the Free Software Foundation,
00016  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  * This file provides the interface to the RTAI dynamic memory
00019  * allocator based on the algorithm described in "Design of a General
00020  * Purpose Memory Allocator for the 4.3BSD Unix Kernel" by Marshall
00021  * K. McKusick and Michael J. Karels.
00022  */
00023 
00024 #ifndef _RTAI_MALLOC_H
00025 #define _RTAI_MALLOC_H
00026 
00027 #include <rtai_types.h>
00028 
00029 #ifdef __KERNEL__
00030 
00031 #include <linux/kernel.h>
00032 #include <linux/list.h>
00033 
00034 /*
00035  * LIMITS:
00036  *
00037  * Minimum page size is 2 ** RTHEAP_MINLOG2 (must be large enough to
00038  * hold a pointer).
00039  *
00040  * Maximum page size is 2 ** RTHEAP_MAXLOG2.
00041  *
00042  * Minimum block size equals the minimum page size.
00043  *
00044  * Requested block size smaller than the minimum block size is
00045  * rounded to the minimum block size.
00046  *
00047  * Requested block size larger than 2 times the page size is rounded
00048  * to the next page boundary and obtained from the free page
00049  * list. So we need a bucket for each power of two between
00050  * RTHEAP_MINLOG2 and RTHEAP_MAXLOG2 inclusive, plus one to honor
00051  * requests ranging from the maximum page size to twice this size.
00052  */
00053 
00054 #define RTHEAP_MINLOG2    4
00055 #define RTHEAP_MAXLOG2    22
00056 #define RTHEAP_MINALLOCSZ (1 << RTHEAP_MINLOG2)
00057 #define RTHEAP_MINALIGNSZ RTHEAP_MINALLOCSZ
00058 #define RTHEAP_NBUCKETS   (RTHEAP_MAXLOG2 - RTHEAP_MINLOG2 + 2)
00059 #define RTHEAP_MAXEXTSZ   (1 << 24) /* i.e. 16 Mo */
00060 #define RTHEAP_GLOBALSZ   (CONFIG_RTAI_MALLOC_HEAPSZ * 1024)
00061 
00062 #define RTHEAP_PFREE   0
00063 #define RTHEAP_PCONT   1
00064 #define RTHEAP_PLIST   2
00065 
00066 #define KMALLOC_LIMIT     (128 * 1024)
00067 
00068 #define RTHEAP_NOMEM   (-1)
00069 #define RTHEAP_PARAM   (-2)
00070 
00071 typedef struct rtextent {
00072 
00073     struct list_head link;
00074 
00075     caddr_t membase,    /* Base address of the page array */
00076             memlim,     /* Memory limit of page array */
00077             freelist;   /* Head of the free page list */
00078 
00079     u_char pagemap[1];  /* Beginning of page map */
00080 
00081 } rtextent_t;
00082 
00083 /* Creation flag */
00084 #define RTHEAP_EXTENDABLE 0x1
00085 
00086 /* Allocation flags */
00087 #define RTHEAP_EXTEND    0x1
00088 
00089 typedef struct rtheap {
00090 
00091     spinlock_t lock;
00092 
00093     int flags;
00094 
00095     u_long extentsize,
00096            pagesize,
00097            pageshift,
00098            hdrsize,
00099            npages,      /* Number of pages per extent */
00100            ubytes,
00101            maxcont;
00102 
00103     struct list_head extents;
00104 
00105     caddr_t buckets[RTHEAP_NBUCKETS];
00106 
00107 } rtheap_t;
00108 
00109 extern rtheap_t rtai_global_heap;
00110 
00111 #define rtheap_page_size(heap)   ((heap)->pagesize)
00112 #define rtheap_page_count(heap)  ((heap)->npages)
00113 #define rtheap_used_mem(heap)    ((heap)->ubytes)
00114 
00115 #define rt_malloc(sz)  rtheap_alloc(&rtai_global_heap,sz,0)
00116 #define rt_free(p)     rtheap_free(&rtai_global_heap,p)
00117 
00118 #ifdef __cplusplus
00119 extern "C" {
00120 #endif
00121 
00122 int __rtai_heap_init(void);
00123 
00124 void __rtai_heap_exit(void);
00125 
00126 int rtheap_init(rtheap_t *heap,
00127                 void *heapaddr,
00128                 u_long heapsize,
00129                 u_long pagesize);
00130 
00131 void rtheap_destroy(rtheap_t *heap);
00132 
00133 void *rtheap_alloc(rtheap_t *heap,
00134                    u_long size,
00135                    int flags);
00136 
00137 int rtheap_free(rtheap_t *heap,
00138                 void *block);
00139 
00140 #ifdef __cplusplus
00141 }
00142 #endif
00143 
00144 #endif /* __KERNEL__ */
00145 
00146 #endif /* !_RTAI_MALLOC_H */

Generated on Sat Jul 24 19:36:03 2004 for RTAI API by doxygen 1.3.4