Currently in AGG I use new/delete to operate with heap, but sometimes it‘s not very good. On the other hand, I wouldn‘t like to do it in a STL-like way, because it will be too bulky. I would do it as follows. There will be agg_allocator.h file that will contain the following: template<class T> struct allocator { static T* allocate_array(unsigned size) { return new T [size]; } static void free_array(T* v, unsigned) { delete [] v; } }; And I will use it instead of new/delete: char* array = allocator<char>::allocate_array(size); . . . allocator<char>::free_array(array); Everything is implemented in such a way that there‘re no arrays that require initialization of the elements (no need to call constructors), because all allocated data is of POD type. So that, you can replace this file with your own, custom allocator, for example: template<class T> struct allocator { static T* allocate_array(unsigned size) { return (T*)malloc(sizeof(T) * size); } static void free_array(T* v, unsigned size) { if(v) free(v); } }; It will be useful if you use your own, super-efficient heap, for example. So, what do you think about this solution?
This would be indeed a(nother) nice feature of the library since there are many memory allocators which outperform by several orders of magnitude the default ones (e.g. new and malloc). I‘d say go for it, using the proposed sollution (template<class T> struct allocator ...)
时间: 2024-11-25 13:23:06