2 ** File: netbuff.c Jun. 10, 2000
4 ** Author: Giovanni Falzoni <gfalzoni@inwind.it>
6 ** This file contains specific implementation of buffering
7 ** for network packets.
10 #include <minix/drivers.h>
11 #include <minix/netdriver.h>
14 #if (HAVE_BUFFERS == 1)
16 static m_hdr_t
*allocptr
= NULL
;
17 static char tx_rx_buff
[8192];
21 ** Function: Allocates a buffer from the common pool.
23 void *alloc_buff(dpeth_t
*dep
, int size
)
25 m_hdr_t
*ptr
, *wrk
= allocptr
;
26 int units
= ((size
+ sizeof(m_hdr_t
) - 1) / sizeof(m_hdr_t
)) + 1;
28 for (ptr
= wrk
->next
;; wrk
= ptr
, ptr
= ptr
->next
) {
29 if (ptr
->size
>= units
) {
30 /* Memory is available, carve requested size from pool */
31 if (ptr
->size
== units
) {
32 wrk
->next
= ptr
->next
;
34 /* Get memory from top address */
42 if (ptr
== allocptr
) break;
44 return NULL
; /* No memory available */
49 ** Function: Returns a buffer to the common pool.
51 void free_buff(dpeth_t
*dep
, void *blk
)
53 m_hdr_t
*wrk
, *ptr
= (m_hdr_t
*) blk
- 1;
55 /* Scan linked list for the correct place */
56 for (wrk
= allocptr
; !(ptr
> wrk
&& ptr
< wrk
->next
); wrk
= wrk
->next
)
57 if (wrk
>= wrk
->next
&& (ptr
> wrk
|| ptr
< wrk
->next
)) break;
59 /* Check if adjacent block is free and join blocks */
60 if (ptr
+ ptr
->size
== wrk
->next
) {
61 ptr
->size
+= wrk
->next
->size
;
62 ptr
->next
= wrk
->next
->next
;
64 ptr
->next
= wrk
->next
;
65 if (wrk
+ wrk
->size
== ptr
) {
66 wrk
->size
+= ptr
->size
;
67 wrk
->next
= ptr
->next
;
70 allocptr
= wrk
; /* Point allocptr to block just released */
75 ** Function: Initalizes driver data structures.
77 void init_buff(dpeth_t
*dep
, buff_t
**tx_buff
)
80 /* Initializes buffer pool */
81 if (allocptr
== NULL
) {
82 m_hdr_t
*rx
= (m_hdr_t
*) tx_rx_buff
;
83 rx
->next
= allocptr
= rx
;
87 rx
->size
= (sizeof(tx_rx_buff
) / sizeof(m_hdr_t
)) - 1;
88 free_buff(dep
, rx
+ 1);
89 dep
->de_recvq_tail
= dep
->de_recvq_head
= NULL
;
90 if (tx_buff
!= NULL
) {
91 *tx_buff
= alloc_buff(dep
,
92 NDEV_ETH_PACKET_MAX
+ sizeof(buff_t
));
98 #endif /* HAVE_BUFFERS */