19#ifndef _COPPERPLATE_HEAPOBJ_H
20#define _COPPERPLATE_HEAPOBJ_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;
59int heapobj_pkg_init_private(
void);
61int __heapobj_init_private(
struct heapobj *hobj,
const char *name,
62 size_t size,
void *mem);
64int heapobj_init_array_private(
struct heapobj *hobj,
const char *name,
65 size_t size,
int elems);
70#ifdef CONFIG_XENO_TLSF
72size_t get_used_size(
void *pool);
73void destroy_memory_pool(
void *pool);
74size_t add_new_area(
void *pool,
size_t size,
void *mem);
75void *malloc_ex(
size_t size,
void *pool);
76void free_ex(
void *pool,
void *ptr);
77void *tlsf_malloc(
size_t size);
78void tlsf_free(
void *ptr);
79size_t malloc_usable_size_ex(
void *ptr,
void *pool);
82void pvheapobj_destroy(
struct heapobj *hobj)
84 destroy_memory_pool(hobj->pool);
88int 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)
98void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size)
100 return malloc_ex(size, hobj->pool);
104void pvheapobj_free(
struct heapobj *hobj,
void *ptr)
106 free_ex(ptr, hobj->pool);
110size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr)
112 return malloc_usable_size_ex(ptr, hobj->pool);
116size_t pvheapobj_inquire(
struct heapobj *hobj)
118 return get_used_size(hobj->pool);
121static inline void *pvmalloc(
size_t size)
123 return tlsf_malloc(size);
126static inline void pvfree(
void *ptr)
131static inline char *pvstrdup(
const char *ptr)
135 str = (
char *)pvmalloc(strlen(ptr) + 1);
139 return strcpy(str, ptr);
142#elif defined(CONFIG_XENO_HEAPMEM)
145#include <boilerplate/heapmem.h>
147extern struct heap_memory heapmem_main;
150void pvheapobj_destroy(
struct heapobj *hobj)
152 heapmem_destroy((
struct heap_memory *)hobj->pool);
153 if (hobj->pool != (
void *)&heapmem_main)
154 __STD(free(hobj->pool));
158int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem)
160 return heapmem_extend((
struct heap_memory *)hobj->pool,
165void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size)
167 return heapmem_alloc((
struct heap_memory *)hobj->pool, size);
171void pvheapobj_free(
struct heapobj *hobj,
void *ptr)
173 heapmem_free((
struct heap_memory *)hobj->pool, ptr);
177size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr)
179 ssize_t size = heapmem_check((
struct heap_memory *)hobj->pool, ptr);
180 return size < 0 ? 0 : (size_t)size;
184size_t pvheapobj_inquire(
struct heapobj *hobj)
186 return heapmem_used_size((
struct heap_memory *)hobj->pool);
189static inline void *pvmalloc(
size_t size)
191 return heapmem_alloc(&heapmem_main, size);
194static inline void pvfree(
void *ptr)
196 heapmem_free(&heapmem_main, ptr);
199static inline char *pvstrdup(
const char *ptr)
203 str = (
char *)pvmalloc(strlen(ptr) + 1);
207 return strcpy(str, ptr);
214static inline void *pvmalloc(
size_t size)
222 return __STD(malloc(size));
225static inline void pvfree(
void *ptr)
230static inline char *pvstrdup(
const char *ptr)
235void pvheapobj_destroy(
struct heapobj *hobj);
237int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem);
239void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size);
241void pvheapobj_free(
struct heapobj *hobj,
void *ptr);
243size_t pvheapobj_inquire(
struct heapobj *hobj);
245size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr);
249#ifdef CONFIG_XENO_PSHARED
251extern void *__main_heap;
253extern struct hash_table *__main_catalog;
254#define main_catalog (*((struct hash_table *)__main_catalog))
256extern struct sysgroup *__main_sysgroup;
258struct sysgroup_memspec {
263static inline void *mainheap_ptr(memoff_t off)
265 return off ? (
void *)__memptr(__main_heap, off) : NULL;
268static inline memoff_t mainheap_off(
void *addr)
270 return addr ? (memoff_t)__memoff(__main_heap, addr) : 0;
280#define mainheap_ref(ptr, type) \
283 assert(__builtin_types_compatible_p(typeof(type), unsigned long) || \
284 __builtin_types_compatible_p(typeof(type), uintptr_t)); \
285 assert(ptr == NULL || __memchk(__main_heap, ptr)); \
286 handle = (type)mainheap_off(ptr); \
294#define mainheap_deref(handle, type) \
297 assert(__builtin_types_compatible_p(typeof(handle), unsigned long) || \
298 __builtin_types_compatible_p(typeof(handle), uintptr_t)); \
299 ptr = (handle & 1) ? (type *)mainheap_ptr(handle & ~1UL) : (type *)handle; \
304__sysgroup_add(
struct sysgroup_memspec *obj,
struct listobj *q,
int *countp)
306 write_lock_nocancel(&__main_sysgroup->lock);
308 list_append(&obj->next, q);
309 write_unlock(&__main_sysgroup->lock);
312#define sysgroup_add(__group, __obj) \
313 __sysgroup_add(__obj, &(__main_sysgroup->__group ## _list), \
314 &(__main_sysgroup->__group ## _count))
317__sysgroup_remove(
struct sysgroup_memspec *obj,
int *countp)
319 write_lock_nocancel(&__main_sysgroup->lock);
321 list_remove(&obj->next);
322 write_unlock(&__main_sysgroup->lock);
325#define sysgroup_remove(__group, __obj) \
326 __sysgroup_remove(__obj, &(__main_sysgroup->__group ## _count))
328static inline void sysgroup_lock(
void)
330 read_lock_nocancel(&__main_sysgroup->lock);
333static inline void sysgroup_unlock(
void)
335 read_unlock(&__main_sysgroup->lock);
338#define sysgroup_count(__group) \
339 (__main_sysgroup->__group ## _count)
341#define for_each_sysgroup(__obj, __tmp, __group) \
342 list_for_each_entry_safe(__obj, __tmp, &(__main_sysgroup->__group ## _list), next)
344int heapobj_pkg_init_shared(
void);
346int heapobj_init(
struct heapobj *hobj,
const char *name,
349static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
350 size_t size,
void *unused)
353 return heapobj_init(hobj, name, size);
356int heapobj_init_array(
struct heapobj *hobj,
const char *name,
357 size_t size,
int elems);
359void heapobj_destroy(
struct heapobj *hobj);
361int heapobj_extend(
struct heapobj *hobj,
362 size_t size,
void *mem);
364void *heapobj_alloc(
struct heapobj *hobj,
367void heapobj_free(
struct heapobj *hobj,
370size_t heapobj_validate(
struct heapobj *hobj,
373size_t heapobj_inquire(
struct heapobj *hobj);
375size_t heapobj_get_size(
struct heapobj *hobj);
377int heapobj_bind_session(
const char *session);
379void heapobj_unbind_session(
void);
381int heapobj_unlink_session(
const char *session);
383void *xnmalloc(
size_t size);
385void xnfree(
void *ptr);
387char *xnstrdup(
const char *ptr);
391struct sysgroup_memspec {
398static inline int pshared_check(
void *heap,
void *addr)
404#define __check_ref_width(__dst, __src) \
406 assert(sizeof(__dst) >= sizeof(__src)); \
407 (typeof(__dst))__src; \
410#define __check_ref_width(__dst, __src) \
411 __builtin_choose_expr( \
412 sizeof(__dst) >= sizeof(__src), (typeof(__dst))__src, \
416#define mainheap_ref(ptr, type) \
419 handle = __check_ref_width(handle, ptr); \
420 assert(ptr == NULL || __memchk(__main_heap, ptr)); \
423#define mainheap_deref(handle, type) \
426 ptr = __check_ref_width(ptr, handle); \
430#define sysgroup_add(__group, __obj) do { } while (0)
431#define sysgroup_remove(__group, __obj) do { } while (0)
433static inline int heapobj_pkg_init_shared(
void)
438static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
439 size_t size,
void *mem)
441 return __heapobj_init_private(hobj, name, size, mem);
444static inline int heapobj_init(
struct heapobj *hobj,
const char *name,
447 return __heapobj_init_private(hobj, name, size, NULL);
450static inline int heapobj_init_array(
struct heapobj *hobj,
const char *name,
451 size_t size,
int elems)
453 return heapobj_init_array_private(hobj, name, size, elems);
456static inline void heapobj_destroy(
struct heapobj *hobj)
458 pvheapobj_destroy(hobj);
461static inline int heapobj_extend(
struct heapobj *hobj,
462 size_t size,
void *mem)
464 return pvheapobj_extend(hobj, size, mem);
467static inline void *heapobj_alloc(
struct heapobj *hobj,
470 return pvheapobj_alloc(hobj, size);
473static inline void heapobj_free(
struct heapobj *hobj,
476 pvheapobj_free(hobj, ptr);
479static inline size_t heapobj_validate(
struct heapobj *hobj,
482 return pvheapobj_validate(hobj, ptr);
485static inline size_t heapobj_inquire(
struct heapobj *hobj)
487 return pvheapobj_inquire(hobj);
490static inline int heapobj_bind_session(
const char *session)
495static inline int heapobj_unlink_session(
const char *session)
500static inline void heapobj_unbind_session(
void) { }
502static inline void *xnmalloc(
size_t size)
504 return pvmalloc(size);
507static inline void xnfree(
void *ptr)
512static inline char *xnstrdup(
const char *ptr)
514 return pvstrdup(ptr);
519static inline const char *heapobj_name(
struct heapobj *hobj)
524static inline size_t heapobj_size(
struct heapobj *hobj)