1 /*
2 * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7 #include "packet_buffer.h"
8
9 #include <util/ring_buffer.h>
10
11 #include <stdlib.h>
12 #include <string.h>
13
14
15 packet_buffer*
create_packet_buffer(size_t size)16 create_packet_buffer(size_t size)
17 {
18 packet_buffer* buffer
19 = (packet_buffer *)malloc(sizeof(packet_buffer));
20 if (buffer == NULL)
21 return NULL;
22
23 buffer->buffer = create_ring_buffer(size);
24 if (buffer->buffer == NULL) {
25 free(buffer);
26 return NULL;
27 }
28 B_INITIALIZE_SPINLOCK(&buffer->lock);
29
30 return buffer;
31 }
32
33
34 void
delete_packet_buffer(packet_buffer * buffer)35 delete_packet_buffer(packet_buffer* buffer)
36 {
37 delete_ring_buffer(buffer->buffer);
38 free(buffer);
39 }
40
41
42 void
packet_buffer_clear(packet_buffer * buffer)43 packet_buffer_clear(packet_buffer* buffer)
44 {
45 cpu_status state = disable_interrupts();
46 acquire_spinlock(&buffer->lock);
47
48 ring_buffer_clear(buffer->buffer);
49
50 release_spinlock(&buffer->lock);
51 restore_interrupts(state);
52 }
53
54
55 size_t
packet_buffer_readable(packet_buffer * buffer)56 packet_buffer_readable(packet_buffer* buffer)
57 {
58 cpu_status state = disable_interrupts();
59 acquire_spinlock(&buffer->lock);
60
61 size_t available = ring_buffer_readable(buffer->buffer);
62
63 release_spinlock(&buffer->lock);
64 restore_interrupts(state);
65
66 return available;
67 }
68
69
70 size_t
packet_buffer_writable(packet_buffer * buffer)71 packet_buffer_writable(packet_buffer* buffer)
72 {
73 cpu_status state = disable_interrupts();
74 acquire_spinlock(&buffer->lock);
75
76 size_t left = ring_buffer_writable(buffer->buffer);
77
78 release_spinlock(&buffer->lock);
79 restore_interrupts(state);
80
81 return left;
82 }
83
84
85 void
packet_buffer_flush(packet_buffer * buffer,size_t length)86 packet_buffer_flush(packet_buffer* buffer, size_t length)
87 {
88 cpu_status state = disable_interrupts();
89 acquire_spinlock(&buffer->lock);
90
91 ring_buffer_flush(buffer->buffer, length);
92
93 release_spinlock(&buffer->lock);
94 restore_interrupts(state);
95 }
96
97
98 size_t
packet_buffer_read(packet_buffer * buffer,uint8 * data,size_t length)99 packet_buffer_read(packet_buffer* buffer, uint8 *data, size_t length)
100 {
101 cpu_status state = disable_interrupts();
102 acquire_spinlock(&buffer->lock);
103
104 size_t bytesRead = ring_buffer_read(buffer->buffer, data, length);
105
106 release_spinlock(&buffer->lock);
107 restore_interrupts(state);
108
109 return bytesRead;
110 }
111
112
113 size_t
packet_buffer_write(packet_buffer * buffer,const uint8 * data,size_t length)114 packet_buffer_write(packet_buffer* buffer, const uint8 *data,
115 size_t length)
116 {
117 cpu_status state = disable_interrupts();
118 acquire_spinlock(&buffer->lock);
119
120 size_t bytesWritten = ring_buffer_write(buffer->buffer, data, length);
121
122 release_spinlock(&buffer->lock);
123 restore_interrupts(state);
124
125 return bytesWritten;
126 }
127
128