Xenomai  3.1-devel
heapobj.h
1 /*
2  * Copyright (C) 2008-2011 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 
19 #ifndef _COPPERPLATE_HEAPOBJ_H
20 #define _COPPERPLATE_HEAPOBJ_H
21 
22 #include <sys/types.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <errno.h>
27 #include <pthread.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>
34 
35 struct heapobj {
36  union {
37  dref_type(void *) pool_ref;
38  void *pool;
39  };
40  size_t size;
41  char name[32];
42 #ifdef CONFIG_XENO_PSHARED
43  char fsname[256];
44 #endif
45 };
46 
47 struct sysgroup {
48  int thread_count;
49  struct listobj thread_list;
50  int heap_count;
51  struct listobj heap_list;
52  pthread_mutex_t lock;
53 };
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 int heapobj_pkg_init_private(void);
60 
61 int __heapobj_init_private(struct heapobj *hobj, const char *name,
62  size_t size, void *mem);
63 
64 int heapobj_init_array_private(struct heapobj *hobj, const char *name,
65  size_t size, int elems);
66 #ifdef __cplusplus
67 }
68 #endif
69 
70 #ifdef CONFIG_XENO_TLSF
71 
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);
80 
81 static inline
82 void pvheapobj_destroy(struct heapobj *hobj)
83 {
84  destroy_memory_pool(hobj->pool);
85 }
86 
87 static inline
88 int pvheapobj_extend(struct heapobj *hobj, size_t size, void *mem)
89 {
90  hobj->size = add_new_area(hobj->pool, size, mem);
91  if (hobj->size == (size_t)-1)
92  return __bt(-EINVAL);
93 
94  return 0;
95 }
96 
97 static inline
98 void *pvheapobj_alloc(struct heapobj *hobj, size_t size)
99 {
100  return malloc_ex(size, hobj->pool);
101 }
102 
103 static inline
104 void pvheapobj_free(struct heapobj *hobj, void *ptr)
105 {
106  free_ex(ptr, hobj->pool);
107 }
108 
109 static inline
110 size_t pvheapobj_validate(struct heapobj *hobj, void *ptr)
111 {
112  return malloc_usable_size_ex(ptr, hobj->pool);
113 }
114 
115 static inline
116 size_t pvheapobj_inquire(struct heapobj *hobj)
117 {
118  return get_used_size(hobj->pool);
119 }
120 
121 static inline void *pvmalloc(size_t size)
122 {
123  return tlsf_malloc(size);
124 }
125 
126 static inline void pvfree(void *ptr)
127 {
128  tlsf_free(ptr);
129 }
130 
131 static inline char *pvstrdup(const char *ptr)
132 {
133  char *str;
134 
135  str = (char *)pvmalloc(strlen(ptr) + 1);
136  if (str == NULL)
137  return NULL;
138 
139  return strcpy(str, ptr);
140 }
141 
142 #elif defined(CONFIG_XENO_HEAPMEM)
143 
144 #include <boilerplate/heapmem.h>
145 
146 extern struct heap_memory heapmem_main;
147 
148 static inline
149 void pvheapobj_destroy(struct heapobj *hobj)
150 {
151  heapmem_destroy((struct heap_memory *)hobj->pool);
152 }
153 
154 static inline
155 int pvheapobj_extend(struct heapobj *hobj, size_t size, void *mem)
156 {
157  return heapmem_extend((struct heap_memory *)hobj->pool,
158  mem, size);
159 }
160 
161 static inline
162 void *pvheapobj_alloc(struct heapobj *hobj, size_t size)
163 {
164  return heapmem_alloc((struct heap_memory *)hobj->pool, size);
165 }
166 
167 static inline
168 void pvheapobj_free(struct heapobj *hobj, void *ptr)
169 {
170  heapmem_free((struct heap_memory *)hobj->pool, ptr);
171 }
172 
173 static inline
174 size_t pvheapobj_validate(struct heapobj *hobj, void *ptr)
175 {
176  ssize_t size = heapmem_check((struct heap_memory *)hobj->pool, ptr);
177  return size < 0 ? 0 : size;
178 }
179 
180 static inline
181 size_t pvheapobj_inquire(struct heapobj *hobj)
182 {
183  return heapmem_used_size((struct heap_memory *)hobj->pool);
184 }
185 
186 static inline void *pvmalloc(size_t size)
187 {
188  return heapmem_alloc(&heapmem_main, size);
189 }
190 
191 static inline void pvfree(void *ptr)
192 {
193  heapmem_free(&heapmem_main, ptr);
194 }
195 
196 static inline char *pvstrdup(const char *ptr)
197 {
198  char *str;
199 
200  str = (char *)pvmalloc(strlen(ptr) + 1);
201  if (str == NULL)
202  return NULL;
203 
204  return strcpy(str, ptr);
205 }
206 
207 #else /* !CONFIG_XENO_HEAPMEM, i.e. malloc */
208 
209 #include <malloc.h>
210 
211 static inline void *pvmalloc(size_t size)
212 {
213  /*
214  * NOTE: We don't want debug _nrt assertions to trigger when
215  * running over Cobalt if the user picked this allocator, so
216  * we make sure to call the glibc directly, not the Cobalt
217  * wrappers.
218  */
219  return __STD(malloc(size));
220 }
221 
222 static inline void pvfree(void *ptr)
223 {
224  __STD(free(ptr));
225 }
226 
227 static inline char *pvstrdup(const char *ptr)
228 {
229  return strdup(ptr);
230 }
231 
232 void pvheapobj_destroy(struct heapobj *hobj);
233 
234 int pvheapobj_extend(struct heapobj *hobj, size_t size, void *mem);
235 
236 void *pvheapobj_alloc(struct heapobj *hobj, size_t size);
237 
238 void pvheapobj_free(struct heapobj *hobj, void *ptr);
239 
240 size_t pvheapobj_inquire(struct heapobj *hobj);
241 
242 size_t pvheapobj_validate(struct heapobj *hobj, void *ptr);
243 
244 #endif /* !CONFIG_XENO_HEAPMEM */
245 
246 #ifdef CONFIG_XENO_PSHARED
247 
248 extern void *__main_heap;
249 
250 extern struct hash_table *__main_catalog;
251 #define main_catalog (*((struct hash_table *)__main_catalog))
252 
253 extern struct sysgroup *__main_sysgroup;
254 
255 struct sysgroup_memspec {
257  struct holder next;
258 };
259 
260 static inline void *mainheap_ptr(memoff_t off)
261 {
262  return off ? (void *)__memptr(__main_heap, off) : NULL;
263 }
264 
265 static inline memoff_t mainheap_off(void *addr)
266 {
267  return addr ? (memoff_t)__memoff(__main_heap, addr) : 0;
268 }
269 
270 /*
271  * ptr shall point to a block of memory allocated within the main heap
272  * if non-null; such address is always 8-byte aligned. Handles of
273  * shared heap pointers are returned with bit #0 set, which serves as
274  * a special tag detected in mainhead_deref(). A null pointer is
275  * always translated as a null handle.
276  */
277 #define mainheap_ref(ptr, type) \
278  ({ \
279  type handle; \
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); \
284  handle|1; \
285  })
286 /*
287  * Handles of shared heap-based pointers have bit #0 set. Other values
288  * are not translated, and the return value is the original handle
289  * cast to a pointer. A null handle is always returned unchanged.
290  */
291 #define mainheap_deref(handle, type) \
292  ({ \
293  type *ptr; \
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; \
297  ptr; \
298  })
299 
300 static inline void
301 __sysgroup_add(struct sysgroup_memspec *obj, struct listobj *q, int *countp)
302 {
303  write_lock_nocancel(&__main_sysgroup->lock);
304  (*countp)++;
305  list_append(&obj->next, q);
306  write_unlock(&__main_sysgroup->lock);
307 }
308 
309 #define sysgroup_add(__group, __obj) \
310  __sysgroup_add(__obj, &(__main_sysgroup->__group ## _list), \
311  &(__main_sysgroup->__group ## _count))
312 
313 static inline void
314 __sysgroup_remove(struct sysgroup_memspec *obj, int *countp)
315 {
316  write_lock_nocancel(&__main_sysgroup->lock);
317  (*countp)--;
318  list_remove(&obj->next);
319  write_unlock(&__main_sysgroup->lock);
320 }
321 
322 #define sysgroup_remove(__group, __obj) \
323  __sysgroup_remove(__obj, &(__main_sysgroup->__group ## _count))
324 
325 static inline void sysgroup_lock(void)
326 {
327  read_lock_nocancel(&__main_sysgroup->lock);
328 }
329 
330 static inline void sysgroup_unlock(void)
331 {
332  read_unlock(&__main_sysgroup->lock);
333 }
334 
335 #define sysgroup_count(__group) \
336  (__main_sysgroup->__group ## _count)
337 
338 #define for_each_sysgroup(__obj, __tmp, __group) \
339  list_for_each_entry_safe(__obj, __tmp, &(__main_sysgroup->__group ## _list), next)
340 
341 int heapobj_pkg_init_shared(void);
342 
343 int heapobj_init(struct heapobj *hobj, const char *name,
344  size_t size);
345 
346 static inline int __heapobj_init(struct heapobj *hobj, const char *name,
347  size_t size, void *unused)
348 {
349  /* Can't work on user-defined memory in shared mode. */
350  return heapobj_init(hobj, name, size);
351 }
352 
353 int heapobj_init_array(struct heapobj *hobj, const char *name,
354  size_t size, int elems);
355 
356 void heapobj_destroy(struct heapobj *hobj);
357 
358 int heapobj_extend(struct heapobj *hobj,
359  size_t size, void *mem);
360 
361 void *heapobj_alloc(struct heapobj *hobj,
362  size_t size);
363 
364 void heapobj_free(struct heapobj *hobj,
365  void *ptr);
366 
367 size_t heapobj_validate(struct heapobj *hobj,
368  void *ptr);
369 
370 size_t heapobj_inquire(struct heapobj *hobj);
371 
372 size_t heapobj_get_size(struct heapobj *hobj);
373 
374 int heapobj_bind_session(const char *session);
375 
376 void heapobj_unbind_session(void);
377 
378 int heapobj_unlink_session(const char *session);
379 
380 void *xnmalloc(size_t size);
381 
382 void xnfree(void *ptr);
383 
384 char *xnstrdup(const char *ptr);
385 
386 #else /* !CONFIG_XENO_PSHARED */
387 
388 struct sysgroup_memspec {
389 };
390 
391 /*
392  * Whether an object is laid in some shared heap. Never if pshared
393  * mode is disabled.
394  */
395 static inline int pshared_check(void *heap, void *addr)
396 {
397  return 0;
398 }
399 
400 #ifdef __cplusplus
401 #define __check_ref_width(__dst, __src) \
402  ({ \
403  assert(sizeof(__dst) >= sizeof(__src)); \
404  (typeof(__dst))__src; \
405  })
406 #else
407 #define __check_ref_width(__dst, __src) \
408  __builtin_choose_expr( \
409  sizeof(__dst) >= sizeof(__src), (typeof(__dst))__src, \
410  ((void)0))
411 #endif
412 
413 #define mainheap_ref(ptr, type) \
414  ({ \
415  type handle; \
416  handle = __check_ref_width(handle, ptr); \
417  assert(ptr == NULL || __memchk(__main_heap, ptr)); \
418  handle; \
419  })
420 #define mainheap_deref(handle, type) \
421  ({ \
422  type *ptr; \
423  ptr = __check_ref_width(ptr, handle); \
424  ptr; \
425  })
426 
427 #define sysgroup_add(__group, __obj) do { } while (0)
428 #define sysgroup_remove(__group, __obj) do { } while (0)
429 
430 static inline int heapobj_pkg_init_shared(void)
431 {
432  return 0;
433 }
434 
435 static inline int __heapobj_init(struct heapobj *hobj, const char *name,
436  size_t size, void *mem)
437 {
438  return __heapobj_init_private(hobj, name, size, mem);
439 }
440 
441 static inline int heapobj_init(struct heapobj *hobj, const char *name,
442  size_t size)
443 {
444  return __heapobj_init_private(hobj, name, size, NULL);
445 }
446 
447 static inline int heapobj_init_array(struct heapobj *hobj, const char *name,
448  size_t size, int elems)
449 {
450  return heapobj_init_array_private(hobj, name, size, elems);
451 }
452 
453 static inline void heapobj_destroy(struct heapobj *hobj)
454 {
455  pvheapobj_destroy(hobj);
456 }
457 
458 static inline int heapobj_extend(struct heapobj *hobj,
459  size_t size, void *mem)
460 {
461  return pvheapobj_extend(hobj, size, mem);
462 }
463 
464 static inline void *heapobj_alloc(struct heapobj *hobj,
465  size_t size)
466 {
467  return pvheapobj_alloc(hobj, size);
468 }
469 
470 static inline void heapobj_free(struct heapobj *hobj,
471  void *ptr)
472 {
473  pvheapobj_free(hobj, ptr);
474 }
475 
476 static inline size_t heapobj_validate(struct heapobj *hobj,
477  void *ptr)
478 {
479  return pvheapobj_validate(hobj, ptr);
480 }
481 
482 static inline size_t heapobj_inquire(struct heapobj *hobj)
483 {
484  return pvheapobj_inquire(hobj);
485 }
486 
487 static inline int heapobj_bind_session(const char *session)
488 {
489  return -ENOSYS;
490 }
491 
492 static inline int heapobj_unlink_session(const char *session)
493 {
494  return 0;
495 }
496 
497 static inline void heapobj_unbind_session(void) { }
498 
499 static inline void *xnmalloc(size_t size)
500 {
501  return pvmalloc(size);
502 }
503 
504 static inline void xnfree(void *ptr)
505 {
506  pvfree(ptr);
507 }
508 
509 static inline char *xnstrdup(const char *ptr)
510 {
511  return pvstrdup(ptr);
512 }
513 
514 #endif /* !CONFIG_XENO_PSHARED */
515 
516 static inline const char *heapobj_name(struct heapobj *hobj)
517 {
518  return hobj->name;
519 }
520 
521 static inline size_t heapobj_size(struct heapobj *hobj)
522 {
523  return hobj->size;
524 }
525 
526 #endif /* _COPPERPLATE_HEAPOBJ_H */