2 * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include "packet_buffer.h"
9 #include <util/ring_buffer.h>
16 create_packet_buffer(size_t size
)
19 = (packet_buffer
*)malloc(sizeof(packet_buffer
));
23 buffer
->buffer
= create_ring_buffer(size
);
24 if (buffer
->buffer
== NULL
) {
28 B_INITIALIZE_SPINLOCK(&buffer
->lock
);
35 delete_packet_buffer(packet_buffer
* buffer
)
37 delete_ring_buffer(buffer
->buffer
);
43 packet_buffer_clear(packet_buffer
* buffer
)
45 cpu_status state
= disable_interrupts();
46 acquire_spinlock(&buffer
->lock
);
48 ring_buffer_clear(buffer
->buffer
);
50 release_spinlock(&buffer
->lock
);
51 restore_interrupts(state
);
56 packet_buffer_readable(packet_buffer
* buffer
)
58 cpu_status state
= disable_interrupts();
59 acquire_spinlock(&buffer
->lock
);
61 size_t available
= ring_buffer_readable(buffer
->buffer
);
63 release_spinlock(&buffer
->lock
);
64 restore_interrupts(state
);
71 packet_buffer_writable(packet_buffer
* buffer
)
73 cpu_status state
= disable_interrupts();
74 acquire_spinlock(&buffer
->lock
);
76 size_t left
= ring_buffer_writable(buffer
->buffer
);
78 release_spinlock(&buffer
->lock
);
79 restore_interrupts(state
);
86 packet_buffer_flush(packet_buffer
* buffer
, size_t length
)
88 cpu_status state
= disable_interrupts();
89 acquire_spinlock(&buffer
->lock
);
91 ring_buffer_flush(buffer
->buffer
, length
);
93 release_spinlock(&buffer
->lock
);
94 restore_interrupts(state
);
99 packet_buffer_read(packet_buffer
* buffer
, uint8
*data
, size_t length
)
101 cpu_status state
= disable_interrupts();
102 acquire_spinlock(&buffer
->lock
);
104 size_t bytesRead
= ring_buffer_read(buffer
->buffer
, data
, length
);
106 release_spinlock(&buffer
->lock
);
107 restore_interrupts(state
);
114 packet_buffer_write(packet_buffer
* buffer
, const uint8
*data
,
117 cpu_status state
= disable_interrupts();
118 acquire_spinlock(&buffer
->lock
);
120 size_t bytesWritten
= ring_buffer_write(buffer
->buffer
, data
, length
);
122 release_spinlock(&buffer
->lock
);
123 restore_interrupts(state
);