ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
Revision: 1.8
Committed: Sat May 26 15:44:03 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.7: +2 -1 lines
Log Message:
- restore after combined mainboard+harddisk crash
- cleanup/fixes for 2.1 release
- fix invoke to actually do work
- refactor invoke shortcuts, gcc cannot inline
  varargs functions.
- optimised invoke to 4-5 insns in the common case.
- optimised (For no good reason) the int-to-ascii
  conversions of dynbuf_text into division-less and
  branchless code (of which I am pretty proud).
- actually move players to their savebed when they did
  not use one and the map has been reste in the meantime.
  does not kill (yet) when too long.
- enter_map is now handled completely in perl.
- goto is now using generation counting to ensure that only the
  most-recently-issues goto will succeed.
- make some heavy use of __builtin_expect to streamline
  rare callbacks even more.
- optimised thawer.

File Contents

# Content
1 #ifndef DYNBUF_H__
2 #define DYNBUF_H__
3
4 #include <cstring>
5 #include <cassert>
6
7 #include "util.h"
8 #include "shstr.h"
9
10 // this is a "buffer" that can grow fast
11 // and is still somewhat space-efficient.
12 // unlike obstacks or other data structures,
13 // it never moves data around. basically,
14 // this is a fast strstream without the overhead.
15
16 struct dynbuf
17 {
18 protected:
19 struct chunk
20 {
21 chunk *next;
22 int alloc;
23 int size;
24 char data[0];
25 };
26
27 char *ptr, *end;
28 int ext;
29 int _size;
30
31 chunk *first, *last;
32
33 void _reserve (int size);
34 void _clear ();
35 void clear ();
36 void finish ();
37
38 public:
39
40 dynbuf (int initial = 4096, int extend = 16384);
41 ~dynbuf ();
42
43 int size () const { return _size + (ptr - last->data); }
44 bool empty () const { return !size (); }
45
46 void linearise (void *data);
47 char *linearise (); // does not 0-terminate(!)
48
49 int room () const { return end - ptr; }
50
51 char *force (int size)
52 {
53 if (expect_false (ptr + size >= end))
54 _reserve (size);
55
56 return ptr;
57 }
58
59 char *alloc (int size)
60 {
61 char *res = force (size);
62 ptr += size;
63 return res;
64 }
65
66 void fadd (char c) { *ptr++ = c; }
67 void fadd (unsigned char c) { fadd (char (c)); }
68
69 void add (const void *p, int len)
70 {
71 memcpy (alloc (len), p, len);
72 }
73
74 void add (char c)
75 {
76 alloc (1)[0] = c;
77 }
78
79 void add (const char *s)
80 {
81 add (s, strlen (s));
82 }
83
84 void add (const shstr &s)
85 {
86 add (s.s, s.length ());
87 }
88
89 //TODO
90 //void add_destructive (dynbuf &buf);
91
92 dynbuf &operator << (char c) { add (c); return *this; }
93 dynbuf &operator << (unsigned char c) { return *this << char (c); }
94 dynbuf &operator << (const char *s) { add (s); return *this; }
95 dynbuf &operator << (const shstr &s) { add (s); return *this; }
96 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; }
97
98 operator std::string ();
99 };
100
101 struct dynbuf_text : dynbuf
102 {
103 dynbuf_text (int initial = 4096, int extend = 16384)
104 : dynbuf (initial, extend)
105 { }
106
107 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);
113 void add (sint64 i);
114
115 void printf (const char *format, ...);
116 };
117
118 #endif
119