Skip to content

Instantly share code, notes, and snippets.

@freecnjet
Last active October 2, 2015 23:48
Show Gist options
  • Save freecnjet/2348231 to your computer and use it in GitHub Desktop.
Save freecnjet/2348231 to your computer and use it in GitHub Desktop.
Kernel memory.
#ifndef _MEMORY_H_
#define _MEMORY_H_
void* Malloc( size_t size );
void Free( void* buf );
void* AlignMalloc( size_t size, size_t align );
void AlignFree( void* buf );
#endif
template<class T>
class MyAllocator : public std::allocator<T>
{
public:
typedef std::allocator<T> base_type;
template<class Other>
struct rebind
{
typedef MyAllocator<Other> other;
};
MyAllocator() {}
MyAllocator(MyAllocator<T> const&) {}
MyAllocator<T>& operator=(MyAllocator<T> const&) { return (*this); }
// idiom: Coercion by Member Template
template<class Other>
MyAllocator(MyAllocator<Other> const&) {}
// idiom: Coercion by Member Template
template<class Other>
MyAllocator<T>& operator=(MyAllocator<Other> const&) { return (*this); }
pointer allocate(size_type count) { return (pointer)MyAlloc(count*sizeof(T)); }
void deallocate(pointer ptr, size_type count) { MyFree(ptr); }
};
template <typename _T>
class MyVector : public std::vector<_T, MyAllocator<_T>>
{
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment