summaryrefslogtreecommitdiff
path: root/tagstats/string_store.hpp
blob: d1c0b64fe507a95aaa92523414a6bc6cfda8fff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef TAGSTATS_STRING_STORE_HPP
#define TAGSTATS_STRING_STORE_HPP

/*

  Copyright 2012, 2014 Jochen Topf <jochen@topf.org>.

  This file is part of Tagstats.

  Tagstats is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Tagstats is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Tagstats.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <list>
#include <stdexcept>
#include <string>

/**
 * class StringStore
 *
 * Storage of lots of strings (const char *). Memory is allocated in chunks.
 * If a string is added and there is no space in the current chunk, a new
 * chunk will be allocated. Strings added to the store must not be larger
 * than the chunk size.
 *
 * All memory is released when the destructor is called. There is no other way
 * to release all or part of the memory.
 *
 */
class StringStore {

    size_t m_chunk_size;

    std::list<std::string> m_chunks;

    void add_chunk() {
        m_chunks.push_front(std::string());
        m_chunks.front().reserve(m_chunk_size);
    }

public:

    StringStore(size_t chunk_size) :
        m_chunk_size(chunk_size),
        m_chunks() {
        add_chunk();
    }

    /**
     * Add a null terminated string to the store. This will
     * automatically get more memory if we are out.
     * Returns a pointer to the copy of the string we have
     * allocated.
     */
    const char* add(const char* string) {
        size_t len = std::strlen(string) + 1;

        assert(len <= m_chunk_size);

        size_t chunk_len = m_chunks.front().size();
        if (chunk_len + len > m_chunks.front().capacity()) {
            add_chunk();
            chunk_len = 0;
        }

        m_chunks.front().append(string);
        m_chunks.front().append(1, '\0');

        return m_chunks.front().c_str() + chunk_len;
    }

    // These functions get you some idea how much memory was
    // used.
    int get_chunk_size() const {
        return m_chunk_size;
    }

    int get_chunk_count() const {
        return m_chunks.size();
    }

    int get_used_bytes_in_last_chunk() const {
        return m_chunks.front().size();
    }

};

#endif // TAGSTATS_STRING_STORE_HPP