]> begriffs open source - cmsis-freertos/blob - Demo/RISC-V_RV32_SiFive_HiFive1-RevB_FreedomStudio/freedom-metal/src/init.c
Updated pack to FreeRTOS 10.4.4
[cmsis-freertos] / Demo / RISC-V_RV32_SiFive_HiFive1-RevB_FreedomStudio / freedom-metal / src / init.c
1 /* Copyright 2019 SiFive Inc. */
2 /* SPDX-License-Identifier: Apache-2.0 */
3
4 #include <metal/init.h>
5
6 /*
7  * These function pointers are created by the linker script
8  * in the .init_array section. The arrays defined by these
9  * and end points are the set of functions defined by instances
10  * of METAL_CONSTRUCTOR() and METAL_DESTRUCTOR().
11  */
12 extern metal_constructor_t metal_constructors_start;
13 extern metal_constructor_t metal_constructors_end;
14 extern metal_destructor_t metal_destructors_start;
15 extern metal_destructor_t metal_destructors_end;
16
17 void metal_init(void) {
18     /* Make sure the constructors only run once */
19     static int init_done = 0;
20     if (init_done) {
21         return;
22     }
23     init_done = 1;
24
25     if (&metal_constructors_end <= &metal_constructors_start) {
26         return;
27     }
28
29     metal_constructor_t *funcptr = &metal_constructors_start;
30     while (funcptr != &metal_constructors_end) {
31         metal_constructor_t func = *funcptr;
32
33         func();
34
35         funcptr += 1;
36     }
37 }
38
39 void metal_fini(void) {
40     /* Make sure the destructors only run once */
41     static int fini_done = 0;
42     if (fini_done) {
43         return;
44     }
45     fini_done = 1;
46
47     if (&metal_destructors_end <= &metal_destructors_start) {
48         return;
49     }
50
51     metal_destructor_t *funcptr = &metal_destructors_start;
52     while (funcptr != &metal_destructors_end) {
53         metal_destructor_t func = *funcptr;
54
55         func();
56
57         funcptr += 1;
58     }
59 }
60
61 /*
62  * metal_init_run() and metal_fini_run() are marked weak so that users
63  * can redefine them for their own purposes, including to no-ops
64  * in the case that users don't want the metal constructors or
65  * destructors to run.
66  */
67
68 void metal_init_run(void) __attribute__((weak));
69 void metal_init_run(void) { metal_init(); }
70
71 void metal_fini_run(void) __attribute__((weak));
72 void metal_fini_run(void) { metal_fini(); }