ia64/xen-unstable

view tools/vnet/libxutil/string_stream.c @ 19848:5839491bbf20

[IA64] replace MAX_VCPUS with d->max_vcpus where necessary.

don't use MAX_VCPUS, and use vcpu::max_vcpus.
The changeset of 2f9e1348aa98 introduced max_vcpus to allow more vcpus
per guest. This patch is ia64 counter part.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Mon Jun 29 11:26:05 2009 +0900 (2009-06-29)
parents 9f22db685802
children
line source
1 /*
2 * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
19 /** @file
20 * IOStream subtype for input and output to strings.
21 * Usable from user or kernel code (with __KERNEL__ defined).
22 */
24 #include "sys_string.h"
25 #include "string_stream.h"
26 #include "allocate.h"
28 static int string_error(IOStream *io);
29 static int string_close(IOStream *io);
30 static void string_free(IOStream *io);
31 static int string_write(IOStream *io, const void *msg, size_t n);
32 static int string_read(IOStream *io, void *buf, size_t n);
34 /** Methods for a string stream. */
35 static IOMethods string_methods = {
36 read: string_read,
37 write: string_write,
38 error: string_error,
39 close: string_close,
40 free: string_free,
41 };
43 /** Get the string stream state.
44 *
45 * @param io string stream
46 * @return state
47 */
48 static inline StringData *get_string_data(IOStream *io){
49 return (StringData*)io->data;
50 }
52 static int string_write(IOStream *io, const void *msg, size_t n){
53 StringData *data = get_string_data(io);
54 int k;
56 k = data->end - data->out;
57 if(n > k) n = k;
58 memcpy(data->out, msg, n);
59 data->out += n;
60 return n;
61 }
63 static int string_read(IOStream *io, void *buf, size_t n){
64 StringData *data = get_string_data(io);
65 int k;
67 k = data->end - data->in;
68 if(n > k) n = k;
69 memcpy(buf, data->in, k);
70 data->in += n;
71 return n;
72 }
74 /** Test if a string stream has an error.
75 *
76 * @param io string stream
77 * @return 0 if ok, error code otherwise
78 */
79 static int string_error(IOStream *io){
80 StringData *data = get_string_data(io);
81 return data->out == NULL;
82 }
84 /** Close a string stream.
85 *
86 * @param io string stream
87 * @return 0
88 */
89 static int string_close(IOStream *io){
90 StringData *data = get_string_data(io);
91 data->in = NULL;
92 data->out = NULL;
93 return 0;
94 }
96 /** Free a string stream.
97 * The stream state is freed, but the underlying string is not.
98 *
99 * @param io string stream
100 */
101 static void string_free(IOStream *io){
102 StringData *data = get_string_data(io);
103 memzero(data, sizeof(*data));
104 deallocate(data);
105 }
107 /** Get the methods to use for a string stream.
108 *
109 * @return methods
110 */
111 IOMethods *string_stream_get_methods(void){
112 return &string_methods;
113 }
115 /** Initialise a string stream, usually from static data.
116 * If the stream and StringData should be freed when
117 * the stream is closed, unset io->nofree.
118 * The string is not freed on close.
119 *
120 * @param io address of IOStream to fill in
121 * @param data address of StringData to fill in
122 * @param s string to use
123 * @param n length of the string
124 */
125 void string_stream_init(IOStream *io, StringData *data, char *s, int n){
126 if(data && io){
127 memzero(data, sizeof(*data));
128 data->string = (char*)s;
129 data->in = data->string;
130 data->out = data->string;
131 data->size = n;
132 data->end = data->string + n;
133 memzero(io, sizeof(*io));
134 io->methods = &string_methods;
135 io->data = data;
136 io->nofree = 1;
137 }
138 }
140 /** Allocate and initialise a string stream.
141 * The stream is freed on close, but the string is not.
142 *
143 * @param s string to use
144 * @param n length of the string
145 * @return new stream (free using IOStream_free)
146 */
147 IOStream *string_stream_new(char *s, int n){
148 int ok = 0;
149 StringData *data = ALLOCATE(StringData);
150 IOStream *io = ALLOCATE(IOStream);
151 if(data && io){
152 ok = 1;
153 string_stream_init(io, data, s, n);
154 io->nofree = 0;
155 }
156 if(!ok){
157 deallocate(data);
158 deallocate(io);
159 io = NULL;
160 }
161 return io;
162 }