《STL源碼剖析》– defalloc.h

來源:互聯網
上載者:User
// Filename:    defalloc.h// Comment By:  凝霜// E-mail:      mdl2009@vip.qq.com// Blog:        http://blog.csdn.net/mdl13412/* * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation.  Hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose.  It is provided "as is" without express or implied warranty. * */// 這個檔案提供原始的HP預設allocator, 僅僅是為了向後相容//// 不要使用這個檔案,除非你使用一個需要HP-style allocator的舊容器// SGI STL使用一個不同的allocator介面// SGI-style的allocator不針對物件類型進行參數化, 他使用void *指標#ifndef DEFALLOC_H#define DEFALLOC_H#include <new.h>#include <stddef.h>#include <stdlib.h>#include <limits.h>#include <iostream.h>#include <algobase.h>// 如果記憶體配置失敗, 則直接退出程式template <class 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) {cerr << "out of memory" << endl;exit(1);    }    return tmp;}template <class T>inline void deallocate(T* buffer){    ::operator delete(buffer);}// 標準的STL allocator介面template <class T>class allocator{public:    // STL type_traits需要的標準定義    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;    pointer allocate(size_type n)    {return ::allocate((difference_type)n, (pointer)0);    }    void deallocate(pointer p) { ::deallocate(p); }    pointer address(reference x) { return (pointer)&x; }    const_pointer const_address(const_reference x)    {return (const_pointer)&x;    }    //     size_type init_page_size()    {return max(size_type(1), size_type(4096/sizeof(T)));    }    size_type max_size() const    {return max(size_type(1), size_type(UINT_MAX/sizeof(T)));    }};// 僅使用void *類型的指標class allocator<void>{public:    typedef void* pointer;};#endif

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.