1 |
27 |
unneback |
#ifndef CYGONCE_LIBC_STDIO_STREAMBUF_INL
|
2 |
|
|
#define CYGONCE_LIBC_STDIO_STREAMBUF_INL
|
3 |
|
|
//===========================================================================
|
4 |
|
|
//
|
5 |
|
|
// streambuf.inl
|
6 |
|
|
//
|
7 |
|
|
// Internal C library stdio stream buffer inline functions
|
8 |
|
|
//
|
9 |
|
|
//===========================================================================
|
10 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
11 |
|
|
// -------------------------------------------
|
12 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
13 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
14 |
|
|
//
|
15 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
16 |
|
|
// the terms of the GNU General Public License as published by the Free
|
17 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
18 |
|
|
//
|
19 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
20 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
21 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
22 |
|
|
// for more details.
|
23 |
|
|
//
|
24 |
|
|
// You should have received a copy of the GNU General Public License along
|
25 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
26 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
27 |
|
|
//
|
28 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
29 |
|
|
// or inline functions from this file, or you compile this file and link it
|
30 |
|
|
// with other works to produce a work based on this file, this file does not
|
31 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
32 |
|
|
// License. However the source code for this file must still be made available
|
33 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
36 |
|
|
// this file might be covered by the GNU General Public License.
|
37 |
|
|
//
|
38 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
39 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
40 |
|
|
// -------------------------------------------
|
41 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
42 |
|
|
//===========================================================================
|
43 |
|
|
//#####DESCRIPTIONBEGIN####
|
44 |
|
|
//
|
45 |
|
|
// Author(s): jlarmour
|
46 |
|
|
// Contributors:
|
47 |
|
|
// Date: 2000-04-19
|
48 |
|
|
// Purpose:
|
49 |
|
|
// Description:
|
50 |
|
|
// Usage: Do not include this file directly, instead
|
51 |
|
|
// #include
|
52 |
|
|
//
|
53 |
|
|
//####DESCRIPTIONEND####
|
54 |
|
|
//
|
55 |
|
|
//===========================================================================
|
56 |
|
|
|
57 |
|
|
// CONFIGURATION
|
58 |
|
|
|
59 |
|
|
#include // Configuration header
|
60 |
|
|
|
61 |
|
|
// Include buffered I/O?
|
62 |
|
|
#if defined(CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO)
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
// INCLUDES
|
66 |
|
|
|
67 |
|
|
#include // Common project-wide type definitions
|
68 |
|
|
#include // Assertion support
|
69 |
|
|
#include // Cyg_ErrNo
|
70 |
|
|
#include // fpos_t and iobuf defines
|
71 |
|
|
#include // free()
|
72 |
|
|
#include // header for this file, just in case
|
73 |
|
|
#include // INT_MAX
|
74 |
|
|
|
75 |
|
|
// FUNCTIONS
|
76 |
|
|
|
77 |
|
|
inline
|
78 |
|
|
Cyg_StdioStreamBuffer::Cyg_StdioStreamBuffer( cyg_ucount32 size,
|
79 |
|
|
cyg_uint8 *new_buffer ) :
|
80 |
|
|
#if defined(CYGSEM_LIBC_STDIO_SETVBUF_MALLOC)
|
81 |
|
|
call_free(false),
|
82 |
|
|
buffer_bottom( NULL ),
|
83 |
|
|
buffer_size(0),
|
84 |
|
|
#else
|
85 |
|
|
buffer_bottom( static_buffer ),
|
86 |
|
|
buffer_size(sizeof(static_buffer)),
|
87 |
|
|
#endif
|
88 |
|
|
buffer_top(NULL),
|
89 |
|
|
current_buffer_position(NULL)
|
90 |
|
|
{
|
91 |
|
|
// NB Many of the above members in the initialisation list may seem
|
92 |
|
|
// unnecessary, but it is to ensure a defined state if e.g. the malloc
|
93 |
|
|
// in set_buffer() should fail
|
94 |
|
|
|
95 |
|
|
(void)set_buffer(size, new_buffer);
|
96 |
|
|
} // Cyg_StdioStreamBuffer constructor
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
inline
|
100 |
|
|
Cyg_StdioStreamBuffer::~Cyg_StdioStreamBuffer()
|
101 |
|
|
{
|
102 |
|
|
#ifdef CYGSEM_LIBC_STDIO_SETVBUF_MALLOC
|
103 |
|
|
if ((buffer_bottom != NULL) && call_free)
|
104 |
|
|
free( buffer_bottom );
|
105 |
|
|
#endif
|
106 |
|
|
} // Cyg_StdioStreamBuffer destructor
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
inline cyg_ucount32
|
110 |
|
|
Cyg_StdioStreamBuffer::get_buffer_space_used( void )
|
111 |
|
|
{
|
112 |
|
|
return (buffer_top - current_buffer_position);
|
113 |
|
|
} // get_buffer_space_used()
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
inline cyg_count32
|
117 |
|
|
Cyg_StdioStreamBuffer::get_buffer_size( void )
|
118 |
|
|
{
|
119 |
|
|
#ifdef CYGSEM_LIBC_STDIO_DYNAMIC_SETVBUF
|
120 |
|
|
if (buffer_bottom==NULL)
|
121 |
|
|
return -1;
|
122 |
|
|
#endif
|
123 |
|
|
return buffer_size;
|
124 |
|
|
} // get_buffer_size()
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
inline cyg_ucount32
|
128 |
|
|
Cyg_StdioStreamBuffer::get_buffer_addr_to_read( cyg_uint8 **buffer )
|
129 |
|
|
{
|
130 |
|
|
*buffer = current_buffer_position;
|
131 |
|
|
|
132 |
|
|
return (buffer_top - current_buffer_position);
|
133 |
|
|
} // get_buffer_addr_to_read()
|
134 |
|
|
|
135 |
|
|
inline void
|
136 |
|
|
Cyg_StdioStreamBuffer::set_bytes_read( cyg_ucount32 bytes )
|
137 |
|
|
{
|
138 |
|
|
cyg_uint8 *buffer_max = &buffer_bottom[ get_buffer_size() ];
|
139 |
|
|
|
140 |
|
|
current_buffer_position += bytes;
|
141 |
|
|
|
142 |
|
|
// INT_MAX is used by some callers to mean infinite.
|
143 |
|
|
CYG_ASSERT( (current_buffer_position <= buffer_top)
|
144 |
|
|
|| (get_buffer_size() == INT_MAX),
|
145 |
|
|
"read too many bytes from buffer" );
|
146 |
|
|
|
147 |
|
|
if (current_buffer_position == buffer_max)
|
148 |
|
|
current_buffer_position = buffer_top = &buffer_bottom[0];
|
149 |
|
|
|
150 |
|
|
} // set_bytes_read()
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
inline cyg_ucount32
|
154 |
|
|
Cyg_StdioStreamBuffer::get_buffer_addr_to_write( cyg_uint8 **buffer )
|
155 |
|
|
{
|
156 |
|
|
cyg_uint8 *buffer_max = &buffer_bottom[ get_buffer_size() ];
|
157 |
|
|
|
158 |
|
|
*buffer = buffer_top;
|
159 |
|
|
|
160 |
|
|
return (buffer_max - buffer_top);
|
161 |
|
|
} // get_buffer_addr_to_write()
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
inline void
|
165 |
|
|
Cyg_StdioStreamBuffer::set_bytes_written( cyg_ucount32 bytes )
|
166 |
|
|
{
|
167 |
|
|
buffer_top += bytes;
|
168 |
|
|
|
169 |
|
|
// INT_MAX is used by some callers to mean infinite.
|
170 |
|
|
CYG_ASSERT( (buffer_top <= &buffer_bottom[ get_buffer_size() ])
|
171 |
|
|
|| (get_buffer_size() == INT_MAX),
|
172 |
|
|
"wrote too many bytes into buffer" );
|
173 |
|
|
} // set_bytes_written()
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
inline void
|
177 |
|
|
Cyg_StdioStreamBuffer::drain_buffer( void )
|
178 |
|
|
{
|
179 |
|
|
buffer_top = current_buffer_position = &buffer_bottom[0];
|
180 |
|
|
} // drain_buffer()
|
181 |
|
|
|
182 |
|
|
#endif // if defined(CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO)
|
183 |
|
|
|
184 |
|
|
#endif // CYGONCE_LIBC_STDIO_STREAMBUF_INL multiple inclusion protection
|
185 |
|
|
|
186 |
|
|
// EOF streambuf.inl
|