Xenomai  3.1
registry-obstack.h
1 /*
2  * Copyright (C) 2014 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 #ifndef _COPPERPLATE_REGISTRY_OBSTACK_H
19 #define _COPPERPLATE_REGISTRY_OBSTACK_H
20 
21 #include <copperplate/registry.h>
22 
23 #ifdef CONFIG_XENO_REGISTRY
24 
25 #include <stdlib.h>
26 #include <boilerplate/obstack.h>
27 #include <copperplate/heapobj.h>
28 
29 struct threadobj;
30 struct syncobj;
31 
32 /*
33  * Obstacks are grown from handlers called by the fusefs server
34  * thread, which has no real-time requirement: malloc/free is fine for
35  * memory management.
36  */
37 #define obstack_chunk_alloc malloc
38 #define obstack_chunk_free free
39 
40 struct threadobj;
41 
42 struct fsobstack {
43  struct obstack obstack;
44  void *data;
45  size_t len;
46 };
47 
48 struct fsobstack_syncops {
49  int (*prepare_cache)(struct fsobstack *o,
50  struct obstack *cache, int item_count);
51  size_t (*collect_data)(void *p, struct threadobj *thobj);
52  size_t (*format_data)(struct fsobstack *o, void *p);
53 };
54 
55 struct syncobj;
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
61 void fsobstack_grow_string(struct fsobstack *o,
62  const char *s);
63 
64 void fsobstack_grow_char(struct fsobstack *o,
65  char c);
66 
67 int fsobstack_grow_format(struct fsobstack *o,
68  const char *fmt, ...);
69 
70 int fsobstack_grow_file(struct fsobstack *o,
71  const char *path);
72 
73 int fsobstack_grow_syncobj_grant(struct fsobstack *o,
74  struct syncobj *sobj,
75  struct fsobstack_syncops *ops);
76 
77 int fsobstack_grow_syncobj_drain(struct fsobstack *o,
78  struct syncobj *sobj,
79  struct fsobstack_syncops *ops);
80 
81 ssize_t fsobstack_pull(struct fsobstack *o,
82  char *buf, size_t size);
83 
84 ssize_t fsobj_obstack_read(struct fsobj *fsobj,
85  char *buf, size_t size, off_t offset,
86  void *priv);
87 
88 int fsobj_obstack_release(struct fsobj *fsobj, void *priv);
89 
90 #ifdef __cplusplus
91 }
92 #endif
93 
94 static inline void fsobstack_init(struct fsobstack *o)
95 {
96  obstack_init(&o->obstack);
97  o->data = NULL;
98  o->len = 0;
99 }
100 
101 static inline void fsobstack_destroy(struct fsobstack *o)
102 {
103  obstack_free(&o->obstack, NULL);
104 }
105 
106 static inline void fsobstack_finish(struct fsobstack *o)
107 {
108  o->len = obstack_object_size(&o->obstack);
109  o->data = obstack_finish(&o->obstack);
110 }
111 
112 static inline
113 void registry_init_file_obstack(struct fsobj *fsobj,
114  const struct registry_operations *ops)
115 {
116  registry_init_file(fsobj, ops, sizeof(struct fsobstack));
117 }
118 
119 #else /* !CONFIG_XENO_REGISTRY */
120 
121 static inline
122 void registry_init_file_obstack(struct fsobj *fsobj,
123  const struct registry_operations *ops)
124 { }
125 
126 #endif /* !CONFIG_XENO_REGISTRY */
127 
128 #endif /* !_COPPERPLATE_REGISTRY_OBSTACK_H */