Nut/OS  4.10.3
API Reference
netbuf.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  * -
00033  * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk>
00034  *
00035  * This file is distributed in the hope that it will be useful, but WITHOUT
00036  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00037  * FITNESS FOR A PARTICULAR PURPOSE.
00038  *
00039  * You can redistribute this file and/or modify it under the terms of the GNU
00040  * General Public License (GPL) as published by the Free Software Foundation;
00041  * either version 2 of the License, or (at your discretion) any later version.
00042  * See the accompanying file "copying-gpl.txt" for more details.
00043  *
00044  * As a special exception to the GPL, permission is granted for additional
00045  * uses of the text contained in this file.  See the accompanying file
00046  * "copying-liquorice.txt" for details.
00047  * -
00048  * Portions Copyright (c) 1983, 1993 by
00049  *  The Regents of the University of California.  All rights reserved.
00050  *
00051  * Redistribution and use in source and binary forms, with or without
00052  * modification, are permitted provided that the following conditions
00053  * are met:
00054  * 1. Redistributions of source code must retain the above copyright
00055  *    notice, this list of conditions and the following disclaimer.
00056  * 2. Redistributions in binary form must reproduce the above copyright
00057  *    notice, this list of conditions and the following disclaimer in the
00058  *    documentation and/or other materials provided with the distribution.
00059  * 3. Neither the name of the University nor the names of its contributors
00060  *    may be used to endorse or promote products derived from this software
00061  *    without specific prior written permission.
00062  *
00063  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00064  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00065  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00066  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00067  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00068  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00069  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00070  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00071  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00072  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00073  * SUCH DAMAGE.
00074  * -
00075  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00076  *
00077  * Permission to use, copy, modify, and distribute this software for any
00078  * purpose with or without fee is hereby granted, provided that the above
00079  * copyright notice and this permission notice appear in all copies, and that
00080  * the name of Digital Equipment Corporation not be used in advertising or
00081  * publicity pertaining to distribution of the document or software without
00082  * specific, written prior permission.
00083  * 
00084  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00085  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00086  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00087  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00088  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00089  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00090  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00091  * SOFTWARE.
00092  */
00093 
00094 /*
00095  * $Log$
00096  * Revision 1.6  2008/08/11 06:59:42  haraldkipp
00097  * BSD types replaced by stdint types (feature request #1282721).
00098  *
00099  * Revision 1.5  2008/08/09 17:35:32  haraldkipp
00100  * Simplified NETBUF allocation and release.
00101  *
00102  * Revision 1.4  2008/04/18 13:31:59  haraldkipp
00103  * Changed size parameter from uint16_t to int, which is easier to handle
00104  * for 32-bit targets. You need to recompile your ARM code. No impact on
00105  * AVR expected
00106  * I changed u_int to int at some places to avoid some warnings during
00107  * compilation of Nut/Net.
00108  * libs.
00109  *
00110  * Revision 1.3  2005/04/30 16:42:41  chaac
00111  * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
00112  * is defined in NutConf, it will make effect where it is used.
00113  *
00114  * Revision 1.2  2004/01/16 07:51:43  drsung
00115  * Bugfix for reallocating smaller network buffers. Thx to Mike Cornelius.
00116  *
00117  * Revision 1.1.1.1  2003/05/09 14:40:46  haraldkipp
00118  * Initial using 3.2.1
00119  *
00120  * Revision 1.12  2003/03/31 14:53:07  harald
00121  * Prepare release 3.1
00122  *
00123  * Revision 1.11  2003/02/04 17:50:54  harald
00124  * Version 3 released
00125  *
00126  * Revision 1.10  2002/06/26 17:29:08  harald
00127  * First pre-release with 2.4 stack
00128  *
00129  */
00130 
00131 #include <cfg/os.h>
00132 #include <string.h>
00133 
00134 #include <sys/heap.h>
00135 #include <dev/netbuf.h>
00136 
00137 #include <sys/nutdebug.h>
00138 
00143 
00144 static int NutNetBufAllocData(NBDATA * nbd, int size)
00145 {
00146     nbd->vp = NutHeapAlloc(size);
00147     if (nbd->vp) {
00148         nbd->sz = size;
00149         return 0;
00150     }
00151     return -1;
00152 }
00153 
00173 NETBUF *NutNetBufAlloc(NETBUF * nb, uint8_t type, int size)
00174 {
00175     NBDATA * nbd;
00176 
00177     NUTASSERT(size > 0);
00178     NUTASSERT(type == NBAF_DATALINK || 
00179         type == NBAF_NETWORK || 
00180         type == NBAF_TRANSPORT || 
00181         type == NBAF_APPLICATION);
00182 
00183     /* Allocate a new buffer, if the caller don't provide one. */
00184     if (nb == NULL) {
00185         nb = NutHeapAllocClear(sizeof(NETBUF));
00186     }
00187     /* Make sure, that the allocation above was successful. */
00188     if (nb) {
00189         /* Determine the data type. */
00190         switch(type) {
00191         case NBAF_DATALINK:
00192             nbd = &nb->nb_dl;
00193             break;
00194         case NBAF_NETWORK:
00195             nbd = &nb->nb_nw;
00196             break;
00197         case NBAF_TRANSPORT:
00198             nbd = &nb->nb_tp;
00199             break;
00200         default:
00201             nbd = &nb->nb_ap;
00202             break;
00203         }
00204         /* Check previously allocated buffer. */
00205         if (nb->nb_flags & type) {
00206             if (nbd->sz < size) {
00207                 /* Existing buffer is too small. */
00208                 NutHeapFree(nbd->vp);
00209                 nbd->sz = 0;
00210             } else {
00211                 /* 
00212                  * Reduce the size. This is actually a bad idea, because
00213                  * we may waste too much memory. One option would be to
00214                  * use the new NutHeapRealloc, but not sure if it will
00215                  * work for 32-bit CPUs after some crashes after its
00216                  * introduction. Further, I'm not fully sure, if our
00217                  * network routines actually re-use network buffers.
00218                  */
00219                 nbd->sz = size;
00220             }
00221         } else {
00222             /* Buffer was not allocated from heap. */
00223             nbd->sz = 0;
00224         }
00225         /* If the size is zero at this point, 
00226            we need to allocate a new buffer. */
00227         if (nbd->sz == 0) {
00228             if (NutNetBufAllocData(nbd, size)) {
00229                 /* Out of memory, release the complete net buffer. */
00230                 NutNetBufFree(nb);
00231                 nb = NULL;
00232             } else {
00233                 /* Success, mark the buffer allocated. */
00234                 nb->nb_flags |= type;
00235             }
00236         }
00237     }
00238     return nb;
00239 }
00240 
00255 NETBUF *NutNetBufClonePart(NETBUF * nb, uint8_t inserts)
00256 {
00257     NETBUF *cb;
00258     
00259     NUTASSERT(nb != NULL);
00260 
00261     if ((nb->nb_flags & NBAF_REFCNT) == NBAF_REFCNT) {
00262         inserts = NBAF_ALL;
00263     }
00264 
00265     cb = NutHeapAllocClear(sizeof(NETBUF));
00266     if (cb) {
00267         uint_fast8_t referenced = 0;
00268         register int e = 0;
00269         register int s;
00270 
00271         s = nb->nb_dl.sz;
00272         if (s) {
00273             if (inserts & NBAF_DATALINK) {
00274                 e = NutNetBufAllocData(&cb->nb_dl, s);
00275                 if (e == 0) {
00276                     memcpy(cb->nb_dl.vp, nb->nb_dl.vp, s);
00277                     cb->nb_flags |= NBAF_DATALINK;
00278                 }
00279             } else {
00280                 cb->nb_dl.vp = nb->nb_dl.vp;
00281                 cb->nb_dl.sz = s;
00282                 referenced = 1;
00283             }
00284         }
00285         s = nb->nb_nw.sz;
00286         if (s && e == 0) {
00287             if (inserts & NBAF_NETWORK) {
00288                 e = NutNetBufAllocData(&cb->nb_nw, s);
00289                 if (e == 0) {
00290                     memcpy(cb->nb_nw.vp, nb->nb_nw.vp, s);
00291                     cb->nb_flags |= NBAF_NETWORK;
00292                 }
00293             } else {
00294                 cb->nb_nw.vp = nb->nb_nw.vp;
00295                 cb->nb_nw.sz = s;
00296                 referenced = 1;
00297             }
00298         }
00299         s = nb->nb_tp.sz;
00300         if (s && e == 0) {
00301             if (inserts & NBAF_TRANSPORT) {
00302                 e = NutNetBufAllocData(&cb->nb_tp, s);
00303                 if (e == 0) {
00304                     memcpy(cb->nb_tp.vp, nb->nb_tp.vp, s);
00305                     cb->nb_flags |= NBAF_TRANSPORT;
00306                 }
00307             } else {
00308                 cb->nb_tp.vp = nb->nb_tp.vp;
00309                 cb->nb_tp.sz = s;
00310                 referenced = 1;
00311             }
00312         }
00313         s = nb->nb_ap.sz;
00314         if (s && e == 0) {
00315             if (inserts & NBAF_APPLICATION) {
00316                 e = NutNetBufAllocData(&cb->nb_ap, s);
00317                 if (e == 0) {
00318                     memcpy(cb->nb_ap.vp, nb->nb_ap.vp, s);
00319                     cb->nb_flags |= NBAF_APPLICATION;
00320                 }
00321             } else {
00322                 cb->nb_ap.vp = nb->nb_ap.vp;
00323                 cb->nb_ap.sz = s;
00324                 referenced = 1;
00325             }
00326         }
00327         if (e) {
00328             NutNetBufFree(cb);
00329             cb = NULL;
00330         } 
00331         else if (referenced) {
00332             cb->nb_ref = nb;
00333             nb->nb_flags++;
00334         }
00335     }
00336     return cb;
00337 }
00338 
00349 NETBUF *NutNetBufClone(NETBUF * nb)
00350 {
00351     return NutNetBufClonePart(nb, NBAF_ALL);
00352 }
00353 
00364 void NutNetBufFree(NETBUF * nb)
00365 {
00366     NUTASSERT(nb);
00367 
00368     if (nb->nb_flags & NBAF_REFCNT) {
00369         nb->nb_flags--;
00370     } else {
00371         if (nb->nb_flags & NBAF_DATALINK) {
00372             NutHeapFree(nb->nb_dl.vp);
00373         }
00374         if (nb->nb_flags & NBAF_NETWORK) {
00375             NutHeapFree(nb->nb_nw.vp);
00376         }
00377         if (nb->nb_flags & NBAF_TRANSPORT) {
00378             NutHeapFree(nb->nb_tp.vp);
00379         }
00380         if (nb->nb_flags & NBAF_APPLICATION) {
00381             NutHeapFree(nb->nb_ap.vp);
00382         }
00383         if (nb->nb_ref) {
00384             NutNetBufFree(nb->nb_ref);
00385         }
00386         NutHeapFree(nb);
00387     }
00388 }
00389 
00397 int NutNetBufCollect(NETBUF * nbq, int total)
00398 {
00399     int rc = 0;
00400     NETBUF *nb;
00401     NETBUF *nbx;
00402     NBDATA nbd;
00403     uint8_t *ap;
00404 
00405     if (NutNetBufAllocData(&nbd, total) == 0) {
00406         ap = (uint8_t *) nbd.vp;
00407         memcpy(nbd.vp, nbq->nb_ap.vp, nbq->nb_ap.sz);
00408         nbd.sz = nbq->nb_ap.sz;
00409         nb = nbq->nb_next;
00410         while (nb && total >= nbd.sz + nb->nb_ap.sz) {
00411             nbx = nb->nb_next;
00412             memcpy(ap + nbd.sz, nb->nb_ap.vp, nb->nb_ap.sz);
00413             nbd.sz += nb->nb_ap.sz;
00414             NutNetBufFree(nb);
00415             nb = nbx;
00416             rc++;
00417         }
00418         nbq->nb_next = nb;
00419         if (nbq->nb_flags & NBAF_APPLICATION) {
00420             NutHeapFree(nbq->nb_ap.vp);
00421         } else {
00422             nbq->nb_flags |= NBAF_APPLICATION;
00423         }
00424         nbq->nb_ap.vp = nbd.vp;
00425         nbq->nb_ap.sz = nbd.sz;
00426     }
00427     return rc;
00428 }
00429