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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
// Hyperbolic Rogue -- memory saving
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
/** \file memory.cpp
* \brief memory saving: memory saving mode, memory warnings, etc.
*/
#include "hyper.h"
namespace hr {
#if HDR
static const int PSEUDOKEY_MEMORY = 16397;
#endif
EX bool memory_saving_mode = true;
EX bool show_memory_warning = true;
EX bool ignored_memory_warning;
static const int LIM = 150;
EX heptagon *last_cleared;
EX void destroycellcontents(cell *c) {
c->land = laMemory;
c->wall = waChasm;
c->item = itNone;
if(!isMultitile(c->monst) && c->monst != moPair)
c->monst = moNone;
}
void degrade(cell *c) {
c->mpdist++;
forCellEx(c2, c)
if(c2->mpdist < c->mpdist - 1)
degrade(c2);
destroycellcontents(c);
}
EX vector<cell*> removed_cells;
void slow_delete_cell(cell *c) {
while(c->mpdist < BARLEV)
degrade(c);
for(int i=0; i<c->type; i++)
if(c->move(i))
c->move(i)->move(c->c.spin(i)) = NULL;
removed_cells.push_back(c);
destroy_cell(c);
}
void delete_heptagon(heptagon *h2) {
cell *c = h2->c7;
if(BITRUNCATED) {
for(int i=0; i<c->type; i++)
if(c->move(i))
slow_delete_cell(c->move(i));
}
slow_delete_cell(c);
for(int i=0; i<S7; i++)
if(h2->move(i))
h2->move(i)->move(h2->c.spin(i)) = NULL;
tailored_delete(h2);
}
void recursive_delete(heptagon *h, int i) {
heptagon *h2 = h->move(i);
{ for(int i=1; i<S7; i++)
if(h2->move(i) && h2->move(i)->move(0) == h2)
recursive_delete(h2, i); }
if(h2->alt && h2->alt->alt == h2->alt) {
DEBB(DF_MEMORY, ("destroying alternate map ", h2->alt));
for(hrmap *& hm: allmaps) {
if(hm->getOrigin() == h2->alt) {
delete hm;
hm = allmaps.back();
allmaps.pop_back();
DEBB(DF_MEMORY, ("map found (", isize(allmaps), " altmaps total)"));
break;
}
}
}
if(h2->alt) {
h2->alt->cdata = NULL;
}
delete_heptagon(h2);
h->move(i) = NULL;
}
bool unsafeLand(cell *c) {
return
isCyclic(c->land) || isGravityLand(c->land) || isHaunted(c->land) ||
among(c->land, laCaribbean, laOcean, laGraveyard, laPrincessQuest);
}
EX void save_memory() {
if(quotient || !hyperbolic || NONSTDVAR) return;
if(!memory_saving_mode) return;
if(unsafeLand(cwt.at)) return;
int d = celldist(cwt.at);
if(d < LIM+10) return;
heptagon *at = cwt.at->master;
heptagon *orig = currentmap->gamestart()->master;
if(recallCell.at) {
if(unsafeLand(recallCell.at)) return;
heptagon *at2 = recallCell.at->master;
int t = 0;
while(at != at2) {
t++; if(t > 10000) return;
if(celldist(at->c7) > celldist(at2->c7))
at = at->move(0);
else
at2 = at2->move(0);
}
}
while(celldist(at->c7) > d-LIM) at = at->move(0);
// go back to such a point X that all the heptagons adjacent to the current 'at'
// are the children of X. This X becomes the new 'at'
if(true) {
heptagon *allh[9];
int hcount = 0;
allh[hcount++] = at;
for(int j=0; j<S7; j++)
if(allh[0]->move(j))
allh[hcount++] = at->move(j);
int deuniq_steps = 0;
int i = 1;
while(i < hcount) {
if(allh[i] == allh[0])
allh[i] = allh[hcount-1], hcount--;
else if(celldist(allh[i]->c7) > celldist(allh[0]->c7))
allh[i] = allh[i]->move(0);
else {
if(allh[0] == orig) return;
allh[0] = allh[0]->move(0);
i = 1;
deuniq_steps++;
if(deuniq_steps == 10) return;
}
}
at = allh[0];
}
if(last_cleared && celldist(at->c7) < celldist(last_cleared->c7))
return;
DEBB(DF_MEMORY, ("celldist = ", make_pair(celldist(cwt.at), celldist(at->c7))));
heptagon *at1 = at;
while(at != last_cleared && at != orig) {
heptagon *atn = at;
at = at->move(0);
for(int i=1; i<S7; i++)
if(at->move(i) && at->move(i) != atn)
recursive_delete(at, i);
}
last_cleared = at1;
DEBB(DF_MEMORY, ("current cellcount = ", cellcount));
sort(removed_cells.begin(), removed_cells.end());
callhooks(hooks_removecells);
removed_cells.clear();
}
EX purehookset hooks_removecells;
EX bool is_cell_removed(cell *c) {
return binary_search(removed_cells.begin(), removed_cells.end(), c);
}
EX void set_if_removed(cell*& c, cell *val) {
if(is_cell_removed(c)) c = val;
}
typedef array<char, 1048576> reserve_block;
EX int reserve_count = 0;
EX int reserve_limit = 128;
const int max_reserve = 4096;
array<reserve_block*, max_reserve> reserve;
std::new_handler default_handler;
EX purehookset hooks_clear_cache;
void reserve_handler() {
if(reserve_count) {
reserve_count--;
delete reserve[reserve_count];
}
if(reserve_count < 32) callhooks(hooks_clear_cache);
if(!reserve_count) std::set_new_handler(default_handler);
}
EX void apply_memory_reserve() {
#if CAP_MEMORY_RESERVE
if(reserve_count > 0) std::set_new_handler(default_handler);
if(reserve_limit > max_reserve) reserve_limit = max_reserve;
if(reserve_limit < 0) reserve_limit = 0;
while(reserve_count > reserve_limit) { reserve_count--; delete reserve[reserve_count]; }
try {
while(reserve_count < reserve_limit) {
reserve[reserve_count] = new reserve_block;
/* only if successful */
reserve_count++;
}
}
catch(std::bad_alloc&) {}
#if ISWINDOWS
// no get_new_handler on this compiler...
default_handler = [] { throw std::bad_alloc(); };
#else
default_handler = std::get_new_handler();
#endif
if(reserve_count > 0) std::set_new_handler(reserve_handler);
#endif
}
EX void memory_for_lib() {
if(reserve_count) { reserve_count--; delete reserve[reserve_count]; }
}
EX void show_memory_menu() {
gamescreen();
dialog::init(XLAT("memory"));
dialog::addHelp(XLAT(
"HyperRogue takes place in a world that is larger than anything Euclidean. "
"Unfortunately, in some cases running it on an Euclidean computer might be "
"a problem -- the computer could simply run out of memory. Some lands (such as the Ocean or the Brown Islands) "
"may use up memory very fast!\n\n"
));
if(sizeof(void*) <= 4)
dialog::addHelp(XLAT(
"You are playing a 32-bit HyperRogue executable, which can only use 4GB of memory.\n\n"));
dialog::addHelp(XLAT(
"Although you are extremely unlikely to return to a place you have already been to, "
"the game never forgets these areas, unless you start a new game, use an Orb of "
"Safety (found in Land of Eternal Motion, the Prairie, and the Ocean), or activate the memory "
"saving mode, which tries to intelligently predict which cells you will never find "
"again and can be safely forgotten.\n\n")
);
if(cheater) dialog::addSelItem(XLAT("cells in memory"), its(cellcount) + "+" + its(heptacount), 0);
dialog::addBoolItem(XLAT("memory saving mode"), memory_saving_mode, 'f');
dialog::add_action([] { memory_saving_mode = !memory_saving_mode; if(memory_saving_mode) save_memory(), apply_memory_reserve(); });
dialog::addBoolItem_action(XLAT("show memory warnings"), show_memory_warning, 'w');
#if CAP_MEMORY_RESERVE
if(reserve_limit > 0 && reserve_count < reserve_limit) {
dialog::addItem(XLAT("just let me find Orb of Safety or finish the game"), 'l');
dialog::add_action([] { ignored_memory_warning = true; popScreen(); });
}
dialog::addSelItem("memory reserve", its(reserve_count) + "/" + its(reserve_limit) + " MB", 'r');
dialog::add_action([] {
dialog::editNumber(reserve_limit, 0, max_reserve, 16, 128, XLAT("memory reserve"),
XLAT("When to show a memory warning.")
);
dialog::bound_low(0);
dialog::bound_up(max_reserve);
dialog::reaction = apply_memory_reserve;
});
#endif
dialog::addItem(XLAT("clear caches"), 'c');
dialog::add_action([] { callhooks(hooks_clear_cache); });
dialog::addBack();
dialog::display();
}
EX bool protect_memory() {
#if CAP_MEMORY_RESERVE
apply_memory_reserve();
if(reserve_limit && reserve_count < reserve_limit && !ignored_memory_warning) {
pushScreen(show_memory_menu);
return true;
}
if(reserve_limit && reserve_count < 8) {
pushScreen(show_memory_menu);
return true;
}
#endif
return false;
}
EX bool memory_issues() {
return reserve_limit && reserve_count < 16;
}
}
|