netbuf_t pppgetbuf(netif_t ifp) { NETBUF_T nb; int len = MAX(if_mtu(ifp), PPP_MTU) + PPP_HDRLEN + PPP_FCSLEN; nb = NB_ALLOC(len); if (nb != NULL) { NB_SHRINK_TOP(nb, PPP_HDRLEN); NB_SHRINK_BOT(nb, PPP_FCSLEN); /* grown by pppstart() */ } return NB_TO_nb(nb); }
int pk_init (void) { PACKET packet; unsigned i; unsigned numpkts = bigbufs + lilbufs; u_char align_req; #ifdef ALIGN_BUFS align_req = ALIGN_BUFS; #else align_req = 0; #endif for (i = 0; i < numpkts; i++) { packet = (PACKET)NB_ALLOC(sizeof(struct netbuf)); if (packet == NULL) goto no_pkt_buf; #ifdef NPDEBUG if (i >= MAXPACKETS) { dprintf("pk_init: bad define\n"); return -1; } pktlog[i] = packet; /* save for debugging */ #endif packet->nb_tstamp = 0L; if (i < bigbufs) { #ifdef NPDEBUG { int j; /* for DEBUG compiles, bracket the data area with special chars */ packet->nb_buff = (char *)BB_ALLOC(bigbufsiz+ALIGN_TYPE+1); if (!(packet->nb_buff)) goto no_pkt_buf; /* Add memory markers for sanity check */ for(j = 0; j < ALIGN_TYPE; j++) *(packet->nb_buff + j) = 'M'; /* MMs at start of buf */ *(packet->nb_buff + bigbufsiz + ALIGN_TYPE) = 'M'; packet->nb_buff += ALIGN_TYPE; /* bump buf past MMs */ } #else packet->nb_buff = (char *)BB_ALLOC(bigbufsiz + align_req); #ifdef ALIGN_BUFS /* align start of buffer pointer to desired offset */ packet->nb_buff += (ALIGN_BUFS - (((u_long) packet->nb_buff) & (ALIGN_BUFS - 1))); #endif #endif if (!(packet->nb_buff)) goto no_pkt_buf; packet->nb_blen = bigbufsiz; q_add(&bigfreeq, packet); /* save it in big pkt free queue */ } else /* get a small buffer */ { #ifdef NPDEBUG { int j; /* for DEBUG compiles, bracket the data area with special chars */ packet->nb_buff = (char *)LB_ALLOC(lilbufsiz+ALIGN_TYPE+1); if (!(packet->nb_buff)) goto no_pkt_buf; /* Add memory markers for sanity check */ for(j = 0; j < ALIGN_TYPE; j++) *(packet->nb_buff + j) = 'M'; /* MMs at start of buf */ *(packet->nb_buff + lilbufsiz + ALIGN_TYPE) = 'M'; packet->nb_buff += ALIGN_TYPE; } #else packet->nb_buff = (char *)LB_ALLOC(lilbufsiz + align_req); #ifdef ALIGN_BUFS /* align start of buffer pointer to desired offset */ packet->nb_buff += (ALIGN_BUFS - (((u_long) packet->nb_buff) & (ALIGN_BUFS - 1))); #endif #endif if (!(packet->nb_buff)) goto no_pkt_buf; packet->nb_blen = lilbufsiz; q_add(&lilfreeq, packet); /* save it in little free queue */ } } bigfreeq.q_min = bigbufs; lilfreeq.q_min = lilbufs; #ifdef HEAPBUFS /* initialize the counters that keep track of the total amount of memory * allocated from the heap and the corresponding high watermark */ heap_curr_mem = 0; heap_curr_mem_hi_watermark = 0; /* set the heap's access type to blocking */ heap_type = HEAP_ACCESS_BLOCKING; #endif return 0; no_pkt_buf: #ifdef NPDEBUG dprintf("Netinit: calloc failed getting buffer %d\n", i); #endif return(-1); }