From c5ed37775302f818d51a33a138d4e80d011e3a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Thu, 18 Sep 2014 14:42:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Allocator=E6=9D=A5=E5=8C=85?= =?UTF-8?q?=E8=A3=85Alloc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Alloc.h | 3 +- TinySTL/Allocator.h | 66 +++++++++++++++++++++++++++++++++ TinySTL/TinySTL.vcxproj | 1 + TinySTL/TinySTL.vcxproj.filters | 3 ++ TinySTL/main.cpp | 9 +++-- 5 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 TinySTL/Allocator.h diff --git a/TinySTL/Alloc.h b/TinySTL/Alloc.h index aea662d..2a4a43c 100644 --- a/TinySTL/Alloc.h +++ b/TinySTL/Alloc.h @@ -6,7 +6,8 @@ namespace TinySTL{ /* - **¿Õ¼äÅäÖÃÆ÷ + **¿Õ¼äÅäÖÃÆ÷£¬ÒÔ×Ö½ÚÊýΪµ¥Î»·ÖÅä + **ÄÚ²¿Ê¹Óà */ class alloc{ private: diff --git a/TinySTL/Allocator.h b/TinySTL/Allocator.h new file mode 100644 index 0000000..0e70378 --- /dev/null +++ b/TinySTL/Allocator.h @@ -0,0 +1,66 @@ +#ifndef _ALLOCATOR_H_ +#define _ALLOCATOR_H_ + +#include "Alloc.h" +#include "Construct.h" + +#include + +namespace TinySTL{ + + /* + **¿Õ¼äÅäÖÃÆ÷£¬ÒÔ±äÁ¿ÊýĿΪµ¥Î»·ÖÅä + */ + template + 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; + public: + static T *allocate(); + static T *allocate(size_t n); + static void deallocate(T *ptr); + static void deallocate(T *ptr, size_t n); + + static void construct(T *ptr); + static void construct(T *ptr, const T& value); + static void destroy(T *ptr); + }; + + template + T *allocator::allocate(){ + return static_cast(alloc::allocate(sizeof(T))); + } + template + T *allocator::allocate(size_t n){ + return static_cast(alloc::allocate(sizeof(T) * n)); + } + template + void allocator::deallocate(T *ptr){ + alloc::deallocate(static_cast(ptr), sizeof(T)); + } + template + void allocator::deallocate(T *ptr, size_t n){ + alloc::deallocate(static_cast(ptr), sizeof(T)* n); + } + + template + void allocator::construct(T *ptr){ + new(ptr)T(); + } + template + void allocator::construct(T *ptr, const T& value){ + new(ptr)T(value); + } + template + void allocator::destroy(T *ptr){ + ptr->~T(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 6dab845..0be76d6 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -80,6 +80,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index 1e1ae10..ee4eeb0 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -29,5 +29,8 @@ 头文件 + + 头文件 + \ No newline at end of file diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index 875d275..96da56c 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -1,16 +1,17 @@ #include #include -#include "Alloc.h" +#include "Allocator.h" #include "Construct.h" using namespace std; int main(){ for (int i = 1; i != 100000; ++i){ - TinySTL::alloc::allocate(i % 128 * sizeof(int)); - //std::allocator alloc; alloc.allocate(i % 128); - //malloc(i % 128 * sizeof(int)); + auto p = TinySTL::allocator::allocate(); + TinySTL::allocator::construct(p, i); + TinySTL::allocator::destroy(p); + TinySTL::allocator::deallocate(p); } system("pause"); return 0;