~ubuntu-branches/ubuntu/vivid/mariadb-5.5/vivid-proposed

« back to all changes in this revision

Viewing changes to storage/tokudb/ft-index/util/memarena.h

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2014-11-14 21:04:24 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20141114210424-xlyna0ozl11647o5
Tags: 5.5.40-0ubuntu0.14.10.1
* SECURITY UPDATE: Update to 5.5.40 to fix security issues (LP: #1391676)
  - CVE-2014-6507
  - CVE-2014-6491
  - CVE-2014-6500
  - CVE-2014-6469
  - CVE-2014-6555
  - CVE-2014-6559
  - CVE-2014-6494
  - CVE-2014-6496
  - CVE-2014-6464
* Add bsdutils as mariadb-server dependency like upstream does in 5.5.40.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
2
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3
 
#ifndef TOKU_MEMARENA_H
4
 
#define TOKU_MEMARENA_H
5
3
 
6
4
#ident "$Id$"
7
5
/*
32
30
 
33
31
COPYRIGHT NOTICE:
34
32
 
35
 
  TokuDB, Tokutek Fractal Tree Indexing Library.
 
33
  TokuFT, Tokutek Fractal Tree Indexing Library.
36
34
  Copyright (C) 2007-2013 Tokutek, Inc.
37
35
 
38
36
DISCLAIMER:
89
87
  under this License.
90
88
*/
91
89
 
 
90
#pragma once
 
91
 
92
92
#ident "Copyright (c) 2007-2013 Tokutek Inc.  All rights reserved."
93
93
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
94
94
 
95
 
/* We have too many memory management tricks:
96
 
 *  memarena (this code) is for a collection of objects that cannot be moved.
97
 
 *    The pattern is allocate more and more stuff.
98
 
 *    Don't free items as you go.
99
 
 *    Free all the items at once.
100
 
 *    Then reuse the same buffer again.
101
 
 *    Allocated objects never move.
102
 
 *  A memarena (as currently implemented) is not suitable for interprocess memory sharing.  No reason it couldn't be made to work though.
 
95
/*
 
96
 * A memarena is used to efficiently store a collection of objects that never move
 
97
 * The pattern is allocate more and more stuff and free all of the items at once.
 
98
 * The underlying memory will store 1 or more objects per chunk. Each chunk is 
 
99
 * contiguously laid out in memory but chunks are not necessarily contiguous with
 
100
 * each other.
103
101
 */
104
 
 
105
 
struct memarena;
106
 
 
107
 
typedef struct memarena *MEMARENA;
108
 
 
109
 
MEMARENA toku_memarena_create_presized (size_t initial_size);
110
 
// Effect: Create a memarena with initial size.  In case of ENOMEM, aborts.
111
 
 
112
 
MEMARENA toku_memarena_create (void);
113
 
// Effect: Create a memarena with default initial size.  In case of ENOMEM, aborts.
114
 
 
115
 
void toku_memarena_clear (MEMARENA ma);
116
 
// Effect: Reset the internal state so that the allocated memory can be used again.
117
 
 
118
 
void* toku_memarena_malloc (MEMARENA ma, size_t size);
119
 
// Effect: Allocate some memory.  The returned value remains valid until the memarena is cleared or closed.
120
 
//  In case of ENOMEM, aborts.
121
 
 
122
 
void *toku_memarena_memdup (MEMARENA ma, const void *v, size_t len);
123
 
 
124
 
void toku_memarena_destroy(MEMARENA *ma);
125
 
 
126
 
void toku_memarena_move_buffers(MEMARENA dest, MEMARENA source);
127
 
// Effect: Move all the memory from SOURCE into DEST.  When SOURCE is closed the memory won't be freed.  When DEST is closed, the memory will be freed.  (Unless DEST moves its memory to another memarena...)
128
 
 
129
 
size_t toku_memarena_total_memory_size (MEMARENA);
130
 
// Effect: Calculate the amount of memory used by a memory arena.
131
 
 
132
 
size_t toku_memarena_total_size_in_use (MEMARENA);
133
 
 
134
 
size_t toku_memarena_total_footprint (MEMARENA);
135
 
 
136
 
#endif
 
102
class memarena {
 
103
public:
 
104
    memarena() :
 
105
        _current_chunk(arena_chunk()),
 
106
        _other_chunks(nullptr),
 
107
        _n_other_chunks(0),
 
108
        _size_of_other_chunks(0),
 
109
        _footprint_of_other_chunks(0) {
 
110
    }
 
111
 
 
112
    // Effect: Create a memarena with the specified initial size
 
113
    void create(size_t initial_size);
 
114
 
 
115
    void destroy(void);
 
116
 
 
117
    // Effect: Allocate some memory.  The returned value remains valid until the memarena is cleared or closed.
 
118
    //  In case of ENOMEM, aborts.
 
119
    void *malloc_from_arena(size_t size);
 
120
 
 
121
    // Effect: Move all the memory from this memarena into DEST. 
 
122
    //         When SOURCE is closed the memory won't be freed. 
 
123
    //         When DEST is closed, the memory will be freed, unless DEST moves its memory to another memarena...
 
124
    void move_memory(memarena *dest);
 
125
 
 
126
    // Effect: Calculate the amount of memory used by a memory arena.
 
127
    size_t total_memory_size(void) const;
 
128
 
 
129
    // Effect: Calculate the used space of the memory arena (ie: excludes unused space)
 
130
    size_t total_size_in_use(void) const;
 
131
 
 
132
    // Effect: Calculate the amount of memory used, according to toku_memory_footprint(),
 
133
    //         which is a more expensive but more accurate count of memory used.
 
134
    size_t total_footprint(void) const;
 
135
 
 
136
    // iterator over the underlying chunks that store objects in the memarena.
 
137
    // a chunk is represented by a pointer to const memory and a usable byte count.
 
138
    class chunk_iterator {
 
139
    public:
 
140
        chunk_iterator(const memarena *ma) :
 
141
            _ma(ma), _chunk_idx(-1) {
 
142
        }
 
143
 
 
144
        // returns: base pointer to the current chunk
 
145
        //          *used set to the number of usable bytes
 
146
        //          if more() is false, returns nullptr and *used = 0
 
147
        const void *current(size_t *used) const;
 
148
 
 
149
        // requires: more() is true
 
150
        void next();
 
151
 
 
152
        bool more() const;
 
153
 
 
154
    private:
 
155
        // -1 represents the 'initial' chunk in a memarena, ie: ma->_current_chunk
 
156
        // >= 0 represents the i'th chunk in the ma->_other_chunks array
 
157
        const memarena *_ma;
 
158
        int _chunk_idx;
 
159
    };
 
160
 
 
161
private:
 
162
    struct arena_chunk {
 
163
        arena_chunk() : buf(nullptr), used(0), size(0) { }
 
164
        char *buf;
 
165
        size_t used;
 
166
        size_t size;
 
167
    };
 
168
 
 
169
    struct arena_chunk _current_chunk;
 
170
    struct arena_chunk *_other_chunks;
 
171
    int _n_other_chunks;
 
172
    size_t _size_of_other_chunks; // the buf_size of all the other chunks.
 
173
    size_t _footprint_of_other_chunks; // the footprint of all the other chunks.
 
174
 
 
175
    friend class memarena_unit_test;
 
176
};