19 #ifndef _COPPERPLATE_HEAPOBJ_H
20 #define _COPPERPLATE_HEAPOBJ_H
22 #include <sys/types.h>
28 #include <xeno_config.h>
29 #include <boilerplate/wrappers.h>
30 #include <boilerplate/list.h>
31 #include <copperplate/reference.h>
32 #include <boilerplate/lock.h>
33 #include <copperplate/debug.h>
37 dref_type(
void *) pool_ref;
42 #ifdef CONFIG_XENO_PSHARED
49 struct listobj thread_list;
51 struct listobj heap_list;
59 int heapobj_pkg_init_private(
void);
61 int __heapobj_init_private(
struct heapobj *hobj,
const char *name,
62 size_t size,
void *mem);
64 int heapobj_init_array_private(
struct heapobj *hobj,
const char *name,
65 size_t size,
int elems);
70 #ifdef CONFIG_XENO_TLSF
72 size_t get_used_size(
void *pool);
73 void destroy_memory_pool(
void *pool);
74 size_t add_new_area(
void *pool,
size_t size,
void *mem);
75 void *malloc_ex(
size_t size,
void *pool);
76 void free_ex(
void *pool,
void *ptr);
77 void *tlsf_malloc(
size_t size);
78 void tlsf_free(
void *ptr);
79 size_t malloc_usable_size_ex(
void *ptr,
void *pool);
82 void pvheapobj_destroy(
struct heapobj *hobj)
84 destroy_memory_pool(hobj->pool);
88 int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem)
90 hobj->size = add_new_area(hobj->pool, size, mem);
91 if (hobj->size == (
size_t)-1)
98 void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size)
100 return malloc_ex(size, hobj->pool);
104 void pvheapobj_free(
struct heapobj *hobj,
void *ptr)
106 free_ex(ptr, hobj->pool);
110 size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr)
112 return malloc_usable_size_ex(ptr, hobj->pool);
116 size_t pvheapobj_inquire(
struct heapobj *hobj)
118 return get_used_size(hobj->pool);
121 static inline void *pvmalloc(
size_t size)
123 return tlsf_malloc(size);
126 static inline void pvfree(
void *ptr)
131 static inline char *pvstrdup(
const char *ptr)
135 str = (
char *)pvmalloc(strlen(ptr) + 1);
139 return strcpy(str, ptr);
146 static inline void *pvmalloc(
size_t size)
154 return __STD(malloc(size));
157 static inline void pvfree(
void *ptr)
162 static inline char *pvstrdup(
const char *ptr)
167 void pvheapobj_destroy(
struct heapobj *hobj);
169 int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem);
171 void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size);
173 void pvheapobj_free(
struct heapobj *hobj,
void *ptr);
175 size_t pvheapobj_inquire(
struct heapobj *hobj);
177 size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr);
181 #ifdef CONFIG_XENO_PSHARED
183 extern void *__main_heap;
185 extern struct hash_table *__main_catalog;
186 #define main_catalog (*((struct hash_table *)__main_catalog))
188 extern struct sysgroup *__main_sysgroup;
190 struct sysgroup_memspec {
195 struct agent_memspec {
200 static inline void *mainheap_ptr(memoff_t off)
202 return off ? (
void *)__memptr(__main_heap, off) : NULL;
205 static inline memoff_t mainheap_off(
void *addr)
207 return addr ? (memoff_t)__memoff(__main_heap, addr) : 0;
217 #define mainheap_ref(ptr, type) \
220 assert(__builtin_types_compatible_p(typeof(type), unsigned long) || \
221 __builtin_types_compatible_p(typeof(type), uintptr_t)); \
222 assert(ptr == NULL || __memchk(__main_heap, ptr)); \
223 handle = (type)mainheap_off(ptr); \
231 #define mainheap_deref(handle, type) \
234 assert(__builtin_types_compatible_p(typeof(handle), unsigned long) || \
235 __builtin_types_compatible_p(typeof(handle), uintptr_t)); \
236 ptr = (handle & 1) ? (type *)mainheap_ptr(handle & ~1UL) : (type *)handle; \
241 __sysgroup_add(
struct sysgroup_memspec *obj,
struct listobj *q,
int *countp)
243 write_lock_nocancel(&__main_sysgroup->lock);
245 list_append(&obj->next, q);
246 write_unlock(&__main_sysgroup->lock);
249 #define sysgroup_add(__group, __obj) \
250 __sysgroup_add(__obj, &(__main_sysgroup->__group ## _list), \
251 &(__main_sysgroup->__group ## _count))
254 __sysgroup_remove(
struct sysgroup_memspec *obj,
int *countp)
256 write_lock_nocancel(&__main_sysgroup->lock);
258 list_remove(&obj->next);
259 write_unlock(&__main_sysgroup->lock);
262 #define sysgroup_remove(__group, __obj) \
263 __sysgroup_remove(__obj, &(__main_sysgroup->__group ## _count))
265 static inline void sysgroup_lock(
void)
267 read_lock_nocancel(&__main_sysgroup->lock);
270 static inline void sysgroup_unlock(
void)
272 read_unlock(&__main_sysgroup->lock);
275 #define sysgroup_count(__group) \
276 (__main_sysgroup->__group ## _count)
278 #define for_each_sysgroup(__obj, __tmp, __group) \
279 list_for_each_entry_safe(__obj, __tmp, &(__main_sysgroup->__group ## _list), next)
281 int heapobj_pkg_init_shared(
void);
283 int heapobj_init(
struct heapobj *hobj,
const char *name,
286 static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
287 size_t size,
void *unused)
290 return heapobj_init(hobj, name, size);
293 int heapobj_init_array(
struct heapobj *hobj,
const char *name,
294 size_t size,
int elems);
296 void heapobj_destroy(
struct heapobj *hobj);
298 int heapobj_extend(
struct heapobj *hobj,
299 size_t size,
void *mem);
301 void *heapobj_alloc(
struct heapobj *hobj,
304 void heapobj_free(
struct heapobj *hobj,
307 size_t heapobj_validate(
struct heapobj *hobj,
310 size_t heapobj_inquire(
struct heapobj *hobj);
312 int heapobj_bind_session(
const char *session);
314 void heapobj_unbind_session(
void);
316 int heapobj_unlink_session(
const char *session);
318 void *xnmalloc(
size_t size);
320 void xnfree(
void *ptr);
322 char *xnstrdup(
const char *ptr);
326 struct sysgroup_memspec {
329 struct agent_memspec {
336 static inline int pshared_check(
void *heap,
void *addr)
342 #define __check_ref_width(__dst, __src) \
344 assert(sizeof(__dst) >= sizeof(__src)); \
345 (typeof(__dst))__src; \
348 #define __check_ref_width(__dst, __src) \
349 __builtin_choose_expr( \
350 sizeof(__dst) >= sizeof(__src), (typeof(__dst))__src, \
354 #define mainheap_ref(ptr, type) \
357 handle = __check_ref_width(handle, ptr); \
358 assert(ptr == NULL || __memchk(__main_heap, ptr)); \
361 #define mainheap_deref(handle, type) \
364 ptr = __check_ref_width(ptr, handle); \
368 #define sysgroup_add(__group, __obj) do { } while (0)
369 #define sysgroup_remove(__group, __obj) do { } while (0)
371 static inline int heapobj_pkg_init_shared(
void)
376 static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
377 size_t size,
void *mem)
379 return __heapobj_init_private(hobj, name, size, mem);
382 static inline int heapobj_init(
struct heapobj *hobj,
const char *name,
385 return __heapobj_init_private(hobj, name, size, NULL);
388 static inline int heapobj_init_array(
struct heapobj *hobj,
const char *name,
389 size_t size,
int elems)
391 return heapobj_init_array_private(hobj, name, size, elems);
394 static inline void heapobj_destroy(
struct heapobj *hobj)
396 pvheapobj_destroy(hobj);
399 static inline int heapobj_extend(
struct heapobj *hobj,
400 size_t size,
void *mem)
402 return pvheapobj_extend(hobj, size, mem);
405 static inline void *heapobj_alloc(
struct heapobj *hobj,
408 return pvheapobj_alloc(hobj, size);
411 static inline void heapobj_free(
struct heapobj *hobj,
414 pvheapobj_free(hobj, ptr);
417 static inline size_t heapobj_validate(
struct heapobj *hobj,
420 return pvheapobj_validate(hobj, ptr);
423 static inline size_t heapobj_inquire(
struct heapobj *hobj)
425 return pvheapobj_inquire(hobj);
428 static inline int heapobj_bind_session(
const char *session)
433 static inline int heapobj_unlink_session(
const char *session)
438 static inline void heapobj_unbind_session(
void) { }
440 static inline void *xnmalloc(
size_t size)
442 return pvmalloc(size);
445 static inline void xnfree(
void *ptr)
450 static inline char *xnstrdup(
const char *ptr)
452 return pvstrdup(ptr);
457 static inline const char *heapobj_name(
struct heapobj *hobj)
462 static inline size_t heapobj_size(
struct heapobj *hobj)