library/src/mmap/BP-MMapSlabAllocator.cc
// attempts to allocate a set of slabs.
attempts to allocate a set of slabs.
attempts to allocate a set of slabs .
P_BP_MMAP_SLAB_ENTRY_HDR BP_SlabAlloc(P_BP_MMAP_SLAB_ALLOCATOR allocator, size_t num_entries)
{
	if(!allocator)
		return NULL;
	if(!num_entries)
		return NULL;
	BP_SLAB_TABLE_LOCK_SEM(allocator->lock);
	BP_SlabAllocUpdateInfo(allocator, BP_TRUE);
	if(num_entries > allocator->largest_contiguous_blocks_available_n)
	{
		BP_SLAB_TABLE_UNLOCK_SEM(allocator->lock);
		return NULL;
	}
	P_BP_MMAP_SLAB_ENTRY_HDR tmp_entry = NULL;
	P_BP_MMAP_SLAB_ENTRY_HDR ret_entry = NULL;
	size_t available_adjacent_n = 0;
	BP_SLAB_ALLOCATOR_ITER_LOOP_ALL(allocator)
	{
		if
		(
			BP_FLAG_IS_SET(allocator->curr_entry->flags, BP_MMAP_SLAB_ENTRY_INFO_AVAILABLE)
		)
		{
			available_adjacent_n = BP_SlabAllocAvailableAdjacentEntries
			(
					allocator,
					allocator->curr_entry
			);
			if(available_adjacent_n >= num_entries)
			{
				printf("\n [+] Preparing for allocation (%u - %u)", available_adjacent_n, num_entries);
				ret_entry = allocator->curr_entry;
				BP_SlabAllocCheckoutEntries
				(
					allocator,
					allocator->curr_entry,
					num_entries
				);
				allocator->curr_entry = ret_entry;
				break;
			}
		}
		available_adjacent_n = 0;
	}
	BP_SLAB_TABLE_UNLOCK_SEM(allocator->lock);
	 before exit
	BP_SlabAllocUpdateInfo(allocator, BP_TRUE);
	return ret_entry;
}
P_BP_MMAP_SLAB_ENTRY_HDR BP_SlabAlloc ( P_BP_MMAP_SLAB_ALLOCATOR allocator , size_t num_entries ) { if ( ! allocator ) return NULL ; if ( ! num_entries ) return NULL ; BP_SLAB_TABLE_LOCK_SEM ( allocator -> lock ) ; BP_SlabAllocUpdateInfo ( allocator , BP_TRUE ) ; if ( num_entries > allocator -> largest_contiguous_blocks_available_n ) { BP_SLAB_TABLE_UNLOCK_SEM ( allocator -> lock ) ; return NULL ; } P_BP_MMAP_SLAB_ENTRY_HDR tmp_entry = NULL ; P_BP_MMAP_SLAB_ENTRY_HDR ret_entry = NULL ; size_t available_adjacent_n = 0 ; BP_SLAB_ALLOCATOR_ITER_LOOP_ALL ( allocator ) { if ( BP_FLAG_IS_SET ( allocator -> curr_entry -> flags , BP_MMAP_SLAB_ENTRY_INFO_AVAILABLE ) ) { available_adjacent_n = BP_SlabAllocAvailableAdjacentEntries ( allocator , allocator -> curr_entry ) ; if ( available_adjacent_n >= num_entries ) { printf ( " \n " , available_adjacent_n , num_entries ) ; ret_entry = allocator -> curr_entry ; BP_SlabAllocCheckoutEntries ( allocator , allocator -> curr_entry , num_entries ) ; allocator -> curr_entry = ret_entry ; break ; } } available_adjacent_n = 0 ; } BP_SLAB_TABLE_UNLOCK_SEM ( allocator -> lock ) ; BP_SlabAllocUpdateInfo ( allocator , BP_TRUE ) ; return ret_entry ; }
attempts to allocate a set of slabs.
attempts to allocate a set of slabs .
// ensure we have an allocator // ensure we have a number of entries to allocate // lock the allocator before display // update allocation info // ensure we have space for allocations // set tmp entry // the entry to be returned (aka start entry) // loop iterator // walk through the allocator and find contiguous set of slabs // if the current slab has never been allocated, indicate that here // count the number of adjacent items // available adjacent number of entries // set the return entry // attempt to check out the chunks // reset the current entry pointer // break after allocation // reset the adjacent item count // unlock allocator before exit // update allocation info before exit // return the allocation