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>
39 #ifdef CONFIG_XENO_PSHARED
46 struct list thread_list;
48 struct list heap_list;
56 int heapobj_pkg_init_private(
void);
58 int __heapobj_init_private(
struct heapobj *hobj,
const char *name,
59 size_t size,
void *mem);
61 int heapobj_init_array_private(
struct heapobj *hobj,
const char *name,
62 size_t size,
int elems);
67 #ifdef CONFIG_XENO_TLSF
69 size_t get_used_size(
void *pool);
70 void destroy_memory_pool(
void *pool);
71 size_t add_new_area(
void *pool,
size_t size,
void *mem);
72 void *malloc_ex(
size_t size,
void *pool);
73 void free_ex(
void *pool,
void *ptr);
74 void *tlsf_malloc(
size_t size);
75 void tlsf_free(
void *ptr);
76 size_t malloc_usable_size_ex(
void *ptr,
void *pool);
79 void pvheapobj_destroy(
struct heapobj *hobj)
81 destroy_memory_pool(hobj->pool);
85 int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem)
87 hobj->size = add_new_area(hobj->pool, size, mem);
88 if (hobj->size == (
size_t)-1)
95 void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size)
97 return malloc_ex(size, hobj->pool);
101 void pvheapobj_free(
struct heapobj *hobj,
void *ptr)
103 free_ex(ptr, hobj->pool);
107 size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr)
109 return malloc_usable_size_ex(ptr, hobj->pool);
113 size_t pvheapobj_inquire(
struct heapobj *hobj)
115 return get_used_size(hobj->pool);
118 static inline void *pvmalloc(
size_t size)
120 return tlsf_malloc(size);
123 static inline void pvfree(
void *ptr)
128 static inline char *pvstrdup(
const char *ptr)
132 str = (
char *)pvmalloc(strlen(ptr) + 1);
136 return strcpy(str, ptr);
143 static inline void *pvmalloc(
size_t size)
151 return __STD(malloc(size));
154 static inline void pvfree(
void *ptr)
159 static inline char *pvstrdup(
const char *ptr)
164 void pvheapobj_destroy(
struct heapobj *hobj);
166 int pvheapobj_extend(
struct heapobj *hobj,
size_t size,
void *mem);
168 void *pvheapobj_alloc(
struct heapobj *hobj,
size_t size);
170 void pvheapobj_free(
struct heapobj *hobj,
void *ptr);
172 size_t pvheapobj_inquire(
struct heapobj *hobj);
174 size_t pvheapobj_validate(
struct heapobj *hobj,
void *ptr);
178 #ifdef CONFIG_XENO_PSHARED
180 extern void *__main_heap;
182 extern struct hash_table *__main_catalog;
183 #define main_catalog (*((struct hash_table *)__main_catalog))
185 extern struct sysgroup *__main_sysgroup;
187 struct sysgroup_memspec {
192 struct agent_memspec {
197 static inline void *mainheap_ptr(memoff_t off)
199 return off ? (
void *)__memptr(__main_heap, off) : NULL;
202 static inline memoff_t mainheap_off(
void *addr)
204 return addr ? (memoff_t)__memoff(__main_heap, addr) : 0;
214 #define mainheap_ref(ptr, type) \
217 assert(__builtin_types_compatible_p(typeof(type), unsigned long) || \
218 __builtin_types_compatible_p(typeof(type), uintptr_t)); \
219 assert(ptr == NULL || __memchk(__main_heap, ptr)); \
220 handle = (type)mainheap_off(ptr); \
228 #define mainheap_deref(handle, type) \
231 assert(__builtin_types_compatible_p(typeof(handle), unsigned long) || \
232 __builtin_types_compatible_p(typeof(handle), uintptr_t)); \
233 ptr = (handle & 1) ? (type *)mainheap_ptr(handle & ~1UL) : (type *)handle; \
238 __sysgroup_add(
struct sysgroup_memspec *obj,
struct list *q,
int *countp)
240 write_lock_nocancel(&__main_sysgroup->lock);
242 list_append(&obj->next, q);
243 write_unlock(&__main_sysgroup->lock);
246 #define sysgroup_add(__group, __obj) \
247 __sysgroup_add(__obj, &(__main_sysgroup->__group ## _list), \
248 &(__main_sysgroup->__group ## _count))
251 __sysgroup_remove(
struct sysgroup_memspec *obj,
int *countp)
253 write_lock_nocancel(&__main_sysgroup->lock);
255 list_remove(&obj->next);
256 write_unlock(&__main_sysgroup->lock);
259 #define sysgroup_remove(__group, __obj) \
260 __sysgroup_remove(__obj, &(__main_sysgroup->__group ## _count))
262 static inline void sysgroup_lock(
void)
264 read_lock_nocancel(&__main_sysgroup->lock);
267 static inline void sysgroup_unlock(
void)
269 read_unlock(&__main_sysgroup->lock);
272 #define sysgroup_count(__group) \
273 (__main_sysgroup->__group ## _count)
275 #define for_each_sysgroup(__obj, __group) \
276 list_for_each_entry(__obj, &(__main_sysgroup->__group ## _list), next)
278 int heapobj_pkg_init_shared(
void);
280 int heapobj_init(
struct heapobj *hobj,
const char *name,
283 static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
284 size_t size,
void *unused)
287 return heapobj_init(hobj, name, size);
290 int heapobj_init_array(
struct heapobj *hobj,
const char *name,
291 size_t size,
int elems);
293 void heapobj_destroy(
struct heapobj *hobj);
295 int heapobj_extend(
struct heapobj *hobj,
296 size_t size,
void *mem);
298 void *heapobj_alloc(
struct heapobj *hobj,
301 void heapobj_free(
struct heapobj *hobj,
304 size_t heapobj_validate(
struct heapobj *hobj,
307 size_t heapobj_inquire(
struct heapobj *hobj);
309 int heapobj_bind_session(
const char *session);
311 void heapobj_unbind_session(
void);
313 void *xnmalloc(
size_t size);
315 void xnfree(
void *ptr);
317 char *xnstrdup(
const char *ptr);
321 struct sysgroup_memspec {
324 struct agent_memspec {
331 static inline int pshared_check(
void *heap,
void *addr)
337 #define __check_ref_width(__dst, __src) \
339 assert(sizeof(__dst) >= sizeof(__src)); \
340 (typeof(__dst))__src; \
343 #define __check_ref_width(__dst, __src) \
344 __builtin_choose_expr( \
345 sizeof(__dst) >= sizeof(__src), (typeof(__dst))__src, \
349 #define mainheap_ref(ptr, type) \
352 handle = __check_ref_width(handle, ptr); \
353 assert(ptr == NULL || __memchk(__main_heap, ptr)); \
356 #define mainheap_deref(handle, type) \
359 ptr = __check_ref_width(ptr, handle); \
363 #define sysgroup_add(__group, __obj) do { } while (0)
364 #define sysgroup_remove(__group, __obj) do { } while (0)
366 static inline int heapobj_pkg_init_shared(
void)
371 static inline int __heapobj_init(
struct heapobj *hobj,
const char *name,
372 size_t size,
void *mem)
374 return __heapobj_init_private(hobj, name, size, mem);
377 static inline int heapobj_init(
struct heapobj *hobj,
const char *name,
380 return __heapobj_init_private(hobj, name, size, NULL);
383 static inline int heapobj_init_array(
struct heapobj *hobj,
const char *name,
384 size_t size,
int elems)
386 return heapobj_init_array_private(hobj, name, size, elems);
389 static inline void heapobj_destroy(
struct heapobj *hobj)
391 pvheapobj_destroy(hobj);
394 static inline int heapobj_extend(
struct heapobj *hobj,
395 size_t size,
void *mem)
397 return pvheapobj_extend(hobj, size, mem);
400 static inline void *heapobj_alloc(
struct heapobj *hobj,
403 return pvheapobj_alloc(hobj, size);
406 static inline void heapobj_free(
struct heapobj *hobj,
409 pvheapobj_free(hobj, ptr);
412 static inline size_t heapobj_validate(
struct heapobj *hobj,
415 return pvheapobj_validate(hobj, ptr);
418 static inline size_t heapobj_inquire(
struct heapobj *hobj)
420 return pvheapobj_inquire(hobj);
423 static inline int heapobj_bind_session(
const char *session)
428 static inline void heapobj_unbind_session(
void) { }
430 static inline void *xnmalloc(
size_t size)
432 return pvmalloc(size);
435 static inline void xnfree(
void *ptr)
440 static inline char *xnstrdup(
const char *ptr)
442 return pvstrdup(ptr);
447 static inline const char *heapobj_name(
struct heapobj *hobj)
452 static inline size_t heapobj_size(
struct heapobj *hobj)