Skip to content

Commit 7746d28

Browse files
author
张世争
authored
优化bsp/simulator自动初始化 (#5634)
1 parent fb61c79 commit 7746d28

File tree

2 files changed

+96
-74
lines changed

2 files changed

+96
-74
lines changed

include/rtdef.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ typedef int (*init_fn_t)(void);
214214
const char* fn_name;
215215
};
216216
#define INIT_EXPORT(fn, level) \
217-
const char __rti_level_##fn[] = level"__rt_init_"#fn; \
217+
const char __rti_level_##fn[] = ".rti_fn." level; \
218218
const char __rti_##fn##_name[] = #fn; \
219219
__declspec(allocate("rti_fn$f")) \
220220
RT_USED const struct rt_init_desc __rt_init_msc_##fn = \
@@ -226,7 +226,7 @@ typedef int (*init_fn_t)(void);
226226
const init_fn_t fn;
227227
};
228228
#define INIT_EXPORT(fn, level) \
229-
const char __rti_level_##fn[] = level"__rt_init_"#fn; \
229+
const char __rti_level_##fn[] = ".rti_fn." level; \
230230
__declspec(allocate("rti_fn$f")) \
231231
RT_USED const struct rt_init_desc __rt_init_msc_##fn = \
232232
{__rti_level_##fn, fn };

libcpu/sim/win32/startup.c

Lines changed: 94 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -69,127 +69,149 @@ static int rti_board_end(void)
6969
{
7070
return 0;
7171
}
72-
INIT_EXPORT(rti_board_end, "1_end");
72+
INIT_EXPORT(rti_board_end, "1.end");
7373

7474
static int rti_end(void)
7575
{
7676
return 0;
7777
}
78-
INIT_EXPORT(rti_end, "6_end");
78+
INIT_EXPORT(rti_end, "6.end");
7979

80-
/**
81-
* Find next init function
82-
*/
83-
static const struct rt_init_desc* rt_init_find_next(const char* lv,
84-
unsigned int* begin, unsigned int* end)
80+
struct rt_init_tag
8581
{
86-
const struct rt_init_desc* ptr;
87-
const struct rt_init_desc* ret_ptr = RT_NULL;
82+
const char *level;
83+
init_fn_t fn;
84+
#if RT_DEBUG_INIT
85+
const char *fn_name;
86+
#endif
87+
};
88+
89+
static rt_size_t rt_init_num = 0;
90+
static struct rt_init_tag rt_init_table[2048] = { 0 };
91+
static rt_bool_t rt_init_flag = RT_FALSE;
92+
93+
static int rt_init_objects_sort(void)
94+
{
95+
rt_size_t index_i, index_j;
96+
struct rt_init_tag init_temp = { 0 };
97+
unsigned int *ptr_begin = (unsigned int *)&__rti_fn_begin;
98+
unsigned int *ptr_end = (unsigned int *)&__rti_fn_end;
99+
struct rt_init_tag *table = rt_init_table;
100+
ptr_begin += (sizeof(struct rt_init_desc) / sizeof(unsigned int));
101+
102+
if (rt_init_flag)
103+
return rt_init_num;
104+
105+
while (*ptr_begin == 0)
106+
ptr_begin++;
88107

89-
while (begin < end)
108+
do (ptr_end--);
109+
while (*ptr_end == 0);
110+
111+
while (ptr_begin < ptr_end)
90112
{
91-
if (*begin != 0)
113+
if (*ptr_begin != 0)
92114
{
93-
ptr = (const struct rt_init_desc*)begin;
94-
if (ret_ptr != RT_NULL)
95-
{
96-
if (rt_strcmp(lv, ptr->level) < 0 &&
97-
rt_strcmp(ret_ptr->level, ptr->level) > 0)
98-
{
99-
ret_ptr = ptr;
100-
}
101-
}
102-
else
103-
{
104-
if (rt_strcmp(lv, ptr->level) < 0)
105-
{
106-
ret_ptr = ptr;
107-
}
108-
}
109-
begin += (sizeof(struct rt_init_desc) / sizeof(unsigned int));
115+
table->level = ((struct rt_init_desc *)ptr_begin)->level;
116+
table->fn = ((struct rt_init_desc *)ptr_begin)->fn;
117+
#if RT_DEBUG_INIT
118+
table->fn_name = ((struct rt_init_desc *)ptr_begin)->fn_name;
119+
#endif
120+
ptr_begin += sizeof(struct rt_init_desc) / sizeof(unsigned int);
121+
table++;
122+
rt_init_num += 1;
110123
}
111124
else
112125
{
113-
begin++;
126+
ptr_begin++;
114127
}
115128
}
116-
return ret_ptr;
129+
130+
if (rt_init_num == 0) /* no need sort */
131+
return rt_init_num;
132+
133+
/* bubble sort algorithms */
134+
for (index_i = 0; index_i < (rt_init_num - 1); index_i++)
135+
{
136+
for (index_j = 0; index_j < ((rt_init_num - 1) - index_i); index_j++)
137+
{
138+
if (rt_strcmp(rt_init_table[index_j].level, rt_init_table[index_j + 1].level) > 0)
139+
{
140+
init_temp = rt_init_table[index_j];
141+
rt_init_table[index_j] = rt_init_table[index_j + 1];
142+
rt_init_table[index_j + 1] = init_temp;
143+
}
144+
}
145+
}
146+
147+
rt_init_flag = RT_TRUE;
148+
return rt_init_num;
117149
}
118150

119151
/**
120152
* RT-Thread Components Initialization for board
121153
*/
122154
void rt_components_board_init(void)
123155
{
124-
const struct rt_init_desc* ptr;
125-
const char* lv_start = "0__rt_init_rti_start";
126-
const char* lv_end = "1_end__rt_init_rti_board_end";
127-
unsigned int* ptr_begin = (unsigned int*)&__rti_fn_begin;
128-
unsigned int* ptr_end = (unsigned int*)&__rti_fn_end;
156+
const char* lv_start = ".rti_fn.0";
157+
const char* lv_end = ".rti_fn.1.end";
158+
rt_size_t index_i;
129159
int result;
130160

131-
ptr_begin += (sizeof(struct rt_init_desc) / sizeof(unsigned int));
132-
while (*ptr_begin == 0) ptr_begin++;
133-
do ptr_end--; while (*ptr_end == 0);
161+
rt_init_objects_sort();
134162

135-
while (1)
163+
for (index_i = 0; index_i < rt_init_num; index_i++)
136164
{
137-
ptr = rt_init_find_next(lv_start, ptr_begin, ptr_end);
138-
if (ptr == RT_NULL ||
139-
rt_strcmp(ptr->level, lv_end) >= 0)
140-
{
141-
break;
142-
}
143-
if (ptr->fn)
165+
if (rt_init_table[index_i].fn)
144166
{
167+
if (rt_strcmp(rt_init_table[index_i].level, lv_end) >= 0)
168+
{
169+
break;
170+
}
145171
#if RT_DEBUG_INIT
146-
rt_kprintf("initialize %s", ptr->fn_name);
147-
result = ptr->fn();
172+
rt_kprintf("initialize %s", rt_init_table[index_i].fn_name);
173+
result = rt_init_table[index_i].fn();
148174
rt_kprintf(":%d done\n", result);
149175
#else
150-
result = ptr->fn();
176+
result = rt_init_table[index_i].fn();
151177
#endif
152178
}
153-
lv_start = ptr->level;
154-
};
179+
}
155180
}
156181

157182
/**
158183
* RT-Thread Components Initialization
159184
*/
160185
void rt_components_init(void)
161186
{
162-
const struct rt_init_desc* ptr;
163-
const char* lv_start = "1_end__rt_init_rti_board_end";
164-
const char* lv_end = "6_end__rt_init_rti_end";
165-
unsigned int* ptr_begin = (unsigned int*)&__rti_fn_begin;
166-
unsigned int* ptr_end = (unsigned int*)&__rti_fn_end;
187+
const char* lv_start = ".rti_fn.1.end";
188+
const char* lv_end = ".rti_fn.6.end";
167189
int result;
190+
rt_size_t index_i;
168191

169-
ptr_begin += (sizeof(struct rt_init_desc) / sizeof(unsigned int));
170-
while (*ptr_begin == 0) ptr_begin++;
171-
do ptr_end--; while (*ptr_end == 0);
192+
rt_init_objects_sort();
172193

173-
while (1)
194+
for (index_i = 0; index_i < rt_init_num; index_i++)
174195
{
175-
ptr = rt_init_find_next(lv_start, ptr_begin, ptr_end);
176-
if (ptr == RT_NULL ||
177-
rt_strcmp(ptr->level, lv_end) >= 0)
178-
{
179-
break;
180-
}
181-
if (ptr->fn)
196+
if (rt_init_table[index_i].fn)
182197
{
198+
if (rt_strcmp(rt_init_table[index_i].level, lv_start) <= 0)
199+
{
200+
continue;
201+
}
202+
if (rt_strcmp(rt_init_table[index_i].level, lv_end) >= 0)
203+
{
204+
break;
205+
}
183206
#if RT_DEBUG_INIT
184-
rt_kprintf("initialize %s", ptr->fn_name);
185-
result = ptr->fn();
207+
rt_kprintf("initialize %s", rt_init_table[index_i].fn_name);
208+
result = rt_init_table[index_i].fn();
186209
rt_kprintf(":%d done\n", result);
187210
#else
188-
result = ptr->fn();
211+
result = rt_init_table[index_i].fn();
189212
#endif
190213
}
191-
lv_start = ptr->level;
192-
};
214+
}
193215
}
194216
#endif /* RT_USING_COMPONENTS_INIT */
195217

0 commit comments

Comments
 (0)