Thread: Programming library with built in manager

  1. #1
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66

    Programming library with built in manager

    Hey guys,

    I have a quick question, but I feel I need to explain things in a bit of detail prior to the actual question. My next project is going to be requiring the use of a memory manager of sorts, what I would like to do is to create a library that contains this manager and providing the developer with a series of "wrapper" functions (basically just providing access to the appropriate dynamic allocation functions with additional support for the memory management operations). To implement the memory manager, I was thinking of implementing a class that handled these operations and providing access to the "wrapper" functions to this class through the means of a global variable visible to only the "inside" of the library. Just for example purposes, the following is a sample of the way the inside of the library will be setup:

    Code:
    // appropriate includes here
    MemoryManager* myMemory;
    
    int InitManager() { myMemory=new MemoryManager(...); }
    int NewMemory(...) { /* do some stuff here */ }
    // etc.
    I know the above example utilizes C++ in some ways, however, the question is actually in terms of access. What I'm wondering is, would the above example have problems if the library's header is included across a larger program? To be more specific, would the MemoryManager class be capable of handling operations across the entire program or would multiple instances be attempted to be created. I have yet to be able to find an example or article talking about such a technique, but I'm guessing this might actually show signs of problems when using a multithreaded application? Correct me if I'm wrong, or if there are any articles detailing this method or perhaps a better one?

    Any input would be great! I guess I could implement it and see, but I would rather not have to spend the time to implement the entire library just to find that out.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    For example, calling malloc from multiple places, or indeed multiple threads isn't an issue.

    Done right, it won't be an issue for you either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Thanks Salem. I realize that calling malloc from multiple sources isn't an issue, but in terms of libraries a program includes the code before compile time only once. Would it be safe to assume that the global variables visible to only the code inside the library would also only exist once even in the context of a multithreaded environment?

    I haven't done a lot of work using global variables inside of libraries, just tend to make a series of functions for a specific purpose usually.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, you'll only have one instance of that global, and only you will be able to see it.

    Or at least it's easy to arrange things so that will be the case.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> myMemory=new MemoryManager(...);

    Hmm, looks like you'll need a memory manager for that memory manager.

    Seriosly, though, if you're using C++ you don't need one - use a smart pointer.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Salem, thanks once again.

    Sebastiani, lol... not entirely. This isn't a true memory manager, though it does provide some features that assist with memory management of sorts. Mainly intended to maintain a series of records of all dynamically allocated memory blocks for guaranteed removal at the termination of a program, just in case one here or there gets forgotten about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  2. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  3. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 04:52 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  5. better c string functions
    By samps005 in forum C Programming
    Replies: 8
    Last Post: 11-04-2003, 01:28 PM