Malloc / STL Free Distributor

Is there a malloc / free distributor in STL? If not, does anyone know a simple copy / paste? I need this for a map that should not call new / delete.

+7
source share
2 answers

First of all, I would like to note that changing the distributor for the card itself will not change the distribution used by the objects stored on the card. For example, if you do something like:

std::map<std::string, int, my_allocator<std::pair<const std::string, int> > m; 

The card itself will allocate memory using the specified allocator, but when std::string on the map allocates memory, they will still use the default allocator (which will use new and delete . So, if you need to avoid new and delete in general, you must make sure that not only the card itself uses the correct allocator, but also all the objects that it stores do the same (I know that I may point out the obvious, but I did not pay attention to it, therefore, it may be worth mentioning )

With this reservation, with the code:

 #ifndef ALLOCATOR_H_INC_ #define ALLOCATOR_H_INC_ #include <stdlib.h> #include <new> #include <limits> namespace JVC { template <class T> struct allocator { typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; template <class U> struct rebind { typedef allocator<U> other; }; allocator() throw() {} allocator(const allocator&) throw() {} template <class U> allocator(const allocator<U>&) throw(){} ~allocator() throw() {} pointer address(reference x) const { return &x; } const_pointer address(const_reference x) const { return &x; } pointer allocate(size_type s, void const * = 0) { if (0 == s) return NULL; pointer temp = (pointer)malloc(s * sizeof(T)); if (temp == NULL) throw std::bad_alloc(); return temp; } void deallocate(pointer p, size_type) { free(p); } size_type max_size() const throw() { return std::numeric_limits<size_t>::max() / sizeof(T); } void construct(pointer p, const T& val) { new((void *)p) T(val); } void destroy(pointer p) { p->~T(); } }; } #endif 

And, a little test code:

 #include <map> #include <vector> #include <iostream> #include <string> #include <iterator> #include "allocator.h" // Technically this isn't allowed, but it only demo code, so we'll live with it. namespace std { std::ostream &operator<<(std::ostream &os, std::pair<std::string, int> const &c) { return os << c.first << ": " << c.second; } } int main() { std::map<std::string, int, std::less<std::string>, JVC::allocator<std::pair<const std::string, int> > > stuff; stuff["string 1"] = 1; stuff["string 2"] = 2; stuff["string 3"] = 3; std::copy(stuff.begin(), stuff.end(), std::ostream_iterator<std::pair<std::string, int> >(std::cout, "\n")); return 0; } 
+11
source

Indeed, as @MichaelBurr suggests, Stephen J, Lavavej 'mallocator' is what you are looking for. I just updated and exceeded the code for this from @Arnaud, really take a look.

0
source

All Articles