FreeBSD/sbin/hastd/activemap.c
/*
 * Initialize activemap structure and allocate memory for internal needs.
 * Function returns 0 on success and -1 if any of the allocations failed.
 */
Initialize activemap structure and allocate memory for internal needs.
Function returns 0 on success and -1 if any of the allocations failed.
Initialize activemap structure and allocate memory for internal needs . Function returns 0 on success and - 1 if any of the allocations failed .
int
activemap_init(struct activemap **ampp, uint64_t mediasize, uint32_t extentsize,
    uint32_t sectorsize, uint32_t keepdirty)
{
	struct activemap *amp;
	PJDLOG_ASSERT(ampp != NULL);
	PJDLOG_ASSERT(mediasize > 0);
	PJDLOG_ASSERT(extentsize > 0);
	PJDLOG_ASSERT(powerof2(extentsize));
	PJDLOG_ASSERT(sectorsize > 0);
	PJDLOG_ASSERT(powerof2(sectorsize));
	PJDLOG_ASSERT(keepdirty > 0);
	amp = malloc(sizeof(*amp));
	if (amp == NULL)
		return (-1);
	amp->am_mediasize = mediasize;
	amp->am_nkeepdirty_limit = keepdirty;
	amp->am_extentsize = extentsize;
	amp->am_extentshift = bitcount32(extentsize - 1);
	amp->am_nextents = ((mediasize - 1) / extentsize) + 1;
	amp->am_mapsize = bitstr_size(amp->am_nextents);
	amp->am_diskmapsize = roundup2(amp->am_mapsize, sectorsize);
	amp->am_ndirty = 0;
	amp->am_syncoff = -2;
	TAILQ_INIT(&amp->am_keepdirty);
	amp->am_nkeepdirty = 0;
	amp->am_memtab = calloc(amp->am_nextents, sizeof(amp->am_memtab[0]));
	amp->am_diskmap = calloc(1, amp->am_diskmapsize);
	amp->am_memmap = bit_alloc(amp->am_nextents);
	amp->am_syncmap = bit_alloc(amp->am_nextents);
	if (amp->am_memtab == NULL || amp->am_diskmap == NULL ||
	    amp->am_memmap == NULL || amp->am_syncmap == NULL) {
		if (amp->am_memtab != NULL)
			free(amp->am_memtab);
		if (amp->am_diskmap != NULL)
			free(amp->am_diskmap);
		if (amp->am_memmap != NULL)
			free(amp->am_memmap);
		if (amp->am_syncmap != NULL)
			free(amp->am_syncmap);
		amp->am_magic = 0;
		free(amp);
		errno = ENOMEM;
		return (-1);
	}
	amp->am_magic = ACTIVEMAP_MAGIC;
	*ampp = amp;
	return (0);
}
int activemap_init ( struct activemap * * ampp , uint64_t mediasize , uint32_t extentsize , uint32_t sectorsize , uint32_t keepdirty ) { struct activemap * amp ; PJDLOG_ASSERT ( ampp != NULL ) ; PJDLOG_ASSERT ( mediasize > 0 ) ; PJDLOG_ASSERT ( extentsize > 0 ) ; PJDLOG_ASSERT ( powerof2 ( extentsize ) ) ; PJDLOG_ASSERT ( sectorsize > 0 ) ; PJDLOG_ASSERT ( powerof2 ( sectorsize ) ) ; PJDLOG_ASSERT ( keepdirty > 0 ) ; amp = malloc ( sizeof ( * amp ) ) ; if ( amp == NULL ) return ( -1 ) ; amp -> am_mediasize = mediasize ; amp -> am_nkeepdirty_limit = keepdirty ; amp -> am_extentsize = extentsize ; amp -> am_extentshift = bitcount32 ( extentsize - 1 ) ; amp -> am_nextents = ( ( mediasize - 1 ) / extentsize ) + 1 ; amp -> am_mapsize = bitstr_size ( amp -> am_nextents ) ; amp -> am_diskmapsize = roundup2 ( amp -> am_mapsize , sectorsize ) ; amp -> am_ndirty = 0 ; amp -> am_syncoff = -2 ; TAILQ_INIT ( & amp -> am_keepdirty ) ; amp -> am_nkeepdirty = 0 ; amp -> am_memtab = calloc ( amp -> am_nextents , sizeof ( amp -> am_memtab [ 0 ] ) ) ; amp -> am_diskmap = calloc ( 1 , amp -> am_diskmapsize ) ; amp -> am_memmap = bit_alloc ( amp -> am_nextents ) ; amp -> am_syncmap = bit_alloc ( amp -> am_nextents ) ; if ( amp -> am_memtab == NULL || amp -> am_diskmap == NULL || amp -> am_memmap == NULL || amp -> am_syncmap == NULL ) { if ( amp -> am_memtab != NULL ) free ( amp -> am_memtab ) ; if ( amp -> am_diskmap != NULL ) free ( amp -> am_diskmap ) ; if ( amp -> am_memmap != NULL ) free ( amp -> am_memmap ) ; if ( amp -> am_syncmap != NULL ) free ( amp -> am_syncmap ) ; amp -> am_magic = 0 ; free ( amp ) ; errno = ENOMEM ; return ( -1 ) ; } amp -> am_magic = ACTIVEMAP_MAGIC ; * ampp = amp ; return ( 0 ) ; }
Initialize activemap structure and allocate memory for internal needs.
Initialize activemap structure and allocate memory for internal needs .
/*
	 * Check to see if any of the allocations above failed.
	 */