Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / net / netbuff.c
blobe97ecd23e9c5aa33f74534e7a43687dbd7c27752
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/err.h>
20 #include <grub/misc.h>
21 #include <grub/mm.h>
22 #include <grub/net/netbuff.h>
24 grub_err_t
25 grub_netbuff_put (struct grub_net_buff *nb, grub_size_t len)
27 nb->tail += len;
28 if (nb->tail > nb->end)
29 return grub_error (GRUB_ERR_BUG, "put out of the packet range.");
30 return GRUB_ERR_NONE;
33 grub_err_t
34 grub_netbuff_unput (struct grub_net_buff *nb, grub_size_t len)
36 nb->tail -= len;
37 if (nb->tail < nb->head)
38 return grub_error (GRUB_ERR_BUG,
39 "unput out of the packet range.");
40 return GRUB_ERR_NONE;
43 grub_err_t
44 grub_netbuff_push (struct grub_net_buff *nb, grub_size_t len)
46 nb->data -= len;
47 if (nb->data < nb->head)
48 return grub_error (GRUB_ERR_BUG,
49 "push out of the packet range.");
50 return GRUB_ERR_NONE;
53 grub_err_t
54 grub_netbuff_pull (struct grub_net_buff *nb, grub_size_t len)
56 nb->data += len;
57 if (nb->data > nb->end)
58 return grub_error (GRUB_ERR_BUG,
59 "pull out of the packet range.");
60 return GRUB_ERR_NONE;
63 grub_err_t
64 grub_netbuff_reserve (struct grub_net_buff *nb, grub_size_t len)
66 nb->data += len;
67 nb->tail += len;
68 if ((nb->tail > nb->end) || (nb->data > nb->end))
69 return grub_error (GRUB_ERR_BUG,
70 "reserve out of the packet range.");
71 return GRUB_ERR_NONE;
74 struct grub_net_buff *
75 grub_netbuff_alloc (grub_size_t len)
77 struct grub_net_buff *nb;
78 void *data;
80 COMPILE_TIME_ASSERT (NETBUFF_ALIGN % sizeof (grub_properly_aligned_t) == 0);
82 if (len < NETBUFFMINLEN)
83 len = NETBUFFMINLEN;
85 len = ALIGN_UP (len, NETBUFF_ALIGN);
86 #ifdef GRUB_MACHINE_EMU
87 data = grub_malloc (len + sizeof (*nb));
88 #else
89 data = grub_memalign (NETBUFF_ALIGN, len + sizeof (*nb));
90 #endif
91 if (!data)
92 return NULL;
93 nb = (struct grub_net_buff *) ((grub_properly_aligned_t *) data
94 + len / sizeof (grub_properly_aligned_t));
95 nb->head = nb->data = nb->tail = data;
96 nb->end = (grub_uint8_t *) nb;
97 return nb;
100 void
101 grub_netbuff_free (struct grub_net_buff *nb)
103 if (!nb)
104 return;
105 grub_free (nb->head);
108 grub_err_t
109 grub_netbuff_clear (struct grub_net_buff *nb)
111 nb->data = nb->tail = nb->head;
112 return GRUB_ERR_NONE;