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.2 by root, Tue Sep 19 10:35:21 2006 UTC vs.
Revision 1.39 by root, Wed Dec 5 19:03:26 2018 UTC

1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7 * Copyright (©) 1992 Frank Tore Johansen
8 *
9 * Deliantra is free software: you can redistribute it and/or modify it under
10 * the terms of the Affero GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the Affero GNU General Public License
20 * and the GNU General Public License along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 *
23 * The authors can be reached via e-mail to <support@deliantra.net>
24 */
25
1#ifndef DYNBUF_H__ 26#ifndef DYNBUF_H__
2#define DYNBUF_H__ 27#define DYNBUF_H__
3 28
4#include <cstring> 29#include <cstring>
5#include <cassert> 30#include <cassert>
31
32#include "compiler.h"
33#include "util.h"
34#include "shstr.h"
6 35
7// this is a "buffer" that can grow fast 36// this is a "buffer" that can grow fast
8// and is still somewhat space-efficient. 37// and is still somewhat space-efficient.
9// unlike obstacks or other data structures, 38// unlike obstacks or other data structures,
10// it never moves data around. basically, 39// it never moves data around. basically,
11// this is a fast strstream without the overhead. 40// this is a fast strstream without the overhead.
12 41
13class dynbuf 42struct dynbuf
14{ 43{
44protected:
15 struct chunk 45 struct chunk
16 { 46 {
17 chunk *next; 47 chunk *next;
48 int alloc;
18 int size; 49 int size;
19 char data[0]; 50 char data[0];
20 }; 51 };
21 52
22 char *ptr; 53 char *ptr, *end;
23 int room;
24 int ext;
25 int _size; 54 int _size;
26 55
56 int extend, cextend;
27 chunk *first, *last; 57 chunk *first, *last;
28 58
29 void _reserve (int size); 59 void reserve (int size);
30 void clear (); 60 void init (int initial); // allocate sinitial chunk
61 void free (chunk *&chain); // free chain of chunks
62 char *_linearise (int extra = 0);
31 void finish (); 63 void finalise ();
32 64
33public: 65public:
34 66
67 // initial - the size of the initial chunk to be allocated
68 // extend - first incremental step when buffer size exceeded
35 dynbuf (int initial = 4096, int extend = 16384); 69 dynbuf (int initial = 4096, int extend = 16384)
70 : extend (extend)
71 {
72 init (initial);
73 }
74
36 ~dynbuf (); 75 ~dynbuf ()
76 {
77 free (first);
78 }
37 79
80 // resets the dynbuf, but does not free the first chunk
81 // which is either of size "initial" or the size of the last
82 // linearise
83 void clear ();
84
38 int size () { return _size + (ptr - last->data); } 85 int size () const { return _size + (ptr - last->data); }
86 bool empty () const { return !size (); }
39 87
40 void linearise (void *data); 88 void linearise (void *data);
41 char *linearise (); 89 char *linearise () // does not 0-terminate(!)
90 {
91 return first->next ? _linearise () : first->data;
92 }
42 93
94 int room () const { return end - ptr; }
95
96 // make sure we have "size" extra room
43 char *force (int size) 97 char *force (int size)
44 { 98 {
45 if (room < size) 99 if (ecb_expect_false (ptr + size > end))
46 _reserve (size); 100 reserve (size);
101
102 ecb_assume (ptr + size <= end);
47 103
48 return ptr; 104 return ptr;
49 } 105 }
50 106
107 // used for force + alloc combo
108 void alloc (char *p)
109 {
110 ptr = p;
111 }
112
113 // allocate size bytes and return pointer to them (caller must force())
114 char *falloc (int size)
115 {
116 char *res = ptr;
117 ptr += size;
118 return res;
119 }
120
121 // allocate size bytes and return pointer to them
51 char *alloc (int size) 122 char *alloc (int size)
52 { 123 {
53 char *res = force (size); 124 force (size);
54 125 return falloc (size);
55 room -= size;
56 ptr += size;
57
58 return res;
59 } 126 }
60 127
61 void fadd (char c) { --room; *ptr++ = c; } 128 void fadd (char c) { *ptr++ = c; }
62 void fadd (unsigned char c) { fadd (char (c)); } 129 void fadd (unsigned char c) { fadd (char (c)); }
130 void fadd (const void *p, int len)
131 {
132 memcpy (falloc (len), p, len);
133 }
63 134
64 void add (const void *p, int len) 135 void add (const void *p, int len)
65 { 136 {
66 memcpy (alloc (len), p, len); 137 force (len);
138 fadd (p, len);
67 } 139 }
68 140
69 void add (char c) 141 void add (char c)
70 { 142 {
71 if (room < 1) 143 alloc (1)[0] = c;
72 _reserve (1);
73
74 room--;
75 *ptr++ = c;
76 } 144 }
77 145
78 void add (const char *s) 146 void add (const char *s)
79 { 147 {
80 add (s, strlen (s)); 148 add (s, strlen (s));
81 } 149 }
82 150
83 static const int max_sint32_size = 11; 151 void add (shstr_tmp s)
84 static const int max_sint64_size = 20; 152 {
153 add (s.s, s.length ());
154 }
85 155
86 void add (sint32 i); 156 // rather inefficient
87 void add (sint64 i); 157 void splice (int offset, int olen, const char *s, int slen);
158 void splice (int offset, int olen) { splice (offset, olen, "", 0); }
88 159
89 //TODO 160 //TODO
90 //void add_destructive (dynbuf &buf); 161 //void add_destructive (dynbuf &buf);
91 162
92 dynbuf &operator << (char c) { add (c); return *this; } 163 dynbuf &operator << (char c) { add (c); return *this; }
93 dynbuf &operator << (unsigned char c) { return *this << char (c); } 164 dynbuf &operator << (unsigned char c) { return *this << char (c); }
94 dynbuf &operator << (const char *s) { add (s); return *this; } 165 dynbuf &operator << (const char *s) { add (s); return *this; }
166 dynbuf &operator << (shstr_tmp s) { add (s); return *this; }
167 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; }
168
169 operator std::string ();
95}; 170};
96 171
172struct dynbuf_text : dynbuf
173{
174 dynbuf_text (int initial = 4096, int extend = 16384)
175 : dynbuf (initial, extend)
176 { }
177
178 using dynbuf::add;
179 void add (sint32 i);
180 void add (sint64 i);
181
182 //TODO: should optimise the case printf "(name %+d)" as it comes up extremely often
183
184 //using dynbuf::operator <<; // doesn't work, sometimes C++ just suxx
185 // instead we use an ugly template function
186 template<typename T>
187 dynbuf_text &operator << (T c) { *(dynbuf *)this << c; return *this; }
188
189 dynbuf_text &operator << (sint16 i) { add (sint32 (i)); return *this; }
190 dynbuf_text &operator << (uint16 i) { add (sint32 (i)); return *this; }
191 dynbuf_text &operator << (sint32 i) { add (sint32 (i)); return *this; }
192 dynbuf_text &operator << (sint64 i) { add (sint64 (i)); return *this; }
193 dynbuf_text &operator << (uint32 i) { add (sint64 (i)); return *this; }
194 dynbuf_text &operator << (uint64 i) { add (sint64 (i)); return *this; }
195
196 void printf (const char *format, ...) ecb_attribute ((format (printf, 2, 3)));
197 void vprintf (const char *format, va_list ap);
198
199 void add_abilities (const char *name, uint32 abilities);
200 void add_paths (const char *name, uint32 paths);
201
202 // we need to redefine, to keep the API :/
203 using dynbuf::splice;
204 void splice (int offset, int olen, const char *s)
205 {
206 dynbuf::splice (offset, olen, s, strlen (s));
207 }
208
209 // returns the string, linearised and with trailing \0
210 operator char *();
211};
212
97#endif 213#endif
214

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines