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) 144 #include <boilerplate/heapmem.h> 146 extern struct heap_memory heapmem_main;
149 void pvheapobj_destroy(
struct heapobj *hobj)
151 heapmem_destroy((
struct heap_memory *)hobj->pool);
155 int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem)
157 return heapmem_extend((
struct heap_memory *)hobj->pool,
162 void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size)
164 return heapmem_alloc((
struct heap_memory *)hobj->pool, size);
168 void pvheapobj_free(
struct heapobj *hobj,
void *ptr)
170 heapmem_free((
struct heap_memory *)hobj->pool, ptr);
174 size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr)
176 ssize_t size = heapmem_check((
struct heap_memory *)hobj->pool, ptr);
177 return size < 0 ? 0 : size;
181 size_t pvheapobj_inquire(
struct heapobj *hobj)
183 return heapmem_used_size((
struct heap_memory *)hobj->pool);
186 static inline void *pvmalloc(
size_t size)
188 return heapmem_alloc(&heapmem_main, size);
191 static inline void pvfree(
void *ptr)
193 heapmem_free(&heapmem_main, ptr);
196 static inline char *pvstrdup(
const char *ptr)
200 str = (
char *)pvmalloc(strlen(ptr) + 1);
204 return strcpy(str, ptr);
211 static inline void *pvmalloc(
size_t size)
219 return __STD(malloc(size));
222 static inline void pvfree(
void *ptr)
227 static inline char *pvstrdup(
const char *ptr)
232 void pvheapobj_destroy(
struct heapobj *hobj);
234 int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem);
236 void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size);
238 void pvheapobj_free(
struct heapobj *hobj,
void *ptr);
240 size_t pvheapobj_inquire(
struct heapobj *hobj);
242 size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr);
246 #ifdef CONFIG_XENO_PSHARED 248 extern void *__main_heap;
250 extern struct hash_table *__main_catalog;
251 #define main_catalog (*((struct hash_table *)__main_catalog)) 253 extern struct sysgroup *__main_sysgroup;
255 struct sysgroup_memspec {
260 static inline void *mainheap_ptr(memoff_t off)
262 return off ? (
void *)__memptr(__main_heap, off) : NULL;
265 static inline memoff_t mainheap_off(
void *addr)
267 return addr ? (memoff_t)__memoff(__main_heap, addr) : 0;
277 #define mainheap_ref(ptr, type) \ 280 assert(__builtin_types_compatible_p(typeof(type), unsigned long) || \ 281 __builtin_types_compatible_p(typeof(type), uintptr_t)); \ 282 assert(ptr == NULL || __memchk(__main_heap, ptr)); \ 283 handle = (type)mainheap_off(ptr); \ 291 #define mainheap_deref(handle, type) \ 294 assert(__builtin_types_compatible_p(typeof(handle), unsigned long) || \ 295 __builtin_types_compatible_p(typeof(handle), uintptr_t)); \ 296 ptr = (handle & 1) ? (type *)mainheap_ptr(handle & ~1UL) : (type *)handle; \ 301 __sysgroup_add(
struct sysgroup_memspec *obj,
struct listobj *q,
int *countp)
303 write_lock_nocancel(&__main_sysgroup->lock);
305 list_append(&obj->next, q);
306 write_unlock(&__main_sysgroup->lock);
309 #define sysgroup_add(__group, __obj) \ 310 __sysgroup_add(__obj, &(__main_sysgroup->__group ## _list), \ 311 &(__main_sysgroup->__group ## _count)) 314 __sysgroup_remove(
struct sysgroup_memspec *obj,
int *countp)
316 write_lock_nocancel(&__main_sysgroup->lock);
318 list_remove(&obj->next);
319 write_unlock(&__main_sysgroup->lock);
322 #define sysgroup_remove(__group, __obj) \ 323 __sysgroup_remove(__obj, &(__main_sysgroup->__group ## _count)) 325 static inline void sysgroup_lock(
void)
327 read_lock_nocancel(&__main_sysgroup->lock);
330 static inline void sysgroup_unlock(
void)
332 read_unlock(&__main_sysgroup->lock);
335 #define sysgroup_count(__group) \ 336 (__main_sysgroup->__group ## _count) 338 #define for_each_sysgroup(__obj, __tmp, __group) \ 339 list_for_each_entry_safe(__obj, __tmp, &(__main_sysgroup->__group ## _list), next) 341 int heapobj_pkg_init_shared(
void);
343 int heapobj_init(
struct heapobj *hobj,
const char *name,
346 static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
347 size_t size,
void *unused)
350 return heapobj_init(hobj, name, size);
353 int heapobj_init_array(
struct heapobj *hobj,
const char *name,
354 size_t size,
int elems);
356 void heapobj_destroy(
struct heapobj *hobj);
358 int heapobj_extend(
struct heapobj *hobj,
359 size_t size,
void *mem);
361 void *heapobj_alloc(
struct heapobj *hobj,
364 void heapobj_free(
struct heapobj *hobj,
367 size_t heapobj_validate(
struct heapobj *hobj,
370 size_t heapobj_inquire(
struct heapobj *hobj);
372 size_t heapobj_get_size(
struct heapobj *hobj);
374 int heapobj_bind_session(
const char *session);
376 void heapobj_unbind_session(
void);
378 int heapobj_unlink_session(
const char *session);
380 void *xnmalloc(
size_t size);
382 void xnfree(
void *ptr);
384 char *xnstrdup(
const char *ptr);
388 struct sysgroup_memspec {
395 static inline int pshared_check(
void *heap,
void *addr)
401 #define __check_ref_width(__dst, __src) \ 403 assert(sizeof(__dst) >= sizeof(__src)); \ 404 (typeof(__dst))__src; \ 407 #define __check_ref_width(__dst, __src) \ 408 __builtin_choose_expr( \ 409 sizeof(__dst) >= sizeof(__src), (typeof(__dst))__src, \ 413 #define mainheap_ref(ptr, type) \ 416 handle = __check_ref_width(handle, ptr); \ 417 assert(ptr == NULL || __memchk(__main_heap, ptr)); \ 420 #define mainheap_deref(handle, type) \ 423 ptr = __check_ref_width(ptr, handle); \ 427 #define sysgroup_add(__group, __obj) do { } while (0) 428 #define sysgroup_remove(__group, __obj) do { } while (0) 430 static inline int heapobj_pkg_init_shared(
void)
435 static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
436 size_t size,
void *mem)
438 return __heapobj_init_private(hobj, name, size, mem);
441 static inline int heapobj_init(
struct heapobj *hobj,
const char *name,
444 return __heapobj_init_private(hobj, name, size, NULL);
447 static inline int heapobj_init_array(
struct heapobj *hobj,
const char *name,
448 size_t size,
int elems)
450 return heapobj_init_array_private(hobj, name, size, elems);
453 static inline void heapobj_destroy(
struct heapobj *hobj)
455 pvheapobj_destroy(hobj);
458 static inline int heapobj_extend(
struct heapobj *hobj,
459 size_t size,
void *mem)
461 return pvheapobj_extend(hobj, size, mem);
464 static inline void *heapobj_alloc(
struct heapobj *hobj,
467 return pvheapobj_alloc(hobj, size);
470 static inline void heapobj_free(
struct heapobj *hobj,
473 pvheapobj_free(hobj, ptr);
476 static inline size_t heapobj_validate(
struct heapobj *hobj,
479 return pvheapobj_validate(hobj, ptr);
482 static inline size_t heapobj_inquire(
struct heapobj *hobj)
484 return pvheapobj_inquire(hobj);
487 static inline int heapobj_bind_session(
const char *session)
492 static inline int heapobj_unlink_session(
const char *session)
497 static inline void heapobj_unbind_session(
void) { }
499 static inline void *xnmalloc(
size_t size)
501 return pvmalloc(size);
504 static inline void xnfree(
void *ptr)
509 static inline char *xnstrdup(
const char *ptr)
511 return pvstrdup(ptr);
516 static inline const char *heapobj_name(
struct heapobj *hobj)
521 static inline size_t heapobj_size(
struct heapobj *hobj)