Date: Sun, 7 Mar 2010 11:53:41 GMT From: Aman Jassal <aman@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 175440 for review Message-ID: <201003071153.o27BrfEV056045@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://p4web.freebsd.org/chv.cgi?CH=175440 Change 175440 by aman@src on 2010/03/07 11:53:08 <Add macros in netstat_util.c, this is the first effort towards macrofying the code a bit and catching the common skeleton of the allocator and iterator functions. The 2 macros introduced are MALLOC_PTR() and MALLOC_ITR()> PR:None Submitted by:Aman JASSAL Reviewed by:Gabor PALI Approved by:Gabor PALI Affected files ... .. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_util.c#71 edit Differences ... ==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_util.c#71 (text+ko) ==== @@ -53,6 +53,15 @@ #include "netstat.h" #include "netstat_internal.h" +/* Macros used to allocate memory for allocators and iterators */ +#define MALLOC_PTR(pointer) pointer = malloc(sizeof(*pointer)); \ + if (pointer == NULL) \ + return(NULL); \ + +#define MALLOC_ITR(pointer) pointer = malloc(sizeof(*pointer)); \ + if (pointer == NULL) \ + return(-1); \ + int kread_data(kvm_t *kvm, u_long kvm_pointer, void *address, size_t size) { @@ -232,9 +241,7 @@ { struct session_type *sstp; - sstp = malloc(sizeof(*sstp)); - if (sstp == NULL) - return (NULL); + MALLOC_PTR(sstp); bzero(sstp, sizeof(*sstp)); @@ -256,9 +263,7 @@ { struct socket_type_list *stlp; - stlp = malloc(sizeof(*stlp)); - if (stlp == NULL) - return NULL; + MALLOC_PTR(stlp); LIST_INIT(&stlp->stl_list); stlp->stl_error = NETSTAT_ERROR_UNDEFINED; @@ -348,9 +353,7 @@ { struct socket_type *stp; - stp = malloc(sizeof(*stp)); - if (stp == NULL) - return (NULL); + MALLOC_PTR(stp); bzero(stp, sizeof(*stp)); @@ -376,9 +379,7 @@ { struct socket_type_iterator *itp; - itp = malloc(sizeof(*itp)); - if (itp == NULL) - return (-1); + MALLOC_ITR(itp); bzero(itp, sizeof(*itp)); @@ -423,9 +424,7 @@ { struct sockaddr_type *satp; - satp = malloc(sizeof(*satp)); - if (satp == NULL) - return (NULL); + MALLOC_PTR(satp); bzero(satp, sizeof(*satp)); satp->sat_type = type; @@ -657,9 +656,7 @@ { struct mbuf_type *mbtp; - mbtp = malloc(sizeof(*mbtp)); - if (mbtp == NULL) - return (NULL); + MALLOC_PTR(mbtp); bzero(mbtp, sizeof(*mbtp)); return (mbtp); @@ -822,9 +819,7 @@ { struct interface_type *itp; - itp = malloc(sizeof(*itp)); - if (itp == NULL) - return (NULL); + MALLOC_PTR(itp); bzero(itp, sizeof(*itp)); @@ -841,9 +836,7 @@ { struct intfaddr_type *iatp; - iatp = malloc(sizeof(*iatp)); - if (iatp == NULL) - return (NULL); + MALLOC_PTR(iatp); bzero(iatp, sizeof(*iatp)); iatp->iat_family = family; @@ -857,9 +850,7 @@ { struct interface_type_list *itlp; - itlp = malloc(sizeof(*itlp)); - if (itlp == NULL) - return (NULL); + MALLOC_PTR(itlp); TAILQ_INIT(&itlp->itl_list); itlp->itl_error = NETSTAT_ERROR_UNDEFINED; @@ -905,9 +896,7 @@ { struct interface_type_iterator *itp; - itp = malloc(sizeof(*itp)); - if (itp == NULL) - return (-1); + MALLOC_ITR(itp); bzero(itp, sizeof(*itp)); @@ -1150,9 +1139,7 @@ { struct bpf_type *btp; - btp = malloc(sizeof(*btp)); - if (btp == NULL) - return (NULL); + MALLOC_PTR(btp); bzero(btp, sizeof(*btp)); strlcpy(btp->bpt_ifname, ifname, BPFTYPE_MAXNAME); @@ -1166,9 +1153,7 @@ { struct bpf_type_list *btlp; - btlp = malloc(sizeof(*btlp)); - if (btlp == NULL) - return (NULL); + MALLOC_PTR(btlp); LIST_INIT(&btlp->bptl_list); btlp->bptl_error = NETSTAT_ERROR_UNDEFINED; @@ -1201,9 +1186,7 @@ { struct bpf_type_iterator *btp; - btp = malloc(sizeof(*btp)); - if (btp == NULL) - return (-1); + MALLOC_ITR(btp); bzero(btp, sizeof(*btp)); btp->bpti_list = list; @@ -1312,9 +1295,7 @@ { struct stat_type *sttp; - sttp = malloc(sizeof(*sttp)); - if (sttp == NULL) - return (NULL); + MALLOC_PTR(sttp); bzero(sttp, sizeof(*sttp)); return (sttp); @@ -2309,9 +2290,7 @@ { struct route_type *rtp; - rtp = malloc(sizeof(*rtp)); - if (rtp == NULL) - return (NULL); + MALLOC_PTR(rtp); bzero(rtp, sizeof(*rtp)); TAILQ_INSERT_TAIL(&list->rtl_list, rtp, rt_list); @@ -2324,9 +2303,7 @@ { struct routeaddr_type *ratp; - ratp = malloc(sizeof(*ratp)); - if (ratp == NULL) - return (NULL); + MALLOC_PTR(ratp); bzero(ratp, sizeof(*ratp)); if (address != NULL && len > 0) { @@ -2348,9 +2325,7 @@ { struct route_type_list *rtlp; - rtlp = malloc(sizeof(*rtlp)); - if (rtlp == NULL) - return (NULL); + MALLOC_PTR(rtlp); TAILQ_INIT(&rtlp->rtl_list); rtlp->rtl_error = NETSTAT_ERROR_UNDEFINED; @@ -2383,9 +2358,7 @@ { struct route_type_iterator *rtip; - rtip = malloc(sizeof(*rtip)); - if (rtip == NULL) - return (-1); + MALLOC_ITR(rtip); bzero(rtip, sizeof(*rtip)); rtip->rti_list = list; @@ -2623,9 +2596,7 @@ { struct mcastif_type *mitp; - mitp = malloc(sizeof(*mitp)); - if (mitp == NULL) - return (NULL); + MALLOC_PTR(mitp); bzero(mitp, sizeof(*mitp)); TAILQ_INSERT_TAIL(&list->mitl_list, mitp, mit_list); @@ -2638,9 +2609,7 @@ { struct mcastif_type_list *mitlp; - mitlp = malloc(sizeof(*mitlp)); - if (mitlp == NULL) - return (NULL); + MALLOC_PTR(mitlp); TAILQ_INIT(&mitlp->mitl_list); mitlp->mitl_error = NETSTAT_ERROR_UNDEFINED; @@ -2673,9 +2642,7 @@ { struct mcastif_type_iterator *mitip; - mitip = malloc(sizeof(*mitip)); - if (mitip == NULL) - return (-1); + MALLOC_ITR(mitip); bzero(mitip, sizeof(*mitip)); mitip->miti_list = list; @@ -2799,9 +2766,7 @@ { struct mroute_type *mrtp; - mrtp = malloc(sizeof(*mrtp)); - if (mrtp == NULL) - return (NULL); + MALLOC_PTR(mrtp); bzero(mrtp, sizeof(*mrtp)); TAILQ_INSERT_TAIL(&list->mrtl_list, mrtp, mrt_list); @@ -2814,9 +2779,7 @@ { struct mroute_type_list *mrtlp; - mrtlp = malloc(sizeof(*mrtlp)); - if (mrtlp == NULL) - return (NULL); + MALLOC_PTR(mrtlp); TAILQ_INIT(&mrtlp->mrtl_list); mrtlp->mrtl_error = NETSTAT_ERROR_UNDEFINED; @@ -2849,9 +2812,7 @@ { struct mroute_type_iterator *mrtip; - mrtip = malloc(sizeof(*mrtip)); - if (mrtip == NULL) - return (-1); + MALLOC_ITR(mrtip); bzero(mrtip, sizeof(*mrtip)); mrtip->mrti_list = list;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201003071153.o27BrfEV056045>