| 1 | /** @file |
| 2 | * @brief Glass freelist |
| 3 | */ |
| 4 | /* Copyright 2014 Olly Betts |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 19 | * USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef XAPIAN_INCLUDED_GLASS_FREELIST_H |
| 23 | #define XAPIAN_INCLUDED_GLASS_FREELIST_Hcxx . . $ `backends/glass/glass_freelist.h:23:9`! |
| 24 | |
| 25 | #include "glass_defs.h"cxx . . $ `<file>/backends/glass/glass_defs.h`/ |
| 26 | #include "pack.h"cxx . . $ `<file>/common/pack.h`/ |
| 27 | |
| 28 | class GlassTablecxx . . $ GlassTable#; |
| 29 | |
| 30 | class GlassFLCursorcxx . . $ GlassFLCursor# { |
| 31 | public: |
| 32 | /// Block number of current freelist chunk. |
| 33 | uint4cxx . . $ uint4# ncxx . . $ GlassFLCursor#n.; |
| 34 | |
| 35 | /// Current offset in block. |
| 36 | unsigned ccxx . . $ GlassFLCursor#c.; |
| 37 | |
| 38 | GlassFLCursorcxx . . $ GlassFLCursor#GlassFLCursor(49f6e7a06ebc5aa8).() : ncxx . . $ GlassFLCursor#n.(0), ccxx . . $ GlassFLCursor#c.(0) { } |
| 39 | |
| 40 | bool operatorcxx . . $ GlassFLCursor#`operator==`(649acc6201ccf967).==(const GlassFLCursorcxx . . $ GlassFLCursor# & o) const { |
| 41 | return ncxx . . $ GlassFLCursor#n. == o.ncxx . . $ GlassFLCursor#n. && ccxx . . $ GlassFLCursor#c. == o.ccxx . . $ GlassFLCursor#c.; |
| 42 | } |
| 43 | |
| 44 | bool operatorcxx . . $ GlassFLCursor#`operator!=`(649acc6201ccf967).!=(const GlassFLCursorcxx . . $ GlassFLCursor# & o) const { |
| 45 | return !(*this ==cxx . . $ GlassFLCursor#`operator==`(649acc6201ccf967). o); |
| 46 | } |
| 47 | |
| 48 | void swapcxx . . $ GlassFLCursor#swap(d273e125cdf22a1e).(GlassFLCursorcxx . . $ GlassFLCursor# &o) { |
| 49 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(ncxx . . $ GlassFLCursor#n., o.ncxx . . $ GlassFLCursor#n.); |
| 50 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(ccxx . . $ GlassFLCursor#c., o.ccxx . . $ GlassFLCursor#c.); |
| 51 | } |
| 52 | |
| 53 | void packcxx . . $ GlassFLCursor#pack(80d0450aec2fd74).(stdcxx . . $ std/::stringcxx . . $ std/string# & buf) { |
| 54 | pack_uintcxx . . $ pack_uint(6c508de3dd6b8711).(buf, ncxx . . $ GlassFLCursor#n.); |
| 55 | pack_uintcxx . . $ pack_uint(6c508de3dd6b8711).(buf, ccxx . . $ GlassFLCursor#c. / 4); |
| 56 | } |
| 57 | |
| 58 | bool unpackcxx . . $ GlassFLCursor#unpack(5c7dd2afcee48131).(const char ** p, const char * end) { |
| 59 | bool r = unpack_uintcxx . . $ unpack_uint(9620d6ecba0dad86).(p, end, &ncxx . . $ GlassFLCursor#n.) && unpack_uintcxx . . $ unpack_uint(9620d6ecba0dad86).(p, end, &ccxx . . $ GlassFLCursor#c.); |
| 60 | if (usualcxx . . $ `config.h:579:10`!(r)) |
| 61 | ccxx . . $ GlassFLCursor#c. *= 4; |
| 62 | return r; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | class GlassFreeListcxx . . $ GlassFreeList# { |
| 67 | GlassFreeListcxx . . $ GlassFreeList#GlassFreeList(2242bab1378dfc19).(const GlassFreeListcxx . . $ GlassFreeList# &); |
| 68 | |
| 69 | void operatorcxx . . $ GlassFreeList#`operator=`(2242bab1378dfc19).=(const GlassFreeListcxx . . $ GlassFreeList# &); |
| 70 | |
| 71 | void read_blockcxx . . $ GlassFreeList#read_block(290c56e80777dc71).(const GlassTablecxx . . $ GlassTable# * B, uint4cxx . . $ uint4# n, uint8_tcxx . . $ uint8_t# * p); |
| 72 | |
| 73 | void write_blockcxx . . $ GlassFreeList#write_block(6b689b6b16391a22).(const GlassTablecxx . . $ GlassTable# * B, uint4cxx . . $ uint4# n, uint8_tcxx . . $ uint8_t# * p, uint4cxx . . $ uint4# rev); |
| 74 | |
| 75 | protected: |
| 76 | uint4cxx . . $ uint4# revisioncxx . . $ GlassFreeList#revision.; |
| 77 | |
| 78 | uint4cxx . . $ uint4# first_unused_blockcxx . . $ GlassFreeList#first_unused_block.; |
| 79 | |
| 80 | GlassFLCursorcxx . . $ GlassFLCursor# flcxx . . $ GlassFreeList#fl., fl_endcxx . . $ GlassFreeList#fl_end., flwcxx . . $ GlassFreeList#flw.; |
| 81 | |
| 82 | bool flw_appendingcxx . . $ GlassFreeList#flw_appending.; |
| 83 | |
| 84 | private: |
| 85 | /// Current freelist block. |
| 86 | uint8_tcxx . . $ uint8_t# * pcxx . . $ GlassFreeList#p.; |
| 87 | |
| 88 | /// Current freelist block we're writing. |
| 89 | uint8_tcxx . . $ uint8_t# * pwcxx . . $ GlassFreeList#pw.; |
| 90 | |
| 91 | public: |
| 92 | GlassFreeListcxx . . $ GlassFreeList#GlassFreeList(49f6e7a06ebc5aa8).() { |
| 93 | revisioncxx . . $ GlassFreeList#revision. = 0; |
| 94 | first_unused_blockcxx . . $ GlassFreeList#first_unused_block. = 0; |
| 95 | flw_appendingcxx . . $ GlassFreeList#flw_appending. = false; |
| 96 | pcxx . . $ GlassFreeList#p. = pwcxx . . $ GlassFreeList#pw. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 97 | } |
| 98 | |
| 99 | void resetcxx . . $ GlassFreeList#reset(49f6e7a06ebc5aa8).() { |
| 100 | revisioncxx . . $ GlassFreeList#revision. = 0; |
| 101 | first_unused_blockcxx . . $ GlassFreeList#first_unused_block. = 0; |
| 102 | flw_appendingcxx . . $ GlassFreeList#flw_appending. = false; |
| 103 | } |
| 104 | |
| 105 | ~cxx . . $ GlassFreeList#`~GlassFreeList`(ced63f7c635d850d).GlassFreeListcxx . . $ GlassFreeList#() { delete [] pcxx . . $ GlassFreeList#p.; delete [] pwcxx . . $ GlassFreeList#pw.; } |
| 106 | |
| 107 | bool emptycxx . . $ GlassFreeList#empty(50ce9a9e25b4a850).() const { return flcxx . . $ GlassFreeList#fl. ==cxx . . $ GlassFLCursor#`operator==`(649acc6201ccf967). fl_endcxx . . $ GlassFreeList#fl_end.; } |
| 108 | |
| 109 | uint4cxx . . $ uint4# get_blockcxx . . $ GlassFreeList#get_block(9c5add000f47e4ff).(const GlassTablecxx . . $ GlassTable# * B, uint4cxx . . $ uint4# block_size, |
| 110 | uint4cxx . . $ uint4# * blk_to_free = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 111 | |
| 112 | uint4cxx . . $ uint4# walkcxx . . $ GlassFreeList#walk(8529022e3eb20a0e).(const GlassTablecxx . . $ GlassTable# *B, uint4cxx . . $ uint4# block_size, bool inclusive); |
| 113 | |
| 114 | void mark_block_unusedcxx . . $ GlassFreeList#mark_block_unused(d680a2879f278aeb).(const GlassTablecxx . . $ GlassTable# * B, uint4cxx . . $ uint4# block_size, uint4cxx . . $ uint4# n); |
| 115 | |
| 116 | uint4cxx . . $ uint4# get_revisioncxx . . $ GlassFreeList#get_revision(ddd8539a2fcdd4ce).() const { return revisioncxx . . $ GlassFreeList#revision.; } |
| 117 | void set_revisioncxx . . $ GlassFreeList#set_revision(cb2f890c2fdad230).(uint4cxx . . $ uint4# revision_) { revisioncxx . . $ GlassFreeList#revision. = revision_; } |
| 118 | |
| 119 | uint4cxx . . $ uint4# get_first_unused_blockcxx . . $ GlassFreeList#get_first_unused_block(ddd8539a2fcdd4ce).() const { return first_unused_blockcxx . . $ GlassFreeList#first_unused_block.; } |
| 120 | |
| 121 | // Used when compacting to a single file. |
| 122 | void set_first_unused_blockcxx . . $ GlassFreeList#set_first_unused_block(cb2f890c2fdad230).(uint4cxx . . $ uint4# base) { first_unused_blockcxx . . $ GlassFreeList#first_unused_block. = base; } |
| 123 | |
| 124 | void commitcxx . . $ GlassFreeList#commit(1da78ebc4321c821).(const GlassTablecxx . . $ GlassTable# * B, uint4cxx . . $ uint4# block_size); |
| 125 | |
| 126 | void packcxx . . $ GlassFreeList#pack(80d0450aec2fd74).(stdcxx . . $ std/::stringcxx . . $ std/string# & buf) { |
| 127 | pack_uintcxx . . $ pack_uint(6c508de3dd6b8711).(buf, revisioncxx . . $ GlassFreeList#revision.); |
| 128 | pack_uintcxx . . $ pack_uint(6c508de3dd6b8711).(buf, first_unused_blockcxx . . $ GlassFreeList#first_unused_block.); |
| 129 | flcxx . . $ GlassFreeList#fl..packcxx . . $ GlassFLCursor#pack(80d0450aec2fd74).(buf); |
| 130 | flwcxx . . $ GlassFreeList#flw..packcxx . . $ GlassFLCursor#pack(80d0450aec2fd74).(buf); |
| 131 | } |
| 132 | |
| 133 | bool unpackcxx . . $ GlassFreeList#unpack(5c7dd2afcee48131).(const char ** pstart, const char * end) { |
| 134 | bool r = unpack_uintcxx . . $ unpack_uint(9620d6ecba0dad86).(pstart, end, &revisioncxx . . $ GlassFreeList#revision.) && |
| 135 | unpack_uintcxx . . $ unpack_uint(9620d6ecba0dad86).(pstart, end, &first_unused_blockcxx . . $ GlassFreeList#first_unused_block.) && |
| 136 | flcxx . . $ GlassFreeList#fl..unpackcxx . . $ GlassFLCursor#unpack(5c7dd2afcee48131).(pstart, end) && |
| 137 | flwcxx . . $ GlassFreeList#flw..unpackcxx . . $ GlassFLCursor#unpack(5c7dd2afcee48131).(pstart, end); |
| 138 | if (r) { |
| 139 | fl_endcxx . . $ GlassFreeList#fl_end. =cxx . . $ GlassFLCursor#`operator=`(8aa3957608049f4f). flwcxx . . $ GlassFreeList#flw.; |
| 140 | flw_appendingcxx . . $ GlassFreeList#flw_appending. = false; |
| 141 | } |
| 142 | return r; |
| 143 | } |
| 144 | |
| 145 | bool unpackcxx . . $ GlassFreeList#unpack(7c222eea44a24d43).(const stdcxx . . $ std/::stringcxx . . $ std/string# & s) { |
| 146 | const char * ptr = s.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(); |
| 147 | const char * end = ptr + s.sizecxx . . $ std/__cxx11/basic_string#size(6f8bd5c0ea8fb29a).(); |
| 148 | return unpackcxx . . $ GlassFreeList#unpack(5c7dd2afcee48131).(&ptr, end) && ptr == end; |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | class GlassFreeListCheckercxx . . $ GlassFreeListChecker# { |
| 153 | // FIXME: uint_fast32_t is probably a good choice. |
| 154 | typedef unsigned long elt_typecxx . . $ GlassFreeListChecker#elt_type#; |
| 155 | |
| 156 | uint4cxx . . $ uint4# bitmap_sizecxx . . $ GlassFreeListChecker#bitmap_size.; |
| 157 | |
| 158 | elt_typecxx . . $ GlassFreeListChecker#elt_type# * bitmapcxx . . $ GlassFreeListChecker#bitmap.; |
| 159 | |
| 160 | // Prevent copying |
| 161 | GlassFreeListCheckercxx . . $ GlassFreeListChecker#GlassFreeListChecker(210930445499fd9e).(const GlassFreeListCheckercxx . . $ GlassFreeListChecker#&); |
| 162 | GlassFreeListCheckercxx . . $ GlassFreeListChecker#& operatorcxx . . $ GlassFreeListChecker#`operator=`(a2f323de2796649).=(const GlassFreeListCheckercxx . . $ GlassFreeListChecker#&); |
| 163 | |
| 164 | public: |
| 165 | explicit GlassFreeListCheckercxx . . $ GlassFreeListChecker#GlassFreeListChecker(2242bab1378dfc19).(const GlassFreeListcxx . . $ GlassFreeList# & fl); |
| 166 | |
| 167 | ~cxx . . $ GlassFreeListChecker#`~GlassFreeListChecker`(ced63f7c635d850d).GlassFreeListCheckercxx . . $ GlassFreeListChecker#() { |
| 168 | delete [] bitmapcxx . . $ GlassFreeListChecker#bitmap.; |
| 169 | } |
| 170 | |
| 171 | bool mark_usedcxx . . $ GlassFreeListChecker#mark_used(dfa6e707f7696010).(uint4cxx . . $ uint4# n) { |
| 172 | const unsigned BITS_PER_ELT = sizeof(elt_typecxx . . $ GlassFreeListChecker#elt_type#) * 8; |
| 173 | elt_typecxx . . $ GlassFreeListChecker#elt_type# mask = static_cast<elt_typecxx . . $ GlassFreeListChecker#elt_type#>(1) << (n & (BITS_PER_ELT - 1)); |
| 174 | n /= BITS_PER_ELT; |
| 175 | if (rarecxx . . $ GlassFreeListChecker#bitmap_size.(n >= bitmap_size)) return false; |
| 176 | if ((bitmapcxx . . $ GlassFreeListChecker#bitmap.[n] & mask) == 0) return false; |
| 177 | bitmapcxx . . $ GlassFreeListChecker#bitmap.[n] &= ~mask; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | /// Count how many bits are still set. |
| 182 | uint4cxx . . $ uint4# count_set_bitscxx . . $ GlassFreeListChecker#count_set_bits(9d74d346fcb748fb).(uint4cxx . . $ uint4# * p_first_bad_blk) const; |
| 183 | }; |
| 184 | |
| 185 | #endif // XAPIAN_INCLUDED_GLASS_FREELIST_H |