]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/contract/fres_contract.h
Suppress warning in fres_contract_id_to_string() on 64 bit system
[frescor/frsh.git] / fres / contract / fres_contract.h
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FRSH (FRescor ScHeduler)                         */
26 /*                                                                        */
27 /* FRSH is free software; you can redistribute it and/or modify it        */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FRSH is distributed in the hope that it will be        */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FRSH; see file       */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FRSH header files in a file,         */
39 /* instantiating FRSH generics or templates, or linking other files       */
40 /* with FRSH objects to produce an executable application, does not       */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46
47 /**
48  * @file   fres_contract.h
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Sun Nov  9 22:15:09 2008
51  * 
52  * @brief  Declaration of contract type and functions
53  * 
54  * 
55  */
56 #ifndef FRES_CONTRACT_H
57 #define FRES_CONTRACT_H
58
59 #include <fres_blocks.h>
60 #include <fres_container.h>
61 #include <ul_gavl.h>
62 #include <fres_contract_type.h>
63 #include <fres_contract_idl.h>
64 #include <forb/server_id.h>
65 #include <stdio.h>
66 #ifdef __cplusplus__
67 #  define __STDC_FORMAT_MACROS
68 #endif
69 #include <inttypes.h>
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73
74 /**
75  * Contract data type.
76  * 
77  */
78 struct fres_contract {
79         fres_contract_id_t id;  /**< Global contract ID */
80         struct fres_container *container;       /**< Pointer to the block container (opaque type). */
81 };
82
83 static inline int fres_contract_id_cmp(const fres_contract_id_t *a,
84                                        const fres_contract_id_t *b)
85 {
86         return forb_server_id_cmp((forb_server_id*)a,
87                                   (forb_server_id*)b);
88 }
89
90 static inline bool fres_contract_id_is_empty(const fres_contract_id_t *id)
91 {
92 /*      bool empty = true; */
93 /*      unsigned i; */
94
95 /*      for (i=0; i<sizeof(id->byte); i++) { */
96 /*              if (id->byte[i] != 0) { */
97 /*                      empty = false; */
98 /*                      break; */
99 /*              } */
100 /*      } */
101 /*      return empty; */
102         return *id == 0;
103 }
104
105 static inline char *fres_contract_id_to_string(char *dest,
106                                                const fres_contract_id_t *id,
107                                                size_t n)
108 {
109         snprintf(dest, n, "%"PRIx64, *id);
110 /*      return forb_server_id_to_string(dest, (forb_server_id*)id, n); */
111         return dest;
112 }
113
114 struct fres_contract *fres_contract_new(void);
115 void fres_contract_destroy(struct fres_contract *contract);
116
117 /** Destructor for easy use in forb_sequence_free() */
118 static inline void fres_contract_ptr_destroy(struct fres_contract **p)
119 {
120         fres_contract_destroy(*p);
121 }
122
123
124 struct fres_contract *fres_contract_duplicate(struct fres_contract *src);
125
126 /** 
127  * Adds a block of the given type to the contract.
128  *
129  * This function uses fres_container_add_block() to do its job.
130  * 
131  * @param contract Where to add the @a block.
132  * @param type Type of contract being added.
133  * @param block Pointer to the malloced block of given @a type.
134  * 
135  * @return Zero on success, -1 on error and errno is set appropriately.
136  */
137 static inline int
138 fres_contract_add_block(struct fres_contract *contract,
139                         enum fres_block_type type, void *block)
140 {
141         return fres_container_add_block(contract->container, type, block);
142 }
143 /** 
144  * Deletes a block from the contract and frees it from memory.
145  *
146  * This function uses fres_container_del_block() to do its job.
147  * 
148  * @param contract Where to delete the block.
149  * @param type Type of contract to delete.
150  */
151 static inline void
152 fres_contract_del_block(struct fres_contract *contract,
153                         enum fres_block_type type)
154 {
155         fres_container_del_block(contract->container, type);
156 }
157
158 /** 
159  * Returns pointer to a contract block of a particular @a type.
160  * 
161  * This function uses fres_container_get_block() to do its job.
162  * 
163  * @param contract
164  * @param type Type of the block to be returned.
165  * 
166  * @return Pointer to the block or NULL if the block is not present or
167  * deserialized. The memory area pointed by this pointer is owned by
168  * the contract. If the user needs to store the block, it must be
169  * duplicated.
170  */
171 static inline void *
172 fres_contract_get_block(struct fres_contract *contract,
173                         enum fres_block_type type)
174 {
175         return fres_container_get_block(contract->container, type);
176 }
177
178 int
179 fres_contract_to_string(char *dest, size_t size, const struct fres_contract *c);
180
181 /* #define FRES_C2S_FL_FMT_ONELINE */
182 /* #define FRES_C2S_FL_FMT_BLOCKPERLINE */
183 int
184 fres_contract_to_string2(char *dest, size_t size, const struct fres_contract *c, int indent, uint32_t flags);
185
186 bool
187 fres_contract_get_deadline(const frsh_contract_t *contract,
188                            frsh_rel_time_t       *deadline);
189 bool
190 fres_contract_get_budget(const frsh_contract_t *contract,
191                          frsh_rel_time_t       *budget);
192 bool
193 fres_contract_get_period(const frsh_contract_t *contract,
194                          frsh_rel_time_t       *period);
195 frsh_contract_type_t
196 fres_contract_get_type(const frsh_contract_t *contract);
197
198 void
199 fres_contract_print(char *prefix, const struct fres_contract *c);
200
201 static inline int
202 fres_contract_get_num_blocks(const struct fres_contract *c)
203 {
204         return fres_container_get_num_blocks(c->container);
205 }
206
207 static inline int
208 fres_contract_merge(struct fres_contract *dest,
209                      const struct fres_contract *src)
210 {
211         return fres_container_merge(dest->container, src->container);
212 }
213
214
215 /**
216  * Macro which defines type-safe contract "accessor" functions for
217  * various blocks.
218  *
219  * This macro declares the following inline functions:
220  * - fres_contract_add_<type>
221  * - fres_contract_get_<type>
222  * - fres_contract_del_<type>
223  *
224  * The defined functions simply use the container "accessor" functions
225  * (usually defined) by #FRES_CONTAINER_ACCESSOR and are equivalent to
226  * fres_contract_add_block(), fres_contract_del_block() and
227  * fres_contract_get_block() with appropriate parameters.
228  */
229 #define FRES_CONTRACT_ACCESSOR(type)                                    \
230         static inline int                                               \
231         fres_contract_add_##type(struct fres_contract *contract,        \
232                                  fres_block_##type *block)              \
233         {                                                               \
234                 return fres_container_add_##type(contract->container,   \
235                                                  block);                \
236         }                                                               \
237         static inline fres_block_##type *                               \
238         fres_contract_get_##type(const struct fres_contract *contract)  \
239         {                                                               \
240                 return fres_container_get_##type(contract->container);  \
241         }                                                               \
242         static inline void                                              \
243         fres_contract_del_##type(struct fres_contract *contract)        \
244         {                                                               \
245                 fres_container_del_##type(contract->container);         \
246         }
247
248 FRES_CONTRACT_ACCESSOR(label)
249 FRES_CONTRACT_ACCESSOR(resource)
250 FRES_CONTRACT_ACCESSOR(basic)
251 FRES_CONTRACT_ACCESSOR(timing_reqs)
252 FRES_CONTRACT_ACCESSOR(csects)
253 FRES_CONTRACT_ACCESSOR(spare_capacity)
254 FRES_CONTRACT_ACCESSOR(power_management)
255
256 #ifdef __cplusplus
257 } /* extern "C"*/
258 #endif
259
260 #endif