22 #ifndef _XENO_NUCLEUS_HEAP_H
23 #define _XENO_NUCLEUS_HEAP_H
25 #include <nucleus/queue.h>
47 #if defined(__KERNEL__) || defined(__XENO_SIM__)
49 #define XNHEAP_PAGE_SIZE 512
50 #define XNHEAP_PAGE_MASK (~(XNHEAP_PAGE_SIZE-1))
51 #define XNHEAP_PAGE_ALIGN(addr) (((addr)+XNHEAP_PAGE_SIZE-1)&XNHEAP_PAGE_MASK)
53 #define XNHEAP_MINLOG2 3
54 #define XNHEAP_MAXLOG2 22
55 #define XNHEAP_MINALLOCSZ (1 << XNHEAP_MINLOG2)
56 #define XNHEAP_MINALIGNSZ (1 << 4)
57 #define XNHEAP_NBUCKETS (XNHEAP_MAXLOG2 - XNHEAP_MINLOG2 + 2)
58 #define XNHEAP_MAXEXTSZ (1 << 31)
60 #define XNHEAP_PFREE 0
61 #define XNHEAP_PCONT 1
62 #define XNHEAP_PLIST 2
64 #define XNHEAP_GFP_NONCACHED (1 << __GFP_BITS_SHIFT)
67 unsigned int type : 8;
68 unsigned int bcount : 24;
71 typedef struct xnextent {
75 #define link2extent(ln) container_of(ln, xnextent_t, link)
81 struct xnpagemap pagemap[1];
85 typedef struct xnheap {
89 #define link2heap(ln) container_of(ln, xnheap_t, link)
101 DECLARE_XNLOCK(lock);
106 } buckets[XNHEAP_NBUCKETS];
108 xnholder_t *idleq[XNARCH_NR_CPUS];
110 xnarch_heapcb_t archdep;
112 XNARCH_DECL_DISPLAY_CONTEXT();
114 xnholder_t stat_link;
116 char label[XNOBJECT_NAME_LEN+16];
120 extern xnheap_t kheap;
122 #if CONFIG_XENO_OPT_SYS_STACKPOOLSZ > 0
123 extern xnheap_t kstacks;
126 #define xnheap_extentsize(heap) ((heap)->extentsize)
127 #define xnheap_page_size(heap) ((heap)->pagesize)
128 #define xnheap_page_count(heap) ((heap)->npages)
129 #define xnheap_usable_mem(heap) ((heap)->maxcont * countq(&(heap)->extents))
130 #define xnheap_used_mem(heap) ((heap)->ubytes)
131 #define xnheap_max_contiguous(heap) ((heap)->maxcont)
133 static inline size_t xnheap_align(
size_t size,
size_t al)
136 return ((size+al-1)&(~(al-1)));
139 static inline size_t xnheap_external_overhead(
size_t hsize,
size_t psize)
141 size_t pages = (hsize + psize - 1) / psize;
142 return xnheap_align(
sizeof(xnextent_t)
143 + pages *
sizeof(
struct xnpagemap), psize);
146 static inline size_t xnheap_internal_overhead(
size_t hsize,
size_t psize)
153 return xnheap_align((
sizeof(xnextent_t) * psize
154 +
sizeof(
struct xnpagemap) * hsize)
155 / (psize +
sizeof(
struct xnpagemap)), psize);
158 #define xnmalloc(size) xnheap_alloc(&kheap,size)
159 #define xnfree(ptr) xnheap_free(&kheap,ptr)
160 #define xnfreesync() xnheap_finalize_free(&kheap)
161 #define xnfreesafe(thread, ptr, ln) \
163 if (xnpod_current_p(thread)) \
164 xnheap_schedule_free(&kheap, ptr, ln); \
166 xnheap_free(&kheap,ptr); \
169 static inline size_t xnheap_rounded_size(
size_t hsize,
size_t psize)
179 if (hsize < 2 * psize)
181 hsize += xnheap_external_overhead(hsize, psize);
182 return xnheap_align(hsize, psize);
193 int xnheap_mount(
void);
195 void xnheap_umount(
void);
197 void xnheap_init_proc(
void);
199 void xnheap_cleanup_proc(
void);
201 int xnheap_init_mapped(xnheap_t *heap,
205 void xnheap_destroy_mapped(xnheap_t *heap,
206 void (*release)(
struct xnheap *heap),
207 void __user *mapaddr);
209 #define xnheap_base_memory(heap) \
210 ((unsigned long)((heap)->archdep.heapbase))
212 #define xnheap_mapped_offset(heap,ptr) \
213 (((caddr_t)(ptr)) - (caddr_t)xnheap_base_memory(heap))
215 #define xnheap_mapped_address(heap,off) \
216 ((caddr_t)xnheap_base_memory(heap) + (off))
218 #define xnheap_mapped_p(heap) \
219 (xnheap_base_memory(heap) != 0)
232 void xnheap_destroy(xnheap_t *heap,
233 void (*flushfn)(xnheap_t *heap,
248 int (*ckfn)(
void *block));
257 void xnheap_finalize_free_inner(xnheap_t *heap,
260 static inline void xnheap_finalize_free(xnheap_t *heap)
262 int cpu = xnarch_current_cpu();
266 xnpod_fatal(
"%s called in unsafe context", __FUNCTION__));
268 if (heap->idleq[cpu])
269 xnheap_finalize_free_inner(heap, cpu);
272 int xnheap_check_block(xnheap_t *heap,
281 #define XNHEAP_DEV_NAME "/dev/rtheap"
282 #define XNHEAP_DEV_MINOR 254
285 #define XNHEAP_PROC_PRIVATE_HEAP 0
286 #define XNHEAP_PROC_SHARED_HEAP 1
287 #define XNHEAP_SYS_HEAP 2
288 #define XNHEAP_SYS_STACKPOOL 3
291 unsigned long handle;
int xnheap_extend(xnheap_t *heap, void *extaddr, u_long extsize)
Extend a memory heap.
Definition: heap.c:966
void * xnheap_alloc(xnheap_t *heap, u_long size)
Allocate a memory block from a memory heap.
Definition: heap.c:570
int xnheap_test_and_free(xnheap_t *heap, void *block, int(*ckfn)(void *block))
Test and release a memory block to a memory heap.
Definition: heap.c:711
void xnheap_schedule_free(xnheap_t *heap, void *block, xnholder_t *link)
Schedule a memory block for release.
Definition: heap.c:1016
int xnheap_free(xnheap_t *heap, void *block)
Release a memory block to a memory heap.
Definition: heap.c:931
void xnheap_set_label(xnheap_t *heap, const char *name,...)
Set the heap's label string.
Definition: heap.c:365
int xnheap_init(xnheap_t *heap, void *heapaddr, u_long heapsize, u_long pagesize)
Initialize a memory heap.
Definition: heap.c:256