visual c++ - Using c++, how do I get the value of the stack pointer in msvc X64 -
using c++, fastest way either stack pointer or rough estimate of maximum value of stack pointer in msvc++ on x64?
i'm going use write inline function:
static __forceinline bool isonstack(const void *p) { return uint_ptr(p) < __esp; }
i use macro if better
#define isonstack(a)
thanks!
edit
i need know if on stack other code won't free it. suffice legacy implementation of smart pointers making faster. have work address of object smart pointer referring to. making change smart pointer can refer item on stack, eliminate superflous heap allocations. 64 bit stack appears relatively low virtual address. our allocator uses predefined virtual base address (16gb right now). assume below on stack. method might accidentally assume allocated malloc or ::new on stack, wouldn't end of world, since aren't supposed ever use those. thought i'd see if there better way idea stack was. performance more important accuracy long don't false negatives.
edit
i know assigning smart pointers allocated memory because use 2 ways set smart pointers:
new(spfoo) cfoo(); // uses overridden new
or
spfoo = spotherfoo;
i thinking of adding:
cfoo foo(); spfoo = &foo;
edit should add not use std libraries (partly because code old , partly because our application edge case , performance important). don't want make debate standard libraries, agree awesome. don't have application. our code runs on internal servers. our code 64 bit. use threads, rare. portability not issue. use microsoft visual studio 2013, vc++ 2012. run on server 2008. upgrading server 2012 day, , visual studio 2015 when comes out.
this started out usual gripe inline assembly not being available. having hard time finding documentation general memory layout (where stack goes, heap goes, etc) of vc++ app running on windows.
ok, longer "comments" worth.
first of "knowing if need free or not" should solved std::shared_ptr
or std::unique_ptr
, not "checking if it's on stack or not". about:
void function(int *p) { if (!isonstack(p)) delete p; } std::vector<int> v; ... fill stuff v ... function(&v[14]);
freeing in middle of std::vector
bad thing - guarantee address not on stack.
static int x; function(&x);
no better there - cause bad free.
as "where stack located" entirely based on runtime environment (combination of os , compiler, etc). may high address. may high address in "main" thread, , lower address in secondary thread - don't want work in "main" thread, you?
the location of thread's stacks of course entirely dependent on os , runtime library provides thread (and stack allocation).
you may able like:
uintptr_t sp_base; bool isinstack(void *p) { int x; uintptr_t sp_top = (uintptr_t)(&x); // assumes stack grows towards 0. return (p > sp_top) && (p < sp_base); } int main() { int x; sp_base = (uintptr_t)(&x); ... }
but far guaranteed work every time, or of time - depending on actual os , compiler choice, result may vary quite wildly.
Comments
Post a Comment