ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
(Generate patch)

Comparing deliantra/server/include/dynbuf.h (file contents):
Revision 1.5 by root, Mon Apr 23 18:21:54 2007 UTC vs.
Revision 1.11 by root, Mon May 28 21:15:56 2007 UTC

1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Crossfire TRT is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * The authors can be reached via e-mail to <crossfire@schmorp.de>
23 */
24
1#ifndef DYNBUF_H__ 25#ifndef DYNBUF_H__
2#define DYNBUF_H__ 26#define DYNBUF_H__
3 27
4#include <cstring> 28#include <cstring>
5#include <cassert> 29#include <cassert>
6 30
31#include "util.h"
7#include "shstr.h" 32#include "shstr.h"
8 33
9// this is a "buffer" that can grow fast 34// this is a "buffer" that can grow fast
10// and is still somewhat space-efficient. 35// and is still somewhat space-efficient.
11// unlike obstacks or other data structures, 36// unlike obstacks or other data structures,
21 int alloc; 46 int alloc;
22 int size; 47 int size;
23 char data[0]; 48 char data[0];
24 }; 49 };
25 50
26 char *ptr; 51 char *ptr, *end;
27 int room;
28 int ext; 52 int ext;
29 int _size; 53 int _size;
30 54
31 chunk *first, *last; 55 chunk *first, *last;
32 56
33 void _reserve (int size); 57 void _reserve (int size);
58 void _clear ();
34 void clear (); 59 void clear ();
35 void finish (); 60 void finish ();
36 61
37public: 62public:
38 63
39 dynbuf (int initial = 4096, int extend = 16384); 64 dynbuf (int initial = 4096, int extend = 16384);
40 ~dynbuf (); 65 ~dynbuf ();
41 66
42 int size () { return _size + (ptr - last->data); } 67 int size () const { return _size + (ptr - last->data); }
68 bool empty () const { return !size (); }
43 69
44 void linearise (void *data); 70 void linearise (void *data);
45 char *linearise (); 71 char *linearise (); // does not 0-terminate(!)
72
73 int room () const { return end - ptr; }
46 74
47 char *force (int size) 75 char *force (int size)
48 { 76 {
49 if (room < size) 77 if (expect_false (ptr + size >= end))
50 _reserve (size); 78 _reserve (size);
51 79
52 return ptr; 80 return ptr;
53 } 81 }
54 82
55 char *alloc (int size) 83 char *falloc (int size)
56 { 84 {
57 char *res = force (size); 85 char *res = ptr;
58
59 room -= size;
60 ptr += size; 86 ptr += size;
61
62 return res; 87 return res;
63 } 88 }
64 89
90 char *alloc (int size)
91 {
92 force (size);
93 return falloc (size);
94 }
95
65 void fadd (char c) { --room; *ptr++ = c; } 96 void fadd (char c) { *ptr++ = c; }
66 void fadd (unsigned char c) { fadd (char (c)); } 97 void fadd (unsigned char c) { fadd (char (c)); }
98 void fadd (const void *p, int len)
99 {
100 memcpy (falloc (len), p, len);
101 }
67 102
68 void add (const void *p, int len) 103 void add (const void *p, int len)
69 { 104 {
70 memcpy (alloc (len), p, len); 105 force (len);
106 fadd (p, len);
71 } 107 }
72 108
73 void add (char c) 109 void add (char c)
74 { 110 {
75 alloc (1)[0] = c; 111 alloc (1)[0] = c;
89 //void add_destructive (dynbuf &buf); 125 //void add_destructive (dynbuf &buf);
90 126
91 dynbuf &operator << (char c) { add (c); return *this; } 127 dynbuf &operator << (char c) { add (c); return *this; }
92 dynbuf &operator << (unsigned char c) { return *this << char (c); } 128 dynbuf &operator << (unsigned char c) { return *this << char (c); }
93 dynbuf &operator << (const char *s) { add (s); return *this; } 129 dynbuf &operator << (const char *s) { add (s); return *this; }
130 dynbuf &operator << (const shstr &s) { add (s); return *this; }
94 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; } 131 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; }
95 132
96 operator std::string (); 133 operator std::string ();
97}; 134};
98 135
102 : dynbuf (initial, extend) 139 : dynbuf (initial, extend)
103 { } 140 { }
104 141
105 using dynbuf::add; 142 using dynbuf::add;
106 143
107 static const int max_sint32_size = 11;
108 static const int max_sint64_size = 20;
109
110 void add (sint32 i); 144 void add (sint32 i);
111 void add (sint64 i); 145 void add (sint64 i);
112 146
113 void printf (const char *format, ...); 147 void printf (const char *format, ...);
114}; 148};
115 149
116#endif 150#endif
151

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines