1 /** 2 * Memory and GC stuff. 3 * 4 * License: 5 * This Source Code Form is subject to the terms of 6 * the Mozilla Public License, v. 2.0. If a copy of 7 * the MPL was not distributed with this file, You 8 * can obtain one at http://mozilla.org/MPL/2.0/. 9 * 10 * Authors: 11 * Vladimir Panteleev <vladimir@thecybershadow.net> 12 */ 13 14 module ae.sys.memory; 15 16 /// Did the GC run since this function's last call on this thread? 17 /// Not 100% reliable (due to false pointers). 18 bool gcRan() 19 { 20 static bool initialized = false; 21 static bool destroyed = false; 22 23 static class Beacon 24 { 25 ~this() 26 { 27 destroyed = true; 28 } 29 } 30 31 if (!initialized) 32 { 33 destroyed = false; 34 new Beacon(); 35 initialized = true; 36 } 37 38 bool result = destroyed; 39 if (destroyed) 40 { 41 destroyed = false; 42 new Beacon(); 43 } 44 45 return result; 46 } 47 48 import core.thread; 49 50 /// Is the given pointer located on the stack of the current thread? 51 /// Useful to assert on before taking the address of e.g. a struct member. 52 bool onStack(const(void)* p) 53 { 54 auto p0 = thread_stackTop(); 55 auto p1 = thread_stackBottom(); 56 return p0 <= p && p <= p1; 57 } 58 59 unittest 60 { 61 /* .......... */ int l; auto pl = &l; 62 static /* ... */ int s; auto ps = &s; 63 static __gshared int g; auto pg = &g; 64 /* ................. */ auto ph = new int; 65 assert( pl.onStack()); 66 assert(!ps.onStack()); 67 assert(!pg.onStack()); 68 assert(!ph.onStack()); 69 }