+static struct ifmultiaddr *
+ifma_alloc(int how)
+{
+ struct ifmultiaddr *ifma;
+
+ ifma = (how == M_WAITOK) ? zalloc(ifma_zone) :
+ zalloc_noblock(ifma_zone);
+
+ if (ifma != NULL) {
+ bzero(ifma, ifma_size);
+ lck_mtx_init(&ifma->ifma_lock, ifa_mtx_grp, ifa_mtx_attr);
+ ifma->ifma_debug |= IFD_ALLOC;
+ if (ifma_debug != 0) {
+ ifma->ifma_debug |= IFD_DEBUG;
+ ifma->ifma_trace = ifma_trace;
+ }
+ }
+ return (ifma);
+}
+
+static void
+ifma_free(struct ifmultiaddr *ifma)
+{
+ IFMA_LOCK(ifma);
+
+ if (ifma->ifma_protospec != NULL) {
+ panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
+ /* NOTREACHED */
+ } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
+ ifma->ifma_anoncnt != 0) {
+ panic("%s: Freeing ifma=%p with outstanding anon req",
+ __func__, ifma);
+ /* NOTREACHED */
+ } else if (ifma->ifma_debug & IFD_ATTACHED) {
+ panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
+ __func__, ifma, ifma->ifma_ifp);
+ /* NOTREACHED */
+ } else if (!(ifma->ifma_debug & IFD_ALLOC)) {
+ panic("%s: ifma %p cannot be freed", __func__, ifma);
+ /* NOTREACHED */
+ } else if (ifma->ifma_refcount != 0) {
+ panic("%s: non-zero refcount ifma=%p", __func__, ifma);
+ /* NOTREACHED */
+ } else if (ifma->ifma_reqcnt != 0) {
+ panic("%s: non-zero reqcnt ifma=%p", __func__, ifma);
+ /* NOTREACHED */
+ } else if (ifma->ifma_ifp != NULL) {
+ panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__,
+ ifma->ifma_ifp, ifma);
+ /* NOTREACHED */
+ } else if (ifma->ifma_ll != NULL) {
+ panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__,
+ ifma->ifma_ll, ifma);
+ /* NOTREACHED */
+ }
+ ifma->ifma_debug &= ~IFD_ALLOC;
+ if ((ifma->ifma_debug & (IFD_DEBUG | IFD_TRASHED)) ==
+ (IFD_DEBUG | IFD_TRASHED)) {
+ lck_mtx_lock(&ifma_trash_lock);
+ TAILQ_REMOVE(&ifma_trash_head, (struct ifmultiaddr_dbg *)ifma,
+ ifma_trash_link);
+ lck_mtx_unlock(&ifma_trash_lock);
+ ifma->ifma_debug &= ~IFD_TRASHED;
+ }
+ IFMA_UNLOCK(ifma);
+
+ if (ifma->ifma_addr != NULL) {
+ FREE(ifma->ifma_addr, M_IFADDR);
+ ifma->ifma_addr = NULL;
+ }
+ lck_mtx_destroy(&ifma->ifma_lock, ifa_mtx_grp);
+ zfree(ifma_zone, ifma);
+}
+
+static void
+ifma_trace(struct ifmultiaddr *ifma, int refhold)
+{
+ struct ifmultiaddr_dbg *ifma_dbg = (struct ifmultiaddr_dbg *)ifma;
+ ctrace_t *tr;
+ u_int32_t idx;
+ u_int16_t *cnt;
+
+ if (!(ifma->ifma_debug & IFD_DEBUG)) {
+ panic("%s: ifma %p has no debug structure", __func__, ifma);
+ /* NOTREACHED */
+ }
+ if (refhold) {
+ cnt = &ifma_dbg->ifma_refhold_cnt;
+ tr = ifma_dbg->ifma_refhold;
+ } else {
+ cnt = &ifma_dbg->ifma_refrele_cnt;
+ tr = ifma_dbg->ifma_refrele;
+ }
+
+ idx = atomic_add_16_ov(cnt, 1) % IFMA_TRACE_HIST_SIZE;
+ ctrace_record(&tr[idx]);
+}
+