- 论坛徽章:
- 2
|
本帖最后由 gvim 于 2014-03-19 00:13 编辑
看不少朋友惊艳于linux的list,怎么说呢,算买个广告吧,看看freebsd的tree
http://fxr.watson.org/fxr/source/sys/tree.h?v=FREEBSD10
/* This file defines data structures for different types of trees:
* splay trees and red-black trees.
...
*/
能明白说什么的,我就不多解释了。
从编码角度说,*BSD比Linux的实现高超不少。包括有朋友提到的初始化,总线抽象这些东西,*BSD在1995年左右就已经设计出来,我记得那时候linux应该是1.x->2.0左右吧还处于设计的蛮荒阶段。当然,本帖说的是从内核里学什么,就只比较设计、编码这些,要比功能,现在的*BSD确实差了不少。
以下两个 for,连同调试、打印一共100多行,就是freebsd的初始化代码,抽象程度之高。
同时比对一下:
http://fxr.watson.org/fxr/source ... REEBSD10;im=10#L198
http://fxr.watson.org/fxr/source ... inux-2.6;im=10#L468- 197 void
- 198 mi_startup(void)
- 199 {
- 200
- 201 register struct sysinit **sipp; /* system initialization*/
- 202 register struct sysinit **xipp; /* interior loop of sort*/
- 203 register struct sysinit *save; /* bubble*/
- 204
- 205 #if defined(VERBOSE_SYSINIT)
- 206 int last;
- 207 int verbose;
- 208 #endif
- 209
- 210 if (boothowto & RB_VERBOSE)
- 211 bootverbose++;
- 212
- 213 if (sysinit == NULL) {
- 214 sysinit = SET_BEGIN(sysinit_set);
- 215 sysinit_end = SET_LIMIT(sysinit_set);
- 216 }
- 217
- 218 restart:
- 219 /*
- 220 * Perform a bubble sort of the system initialization objects by
- 221 * their subsystem (primary key) and order (secondary key).
- 222 */
- 223 for (sipp = sysinit; sipp < sysinit_end; sipp++) {
- 224 for (xipp = sipp + 1; xipp < sysinit_end; xipp++) {
- 225 if ((*sipp)->subsystem < (*xipp)->subsystem ||
- 226 ((*sipp)->subsystem == (*xipp)->subsystem &&
- 227 (*sipp)->order <= (*xipp)->order))
- 228 continue; /* skip*/
- 229 save = *sipp;
- 230 *sipp = *xipp;
- 231 *xipp = save;
- 232 }
- 233 }
- 234
- 235 #if defined(VERBOSE_SYSINIT)
- 236 last = SI_SUB_COPYRIGHT;
- 237 verbose = 0;
- 238 #if !defined(DDB)
- 239 printf("VERBOSE_SYSINIT: DDB not enabled, symbol lookups disabled.\n");
- 240 #endif
- 241 #endif
- 242
- 243 /*
- 244 * Traverse the (now) ordered list of system initialization tasks.
- 245 * Perform each task, and continue on to the next task.
- 246 */
- 247 for (sipp = sysinit; sipp < sysinit_end; sipp++) {
- 248
- 249 if ((*sipp)->subsystem == SI_SUB_DUMMY)
- 250 continue; /* skip dummy task(s)*/
- 251
- 252 if ((*sipp)->subsystem == SI_SUB_DONE)
- 253 continue;
- 254
- 255 #if defined(VERBOSE_SYSINIT)
- 256 if ((*sipp)->subsystem > last) {
- 257 verbose = 1;
- 258 last = (*sipp)->subsystem;
- 259 printf("subsystem %x\n", last);
- 260 }
- 261 if (verbose) {
- 262 #if defined(DDB)
- 263 const char *func, *data;
- 264
- 265 func = symbol_name((vm_offset_t)(*sipp)->func,
- 266 DB_STGY_PROC);
- 267 data = symbol_name((vm_offset_t)(*sipp)->udata,
- 268 DB_STGY_ANY);
- 269 if (func != NULL && data != NULL)
- 270 printf(" %s(&%s)... ", func, data);
- 271 else if (func != NULL)
- 272 printf(" %s(%p)... ", func, (*sipp)->udata);
- 273 else
- 274 #endif
- 275 printf(" %p(%p)... ", (*sipp)->func,
- 276 (*sipp)->udata);
- 277 }
- 278 #endif
- 279
- 280 /* Call function */
- 281 (*((*sipp)->func))((*sipp)->udata);
- 282
- 283 #if defined(VERBOSE_SYSINIT)
- 284 if (verbose)
- 285 printf("done.\n");
- 286 #endif
- 287
- 288 /* Check off the one we're just done */
- 289 (*sipp)->subsystem = SI_SUB_DONE;
- 290
- 291 /* Check if we've installed more sysinit items via KLD */
- 292 if (newsysinit != NULL) {
- 293 if (sysinit != SET_BEGIN(sysinit_set))
- 294 free(sysinit, M_TEMP);
- 295 sysinit = newsysinit;
- 296 sysinit_end = newsysinit_end;
- 297 newsysinit = NULL;
- 298 newsysinit_end = NULL;
- 299 goto restart;
- 300 }
- 301 }
- 302
- 303 mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED);
- 304 mtx_unlock(&Giant);
- 305
- 306 /*
- 307 * Now hand over this thread to swapper.
- 308 */
- 309 swapper();
- 310 /* NOTREACHED*/
- 311 }
复制代码 |
评分
-
查看全部评分
|