MCPcopy Create free account
hub / github.com/F-Stack/f-stack / devstat_alloc

Function devstat_alloc

freebsd/kern/subr_devstat.c:514–577  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

512}
513
514static struct devstat *
515devstat_alloc(void)
516{
517 struct devstat *dsp;
518 struct statspage *spp, *spp2;
519 u_int u;
520 static int once;
521
522 mtx_assert(&devstat_mutex, MA_NOTOWNED);
523 if (!once) {
524 make_dev_credf(MAKEDEV_ETERNAL | MAKEDEV_CHECKNAME,
525 &devstat_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0444,
526 DEVSTAT_DEVICE_NAME);
527 once = 1;
528 }
529 spp2 = NULL;
530 mtx_lock(&devstat_mutex);
531 for (;;) {
532 TAILQ_FOREACH(spp, &pagelist, list) {
533 if (spp->nfree > 0)
534 break;
535 }
536 if (spp != NULL)
537 break;
538 mtx_unlock(&devstat_mutex);
539 spp2 = malloc(sizeof *spp, M_DEVSTAT, M_ZERO | M_WAITOK);
540 spp2->stat = malloc(PAGE_SIZE, M_DEVSTAT, M_ZERO | M_WAITOK);
541 spp2->nfree = statsperpage;
542
543 /*
544 * If free statspages were added while the lock was released
545 * just reuse them.
546 */
547 mtx_lock(&devstat_mutex);
548 TAILQ_FOREACH(spp, &pagelist, list)
549 if (spp->nfree > 0)
550 break;
551 if (spp == NULL) {
552 spp = spp2;
553
554 /*
555 * It would make more sense to add the new page at the
556 * head but the order on the list determine the
557 * sequence of the mapping so we can't do that.
558 */
559 TAILQ_INSERT_TAIL(&pagelist, spp, list);
560 } else
561 break;
562 }
563 dsp = spp->stat;
564 for (u = 0; u < statsperpage; u++) {
565 if (dsp->allocated == 0)
566 break;
567 dsp++;
568 }
569 spp->nfree--;
570 dsp->allocated = 1;
571 mtx_unlock(&devstat_mutex);

Callers 1

devstat_new_entryFunction · 0.85

Calls 6

mtx_assertFunction · 0.85
make_dev_credfFunction · 0.85
mallocFunction · 0.85
mtx_lockFunction · 0.70
mtx_unlockFunction · 0.70
freeFunction · 0.70

Tested by

no test coverage detected