Xenomai  3.0-rc7
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 #else /* !CONFIG_XENO_TLSF, i.e. malloc */
143 
144 #include <malloc.h>
145 
146 static inline void *pvmalloc(size_t size)
147 {
148  /*
149  * NOTE: We don't want debug _nrt assertions to trigger when
150  * running over Cobalt if the user picked this allocator, so
151  * we make sure to call the glibc directly, not the Cobalt
152  * wrappers.
153  */
154  return __STD(malloc(size));
155 }
156 
157 static inline void pvfree(void *ptr)
158 {
159  __STD(free(ptr));
160 }
161 
162 static inline char *pvstrdup(const char *ptr)
163 {
164  return strdup(ptr);
165 }
166 
167 void pvheapobj_destroy(struct heapobj *hobj);
168 
169 int pvheapobj_extend(struct heapobj *hobj, size_t size, void *mem);
170 
171 void *pvheapobj_alloc(struct heapobj *hobj, size_t size);
172 
173 void pvheapobj_free(struct heapobj *hobj, void *ptr);
174 
175 size_t pvheapobj_inquire(struct heapobj *hobj);
176 
177 size_t pvheapobj_validate(struct heapobj *hobj, void *ptr);
178 
179 #endif /* !CONFIG_XENO_TLSF */
180 
181 #ifdef CONFIG_XENO_PSHARED
182 
183 extern void *__main_heap;
184 
185 extern struct hash_table *__main_catalog;
186 #define main_catalog (*((struct hash_table *)__main_catalog))
187 
188 extern struct sysgroup *__main_sysgroup;
189 
190 struct sysgroup_memspec {
192  struct holder next;
193 };
194 
195 struct agent_memspec {
197  pid_t pid;
198 };
199 
200 static inline void *mainheap_ptr(memoff_t off)
201 {
202  return off ? (void *)__memptr(__main_heap, off) : NULL;
203 }
204 
205 static inline memoff_t mainheap_off(void *addr)
206 {
207  return addr ? (memoff_t)__memoff(__main_heap, addr) : 0;
208 }
209 
210 /*
211  * ptr shall point to a block of memory allocated within the main heap
212  * if non-null; such address is always 8-byte aligned. Handles of
213  * shared heap pointers are returned with bit #0 set, which serves as
214  * a special tag detected in mainhead_deref(). A null pointer is
215  * always translated as a null handle.
216  */
217 #define mainheap_ref(ptr, type) \
218  ({ \
219  type handle; \
220  assert(__builtin_types_compatible_p(typeof(type), unsigned long) || \
221  __builtin_types_compatible_p(typeof(type), uintptr_t)); \
222  assert(ptr == NULL || __memchk(__main_heap, ptr)); \
223  handle = (type)mainheap_off(ptr); \
224  handle|1; \
225  })
226 /*
227  * Handles of shared heap-based pointers have bit #0 set. Other values
228  * are not translated, and the return value is the original handle
229  * cast to a pointer. A null handle is always returned unchanged.
230  */
231 #define mainheap_deref(handle, type) \
232  ({ \
233  type *ptr; \
234  assert(__builtin_types_compatible_p(typeof(handle), unsigned long) || \
235  __builtin_types_compatible_p(typeof(handle), uintptr_t)); \
236  ptr = (handle & 1) ? (type *)mainheap_ptr(handle & ~1UL) : (type *)handle; \
237  ptr; \
238  })
239 
240 static inline void
241 __sysgroup_add(struct sysgroup_memspec *obj, struct listobj *q, int *countp)
242 {
243  write_lock_nocancel(&__main_sysgroup->lock);
244  (*countp)++;
245  list_append(&obj->next, q);
246  write_unlock(&__main_sysgroup->lock);
247 }
248 
249 #define sysgroup_add(__group, __obj) \
250  __sysgroup_add(__obj, &(__main_sysgroup->__group ## _list), \
251  &(__main_sysgroup->__group ## _count))
252 
253 static inline void
254 __sysgroup_remove(struct sysgroup_memspec *obj, int *countp)
255 {
256  write_lock_nocancel(&__main_sysgroup->lock);
257  (*countp)--;
258  list_remove(&obj->next);
259  write_unlock(&__main_sysgroup->lock);
260 }
261 
262 #define sysgroup_remove(__group, __obj) \
263  __sysgroup_remove(__obj, &(__main_sysgroup->__group ## _count))
264 
265 static inline void sysgroup_lock(void)
266 {
267  read_lock_nocancel(&__main_sysgroup->lock);
268 }
269 
270 static inline void sysgroup_unlock(void)
271 {
272  read_unlock(&__main_sysgroup->lock);
273 }
274 
275 #define sysgroup_count(__group) \
276  (__main_sysgroup->__group ## _count)
277 
278 #define for_each_sysgroup(__obj, __tmp, __group) \
279  list_for_each_entry_safe(__obj, __tmp, &(__main_sysgroup->__group ## _list), next)
280 
281 int heapobj_pkg_init_shared(void);
282 
283 int heapobj_init(struct heapobj *hobj, const char *name,
284  size_t size);
285 
286 static inline int __heapobj_init(struct heapobj *hobj, const char *name,
287  size_t size, void *unused)
288 {
289  /* Can't work on user-defined memory in shared mode. */
290  return heapobj_init(hobj, name, size);
291 }
292 
293 int heapobj_init_array(struct heapobj *hobj, const char *name,
294  size_t size, int elems);
295 
296 void heapobj_destroy(struct heapobj *hobj);
297 
298 int heapobj_extend(struct heapobj *hobj,
299  size_t size, void *mem);
300 
301 void *heapobj_alloc(struct heapobj *hobj,
302  size_t size);
303 
304 void heapobj_free(struct heapobj *hobj,
305  void *ptr);
306 
307 size_t heapobj_validate(struct heapobj *hobj,
308  void *ptr);
309 
310 size_t heapobj_inquire(struct heapobj *hobj);
311 
312 int heapobj_bind_session(const char *session);
313 
314 void heapobj_unbind_session(void);
315 
316 int heapobj_unlink_session(const char *session);
317 
318 void *xnmalloc(size_t size);
319 
320 void xnfree(void *ptr);
321 
322 char *xnstrdup(const char *ptr);
323 
324 #else /* !CONFIG_XENO_PSHARED */
325 
326 struct sysgroup_memspec {
327 };
328 
329 struct agent_memspec {
330 };
331 
332 /*
333  * Whether an object is laid in some shared heap. Never if pshared
334  * mode is disabled.
335  */
336 static inline int pshared_check(void *heap, void *addr)
337 {
338  return 0;
339 }
340 
341 #ifdef __cplusplus
342 #define __check_ref_width(__dst, __src) \
343  ({ \
344  assert(sizeof(__dst) >= sizeof(__src)); \
345  (typeof(__dst))__src; \
346  })
347 #else
348 #define __check_ref_width(__dst, __src) \
349  __builtin_choose_expr( \
350  sizeof(__dst) >= sizeof(__src), (typeof(__dst))__src, \
351  ((void)0))
352 #endif
353 
354 #define mainheap_ref(ptr, type) \
355  ({ \
356  type handle; \
357  handle = __check_ref_width(handle, ptr); \
358  assert(ptr == NULL || __memchk(__main_heap, ptr)); \
359  handle; \
360  })
361 #define mainheap_deref(handle, type) \
362  ({ \
363  type *ptr; \
364  ptr = __check_ref_width(ptr, handle); \
365  ptr; \
366  })
367 
368 #define sysgroup_add(__group, __obj) do { } while (0)
369 #define sysgroup_remove(__group, __obj) do { } while (0)
370 
371 static inline int heapobj_pkg_init_shared(void)
372 {
373  return 0;
374 }
375 
376 static inline int __heapobj_init(struct heapobj *hobj, const char *name,
377  size_t size, void *mem)
378 {
379  return __heapobj_init_private(hobj, name, size, mem);
380 }
381 
382 static inline int heapobj_init(struct heapobj *hobj, const char *name,
383  size_t size)
384 {
385  return __heapobj_init_private(hobj, name, size, NULL);
386 }
387 
388 static inline int heapobj_init_array(struct heapobj *hobj, const char *name,
389  size_t size, int elems)
390 {
391  return heapobj_init_array_private(hobj, name, size, elems);
392 }
393 
394 static inline void heapobj_destroy(struct heapobj *hobj)
395 {
396  pvheapobj_destroy(hobj);
397 }
398 
399 static inline int heapobj_extend(struct heapobj *hobj,
400  size_t size, void *mem)
401 {
402  return pvheapobj_extend(hobj, size, mem);
403 }
404 
405 static inline void *heapobj_alloc(struct heapobj *hobj,
406  size_t size)
407 {
408  return pvheapobj_alloc(hobj, size);
409 }
410 
411 static inline void heapobj_free(struct heapobj *hobj,
412  void *ptr)
413 {
414  pvheapobj_free(hobj, ptr);
415 }
416 
417 static inline size_t heapobj_validate(struct heapobj *hobj,
418  void *ptr)
419 {
420  return pvheapobj_validate(hobj, ptr);
421 }
422 
423 static inline size_t heapobj_inquire(struct heapobj *hobj)
424 {
425  return pvheapobj_inquire(hobj);
426 }
427 
428 static inline int heapobj_bind_session(const char *session)
429 {
430  return -ENOSYS;
431 }
432 
433 static inline int heapobj_unlink_session(const char *session)
434 {
435  return 0;
436 }
437 
438 static inline void heapobj_unbind_session(void) { }
439 
440 static inline void *xnmalloc(size_t size)
441 {
442  return pvmalloc(size);
443 }
444 
445 static inline void xnfree(void *ptr)
446 {
447  pvfree(ptr);
448 }
449 
450 static inline char *xnstrdup(const char *ptr)
451 {
452  return pvstrdup(ptr);
453 }
454 
455 #endif /* !CONFIG_XENO_PSHARED */
456 
457 static inline const char *heapobj_name(struct heapobj *hobj)
458 {
459  return hobj->name;
460 }
461 
462 static inline size_t heapobj_size(struct heapobj *hobj)
463 {
464  return hobj->size;
465 }
466 
467 #endif /* _COPPERPLATE_HEAPOBJ_H */