BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / bus_managers / ps2 / packet_buffer.cpp
bloba8b1b26aee1df66f80123d7409025bc8caef5bc7
1 /*
2 * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "packet_buffer.h"
9 #include <util/ring_buffer.h>
11 #include <stdlib.h>
12 #include <string.h>
15 packet_buffer*
16 create_packet_buffer(size_t size)
18 packet_buffer* buffer
19 = (packet_buffer *)malloc(sizeof(packet_buffer));
20 if (buffer == NULL)
21 return NULL;
23 buffer->buffer = create_ring_buffer(size);
24 if (buffer->buffer == NULL) {
25 free(buffer);
26 return NULL;
28 B_INITIALIZE_SPINLOCK(&buffer->lock);
30 return buffer;
34 void
35 delete_packet_buffer(packet_buffer* buffer)
37 delete_ring_buffer(buffer->buffer);
38 free(buffer);
42 void
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);
55 size_t
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);
66 return available;
70 size_t
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);
81 return left;
85 void
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);
98 size_t
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);
109 return bytesRead;
113 size_t
114 packet_buffer_write(packet_buffer* buffer, const uint8 *data,
115 size_t length)
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);
125 return bytesWritten;