C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-25-2009, 08:07 AM   #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.
cyreon is offline   Reply With Quote
Old 06-25-2009, 08:54 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 06-25-2009, 09:07 AM   #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.
cyreon is offline   Reply With Quote
Old 06-25-2009, 09:54 AM   #4
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 06-25-2009, 12:00 PM   #5
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> 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.
Sebastiani is offline   Reply With Quote
Old 06-26-2009, 12:49 PM   #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.
cyreon is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:19 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22