FW Profile - C1 Implementation
FwPrDCreate.c
Go to the documentation of this file.
1 
18 #include "FwPrDCreate.h"
19 #include "FwPrPrivate.h"
20 #include <stdlib.h>
21 
22 /* ----------------------------------------------------------------------------------------------------------------- */
25 
28  FwPrDesc_t prDesc;
29 
30  if (nOfFlows < 2) {
31  return NULL;
32  }
33 
34  if (nOfANodes < 1) {
35  return NULL;
36  }
37 
38  if (nOfDNodes < 0) {
39  return NULL;
40  }
41 
42  if (nOfActions < 1) {
43  return NULL;
44  }
45 
46  if (nOfActions > nOfANodes) {
47  return NULL;
48  }
49 
50  if (nOfGuards < 0) {
51  return NULL;
52  }
53 
54  if (nOfGuards > nOfFlows) {
55  return NULL;
56  }
57 
58  prDesc = (FwPrDesc_t)malloc(sizeof(struct FwPrDesc));
59  if (prDesc == NULL) {
60  return NULL;
61  }
62 
63  prBase = (PrBaseDesc_t*)malloc(sizeof(PrBaseDesc_t));
64  if (prBase == NULL) {
65  return NULL;
66  }
67 
68  prBase->aNodes = (PrANode_t*)malloc(((FwPrCounterU4_t)(nOfANodes)) * sizeof(PrANode_t));
69  if (prBase->aNodes == NULL) {
70  return NULL;
71  }
72  for (i = 0; i < nOfANodes; i++) {
73  prBase->aNodes[i].iFlow = -1;
74  }
75 
76  if (nOfDNodes > 0) {
77  prBase->dNodes = (PrDNode_t*)malloc(((FwPrCounterU4_t)(nOfDNodes)) * sizeof(PrDNode_t));
78  if (prBase->dNodes == NULL) {
79  return NULL;
80  }
81  for (i = 0; i < nOfDNodes; i++) {
82  prBase->dNodes[i].outFlowIndex = -1;
83  }
84  }
85  else {
86  prBase->dNodes = NULL;
87  }
88 
89  prBase->flows = (PrFlow_t*)malloc(((FwPrCounterU4_t)(nOfFlows)) * sizeof(PrFlow_t));
90  if (prBase->flows == NULL) {
91  return NULL;
92  }
93  for (i = 0; i < nOfFlows; i++) {
94  prBase->flows[i].iGuard = -1;
95  }
96 
97  prDesc->prActions = (FwPrAction_t*)malloc(((FwPrCounterU4_t)(nOfActions)) * sizeof(FwPrAction_t));
98  if (prDesc->prActions == NULL) {
99  return NULL;
100  }
101  for (i = 0; i < nOfActions; i++) {
102  prDesc->prActions[i] = NULL;
103  }
104 
105  prDesc->prGuards = (FwPrGuard_t*)malloc(((FwPrCounterU4_t)(nOfGuards + 1)) * sizeof(FwPrGuard_t));
106  if (prDesc->prGuards == NULL) {
107  return NULL;
108  }
109 
110  for (i = 1; i <= nOfGuards; i++) {
111  prDesc->prGuards[i] = NULL;
112  }
113  prDesc->prGuards[0] = &PrDummyGuard;
114 
115  prBase->nOfANodes = nOfANodes;
116  prBase->nOfDNodes = nOfDNodes;
117  prBase->nOfFlows = nOfFlows;
118  prDesc->prBase = prBase;
119  prDesc->curNode = 0;
120  prDesc->prData = NULL;
121  prDesc->flowCnt = 1;
122  prDesc->nOfActions = nOfActions;
123  prDesc->nOfGuards = (FwPrCounterS1_t)(nOfGuards + 1);
124  prDesc->errCode = prSuccess;
125  prDesc->nodeExecCnt = 0;
126  prDesc->prExecCnt = 0;
127 
128  return prDesc;
129 }
130 
131 /* ----------------------------------------------------------------------------------------------------------------- */
133  FwPrCounterS1_t i;
134  PrBaseDesc_t* prBase = prDesc->prBase;
135  FwPrDesc_t extPrDesc;
136 
137  /* Create descriptor for derived SM */
138  extPrDesc = (FwPrDesc_t)malloc(sizeof(struct FwPrDesc));
139  if (extPrDesc == NULL) {
140  return NULL;
141  }
142 
143  /* Create array of actions in the derived SM */
144  extPrDesc->prActions = (FwPrAction_t*)malloc(((FwPrCounterU4_t)(prDesc->nOfActions)) * sizeof(FwPrAction_t));
145  if (extPrDesc->prActions == NULL) {
146  return NULL;
147  }
148  for (i = 0; i < prDesc->nOfActions; i++) {
149  extPrDesc->prActions[i] = prDesc->prActions[i];
150  }
151 
152  /* Create array of guards in the derived SM (NB: number of guards is guaranteed to be greater than 0 */
153  extPrDesc->prGuards = (FwPrGuard_t*)malloc(((FwPrCounterU4_t)(prDesc->nOfGuards)) * sizeof(FwPrGuard_t));
154  if (extPrDesc->prGuards == NULL) {
155  return NULL;
156  }
157  for (i = 0; i < prDesc->nOfGuards; i++) {
158  extPrDesc->prGuards[i] = prDesc->prGuards[i];
159  }
160 
161  extPrDesc->prBase = prBase;
162  extPrDesc->curNode = 0;
163  extPrDesc->prData = NULL;
164  extPrDesc->flowCnt = 0;
165  extPrDesc->nOfActions = prDesc->nOfActions;
166  extPrDesc->nOfGuards = prDesc->nOfGuards;
167  extPrDesc->errCode = prDesc->errCode;
168  extPrDesc->nodeExecCnt = 0;
169  extPrDesc->prExecCnt = 0;
170 
171  return extPrDesc;
172 }
173 
174 /* ----------------------------------------------------------------------------------------------------------------- */
175 void FwPrRelease(FwPrDesc_t prDesc) {
177 
178  /* Release memory allocated to base descriptor (base descriptor is always allocated and
179  * hence it is not necessary to check whether it is non-NULL) */
180  prBase = prDesc->prBase;
181 
182  /* Release memory allocated to action node array (this is guaranteed to have at least one entry
183  * since procedure are constrained to have at least one action node) */
184  free(prBase->aNodes);
185 
186  /* Release memory allocated to decision node array (this may be empty) */
187  if (prBase->dNodes != NULL) {
188  free(prBase->dNodes);
189  }
190 
191  /* Release pointer to transition array (note that the transition array is guaranteed to exist and to
192  * have at least one element, see operation FwPrCreate) */
193  free(prBase->flows);
194 
195  /* Release memory allocated to base descriptor */
196  free(prDesc->prBase);
197 
198  /* Release memory allocated to extension part of the state machine descriptor */
199  FwPrReleaseDer(prDesc);
200 
201  return;
202 }
203 
204 /* ----------------------------------------------------------------------------------------------------------------- */
206 
207  /* Release pointer to the action and guard arrays (note that both arrays are guaranteed to
208  * have non-zero length) */
209  free(prDesc->prActions);
210  free(prDesc->prGuards);
211 
212  /* Release pointer to state machine descriptor */
213  free(prDesc);
214  prDesc = NULL;
215 
216  return;
217 }
Structure representing a decision node in a procedure.
Definition: FwPrPrivate.h:121
Structure representing an action node in a procedure.
Definition: FwPrPrivate.h:98
FwPrCounterS1_t nOfGuards
the number of guards in the procedure
Definition: FwPrPrivate.h:264
Structure representing the base descriptor of a procedure.
Definition: FwPrPrivate.h:186
void * prData
the pointer to the data manipulated by the procedure actions and guards
Definition: FwPrPrivate.h:276
FwPrCounterS1_t nOfActions
the number of actions in the procedure
Definition: FwPrPrivate.h:262
FwPrCounterS1_t nOfANodes
the number of action nodes in the procedure
Definition: FwPrPrivate.h:194
PrFlow_t * flows
array holding the control flows in the procedure
Definition: FwPrPrivate.h:192
FwPrCounterS1_t nOfFlows
the number of control flows in the procedure (excluding control flow from initial node) ...
Definition: FwPrPrivate.h:198
PrBaseDesc_t * prBase
pointer to the base descriptor
Definition: FwPrPrivate.h:256
PrANode_t * aNodes
array holding the action nodes in the procedure
Definition: FwPrPrivate.h:188
FwPrCounterS1_t flowCnt
the counter for the number of control flows added to the procedure
Definition: FwPrPrivate.h:266
PrDNode_t * dNodes
array holding the decision nodes in the procedure
Definition: FwPrPrivate.h:190
FwPrCounterS1_t iGuard
the index of the guard associated to the control flow
Definition: FwPrPrivate.h:156
void FwPrRelease(FwPrDesc_t prDesc)
Release the memory which was allocated when the procedure descriptor was created. ...
Definition: FwPrDCreate.c:175
FwPrBool_t PrDummyGuard(FwPrDesc_t prDesc)
Dummy guard which always returns true.
Definition: FwPrCore.c:23
FwPrDesc_t FwPrCreate(FwPrCounterS1_t nOfANodes, FwPrCounterS1_t nOfDNodes, FwPrCounterS1_t nOfFlows, FwPrCounterS1_t nOfActions, FwPrCounterS1_t nOfGuards)
Create a new procedure descriptor.
Definition: FwPrDCreate.c:23
FwPrDesc_t FwPrCreateDer(FwPrDesc_t prDesc)
Create the descriptor of a derived procedure.
Definition: FwPrDCreate.c:132
void(* FwPrAction_t)(FwPrDesc_t)
Type for a pointer to a procedure action.
Definition: FwPrConstants.h:45
Structure representing a control flow.
Definition: FwPrPrivate.h:152
FwPrCounterU3_t nodeExecCnt
the node execution counter
Definition: FwPrPrivate.h:274
void FwPrReleaseDer(FwPrDesc_t prDesc)
Release the memory allocated to a derived procedure descriptor.
Definition: FwPrDCreate.c:205
FwPrErrCode_t errCode
either &#39;success&#39; or the code of the last error encountered by the procedure
Definition: FwPrPrivate.h:270
long unsigned int FwPrCounterU4_t
Type used for unsigned counters with a "long int" range.
Definition: FwPrConstants.h:75
FwPrCounterS1_t outFlowIndex
index of first out-going control flow in control flow array
Definition: FwPrPrivate.h:123
Return codes of a function which has completed execution without errors.
Definition: FwPrConstants.h:85
signed char FwPrCounterS1_t
Type used for signed counters with a "short" range.
Definition: FwPrConstants.h:78
Structure representing a procedure descriptor.
Definition: FwPrPrivate.h:254
FwPrAction_t * prActions
the procedure actions
Definition: FwPrPrivate.h:258
Declaration of the dynamical creation interface for a FW Procedure.
Declaration of the internal data structures of the FW Procedure Module.
struct FwPrDesc * FwPrDesc_t
Forward declaration for the pointer to a procedure descriptor.
Definition: FwPrConstants.h:32
FwPrCounterS1_t iFlow
index of out-going control flows
Definition: FwPrPrivate.h:100
FwPrCounterU3_t prExecCnt
the procedure execution counter
Definition: FwPrPrivate.h:272
FwPrGuard_t * prGuards
the control flow guards in the procedure
Definition: FwPrPrivate.h:260
FwPrCounterS1_t nOfDNodes
the number of decision nodes in the procedure
Definition: FwPrPrivate.h:196
FwPrCounterS1_t curNode
the current node of the procedure
Definition: FwPrPrivate.h:268
FwPrBool_t(* FwPrGuard_t)(FwPrDesc_t)
Type for a pointer to a procedure guard.
Definition: FwPrConstants.h:63
P&P Software GmbH, Copyright 2011, All Rights Reserved