FreeBSD/sys/kern/kern_malloc.c
/*
 *	malloc:
 *
 *	Allocate a block of memory.
 *
 *	If M_NOWAIT is set, this routine will not block and return NULL if
 *	the allocation fails.
 */
Allocate a block of memory.

If M_NOWAIT is set, this routine will not block and return NULL if
the allocation fails.
Allocate a block of memory . If M_NOWAIT is set this routine will not block and return NULL if the allocation fails .
void *
malloc(unsigned long size, struct malloc_type *mtp, int flags)
{
	int indx;
	struct malloc_type_internal *mtip;
	caddr_t va;
	uma_zone_t zone;
#if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE)
	unsigned long osize = size;
#endif
#ifdef INVARIANTS
	KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic"));
	indx = flags & (M_WAITOK | M_NOWAIT);
	if (indx != M_NOWAIT && indx != M_WAITOK) {
		static	struct timeval lasterr;
		static	int curerr, once;
		if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
			printf("Bad malloc flags: %x\n", indx);
			kdb_backtrace();
			flags |= M_WAITOK;
			once++;
		}
	}
#endif
#ifdef MALLOC_MAKE_FAILURES
	if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
		atomic_add_int(&malloc_nowait_count, 1);
		if ((malloc_nowait_count % malloc_failure_rate) == 0) {
			atomic_add_int(&malloc_failure_count, 1);
			t_malloc_fail = time_uptime;
			return (NULL);
		}
	}
#endif
	if (flags & M_WAITOK)
		KASSERT(curthread->td_intr_nesting_level == 0,
		   ("malloc(M_WAITOK) in interrupt context"));
	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
	    ("malloc: called with spinlock or critical section held"));
#ifdef DEBUG_MEMGUARD
	if (memguard_cmp_mtp(mtp, size)) {
		va = memguard_alloc(size, flags);
		if (va != NULL)
			return (va);
	}
#endif
#ifdef DEBUG_REDZONE
	size = redzone_size_ntor(size);
#endif
	if (size <= kmem_zmax) {
		mtip = mtp->ks_handle;
		if (size & KMEM_ZMASK)
			size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
		indx = kmemsize[size >> KMEM_ZSHIFT];
		KASSERT(mtip->mti_zone < numzones,
		    ("mti_zone %u out of range %d",
		    mtip->mti_zone, numzones));
		zone = kmemzones[indx].kz_zone[mtip->mti_zone];
#ifdef MALLOC_PROFILE
		krequests[size >> KMEM_ZSHIFT]++;
#endif
		va = uma_zalloc(zone, flags);
		if (va != NULL)
			size = zone->uz_size;
		malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
	} else {
		size = roundup(size, PAGE_SIZE);
		zone = NULL;
		va = uma_large_malloc(size, flags);
		malloc_type_allocated(mtp, va == NULL ? 0 : size);
	}
	if (flags & M_WAITOK)
		KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
	else if (va == NULL)
		t_malloc_fail = time_uptime;
#ifdef DIAGNOSTIC
	if (va != NULL && !(flags & M_ZERO)) {
		memset(va, 0x70, osize);
	}
#endif
#ifdef DEBUG_REDZONE
	if (va != NULL)
		va = redzone_setup(va, osize);
#endif
	return ((void *) va);
}
void * malloc ( unsigned long size , struct malloc_type * mtp , int flags ) { int indx ; struct malloc_type_internal * mtip ; caddr_t va ; uma_zone_t zone ; #if defined ( DIAGNOSTIC ) || defined ( DEBUG_REDZONE ) unsigned long osize = size ; #endif #ifdef INVARIANTS KASSERT ( mtp -> ks_magic == M_MAGIC , ( " " ) ) ; indx = flags & ( M_WAITOK | M_NOWAIT ) ; if ( indx != M_NOWAIT && indx != M_WAITOK ) { static struct timeval lasterr ; static int curerr , once ; if ( once == 0 && ppsratecheck ( & lasterr , & curerr , 1 ) ) { printf ( " \n " , indx ) ; kdb_backtrace ( ) ; flags |= M_WAITOK ; once ++ ; } } #endif #ifdef MALLOC_MAKE_FAILURES if ( ( flags & M_NOWAIT ) && ( malloc_failure_rate != 0 ) ) { atomic_add_int ( & malloc_nowait_count , 1 ) ; if ( ( malloc_nowait_count % malloc_failure_rate ) == 0 ) { atomic_add_int ( & malloc_failure_count , 1 ) ; t_malloc_fail = time_uptime ; return ( NULL ) ; } } #endif if ( flags & M_WAITOK ) KASSERT ( curthread -> td_intr_nesting_level == 0 , ( " " ) ) ; KASSERT ( curthread -> td_critnest == 0 || SCHEDULER_STOPPED ( ) , ( " " ) ) ; #ifdef DEBUG_MEMGUARD if ( memguard_cmp_mtp ( mtp , size ) ) { va = memguard_alloc ( size , flags ) ; if ( va != NULL ) return ( va ) ; } #endif #ifdef DEBUG_REDZONE size = redzone_size_ntor ( size ) ; #endif if ( size <= kmem_zmax ) { mtip = mtp -> ks_handle ; if ( size & KMEM_ZMASK ) size = ( size & ~ KMEM_ZMASK ) + KMEM_ZBASE ; indx = kmemsize [ size >> KMEM_ZSHIFT ] ; KASSERT ( mtip -> mti_zone < numzones , ( " " , mtip -> mti_zone , numzones ) ) ; zone = kmemzones [ indx ] . kz_zone [ mtip -> mti_zone ] ; #ifdef MALLOC_PROFILE krequests [ size >> KMEM_ZSHIFT ] ++ ; #endif va = uma_zalloc ( zone , flags ) ; if ( va != NULL ) size = zone -> uz_size ; malloc_type_zone_allocated ( mtp , va == NULL ? 0 : size , indx ) ; } else { size = roundup ( size , PAGE_SIZE ) ; zone = NULL ; va = uma_large_malloc ( size , flags ) ; malloc_type_allocated ( mtp , va == NULL ? 0 : size ) ; } if ( flags & M_WAITOK ) KASSERT ( va != NULL , ( " " ) ) ; else if ( va == NULL ) t_malloc_fail = time_uptime ; #ifdef DIAGNOSTIC if ( va != NULL && ! ( flags & M_ZERO ) ) { memset ( va , 0x70 , osize ) ; } #endif #ifdef DEBUG_REDZONE if ( va != NULL ) va = redzone_setup ( va , osize ) ; #endif return ( ( void * ) va ) ; }
malloc:
      Allocate a block of memory.
malloc : Allocate a block of memory .
/*
	 * Check that exactly one of M_WAITOK or M_NOWAIT is specified.
	 */ /* This is unfortunate but should not be fatal. */