空间配置器简版

来源:互联网 发布:半身裙淘宝店好一点 编辑:程序博客网 时间:2024/05/20 07:19

标准库的空间配置器,allocate的简版实践,就是将空间分配与对象构造这两个事情分离开,同样对对象析构以及空间释放分开,这样有利于效率的优化,这里是一个简单版本。
首先写一个简单的小代码,ptrdiff_t 这个变量是指两个指针之间的差值。

#include <stdio.h>#include <stddef.h>#include <string.h>int main(){    char str[] = "hello world";    char *pstart = str;    char *pend = str + strlen(str);    ptrdiff_t difp1 = pend - pstart;    ptrdiff_t difp2 = pstart - pend;    printf("the str: %s \t and len: %d\n", str, strlen(str));    printf("the diff point %d\t%d\n", difp1, difp2);    return 0;}

下面就是allocate的简版实现,实际上就是将new delete 两个操作符做了很简单的包装,将来期望能有更多的实现。
头文件代码如下:

#ifndef __JJALLOC_H_#define __JJALLOC_H_#include <new>#include <cstddef>#include <cstdlib>#include <climits>#include <iostream>namespace JJ {    template <typename T>    inline T* _allocate(ptrdiff_t size, T*) {        //set_new_handler(0);        T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));        if (tmp == 0) {            std::cerr << "out of memory" << std::endl;            exit(-1);        }        //std::cout << std::endl << "_allocate" << std::endl;        return tmp;    }    template <typename T>    inline void _deallocate(T* buffer) {        ::operator delete(buffer);        //std::cout << std::endl << "_deallocate" << std::endl;    }    template <typename T1, typename T2>    inline void _construct(T1 *p, const T2& value) {        new(p) T1(value);        //std::cout << std::endl << "_construct" << std::endl;    }    template <typename T>    inline void _destory(T* ptr) {        ptr->~T();        //std::cout << std::endl << "_destory" << std::endl;    }    template <typename T>    class allocator {        public:            typedef T value_type;            typedef T* pointer;            typedef const T* const_pointer;            typedef T& reference;            typedef const T& const_reference;            typedef size_t size_type;            typedef ptrdiff_t difference_type;            template <typename U>            struct rebind {                typedef allocator<U> other;            };            pointer allocate(size_type n, const void* hint=0) {                return _allocate((difference_type)n, (pointer)0);            }            void deallocate(pointer p, size_type n) {                _deallocate(p);            }            void construct(pointer p, const T& value) {                _construct(p, value);            }            void destory(pointer p) {                _destory(p);            }            pointer address(reference x) {                return (pointer) &x;            }            const_pointer const_address(const_reference x) {                return (const_reference) &x;            }            size_type max_size() const {                return size_type(UINT_MAX/sizeof(T));            }    };}#endif  //__JJALLOC_H_

这个又回到了最原始的一个问题,那就是模板需要放在头文件中,包括cpp的实现,不需要参与编译生成 .o文件,这个是一个小的知识点。

#include "jjalloc.h"#include <vector>#include <iostream>using namespace std;int main(){    int ia[5] = {0, 1, 2, 3, 4};    unsigned int i;    vector<int, JJ::allocator<int> > iv(ia, ia+5);    for (i = 0; i < iv.size(); i++)        cout << iv[i] << ' ';    cout << endl;    return 0;}
原创粉丝点击