benchmark silo added
[c11concurrency-benchmarks.git] / silo / masstree / kvio.hh
1 /* Masstree
2  * Eddie Kohler, Yandong Mao, Robert Morris
3  * Copyright (c) 2012-2014 President and Fellows of Harvard College
4  * Copyright (c) 2012-2014 Massachusetts Institute of Technology
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, subject to the conditions
9  * listed in the Masstree LICENSE file. These conditions include: you must
10  * preserve this copyright notice, and you cannot mention the copyright
11  * holders in advertising related to the Software without their permission.
12  * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13  * notice is a summary of the Masstree LICENSE file; the license in that file
14  * is legally binding.
15  */
16 #ifndef KVIO_H
17 #define KVIO_H
18 #include <string>
19 #include <vector>
20 #include <stdlib.h>
21 #include "string.hh"
22 #include "str.hh"
23
24 struct kvout {
25     int fd;
26     char* buf;
27     unsigned capacity; // allocated size of buf
28     unsigned n;   // # of chars we've written to buf
29
30     inline void append(char c);
31     inline char* reserve(int n);
32     inline void adjust_length(int delta);
33     inline void set_end(char* end);
34     void grow(unsigned want);
35 };
36
37 kvout* new_kvout(int fd, int buflen);
38 kvout* new_bufkvout();
39 void kvout_reset(kvout* kv);
40 void free_kvout(kvout* kv);
41 int kvwrite(kvout* kv, const void* buf, unsigned int n);
42 void kvflush(kvout* kv);
43
44 inline void kvout::append(char c) {
45     if (n == capacity)
46         grow(0);
47     buf[n] = c;
48     ++n;
49 }
50
51 inline char* kvout::reserve(int nchars) {
52     if (n + nchars > capacity)
53         grow(n + nchars);
54     return buf + n;
55 }
56
57 inline void kvout::adjust_length(int delta) {
58     masstree_precondition(n + delta <= capacity);
59     n += delta;
60 }
61
62 inline void kvout::set_end(char* x) {
63     masstree_precondition(x >= buf && x <= buf + capacity);
64     n = x - buf;
65 }
66
67 #endif