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);
142 #elif defined(CONFIG_XENO_HEAPMEM) 145 #include <boilerplate/heapmem.h> 147 extern struct heap_memory heapmem_main;
150 void 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));
158 int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem)
160 return heapmem_extend((
struct heap_memory *)hobj->pool,
165 void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size)
167 return heapmem_alloc((
struct heap_memory *)hobj->pool, size);
171 void pvheapobj_free(
struct heapobj *hobj,
void *ptr)
173 heapmem_free((
struct heap_memory *)hobj->pool, ptr);
177 size_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;
184 size_t pvheapobj_inquire(
struct heapobj *hobj)
186 return heapmem_used_size((
struct heap_memory *)hobj->pool);
189 static inline void *pvmalloc(
size_t size)
191 return heapmem_alloc(&heapmem_main, size);
194 static inline void pvfree(
void *ptr)
196 heapmem_free(&heapmem_main, ptr);
199 static inline char *pvstrdup(
const char *ptr)
203 str = (
char *)pvmalloc(strlen(ptr) + 1);
207 return strcpy(str, ptr);
214 static inline void *pvmalloc(
size_t size)
222 return __STD(malloc(size));
225 static inline void pvfree(
void *ptr)
230 static inline char *pvstrdup(
const char *ptr)
235 void pvheapobj_destroy(
struct heapobj *hobj);
237 int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem);
239 void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size);
241 void pvheapobj_free(
struct heapobj *hobj,
void *ptr);
243 size_t pvheapobj_inquire(
struct heapobj *hobj);
245 size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr);
249 #ifdef CONFIG_XENO_PSHARED 251 extern void *__main_heap;
253 extern struct hash_table *__main_catalog;
254 #define main_catalog (*((struct hash_table *)__main_catalog)) 256 extern struct sysgroup *__main_sysgroup;
258 struct sysgroup_memspec {
263 static inline void *mainheap_ptr(memoff_t off)
265 return off ? (
void *)__memptr(__main_heap, off) : NULL;
268 static 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)) 328 static inline void sysgroup_lock(
void)
330 read_lock_nocancel(&__main_sysgroup->lock);
333 static 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) 344 int heapobj_pkg_init_shared(
void);
346 int heapobj_init(
struct heapobj *hobj,
const char *name,
349 static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
350 size_t size,
void *unused)
353 return heapobj_init(hobj, name, size);
356 int heapobj_init_array(
struct heapobj *hobj,
const char *name,
357 size_t size,
int elems);
359 void heapobj_destroy(
struct heapobj *hobj);
361 int heapobj_extend(
struct heapobj *hobj,
362 size_t size,
void *mem);
364 void *heapobj_alloc(
struct heapobj *hobj,
367 void heapobj_free(
struct heapobj *hobj,
370 size_t heapobj_validate(
struct heapobj *hobj,
373 size_t heapobj_inquire(
struct heapobj *hobj);
375 size_t heapobj_get_size(
struct heapobj *hobj);
377 int heapobj_bind_session(
const char *session);
379 void heapobj_unbind_session(
void);
381 int heapobj_unlink_session(
const char *session);
383 void *xnmalloc(
size_t size);
385 void xnfree(
void *ptr);
387 char *xnstrdup(
const char *ptr);
391 struct sysgroup_memspec {
398 static 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) 433 static inline int heapobj_pkg_init_shared(
void)
438 static 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);
444 static inline int heapobj_init(
struct heapobj *hobj,
const char *name,
447 return __heapobj_init_private(hobj, name, size, NULL);
450 static 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);
456 static inline void heapobj_destroy(
struct heapobj *hobj)
458 pvheapobj_destroy(hobj);
461 static inline int heapobj_extend(
struct heapobj *hobj,
462 size_t size,
void *mem)
464 return pvheapobj_extend(hobj, size, mem);
467 static inline void *heapobj_alloc(
struct heapobj *hobj,
470 return pvheapobj_alloc(hobj, size);
473 static inline void heapobj_free(
struct heapobj *hobj,
476 pvheapobj_free(hobj, ptr);
479 static inline size_t heapobj_validate(
struct heapobj *hobj,
482 return pvheapobj_validate(hobj, ptr);
485 static inline size_t heapobj_inquire(
struct heapobj *hobj)
487 return pvheapobj_inquire(hobj);
490 static inline int heapobj_bind_session(
const char *session)
495 static inline int heapobj_unlink_session(
const char *session)
500 static inline void heapobj_unbind_session(
void) { }
502 static inline void *xnmalloc(
size_t size)
504 return pvmalloc(size);
507 static inline void xnfree(
void *ptr)
512 static inline char *xnstrdup(
const char *ptr)
514 return pvstrdup(ptr);
519 static inline const char *heapobj_name(
struct heapobj *hobj)
524 static inline size_t heapobj_size(
struct heapobj *hobj)