简体   繁体   English

malloc /免费的STL分配器

[英]malloc/free based STL allocator

Is there a malloc/free based allocator in the STL? STL中是否有基于malloc / free的分配器? If not, does anyone know of a simple copy/paste one? 如果没有,有没有人知道一个简单的复制/粘贴? I need it for a map that must not call new/delete. 我需要它为一个不能调用new / delete的地图。

First, I'd note that changing the allocator for the map itself won't change the allocation used by the objects stored in the map. 首先,我注意到更改地图本身的分配器不会更改存储地图中的对象使用的分配。 For example, if you do something like: 例如,如果您执行以下操作:

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

The map itself will allocate memory using the specified allocator, but when the std::string s in the map allocate memory, they'll still use the default allocator (which will use new and delete . So, if you need to avoid new and delete in general, you have to ensure that not only the map itself uses the right allocator, but that any objects it stores do the same (I know that's probably stating the obvious, but I've overlooked it, so maybe it's worth mentioning). 映射本身将使用指定的分配器分配内存, 但是当映射中的std::string s分配内存时,它们仍将使用默认分配器(将使用newdelete 。因此,如果您需要避免使用newdelete一般来说,你必须确保不仅地图本身使用正确的分配器,而且它存储的任何对象都是相同的(我知道这可能说明显而易见,但我忽略了它,所以也许值得一提) 。

With that proviso, on 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's 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;
}

Indeed, as @MichaelBurr suggests, Stephen J, Lavavej's 'mallocator' is what you're looking for. 事实上,正如@MichaelBurr所说,Lavavej的'mallocator'是你正在寻找的。 I just got updated and prettied-up code for it in this answer by @Arnaud today, do have a look. 我今天刚刚在@Arnaud的答案中得到了更新和漂亮的代码,请看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM