aboutsummaryrefslogtreecommitdiffstats
path: root/src/framework/init/fw_module.c
blob: 1f22ecbba7fcd48fa8acd3f29ab4655fdc7c695f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "nstack_securec.h"
#include "fw_module.h"

#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C"{
/* *INDENT-ON* */
#endif /* __cplusplus */

COMPAT_PROTECT (NSFW_MODULE_INSTANCE_POOL_SIZE, 64);
COMPAT_PROTECT (NSFW_MODULE_DEPENDS_POOL_SIZE, 128);

nsfw_module_instance_pool_t g_nsfw_module_inst_pool;
nsfw_module_depends_pool_t g_nsfw_module_deps_pool;

/*
* WARNING!!!:
*    This function is only used in constructor progress. Multi-thread concurrent is not supported!
*/
NSTACK_STATIC nsfw_module_instance_t *
nsfw_module_malloc_instance ()
{
  if (g_nsfw_module_inst_pool.last_idx >= NSFW_MODULE_INSTANCE_POOL_SIZE)
    {
      return NULL;
    }
  return
    &g_nsfw_module_inst_pool.module_instance_pool[g_nsfw_module_inst_pool.
                                                  last_idx++];
}

/*
* WARNING!!!:
*    This function is only used in constructor progress. Multi-thread concurrent is not supported!
*/
NSTACK_STATIC nsfw_module_depends_t *
nsfw_module_malloc_depends ()
{
  if (g_nsfw_module_deps_pool.last_idx >= NSFW_MODULE_DEPENDS_POOL_SIZE)
    {
      return NULL;
    }
  return
    &g_nsfw_module_deps_pool.module_depends_pool[g_nsfw_module_deps_pool.
                                                 last_idx++];
}

NSTACK_STATIC void
nsfw_module_setChildInstance (nsfw_module_instance_t *
                              father, nsfw_module_instance_t * child)
{
  if (NULL == father || NULL == child)
    return;
  child->father = father;
}

nsfw_module_depends_t *
nsfw_module_create_depends (char *name)
{
  if (NULL == name)
    return NULL;

  /*Change module malloc to pool array */
  nsfw_module_depends_t *dep = nsfw_module_malloc_depends ();

  if (NULL == dep)
    return NULL;

  if (EOK !=
      MEMSET_S (dep, sizeof (nsfw_module_depends_t), 0,
                sizeof (nsfw_module_depends_t)))
    {
      goto fail;
    }

  /*change destMax from nameSize - 1 to sizeof(dep->name) */
  if (EOK != STRCPY_S (dep->name, sizeof (dep->name), name))
    {
      goto fail;
    }
  dep->isReady = 0;

  return dep;

fail:
  // NOTE: if dep is not null, we do not free it and just leave it there.
  return NULL;
}

nsfw_module_instance_t *
nsfw_module_create_instance (void)
{
  /*Change module malloc to pool array */
  nsfw_module_instance_t *inst = nsfw_module_malloc_instance ();
  if (NULL == inst)
    return NULL;

  if (EOK !=
      MEMSET_S (inst, sizeof (nsfw_module_instance_t), 0,
                sizeof (nsfw_module_instance_t)))
    {
      // NOTE: if inst is not null, we do not free it and just leave it there.
      return NULL;
    }

  inst->stat = NSFW_INST_STAT_CHECKING;
  return inst;
}

void
nsfw_module_set_instance_name (nsfw_module_instance_t * inst, char *name)
{
  if (NULL == inst || NULL == name || inst->name[0] != '\0')
    {
      return;
    }

  /*change destMax from nameSize - 1 to sizeof(inst->name) */
  if (EOK != STRCPY_S (inst->name, sizeof (inst->name), name))
    {
      return;
    }

  // Now we need to search if it's any instance's father
  nsfw_module_instance_t *curInst = nsfw_module_getManager ()->inst;
  while (curInst)
    {
      if (curInst == inst)
        goto next_loop;

      if (0 == strcmp (curInst->fatherName, inst->name)
          && NULL == curInst->father)
        {
          nsfw_module_setChildInstance (inst, curInst);
        }
    next_loop:
      curInst = curInst->next;
    }
  return;
}

void
nsfw_module_set_instance_father (nsfw_module_instance_t * inst,
                                 char *fatherName)
{
  if (NULL == inst || NULL == fatherName)
    {
      return;
    }

  if (EOK !=
      STRCPY_S (inst->fatherName, sizeof (inst->fatherName), fatherName))
    {
      return;
    }

  nsfw_module_instance_t *fatherInst =
    nsfw_module_getModuleByName (fatherName);
  if (fatherInst)
    {
      nsfw_module_setChildInstance (fatherInst, inst);
    }
  return;
}

void
nsfw_module_set_instance_priority (nsfw_module_instance_t * inst,
                                   int priority)
{
  if (NULL == inst)
    return;
  inst->priority = priority;

  nsfw_module_del_instance (inst);
  nsfw_module_add_instance (inst);
  return;
}

void
nsfw_module_set_instance_initfn (nsfw_module_instance_t * inst,
                                 nsfw_module_init_fn fn)
{
  if (NULL == inst)
    return;
  inst->fnInit = fn;
  return;
}

void
nsfw_module_set_instance_depends (nsfw_module_instance_t * inst, char *name)
{
  if (NULL == inst || name == NULL)
    return;

  // Check if depends already set
  nsfw_module_depends_t *dep = inst->depends;
  while (dep)
    {
      if (0 == strcmp (dep->name, name))
        return;
      dep = dep->next;
    }

  dep = nsfw_module_create_depends (name);
  if (NULL == dep)
    return;

  if (NULL == inst->depends)
    inst->depends = dep;
  else
    inst->depends->next = dep;
}

/* *INDENT-OFF* */
nsfw_module_manager_t g_nsfw_module_manager = {.inst = NULL, .initMutex =
    PTHREAD_MUTEX_INITIALIZER, .done = 0};
/* *INDENT-ON* */

void
nsfw_module_add_instance (nsfw_module_instance_t * inst)
{
  nsfw_module_instance_t *curInst = nsfw_module_getManager ()->inst;

  if (NULL == curInst || curInst->priority > inst->priority)
    {
      inst->next = curInst;
      nsfw_module_getManager ()->inst = inst;
    }
  else
    {
      while (curInst->next && curInst->next->priority <= inst->priority)
        {
          curInst = curInst->next;
        }
      inst->next = curInst->next;
      curInst->next = inst;
    }
}

void
nsfw_module_del_instance (nsfw_module_instance_t * inst)
{
  if (nsfw_module_getManager ()->inst == inst)
    {
      nsfw_module_getManager ()->inst = inst->next;
      return;
    }

  nsfw_module_instance_t *curInst = nsfw_module_getManager ()->inst;

  while (curInst->next && curInst->next != inst)
    curInst = curInst->next;

  if (!curInst->next)
    return;
  curInst->next = inst->next;
}

nsfw_module_instance_t *
nsfw_module_getModuleByName (char *name)
{
  if (NULL == name)
    return NULL;

  nsfw_module_instance_t *curInst = nsfw_module_getManager ()->inst;
  while (curInst)
    {
      if (0 == strcmp (curInst->name, name))
        {
          return curInst;
        }
      curInst = curInst->next;
    }

  return NULL;
}

int
nsfw_module_addDoneNode (nsfw_module_instance_t * inst)
{
  nsfw_module_doneNode_t *node =
    (nsfw_module_doneNode_t *) malloc (sizeof (nsfw_module_doneNode_t));
  if (NULL == node)
    return -1;
  node->inst = inst;
  node->next = NULL;

  nsfw_module_manager_t *manager = nsfw_module_getManager ();
  if (NULL == manager->doneHead)
    {
      manager->doneHead = node;
    }
  else
    {
      nsfw_module_doneNode_t *tail = manager->doneHead;
      while (tail->next)
        {
          tail = tail->next;
        }

      tail->next = node;
    }

  return 0;
}

#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif /* __cplusplus */