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/>.
20 #include <grub/misc.h>
22 #include <grub/net/netbuff.h>
25 grub_netbuff_put (struct grub_net_buff
*nb
, grub_size_t len
)
28 if (nb
->tail
> nb
->end
)
29 return grub_error (GRUB_ERR_BUG
, "put out of the packet range.");
34 grub_netbuff_unput (struct grub_net_buff
*nb
, grub_size_t len
)
37 if (nb
->tail
< nb
->head
)
38 return grub_error (GRUB_ERR_BUG
,
39 "unput out of the packet range.");
44 grub_netbuff_push (struct grub_net_buff
*nb
, grub_size_t len
)
47 if (nb
->data
< nb
->head
)
48 return grub_error (GRUB_ERR_BUG
,
49 "push out of the packet range.");
54 grub_netbuff_pull (struct grub_net_buff
*nb
, grub_size_t len
)
57 if (nb
->data
> nb
->end
)
58 return grub_error (GRUB_ERR_BUG
,
59 "pull out of the packet range.");
64 grub_netbuff_reserve (struct grub_net_buff
*nb
, grub_size_t 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.");
74 struct grub_net_buff
*
75 grub_netbuff_alloc (grub_size_t len
)
77 struct grub_net_buff
*nb
;
80 COMPILE_TIME_ASSERT (NETBUFF_ALIGN
% sizeof (grub_properly_aligned_t
) == 0);
82 if (len
< NETBUFFMINLEN
)
85 len
= ALIGN_UP (len
, NETBUFF_ALIGN
);
86 #ifdef GRUB_MACHINE_EMU
87 data
= grub_malloc (len
+ sizeof (*nb
));
89 data
= grub_memalign (NETBUFF_ALIGN
, len
+ sizeof (*nb
));
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
;
101 grub_netbuff_free (struct grub_net_buff
*nb
)
105 grub_free (nb
->head
);
109 grub_netbuff_clear (struct grub_net_buff
*nb
)
111 nb
->data
= nb
->tail
= nb
->head
;
112 return GRUB_ERR_NONE
;