Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:52:48+00:00 2026-06-05T21:52:48+00:00

The implementation of a std::allocator (based on MAllocator ) backed by Windows named shared

  • 0

The implementation of a std::allocator (based on MAllocator) backed by Windows named shared memory, adds a couple of items to MAllocator.

  • An additional parameter name for allocate(), alters the signature over std::allocator. Is this ok, considering that allocators are typically template parameters (errors will be caught at compile time)?

    template<class T> T *allocate(const size_t n, const char* name);

  • State is maintained in the form of a map of pointers to handles for deallocate(). The allocator denys equality with other instances to preserve state integrity. Is this sufficient or are additional safeguards needed?

    typedef std::map<uintptr_t, HANDLE> HandleMap;
    HandleMap mHandles;
    
    deallocate(T *const p, size_t n = 0) 
    {
        if (mHandles.find((uintptr_t)p) == mHandles.end())  //we don't own this pointer
        {
            std::ostringstream msg;
            msg << "Error in deallocate(): Invalid pointer - we don't own it - we can't delete it!";
            throw std::exception(msg.str().c_str() );
        }
    
        unmapView(p);
        closeHandle(p);
        mHandles.erase((uintptr_t)p);
    }
    

Full code on github

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:52:50+00:00Added an answer on June 5, 2026 at 9:52 pm

    Firstly, your question says “for a std::allocator” but this is not a std::allocator, it’s a ShmAllocator. std::allocator is a class template provided by the standard library, your allocator is not that class template, it’s a different type. I think you mean “for an allocator” or just “for a standard allocator”, which refers to a type conforming to the allocator requirements (std::allocator is just one example of such a type.)

    This fails to meet the allocator requirements, specifically a copy is not equal to the original object. If you plan to use this allocator with code that expects a standard-conforming allocator it could fail. If you don’t plan to use it with code that expects a standard allocator, then why bother copying its interface?

    The requirements for the copy constructor require that for an object a of type ShmAllocator<T> the statement ShmAllocator<T> a1(a); is valid, with the post-condition that a1 == a, and for an object b of type ShmAllocator<U> the statement ShmAllocator<T> a(b); is valid, with the post-condition that ShmAllocator<U>(a) == b and a == ShmAllocator<T>(b).

    These requirements are necessary because containers need to be able to rebind allocators and make copies which must be able to free each others memory. Equality for stateful allocators should be dependent on value not identity.

    To make your allocator meet the requirements you could store mHandles as shared_ptr<mHandles> so that copies of the allocator can all share the same collection.

    How do you plan to use this allocator? I don’t know the Windows API functions, but it appears that every time you call allocate you need to use a different value for name or you will get the same pointer returned, which means it can’t be used by standard containers. I would expect a shared-memory allocator to create a shared-memory region in its constructor (or be constructed with a reference to some existing region, e.g. see Boost.Interprocess allocators) then divide that region up and return different chunks of it every time allocate is called, rather than create a new region on every call to allocate. Is this intended to be user to share objects between processes? How will another process get access to an existing object? It seems the other process would have to call allocate to get a pointer to an already constructed object. That’s confusing, allocate should give new, uninitialized memory. A shared-memory allocator would usually define a custom pointer type which stores an offset into the shared-memory region not an absolute address, so that when such a pointer is used in a different process the offset is still valid, even if the region is mapped to a different address.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched for an implementation of std::map runtime ordering and have found this solution:
What strategy should I use if I have an implementation of std::fstream with 32-bit
While working on this question , I noticed that GCC (v4.7)'s implementation of std::function
If we consider a std::string implementation that uses reference counting, consider this scenario: int
Apparently there's a maximum number of arguments for std::thread for the std::thread implementation in
here is my code for xor linked list implementation #include<iostream> using namespace std; struct
I'm having an issue with (specifically the MSFT VS 10.0 implementation of) std::unique_ptrs. When
I'm writing an implementation of std::codecvt facet that uses iconv. This implementation stores a
I wrote a generic in-memory B+Tree implementation in C++ few times ago, and I'm
I'd like to use a std::vector to control a given piece of memory. First

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.