Xenomai  3.0.8
heap.h
1 /*
2  * Copyright (C) 2001,2002,2003 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * Xenomai is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * Xenomai is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Xenomai; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17  * 02111-1307, USA.
18  */
19 #ifndef _COBALT_KERNEL_HEAP_H
20 #define _COBALT_KERNEL_HEAP_H
21 
22 #include <linux/string.h>
23 #include <cobalt/kernel/lock.h>
24 #include <cobalt/kernel/list.h>
25 #include <cobalt/uapi/kernel/types.h>
26 #include <cobalt/uapi/kernel/heap.h>
27 
47 #define XNHEAP_PAGESZ PAGE_SIZE
48 #define XNHEAP_MINLOG2 3
49 #define XNHEAP_MAXLOG2 22 /* Holds pagemap.bcount blocks */
50 #define XNHEAP_MINALLOCSZ (1 << XNHEAP_MINLOG2)
51 #define XNHEAP_MINALIGNSZ (1 << 4) /* i.e. 16 bytes */
52 #define XNHEAP_NBUCKETS (XNHEAP_MAXLOG2 - XNHEAP_MINLOG2 + 2)
53 #define XNHEAP_MAXHEAPSZ (1 << 31) /* i.e. 2Gb */
54 
55 #define XNHEAP_PFREE 0
56 #define XNHEAP_PCONT 1
57 #define XNHEAP_PLIST 2
58 
59 struct xnpagemap {
61  u32 type : 8;
63  u32 bcount : 24;
64 };
65 
66 struct xnheap {
68  DECLARE_XNLOCK(lock);
70  caddr_t membase;
72  caddr_t memlim;
74  int npages;
76  caddr_t freelist;
78  struct xnpagemap *pagemap;
80  struct list_head next;
82  struct xnbucket {
83  caddr_t freelist;
84  int fcount;
85  } buckets[XNHEAP_NBUCKETS];
86  char name[XNOBJECT_NAME_LEN];
88  u32 size;
90  u32 used;
91 };
92 
93 extern struct xnheap cobalt_heap;
94 
95 #define xnmalloc(size) xnheap_alloc(&cobalt_heap, size)
96 #define xnfree(ptr) xnheap_free(&cobalt_heap, ptr)
97 
98 static inline u32 xnheap_get_size(const struct xnheap *heap)
99 {
100  return heap->size;
101 }
102 
103 static inline u32 xnheap_get_free(const struct xnheap *heap)
104 {
105  return heap->size - heap->used;
106 }
107 
108 static inline void *xnheap_get_membase(const struct xnheap *heap)
109 {
110  return heap->membase;
111 }
112 
113 static inline u32 xnheap_rounded_size(u32 size)
114 {
115  if (size < 2 * XNHEAP_PAGESZ)
116  return 2 * XNHEAP_PAGESZ;
117 
118  return ALIGN(size, XNHEAP_PAGESZ);
119 }
120 
121 /* Private interface. */
122 
123 #ifdef CONFIG_XENO_OPT_VFILE
124 void xnheap_init_proc(void);
125 void xnheap_cleanup_proc(void);
126 #else /* !CONFIG_XENO_OPT_VFILE */
127 static inline void xnheap_init_proc(void) { }
128 static inline void xnheap_cleanup_proc(void) { }
129 #endif /* !CONFIG_XENO_OPT_VFILE */
130 
131 /* Public interface. */
132 
133 void *xnheap_vmalloc(size_t size);
134 
135 void xnheap_vfree(void *p);
136 
137 int xnheap_init(struct xnheap *heap, void *membase, u32 size);
138 
139 void xnheap_set_name(struct xnheap *heap,
140  const char *name, ...);
141 
142 void xnheap_destroy(struct xnheap *heap);
143 
144 void *xnheap_alloc(struct xnheap *heap, u32 size);
145 
146 void xnheap_free(struct xnheap *heap, void *block);
147 
148 int xnheap_check_block(struct xnheap *heap, void *block);
149 
150 static inline char *xnstrdup(const char *s)
151 {
152  char *p;
153 
154  p = xnmalloc(strlen(s) + 1);
155  if (p == NULL)
156  return NULL;
157 
158  return strcpy(p, s);
159 }
160 
163 #endif /* !_COBALT_KERNEL_HEAP_H */
void xnheap_free(struct xnheap *heap, void *block)
Release a block to a memory heap.
Definition: heap.c:492
int xnheap_init(struct xnheap *heap, void *membase, u32 size)
Initialize a memory heap.
Definition: heap.c:196
void * xnheap_alloc(struct xnheap *heap, u32 size)
Allocate a memory block from a memory heap.
Definition: heap.c:406
void xnheap_destroy(struct xnheap *heap)
Destroys a memory heap.
Definition: heap.c:257
void xnheap_set_name(struct xnheap *heap, const char *name,...)
Set the heap&#39;s name string.
Definition: heap.c:285
log2 bucket list
Definition: heap.h:82