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.9 by root, Sun May 27 23:22:29 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"
7#include "util.h" 33#include "util.h"
8#include "shstr.h" 34#include "shstr.h"
9 35
10// this is a "buffer" that can grow fast 36// this is a "buffer" that can grow fast
11// and is still somewhat space-efficient. 37// and is still somewhat space-efficient.
23 int size; 49 int size;
24 char data[0]; 50 char data[0];
25 }; 51 };
26 52
27 char *ptr, *end; 53 char *ptr, *end;
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
35 void clear (); 61 void free (chunk *&chain); // free chain of chunks
62 char *_linearise (int extra = 0);
36 void finish (); 63 void finalise ();
37 64
38public: 65public:
39 66
67 // initial - the size of the initial chunk to be allocated
68 // extend - first incremental step when buffer size exceeded
40 dynbuf (int initial = 4096, int extend = 16384); 69 dynbuf (int initial = 4096, int extend = 16384)
70 : extend (extend)
71 {
72 init (initial);
73 }
74
41 ~dynbuf (); 75 ~dynbuf ()
76 {
77 free (first);
78 }
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 ();
42 84
43 int size () const { return _size + (ptr - last->data); } 85 int size () const { return _size + (ptr - last->data); }
44 bool empty () const { return !size (); } 86 bool empty () const { return !size (); }
45 87
46 void linearise (void *data); 88 void linearise (void *data);
47 char *linearise (); // does not 0-terminate(!) 89 char *linearise () // does not 0-terminate(!)
90 {
91 return first->next ? _linearise () : first->data;
92 }
48 93
49 int room () const { return end - ptr; } 94 int room () const { return end - ptr; }
50 95
96 // make sure we have "size" extra room
51 char *force (int size) 97 char *force (int size)
52 { 98 {
53 if (expect_false (ptr + size >= end)) 99 if (expect_false (ptr + size > end))
54 _reserve (size); 100 reserve (size);
101
102 ecb_assume (ptr + size <= end);
55 103
56 return ptr; 104 return ptr;
57 } 105 }
58 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())
59 char *alloc (int size) 114 char *falloc (int size)
60 { 115 {
61 char *res = force (size); 116 char *res = ptr;
62 ptr += size; 117 ptr += size;
63 return res; 118 return res;
64 } 119 }
65 120
121 // allocate size bytes and return pointer to them
122 char *alloc (int size)
123 {
124 force (size);
125 return falloc (size);
126 }
127
66 void fadd (char c) { *ptr++ = c; } 128 void fadd (char c) { *ptr++ = c; }
67 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 }
68 134
69 void add (const void *p, int len) 135 void add (const void *p, int len)
70 { 136 {
71 memcpy (alloc (len), p, len); 137 force (len);
138 fadd (p, len);
72 } 139 }
73 140
74 void add (char c) 141 void add (char c)
75 { 142 {
76 alloc (1)[0] = c; 143 alloc (1)[0] = c;
79 void add (const char *s) 146 void add (const char *s)
80 { 147 {
81 add (s, strlen (s)); 148 add (s, strlen (s));
82 } 149 }
83 150
84 void add (const shstr &s) 151 void add (shstr_tmp s)
85 { 152 {
86 add (s.s, s.length ()); 153 add (s.s, s.length ());
87 } 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); }
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; }
95 dynbuf &operator << (const shstr &s) { add (s); return *this; } 166 dynbuf &operator << (shstr_tmp s) { add (s); return *this; }
96 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; }
97 168
98 operator std::string (); 169 operator std::string ();
99}; 170};
100 171
103 dynbuf_text (int initial = 4096, int extend = 16384) 174 dynbuf_text (int initial = 4096, int extend = 16384)
104 : dynbuf (initial, extend) 175 : dynbuf (initial, extend)
105 { } 176 { }
106 177
107 using dynbuf::add; 178 using dynbuf::add;
108
109 static const int max_sint32_size = 11;
110 static const int max_sint64_size = 20;
111
112 void add (sint32 i); 179 void add (sint32 i);
113 void add (sint64 i); 180 void add (sint64 i);
114 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)));
115 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 *();
116}; 211};
117 212
118#endif 213#endif
119 214

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines