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 static inline void *mainheap_ptr(memoff_t off)
197 return off ? (
void *)__memptr(__main_heap, off) : NULL;
200 static inline memoff_t mainheap_off(
void *addr)
202 return addr ? (memoff_t)__memoff(__main_heap, addr) : 0;
212 #define mainheap_ref(ptr, type) \ 215 assert(__builtin_types_compatible_p(typeof(type), unsigned long) || \ 216 __builtin_types_compatible_p(typeof(type), uintptr_t)); \ 217 assert(ptr == NULL || __memchk(__main_heap, ptr)); \ 218 handle = (type)mainheap_off(ptr); \ 226 #define mainheap_deref(handle, type) \ 229 assert(__builtin_types_compatible_p(typeof(handle), unsigned long) || \ 230 __builtin_types_compatible_p(typeof(handle), uintptr_t)); \ 231 ptr = (handle & 1) ? (type *)mainheap_ptr(handle & ~1UL) : (type *)handle; \ 236 __sysgroup_add(
struct sysgroup_memspec *obj,
struct listobj *q,
int *countp)
238 write_lock_nocancel(&__main_sysgroup->lock);
240 list_append(&obj->next, q);
241 write_unlock(&__main_sysgroup->lock);
244 #define sysgroup_add(__group, __obj) \ 245 __sysgroup_add(__obj, &(__main_sysgroup->__group ## _list), \ 246 &(__main_sysgroup->__group ## _count)) 249 __sysgroup_remove(
struct sysgroup_memspec *obj,
int *countp)
251 write_lock_nocancel(&__main_sysgroup->lock);
253 list_remove(&obj->next);
254 write_unlock(&__main_sysgroup->lock);
257 #define sysgroup_remove(__group, __obj) \ 258 __sysgroup_remove(__obj, &(__main_sysgroup->__group ## _count)) 260 static inline void sysgroup_lock(
void)
262 read_lock_nocancel(&__main_sysgroup->lock);
265 static inline void sysgroup_unlock(
void)
267 read_unlock(&__main_sysgroup->lock);
270 #define sysgroup_count(__group) \ 271 (__main_sysgroup->__group ## _count) 273 #define for_each_sysgroup(__obj, __tmp, __group) \ 274 list_for_each_entry_safe(__obj, __tmp, &(__main_sysgroup->__group ## _list), next) 276 int heapobj_pkg_init_shared(
void);
278 int heapobj_init(
struct heapobj *hobj,
const char *name,
281 static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
282 size_t size,
void *unused)
285 return heapobj_init(hobj, name, size);
288 int heapobj_init_array(
struct heapobj *hobj,
const char *name,
289 size_t size,
int elems);
291 void heapobj_destroy(
struct heapobj *hobj);
293 int heapobj_extend(
struct heapobj *hobj,
294 size_t size,
void *mem);
296 void *heapobj_alloc(
struct heapobj *hobj,
299 void heapobj_free(
struct heapobj *hobj,
302 size_t heapobj_validate(
struct heapobj *hobj,
305 size_t heapobj_inquire(
struct heapobj *hobj);
307 int heapobj_bind_session(
const char *session);
309 void heapobj_unbind_session(
void);
311 int heapobj_unlink_session(
const char *session);
313 void *xnmalloc(
size_t size);
315 void xnfree(
void *ptr);
317 char *xnstrdup(
const char *ptr);
321 struct sysgroup_memspec {
328 static inline int pshared_check(
void *heap,
void *addr)
334 #define __check_ref_width(__dst, __src) \ 336 assert(sizeof(__dst) >= sizeof(__src)); \ 337 (typeof(__dst))__src; \ 340 #define __check_ref_width(__dst, __src) \ 341 __builtin_choose_expr( \ 342 sizeof(__dst) >= sizeof(__src), (typeof(__dst))__src, \ 346 #define mainheap_ref(ptr, type) \ 349 handle = __check_ref_width(handle, ptr); \ 350 assert(ptr == NULL || __memchk(__main_heap, ptr)); \ 353 #define mainheap_deref(handle, type) \ 356 ptr = __check_ref_width(ptr, handle); \ 360 #define sysgroup_add(__group, __obj) do { } while (0) 361 #define sysgroup_remove(__group, __obj) do { } while (0) 363 static inline int heapobj_pkg_init_shared(
void)
368 static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
369 size_t size,
void *mem)
371 return __heapobj_init_private(hobj, name, size, mem);
374 static inline int heapobj_init(
struct heapobj *hobj,
const char *name,
377 return __heapobj_init_private(hobj, name, size, NULL);
380 static inline int heapobj_init_array(
struct heapobj *hobj,
const char *name,
381 size_t size,
int elems)
383 return heapobj_init_array_private(hobj, name, size, elems);
386 static inline void heapobj_destroy(
struct heapobj *hobj)
388 pvheapobj_destroy(hobj);
391 static inline int heapobj_extend(
struct heapobj *hobj,
392 size_t size,
void *mem)
394 return pvheapobj_extend(hobj, size, mem);
397 static inline void *heapobj_alloc(
struct heapobj *hobj,
400 return pvheapobj_alloc(hobj, size);
403 static inline void heapobj_free(
struct heapobj *hobj,
406 pvheapobj_free(hobj, ptr);
409 static inline size_t heapobj_validate(
struct heapobj *hobj,
412 return pvheapobj_validate(hobj, ptr);
415 static inline size_t heapobj_inquire(
struct heapobj *hobj)
417 return pvheapobj_inquire(hobj);
420 static inline int heapobj_bind_session(
const char *session)
425 static inline int heapobj_unlink_session(
const char *session)
430 static inline void heapobj_unbind_session(
void) { }
432 static inline void *xnmalloc(
size_t size)
434 return pvmalloc(size);
437 static inline void xnfree(
void *ptr)
442 static inline char *xnstrdup(
const char *ptr)
444 return pvstrdup(ptr);
449 static inline const char *heapobj_name(
struct heapobj *hobj)
454 static inline size_t heapobj_size(
struct heapobj *hobj)