Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting a C++ Vector as a Member in a Class that Uses a Memory Pool

I've been writing a multi-threaded DLL for database access using ADO/ODBC for use with a legacy application. I need to keep multiple database connections for each thread, so I've put the ADO objects for each connection in an object and thinking of keeping an array of them inside a custom threadInfo object. Obviously a vector would serve better here - I need to delete/rearrange objects on the go and a vector would simplify that. Problem is, I'm allocating a heap for each thread to avoid heap contention and stuff and allocating all my memory from there.

So my question is: how do I make the vector allocate from the thread-specific heap? (Or would it know internally to allocate memory from the same heap as its wrapper class - sounds unlikely, but I'm not a C++ guy) I've googled a bit and it looks like I might need to write an allocator or something - which looks like so much of work I don't want. Is there any other way? I've heard vector uses placement-new for all its stuff inside, so can overloading operator new be worked into it?

My scant knowledge of the insides of C++ doesn't help, seeing as I'm mainly a C programmer (even that - relatively). It's very possible I'm missing something elementary somewhere. If nothing easier comes up - I might just go and do the array thing, but hopefully it won't come to that.

I'm using MS-VC++ 6.0 (hey, it's rude to laugh! :-P ).

Any/all help will be much appreciated.

like image 569
Deep-B Avatar asked Oct 25 '22 17:10

Deep-B


1 Answers

how do I make the vector allocate from the thread-specific heap?

You pass it (at compile-time) an appropriate allocator. Here is a classic on how to do so. If you follow that article's advice (or even just copy the code and adapt it where needed), for a C programmer writing an allocator might be easier than getting right the copy semantics of a class with a dynamically allocated array.

Note that, if you put objects into the vector (or your own array, FTM), which themselves use the heap (strings, for example), you need to take that they use your special heap, too. For containers of the standard library (std::basic_string<> is such a container) it's easy since you can pass them your allocator as well. For your own types you have to make that sure yourself.

And try to get away from VC6 as fast as possible. It's poisonous.

like image 69
sbi Avatar answered Nov 15 '22 05:11

sbi