|
@@ -18,114 +18,127 @@
|
18
|
18
|
|
19
|
19
|
#include "stddef.h"
|
20
|
20
|
#include "string.h"
|
|
21
|
+#include "io.h"
|
21
|
22
|
#include "buffer.h"
|
22
|
23
|
|
23
|
24
|
/*
|
24
|
|
- * Split a free block at the specified address, to produce two
|
25
|
|
- * consecutive free blocks. If the address is not within the free
|
26
|
|
- * block, do nothing and return success. If one of the resulting free
|
27
|
|
- * blocks would be too small to contain the free block descriptor,
|
28
|
|
- * return failure.
|
|
25
|
+ * Initialise a buffer
|
29
|
26
|
*
|
30
|
27
|
*/
|
31
|
|
-static int split_free_block ( struct buffer_free_block *block, void *split ) {
|
32
|
|
- struct buffer_free_block *new_block = split;
|
|
28
|
+void init_buffer ( struct buffer *buffer, physaddr_t start, size_t len ) {
|
|
29
|
+ buffer->start = start;
|
|
30
|
+ buffer->end = start + len;
|
|
31
|
+ buffer->first_free = start;
|
33
|
32
|
|
34
|
|
- if ( ( split <= ( void * ) block ) || ( split >= block->end ) ) {
|
35
|
|
- /* Split is outside block; nothing to do */
|
36
|
|
- return 1;
|
37
|
|
- }
|
38
|
|
-
|
39
|
|
- if ( ( ( block + 1 ) > new_block ) ||
|
40
|
|
- ( ( ( void * ) ( new_block + 1 ) ) > block->end ) ) {
|
41
|
|
- /* Split block would be too small; fail */
|
42
|
|
- return 0;
|
|
33
|
+ if ( len ) {
|
|
34
|
+ char tail = 1;
|
|
35
|
+ copy_to_phys ( start, &tail, sizeof ( tail ) );
|
43
|
36
|
}
|
44
|
|
-
|
45
|
|
- /* Create new block, link into free list */
|
46
|
|
- new_block->next = block->next;
|
47
|
|
- new_block->next->prev = new_block;
|
48
|
|
- new_block->prev = block->prev;
|
49
|
|
- new_block->end = block->end;
|
50
|
|
- block->next = new_block;
|
51
|
|
- block->end = new_block;
|
52
|
|
- return 1;
|
53
|
37
|
}
|
54
|
38
|
|
55
|
39
|
/*
|
56
|
|
- * Remove a block from the free list.
|
57
|
|
- *
|
58
|
|
- * Note that this leaves block->next intact.
|
|
40
|
+ * Split a free block
|
59
|
41
|
*
|
60
|
42
|
*/
|
61
|
|
-static inline void unfree_block ( struct buffer_free_block *block ) {
|
62
|
|
- block->prev->next = block->next;
|
63
|
|
- block->next->prev = block->prev;
|
64
|
|
-}
|
|
43
|
+static void split_free_block ( struct buffer_free_block *desc,
|
|
44
|
+ physaddr_t block, physaddr_t split ) {
|
|
45
|
+ /* If split point is before start of block, do nothing */
|
|
46
|
+ if ( split <= block )
|
|
47
|
+ return;
|
65
|
48
|
|
66
|
|
-/*
|
67
|
|
- * Mark a stretch of memory within a buffer as allocated.
|
68
|
|
- *
|
69
|
|
- */
|
70
|
|
-static inline int mark_allocated ( struct buffer *buffer,
|
71
|
|
- void *start, void *end ) {
|
72
|
|
- struct buffer_free_block *block = buffer->free_blocks.next;
|
73
|
|
-
|
74
|
|
- while ( block != &buffer->free_blocks ) {
|
75
|
|
- if ( ! ( split_free_block ( block, start ) &&
|
76
|
|
- split_free_block ( block, end ) ) ) {
|
77
|
|
- /* Block split failure; fail */
|
78
|
|
- return 0;
|
79
|
|
- }
|
80
|
|
- /* At this point, block can be entirely contained
|
81
|
|
- * within [start,end), but it can't overlap.
|
82
|
|
- */
|
83
|
|
- if ( ( ( ( void * ) block ) >= start ) &&
|
84
|
|
- ( ( ( void * ) block ) < end ) ) {
|
85
|
|
- unfree_block ( block );
|
86
|
|
- }
|
87
|
|
- block = block->next;
|
88
|
|
- }
|
|
49
|
+ /* If split point is after end of block, do nothing */
|
|
50
|
+ if ( split >= desc->end )
|
|
51
|
+ return;
|
|
52
|
+
|
|
53
|
+ /* Create descriptor for new free block */
|
|
54
|
+ copy_to_phys ( split, &desc->tail, sizeof ( desc->tail ) );
|
|
55
|
+ if ( ! desc->tail )
|
|
56
|
+ copy_to_phys ( split, desc, sizeof ( *desc ) );
|
89
|
57
|
|
90
|
|
- return 1;
|
|
58
|
+ /* Update descriptor for old free block */
|
|
59
|
+ desc->tail = 0;
|
|
60
|
+ desc->next_free = split;
|
|
61
|
+ desc->end = split;
|
|
62
|
+ copy_to_phys ( block, desc, sizeof ( *desc ) );
|
91
|
63
|
}
|
92
|
64
|
|
93
|
65
|
/*
|
94
|
|
- * Place data into a buffer
|
|
66
|
+ * Mark a free block as used
|
95
|
67
|
*
|
96
|
68
|
*/
|
97
|
|
-int fill_buffer ( struct buffer *buffer, void *data,
|
98
|
|
- off_t offset, size_t len ) {
|
99
|
|
- void *start = buffer->start + offset;
|
100
|
|
- void *end = start + len;
|
101
|
|
-
|
102
|
|
- if ( ! mark_allocated ( buffer, start, end ) ) {
|
103
|
|
- /* Allocation failure; fail */
|
104
|
|
- return 0;
|
|
69
|
+static inline void unfree_block ( struct buffer *buffer,
|
|
70
|
+ struct buffer_free_block *desc,
|
|
71
|
+ physaddr_t prev_block ) {
|
|
72
|
+ struct buffer_free_block prev_desc;
|
|
73
|
+
|
|
74
|
+ /* If this is the first block, just update first_free */
|
|
75
|
+ if ( ! prev_block ) {
|
|
76
|
+ buffer->first_free = desc->next_free;
|
|
77
|
+ return;
|
105
|
78
|
}
|
106
|
|
- memcpy ( start, data, len );
|
107
|
|
- return 1;
|
|
79
|
+
|
|
80
|
+ /* Get descriptor for previous block (which cannot be a tail block) */
|
|
81
|
+ copy_from_phys ( &prev_desc, prev_block, sizeof ( prev_desc ) );
|
|
82
|
+
|
|
83
|
+ /* Modify descriptor for previous block and write it back */
|
|
84
|
+ prev_desc.next_free = desc->next_free;
|
|
85
|
+ copy_to_phys ( prev_block, &prev_desc, sizeof ( prev_desc ) );
|
108
|
86
|
}
|
109
|
87
|
|
110
|
88
|
/*
|
111
|
|
- * Initialise a buffer
|
|
89
|
+ * Write data into a buffer
|
|
90
|
+ *
|
|
91
|
+ * It is the caller's responsibility to ensure that the boundaries
|
|
92
|
+ * between data blocks are more than sizeof(struct buffer_free_block)
|
|
93
|
+ * apart. If this condition is not satisfied, data corruption will
|
|
94
|
+ * occur.
|
|
95
|
+ *
|
|
96
|
+ * Returns the offset to the first gap in the buffer. (When the
|
|
97
|
+ * buffer is full, returns the offset to the byte past the end of the
|
|
98
|
+ * buffer.)
|
112
|
99
|
*
|
113
|
100
|
*/
|
114
|
|
-static void init_buffer ( struct buffer *buffer, void *start, size_t len ) {
|
115
|
|
- struct buffer_free_block *block;
|
|
101
|
+off_t fill_buffer ( struct buffer *buffer, void *data,
|
|
102
|
+ off_t offset, size_t len ) {
|
|
103
|
+ struct buffer_free_block desc;
|
|
104
|
+ physaddr_t block, prev_block;
|
|
105
|
+ physaddr_t data_start, data_end;
|
116
|
106
|
|
117
|
|
- block = start;
|
118
|
|
- block->next = &buffer->free_blocks;
|
119
|
|
- block->prev = &buffer->free_blocks;
|
120
|
|
- block->end = start + len;
|
|
107
|
+ /* Calculate start and end addresses of data */
|
|
108
|
+ data_start = buffer->start + offset;
|
|
109
|
+ data_end = data_start + len;
|
121
|
110
|
|
122
|
|
- buffer->free_blocks.next = block;
|
123
|
|
- buffer->free_blocks.prev = block;
|
124
|
|
- buffer->start = start;
|
125
|
|
- buffer->end = start + len;
|
126
|
|
-}
|
|
111
|
+ /* Iterate through the buffer's free blocks */
|
|
112
|
+ prev_block = 0;
|
|
113
|
+ block = buffer->first_free;
|
|
114
|
+ while ( block < buffer->end ) {
|
|
115
|
+ /* Read block descriptor */
|
|
116
|
+ desc.next_free = buffer->end;
|
|
117
|
+ desc.end = buffer->end;
|
|
118
|
+ copy_from_phys ( &desc.tail, block, sizeof ( desc.tail ) );
|
|
119
|
+ if ( ! desc.tail )
|
|
120
|
+ copy_from_phys ( &desc, block, sizeof ( desc ) );
|
127
|
121
|
|
128
|
|
-/*
|
129
|
|
- * Move a buffer
|
130
|
|
- *
|
131
|
|
- */
|
|
122
|
+ /* Split block at data start and end markers */
|
|
123
|
+ split_free_block ( &desc, block, data_start );
|
|
124
|
+ split_free_block ( &desc, block, data_end );
|
|
125
|
+
|
|
126
|
+ /* Block is now either completely contained by or
|
|
127
|
+ * completely outside the data area
|
|
128
|
+ */
|
|
129
|
+ if ( ( block >= data_start ) && ( block <= data_end ) ) {
|
|
130
|
+ /* Block is within the data area */
|
|
131
|
+ unfree_block ( buffer, &desc, prev_block );
|
|
132
|
+ copy_to_phys ( block, data + ( block - data_start ),
|
|
133
|
+ desc.end - block );
|
|
134
|
+ } else {
|
|
135
|
+ /* Block is outside the data area */
|
|
136
|
+ prev_block = block;
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ /* Move to next free block */
|
|
140
|
+ block = desc.next_free;
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ return ( buffer->first_free - buffer->start );
|
|
144
|
+}
|