int ddi_soft_state_init(void **state_p, size_t size, size_t n_items) { struct i_ddi_soft_state *ss; if (state_p == NULL || *state_p != NULL || size == 0) return (EINVAL); ss = kmem_zalloc(sizeof (*ss), KM_SLEEP); mutex_init(&ss->lock, NULL, MUTEX_DRIVER, NULL); ss->size = size; if (n_items < MIN_N_ITEMS) ss->n_items = MIN_N_ITEMS; else { int bitlog; if ((bitlog = ddi_fls(n_items)) == ddi_ffs(n_items)) bitlog--; ss->n_items = 1 << bitlog; } ASSERT(ss->n_items >= n_items); ss->array = kmem_zalloc(ss->n_items * sizeof (void *), KM_SLEEP); *state_p = ss; return (0); }
/* check to see if value is power of 2 or not. */ static int isnot_pow2(uint64_t value) { uint32_t low; uint32_t hi; low = value & 0xffffffff; hi = value >> 32; /* * ddi_ffs and ddi_fls gets long values, so in 32bit environment * won't work correctly for 64bit values */ if ((ddi_ffs(low) == ddi_fls(low)) && (ddi_ffs(hi) == ddi_fls(hi))) return (0); return (1); }