Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1360)

Side by Side Diff: net/base/nss_memio.c

Issue 42013: Slight code change to make some global variables const. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/net_util_unittest.cc ('k') | net/base/ssl_client_socket_nss.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // Written in NSPR style to also be suitable for adding to the NSS demo suite 4 // Written in NSPR style to also be suitable for adding to the NSS demo suite
5 5
6 /* memio is a simple NSPR I/O layer that lets you decouple NSS from 6 /* memio is a simple NSPR I/O layer that lets you decouple NSS from
7 * the real network. It's rather like openssl's memory bio, 7 * the real network. It's rather like openssl's memory bio,
8 * and is useful when your app absolutely, positively doesn't 8 * and is useful when your app absolutely, positively doesn't
9 * want to let NSS do its own networking. 9 * want to let NSS do its own networking.
10 */ 10 */
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 /* Buffer not full */ 161 /* Buffer not full */
162 memcpy(&mb->buf[mb->tail], buf, len); 162 memcpy(&mb->buf[mb->tail], buf, len);
163 mb->tail += len; 163 mb->tail += len;
164 if (mb->tail == mb->bufsize) 164 if (mb->tail == mb->bufsize)
165 mb->tail = 0; 165 mb->tail = 0;
166 n -= len; 166 n -= len;
167 buf += len; 167 buf += len;
168 transferred += len; 168 transferred += len;
169 169
170 /* Handle part after wrap */ 170 /* Handle part after wrap */
171 » len = PR_MIN(n, memio_buffer_unused_contiguous(mb)); 171 len = PR_MIN(n, memio_buffer_unused_contiguous(mb));
172 » if (len > 0) { 172 if (len > 0) {
173 /* Output buffer still not full, input buffer still not empty */ 173 /* Output buffer still not full, input buffer still not empty */
174 » memcpy(&mb->buf[mb->tail], buf, len); 174 memcpy(&mb->buf[mb->tail], buf, len);
175 » mb->tail += len; 175 mb->tail += len;
176 if (mb->tail == mb->bufsize) 176 if (mb->tail == mb->bufsize)
177 mb->tail = 0; 177 mb->tail = 0;
178 » transferred += len; 178 transferred += len;
179 } 179 }
180 } 180 }
181 181
182 return transferred; 182 return transferred;
183 } 183 }
184 184
185 185
186 /* Read n bytes from the buffer. Returns number of bytes read. */ 186 /* Read n bytes from the buffer. Returns number of bytes read. */
187 static int memio_buffer_get(struct memio_buffer *mb, char *buf, int n) 187 static int memio_buffer_get(struct memio_buffer *mb, char *buf, int n)
188 { 188 {
189 int len; 189 int len;
190 int transferred = 0; 190 int transferred = 0;
191 191
192 /* Handle part before wrap */ 192 /* Handle part before wrap */
193 len = PR_MIN(n, memio_buffer_used_contiguous(mb)); 193 len = PR_MIN(n, memio_buffer_used_contiguous(mb));
194 if (len) { 194 if (len) {
195 memcpy(buf, &mb->buf[mb->head], len); 195 memcpy(buf, &mb->buf[mb->head], len);
196 mb->head += len; 196 mb->head += len;
197 if (mb->head == mb->bufsize) 197 if (mb->head == mb->bufsize)
198 mb->head = 0; 198 mb->head = 0;
199 n -= len; 199 n -= len;
200 buf += len; 200 buf += len;
201 transferred += len; 201 transferred += len;
202 202
203 /* Handle part after wrap */ 203 /* Handle part after wrap */
204 » len = PR_MIN(n, memio_buffer_used_contiguous(mb)); 204 len = PR_MIN(n, memio_buffer_used_contiguous(mb));
205 » if (len) { 205 if (len) {
206 » memcpy(buf, &mb->buf[mb->head], len); 206 memcpy(buf, &mb->buf[mb->head], len);
207 » mb->head += len; 207 mb->head += len;
208 if (mb->head == mb->bufsize) 208 if (mb->head == mb->bufsize)
209 mb->head = 0; 209 mb->head = 0;
210 » transferred += len; 210 transferred += len;
211 } 211 }
212 } 212 }
213 213
214 return transferred; 214 return transferred;
215 } 215 }
216 216
217 /*--------------- private memio functions -----------------------*/ 217 /*--------------- private memio functions -----------------------*/
218 218
219 static PRStatus PR_CALLBACK memio_Close(PRFileDesc *fd) 219 static PRStatus PR_CALLBACK memio_Close(PRFileDesc *fd)
220 { 220 {
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 } 422 }
423 423
424 void memio_PutReadResult(memio_Private *secret, int bytes_read) 424 void memio_PutReadResult(memio_Private *secret, int bytes_read)
425 { 425 {
426 struct memio_buffer* mb = &((PRFilePrivate *)secret)->readbuf; 426 struct memio_buffer* mb = &((PRFilePrivate *)secret)->readbuf;
427 PR_ASSERT(mb->bufsize); 427 PR_ASSERT(mb->bufsize);
428 428
429 if (bytes_read > 0) { 429 if (bytes_read > 0) {
430 mb->tail += bytes_read; 430 mb->tail += bytes_read;
431 if (mb->tail == mb->bufsize) 431 if (mb->tail == mb->bufsize)
432 » mb->tail = 0; 432 mb->tail = 0;
433 } else if (bytes_read == 0) { 433 } else if (bytes_read == 0) {
434 /* Record EOF condition and report to caller when buffer runs dry */ 434 /* Record EOF condition and report to caller when buffer runs dry */
435 ((PRFilePrivate *)secret)->eof = PR_TRUE; 435 ((PRFilePrivate *)secret)->eof = PR_TRUE;
436 } else /* if (bytes_read < 0) */ { 436 } else /* if (bytes_read < 0) */ {
437 mb->last_err = bytes_read; 437 mb->last_err = bytes_read;
438 } 438 }
439 } 439 }
440 440
441 int memio_GetWriteParams(memio_Private *secret, const char **buf) 441 int memio_GetWriteParams(memio_Private *secret, const char **buf)
442 { 442 {
(...skipping 22 matching lines...) Expand all
465 465
466 /* Even a trivial unit test is very helpful when doing circular buffers. */ 466 /* Even a trivial unit test is very helpful when doing circular buffers. */
467 /*#define TRIVIAL_SELF_TEST*/ 467 /*#define TRIVIAL_SELF_TEST*/
468 #ifdef TRIVIAL_SELF_TEST 468 #ifdef TRIVIAL_SELF_TEST
469 #include <stdio.h> 469 #include <stdio.h>
470 470
471 #define TEST_BUFLEN 7 471 #define TEST_BUFLEN 7
472 472
473 #define CHECKEQ(a, b) { \ 473 #define CHECKEQ(a, b) { \
474 if ((a) != (b)) { \ 474 if ((a) != (b)) { \
475 » printf("%d != %d, Test failed line %d\n", a, b, __LINE__); \ 475 printf("%d != %d, Test failed line %d\n", a, b, __LINE__); \
476 » exit(1); \ 476 exit(1); \
477 } \ 477 } \
478 } 478 }
479 479
480 int main() 480 int main()
481 { 481 {
482 struct memio_buffer mb; 482 struct memio_buffer mb;
483 char buf[100]; 483 char buf[100];
484 int i; 484 int i;
485 485
486 memio_buffer_new(&mb, TEST_BUFLEN); 486 memio_buffer_new(&mb, TEST_BUFLEN);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 CHECKEQ(memio_buffer_used_contiguous(&mb), 1); 538 CHECKEQ(memio_buffer_used_contiguous(&mb), 1);
539 CHECKEQ(memio_buffer_used(&mb), 6); 539 CHECKEQ(memio_buffer_used(&mb), 6);
540 540
541 /* TODO: add more cases */ 541 /* TODO: add more cases */
542 542
543 printf("Test passed\n"); 543 printf("Test passed\n");
544 exit(0); 544 exit(0);
545 } 545 }
546 546
547 #endif 547 #endif
OLDNEW
« no previous file with comments | « net/base/net_util_unittest.cc ('k') | net/base/ssl_client_socket_nss.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698