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.38 by root, Sat Nov 17 23:40:00 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>
6 31
32#include "compiler.h"
33#include "util.h"
7#include "shstr.h" 34#include "shstr.h"
8 35
9// this is a "buffer" that can grow fast 36// this is a "buffer" that can grow fast
10// and is still somewhat space-efficient. 37// and is still somewhat space-efficient.
11// unlike obstacks or other data structures, 38// unlike obstacks or other data structures,
21 int alloc; 48 int alloc;
22 int size; 49 int size;
23 char data[0]; 50 char data[0];
24 }; 51 };
25 52
26 char *ptr; 53 char *ptr, *end;
27 int room;
28 int ext;
29 int _size; 54 int _size;
30 55
56 int extend, cextend;
31 chunk *first, *last; 57 chunk *first, *last;
32 58
33 void _reserve (int size); 59 void reserve (int size);
34 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);
35 void finish (); 63 void finalise ();
36 64
37public: 65public:
38 66
67 // initial - the size of the initial chunk to be allocated
68 // extend - first incremental step when buffer size exceeded
39 dynbuf (int initial = 4096, int extend = 16384); 69 dynbuf (int initial = 4096, int extend = 16384)
70 : extend (extend)
71 {
72 init (initial);
73 }
74
40 ~dynbuf (); 75 ~dynbuf ()
76 {
77 free (first);
78 }
41 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
42 int size () { return _size + (ptr - last->data); } 85 int size () const { return _size + (ptr - last->data); }
86 bool empty () const { return !size (); }
43 87
44 void linearise (void *data); 88 void linearise (void *data);
45 char *linearise (); 89 char *linearise () // does not 0-terminate(!)
90 {
91 return first->next ? _linearise () : first->data;
92 }
46 93
94 int room () const { return end - ptr; }
95
96 // make sure we have "size" extra room
47 char *force (int size) 97 char *force (int size)
48 { 98 {
49 if (room < size) 99 if (expect_false (ptr + size > end))
50 _reserve (size); 100 reserve (size);
101
102 ecb_assume (ptr + size <= end);
51 103
52 return ptr; 104 return ptr;
53 } 105 }
54 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
55 char *alloc (int size) 122 char *alloc (int size)
56 { 123 {
57 char *res = force (size); 124 force (size);
58 125 return falloc (size);
59 room -= size;
60 ptr += size;
61
62 return res;
63 } 126 }
64 127
65 void fadd (char c) { --room; *ptr++ = c; } 128 void fadd (char c) { *ptr++ = c; }
66 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 }
67 134
68 void add (const void *p, int len) 135 void add (const void *p, int len)
69 { 136 {
70 memcpy (alloc (len), p, len); 137 force (len);
138 fadd (p, len);
71 } 139 }
72 140
73 void add (char c) 141 void add (char c)
74 { 142 {
75 alloc (1)[0] = c; 143 alloc (1)[0] = c;
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 void add (const shstr &s) 151 void add (shstr_tmp s)
84 { 152 {
85 add (s.s, s.length ()); 153 add (s.s, s.length ());
86 } 154 }
155
156 // rather inefficient
157 void splice (int offset, int olen, const char *s, int slen);
158 void splice (int offset, int olen) { splice (offset, olen, "", 0); }
87 159
88 //TODO 160 //TODO
89 //void add_destructive (dynbuf &buf); 161 //void add_destructive (dynbuf &buf);
90 162
91 dynbuf &operator << (char c) { add (c); return *this; } 163 dynbuf &operator << (char c) { add (c); return *this; }
92 dynbuf &operator << (unsigned char c) { return *this << char (c); } 164 dynbuf &operator << (unsigned char c) { return *this << char (c); }
93 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; }
94 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; } 167 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; }
95 168
96 operator std::string (); 169 operator std::string ();
97}; 170};
98 171
101 dynbuf_text (int initial = 4096, int extend = 16384) 174 dynbuf_text (int initial = 4096, int extend = 16384)
102 : dynbuf (initial, extend) 175 : dynbuf (initial, extend)
103 { } 176 { }
104 177
105 using dynbuf::add; 178 using dynbuf::add;
106
107 static const int max_sint32_size = 11;
108 static const int max_sint64_size = 20;
109
110 void add (sint32 i); 179 void add (sint32 i);
111 void add (sint64 i); 180 void add (sint64 i);
112 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)));
113 void printf (const char *format, ...); 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 *();
114}; 211};
115 212
116#endif 213#endif
214

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines