vaseboot/VasEBoot-core/net/netbuff.c

134 lines
3.5 KiB
C

/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2010 Free Software Foundation, Inc.
*
* VasEBoot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VasEBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/err.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/net/netbuff.h>
VasEBoot_err_t
VasEBoot_netbuff_put (struct VasEBoot_net_buff *nb, VasEBoot_size_t len)
{
nb->tail += len;
if (nb->tail > nb->end)
return VasEBoot_error (VasEBoot_ERR_BUG, "put out of the packet range.");
return VasEBoot_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_netbuff_unput (struct VasEBoot_net_buff *nb, VasEBoot_size_t len)
{
nb->tail -= len;
if (nb->tail < nb->head)
return VasEBoot_error (VasEBoot_ERR_BUG,
"unput out of the packet range.");
return VasEBoot_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_netbuff_push (struct VasEBoot_net_buff *nb, VasEBoot_size_t len)
{
nb->data -= len;
if (nb->data < nb->head)
return VasEBoot_error (VasEBoot_ERR_BUG,
"push out of the packet range.");
return VasEBoot_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_netbuff_pull (struct VasEBoot_net_buff *nb, VasEBoot_size_t len)
{
nb->data += len;
if (nb->data > nb->end)
return VasEBoot_error (VasEBoot_ERR_BUG,
"pull out of the packet range.");
return VasEBoot_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_netbuff_reserve (struct VasEBoot_net_buff *nb, VasEBoot_size_t len)
{
nb->data += len;
nb->tail += len;
if ((nb->tail > nb->end) || (nb->data > nb->end))
return VasEBoot_error (VasEBoot_ERR_BUG,
"reserve out of the packet range.");
return VasEBoot_ERR_NONE;
}
struct VasEBoot_net_buff *
VasEBoot_netbuff_alloc (VasEBoot_size_t len)
{
struct VasEBoot_net_buff *nb;
void *data;
COMPILE_TIME_ASSERT (NETBUFF_ALIGN % sizeof (VasEBoot_properly_aligned_t) == 0);
if (len < NETBUFFMINLEN)
len = NETBUFFMINLEN;
len = ALIGN_UP (len, NETBUFF_ALIGN);
#ifdef VasEBoot_MACHINE_EMU
data = VasEBoot_malloc (len + sizeof (*nb));
#else
data = VasEBoot_memalign (NETBUFF_ALIGN, len + sizeof (*nb));
#endif
if (!data)
return NULL;
nb = (struct VasEBoot_net_buff *) ((VasEBoot_properly_aligned_t *) data
+ len / sizeof (VasEBoot_properly_aligned_t));
nb->head = nb->data = nb->tail = data;
nb->end = (VasEBoot_uint8_t *) nb;
return nb;
}
struct VasEBoot_net_buff *
VasEBoot_netbuff_make_pkt (VasEBoot_size_t len)
{
struct VasEBoot_net_buff *nb;
VasEBoot_err_t err;
nb = VasEBoot_netbuff_alloc (len + 512);
if (!nb)
return NULL;
err = VasEBoot_netbuff_reserve (nb, len + 512);
if (err)
goto fail;
err = VasEBoot_netbuff_push (nb, len);
if (err)
goto fail;
return nb;
fail:
VasEBoot_netbuff_free (nb);
return NULL;
}
void
VasEBoot_netbuff_free (struct VasEBoot_net_buff *nb)
{
if (!nb)
return;
VasEBoot_free (nb->head);
}
VasEBoot_err_t
VasEBoot_netbuff_clear (struct VasEBoot_net_buff *nb)
{
nb->data = nb->tail = nb->head;
return VasEBoot_ERR_NONE;
}