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.7 by root, Thu May 3 09:26:45 2007 UTC vs.
Revision 1.18 by root, Sat Aug 30 05:19:03 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines