Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compact offset pointer , existing implementations?

Is there template in stl,boost or other LGPL open-source toolkit which behaves exactly like this:-
- a relative pointer with custom alignment,option to store fewer bits to reduce range. a possible implementation to illustrate:-

template<typename T, typename OFFSET=int, 
    int ALIGN_SHIFT=2>
class   OffsetPtr 
{
    OFFSET  ofs;

public:
    T* operator->() {
        return  (T*) (((((size_t)this)>>ALIGN_SHIFT)+ofs)<<ALIGN_SHIFT);
    };
    void operator=(T* src) {
        size_t ofs_shifted = (((size_t) src)>>ALIGN_SHIFT) - (((size_t) this)>>ALIGN_SHIFT); //asserts..
        ofs = (OFFSET) (ofs_shifted);
    }
    //...
};

Its something I would routinely create in the past (compact cache-friendly precompiled data-structures), e.g. for data broken into sub 128k chunks OFFSET=short
Another variation I'd use in ancient C #defines would use offsets from a header, where the alignments would be more useful.

I've seen something about an 'interprocess library' in boost having an 'offset_ptr', that looks very similar, so it seems likely there's an existing implementation including this exact pattern somewhere. It's quick to write but there might be benefits to an existing implementation like a suite of associated stl compliant data structures built around the same concept - a 'near vector' with 16bit offset pointer & 16bit count for example

like image 585
centaurian_slug Avatar asked Nov 04 '22 10:11

centaurian_slug


1 Answers

If you're using Visual C++, you might like to use __based pointers.

like image 115
user541686 Avatar answered Nov 13 '22 05:11

user541686