Xenomai  3.0.8
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 static inline void *mainheap_ptr(memoff_t off)
196 {
197  return off ? (void *)__memptr(__main_heap, off) : NULL;
198 }
199 
200 static inline memoff_t mainheap_off(void *addr)
201 {
202  return addr ? (memoff_t)__memoff(__main_heap, addr) : 0;
203 }
204 
205 /*
206  * ptr shall point to a block of memory allocated within the main heap
207  * if non-null; such address is always 8-byte aligned. Handles of
208  * shared heap pointers are returned with bit #0 set, which serves as
209  * a special tag detected in mainhead_deref(). A null pointer is
210  * always translated as a null handle.
211  */
212 #define mainheap_ref(ptr, type) \
213  ({ \
214  type handle; \
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); \
219  handle|1; \
220  })
221 /*
222  * Handles of shared heap-based pointers have bit #0 set. Other values
223  * are not translated, and the return value is the original handle
224  * cast to a pointer. A null handle is always returned unchanged.
225  */
226 #define mainheap_deref(handle, type) \
227  ({ \
228  type *ptr; \
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; \
232  ptr; \
233  })
234 
235 static inline void
236 __sysgroup_add(struct sysgroup_memspec *obj, struct listobj *q, int *countp)
237 {
238  write_lock_nocancel(&__main_sysgroup->lock);
239  (*countp)++;
240  list_append(&obj->next, q);
241  write_unlock(&__main_sysgroup->lock);
242 }
243 
244 #define sysgroup_add(__group, __obj) \
245  __sysgroup_add(__obj, &(__main_sysgroup->__group ## _list), \
246  &(__main_sysgroup->__group ## _count))
247 
248 static inline void
249 __sysgroup_remove(struct sysgroup_memspec *obj, int *countp)
250 {
251  write_lock_nocancel(&__main_sysgroup->lock);
252  (*countp)--;
253  list_remove(&obj->next);
254  write_unlock(&__main_sysgroup->lock);
255 }
256 
257 #define sysgroup_remove(__group, __obj) \
258  __sysgroup_remove(__obj, &(__main_sysgroup->__group ## _count))
259 
260 static inline void sysgroup_lock(void)
261 {
262  read_lock_nocancel(&__main_sysgroup->lock);
263 }
264 
265 static inline void sysgroup_unlock(void)
266 {
267  read_unlock(&__main_sysgroup->lock);
268 }
269 
270 #define sysgroup_count(__group) \
271  (__main_sysgroup->__group ## _count)
272 
273 #define for_each_sysgroup(__obj, __tmp, __group) \
274  list_for_each_entry_safe(__obj, __tmp, &(__main_sysgroup->__group ## _list), next)
275 
276 int heapobj_pkg_init_shared(void);
277 
278 int heapobj_init(struct heapobj *hobj, const char *name,
279  size_t size);
280 
281 static inline int __heapobj_init(struct heapobj *hobj, const char *name,
282  size_t size, void *unused)
283 {
284  /* Can't work on user-defined memory in shared mode. */
285  return heapobj_init(hobj, name, size);
286 }
287 
288 int heapobj_init_array(struct heapobj *hobj, const char *name,
289  size_t size, int elems);
290 
291 void heapobj_destroy(struct heapobj *hobj);
292 
293 int heapobj_extend(struct heapobj *hobj,
294  size_t size, void *mem);
295 
296 void *heapobj_alloc(struct heapobj *hobj,
297  size_t size);
298 
299 void heapobj_free(struct heapobj *hobj,
300  void *ptr);
301 
302 size_t heapobj_validate(struct heapobj *hobj,
303  void *ptr);
304 
305 size_t heapobj_inquire(struct heapobj *hobj);
306 
307 int heapobj_bind_session(const char *session);
308 
309 void heapobj_unbind_session(void);
310 
311 int heapobj_unlink_session(const char *session);
312 
313 void *xnmalloc(size_t size);
314 
315 void xnfree(void *ptr);
316 
317 char *xnstrdup(const char *ptr);
318 
319 #else /* !CONFIG_XENO_PSHARED */
320 
321 struct sysgroup_memspec {
322 };
323 
324 /*
325  * Whether an object is laid in some shared heap. Never if pshared
326  * mode is disabled.
327  */
328 static inline int pshared_check(void *heap, void *addr)
329 {
330  return 0;
331 }
332 
333 #ifdef __cplusplus
334 #define __check_ref_width(__dst, __src) \
335  ({ \
336  assert(sizeof(__dst) >= sizeof(__src)); \
337  (typeof(__dst))__src; \
338  })
339 #else
340 #define __check_ref_width(__dst, __src) \
341  __builtin_choose_expr( \
342  sizeof(__dst) >= sizeof(__src), (typeof(__dst))__src, \
343  ((void)0))
344 #endif
345 
346 #define mainheap_ref(ptr, type) \
347  ({ \
348  type handle; \
349  handle = __check_ref_width(handle, ptr); \
350  assert(ptr == NULL || __memchk(__main_heap, ptr)); \
351  handle; \
352  })
353 #define mainheap_deref(handle, type) \
354  ({ \
355  type *ptr; \
356  ptr = __check_ref_width(ptr, handle); \
357  ptr; \
358  })
359 
360 #define sysgroup_add(__group, __obj) do { } while (0)
361 #define sysgroup_remove(__group, __obj) do { } while (0)
362 
363 static inline int heapobj_pkg_init_shared(void)
364 {
365  return 0;
366 }
367 
368 static inline int __heapobj_init(struct heapobj *hobj, const char *name,
369  size_t size, void *mem)
370 {
371  return __heapobj_init_private(hobj, name, size, mem);
372 }
373 
374 static inline int heapobj_init(struct heapobj *hobj, const char *name,
375  size_t size)
376 {
377  return __heapobj_init_private(hobj, name, size, NULL);
378 }
379 
380 static inline int heapobj_init_array(struct heapobj *hobj, const char *name,
381  size_t size, int elems)
382 {
383  return heapobj_init_array_private(hobj, name, size, elems);
384 }
385 
386 static inline void heapobj_destroy(struct heapobj *hobj)
387 {
388  pvheapobj_destroy(hobj);
389 }
390 
391 static inline int heapobj_extend(struct heapobj *hobj,
392  size_t size, void *mem)
393 {
394  return pvheapobj_extend(hobj, size, mem);
395 }
396 
397 static inline void *heapobj_alloc(struct heapobj *hobj,
398  size_t size)
399 {
400  return pvheapobj_alloc(hobj, size);
401 }
402 
403 static inline void heapobj_free(struct heapobj *hobj,
404  void *ptr)
405 {
406  pvheapobj_free(hobj, ptr);
407 }
408 
409 static inline size_t heapobj_validate(struct heapobj *hobj,
410  void *ptr)
411 {
412  return pvheapobj_validate(hobj, ptr);
413 }
414 
415 static inline size_t heapobj_inquire(struct heapobj *hobj)
416 {
417  return pvheapobj_inquire(hobj);
418 }
419 
420 static inline int heapobj_bind_session(const char *session)
421 {
422  return -ENOSYS;
423 }
424 
425 static inline int heapobj_unlink_session(const char *session)
426 {
427  return 0;
428 }
429 
430 static inline void heapobj_unbind_session(void) { }
431 
432 static inline void *xnmalloc(size_t size)
433 {
434  return pvmalloc(size);
435 }
436 
437 static inline void xnfree(void *ptr)
438 {
439  pvfree(ptr);
440 }
441 
442 static inline char *xnstrdup(const char *ptr)
443 {
444  return pvstrdup(ptr);
445 }
446 
447 #endif /* !CONFIG_XENO_PSHARED */
448 
449 static inline const char *heapobj_name(struct heapobj *hobj)
450 {
451  return hobj->name;
452 }
453 
454 static inline size_t heapobj_size(struct heapobj *hobj)
455 {
456  return hobj->size;
457 }
458 
459 #endif /* _COPPERPLATE_HEAPOBJ_H */