| 498 | |
| 499 | #ifdef MALLOC_DEBUG |
| 500 | static int |
| 501 | malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_type *mtp, |
| 502 | int flags) |
| 503 | { |
| 504 | #ifdef INVARIANTS |
| 505 | int indx; |
| 506 | |
| 507 | KASSERT(mtp->ks_version == M_VERSION, ("malloc: bad malloc type version")); |
| 508 | /* |
| 509 | * Check that exactly one of M_WAITOK or M_NOWAIT is specified. |
| 510 | */ |
| 511 | indx = flags & (M_WAITOK | M_NOWAIT); |
| 512 | if (indx != M_NOWAIT && indx != M_WAITOK) { |
| 513 | static struct timeval lasterr; |
| 514 | static int curerr, once; |
| 515 | if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { |
| 516 | printf("Bad malloc flags: %x\n", indx); |
| 517 | kdb_backtrace(); |
| 518 | flags |= M_WAITOK; |
| 519 | once++; |
| 520 | } |
| 521 | } |
| 522 | #endif |
| 523 | #ifdef MALLOC_MAKE_FAILURES |
| 524 | if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { |
| 525 | atomic_add_int(&malloc_nowait_count, 1); |
| 526 | if ((malloc_nowait_count % malloc_failure_rate) == 0) { |
| 527 | atomic_add_int(&malloc_failure_count, 1); |
| 528 | *vap = NULL; |
| 529 | return (EJUSTRETURN); |
| 530 | } |
| 531 | } |
| 532 | #endif |
| 533 | if (flags & M_WAITOK) { |
| 534 | KASSERT(curthread->td_intr_nesting_level == 0, |
| 535 | ("malloc(M_WAITOK) in interrupt context")); |
| 536 | if (__predict_false(!THREAD_CAN_SLEEP())) { |
| 537 | #ifdef EPOCH_TRACE |
| 538 | epoch_trace_list(curthread); |
| 539 | #endif |
| 540 | KASSERT(1, |
| 541 | ("malloc(M_WAITOK) with sleeping prohibited")); |
| 542 | } |
| 543 | } |
| 544 | KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), |
| 545 | ("malloc: called with spinlock or critical section held")); |
| 546 | |
| 547 | #ifdef DEBUG_MEMGUARD |
| 548 | if (memguard_cmp_mtp(mtp, *sizep)) { |
| 549 | *vap = memguard_alloc(*sizep, flags); |
| 550 | if (*vap != NULL) |
| 551 | return (EJUSTRETURN); |
| 552 | /* This is unfortunate but should not be fatal. */ |
| 553 | } |
| 554 | #endif |
| 555 | |
| 556 | #ifdef DEBUG_REDZONE |
| 557 | *sizep = redzone_size_ntor(*sizep); |
no test coverage detected