| 1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: |
| 3 | #ident "$Id$" |
| 4 | /*====== |
| 5 | This file is part of PerconaFT. |
| 6 | |
| 7 | |
| 8 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
| 9 | |
| 10 | PerconaFT is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License, version 2, |
| 12 | as published by the Free Software Foundation. |
| 13 | |
| 14 | PerconaFT is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | ---------------------------------------- |
| 23 | |
| 24 | PerconaFT is free software: you can redistribute it and/or modify |
| 25 | it under the terms of the GNU Affero General Public License, version 3, |
| 26 | as published by the Free Software Foundation. |
| 27 | |
| 28 | PerconaFT is distributed in the hope that it will be useful, |
| 29 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 | GNU Affero General Public License for more details. |
| 32 | |
| 33 | You should have received a copy of the GNU Affero General Public License |
| 34 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 35 | ======= */ |
| 36 | |
| 37 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
| 38 | |
| 39 | #pragma once |
| 40 | |
| 41 | /* |
| 42 | * A memarena is used to efficiently store a collection of objects that never move |
| 43 | * The pattern is allocate more and more stuff and free all of the items at once. |
| 44 | * The underlying memory will store 1 or more objects per chunk. Each chunk is |
| 45 | * contiguously laid out in memory but chunks are not necessarily contiguous with |
| 46 | * each other. |
| 47 | */ |
| 48 | class memarena { |
| 49 | public: |
| 50 | memarena() : |
| 51 | _current_chunk(arena_chunk()), |
| 52 | _other_chunks(nullptr), |
| 53 | _n_other_chunks(0), |
| 54 | _size_of_other_chunks(0), |
| 55 | _footprint_of_other_chunks(0) { |
| 56 | } |
| 57 | |
| 58 | // Effect: Create a memarena with the specified initial size |
| 59 | void create(size_t initial_size); |
| 60 | |
| 61 | void destroy(void); |
| 62 | |
| 63 | // Effect: Allocate some memory. The returned value remains valid until the memarena is cleared or closed. |
| 64 | // In case of ENOMEM, aborts. |
| 65 | void *malloc_from_arena(size_t size); |
| 66 | |
| 67 | // Effect: Move all the memory from this memarena into DEST. |
| 68 | // When SOURCE is closed the memory won't be freed. |
| 69 | // When DEST is closed, the memory will be freed, unless DEST moves its memory to another memarena... |
| 70 | void move_memory(memarena *dest); |
| 71 | |
| 72 | // Effect: Calculate the amount of memory used by a memory arena. |
| 73 | size_t total_memory_size(void) const; |
| 74 | |
| 75 | // Effect: Calculate the used space of the memory arena (ie: excludes unused space) |
| 76 | size_t total_size_in_use(void) const; |
| 77 | |
| 78 | // Effect: Calculate the amount of memory used, according to toku_memory_footprint(), |
| 79 | // which is a more expensive but more accurate count of memory used. |
| 80 | size_t (void) const; |
| 81 | |
| 82 | // iterator over the underlying chunks that store objects in the memarena. |
| 83 | // a chunk is represented by a pointer to const memory and a usable byte count. |
| 84 | class chunk_iterator { |
| 85 | public: |
| 86 | chunk_iterator(const memarena *ma) : |
| 87 | _ma(ma), _chunk_idx(-1) { |
| 88 | } |
| 89 | |
| 90 | // returns: base pointer to the current chunk |
| 91 | // *used set to the number of usable bytes |
| 92 | // if more() is false, returns nullptr and *used = 0 |
| 93 | const void *current(size_t *used) const; |
| 94 | |
| 95 | // requires: more() is true |
| 96 | void next(); |
| 97 | |
| 98 | bool more() const; |
| 99 | |
| 100 | private: |
| 101 | // -1 represents the 'initial' chunk in a memarena, ie: ma->_current_chunk |
| 102 | // >= 0 represents the i'th chunk in the ma->_other_chunks array |
| 103 | const memarena *_ma; |
| 104 | int _chunk_idx; |
| 105 | }; |
| 106 | |
| 107 | private: |
| 108 | struct arena_chunk { |
| 109 | arena_chunk() : buf(nullptr), used(0), size(0) { } |
| 110 | char *buf; |
| 111 | size_t used; |
| 112 | size_t size; |
| 113 | }; |
| 114 | |
| 115 | struct arena_chunk _current_chunk; |
| 116 | struct arena_chunk *_other_chunks; |
| 117 | int _n_other_chunks; |
| 118 | size_t _size_of_other_chunks; // the buf_size of all the other chunks. |
| 119 | size_t ; // the footprint of all the other chunks. |
| 120 | |
| 121 | friend class memarena_unit_test; |
| 122 | }; |
| 123 | |