baseten / Sources / PGTSScannedMemoryAllocator.h

// PGTSScannedMemoryAllocator.h
// BaseTen
// Copyright (C) 2008 Marko Karppinen & Co. LLC.
// Before using this software, please review the available licensing options
// by visiting or by contacting
// us at Without an additional license, this software
// may be distributed only in compliance with the GNU General Public License.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
// $Id$


#import <Foundation/Foundation.h>
#import <new>
#import <limits>

namespace PGTS 
	class scanned_memory_allocator_env {
		static BOOL allocate_scanned;
	template <typename T> 
	class scanned_memory_allocator {
		typedef T                 value_type;
		typedef value_type*       pointer;
		typedef const value_type* const_pointer;
		typedef value_type&       reference;
		typedef const value_type& const_reference;
		typedef std::size_t       size_type;
		typedef std::ptrdiff_t    difference_type;
		template <typename U> struct rebind { typedef scanned_memory_allocator <U> other; };
		explicit scanned_memory_allocator () {}
		scanned_memory_allocator (const scanned_memory_allocator&) {}
		template <typename U> scanned_memory_allocator (const scanned_memory_allocator <U> &) {}
		~scanned_memory_allocator () {}
		pointer address (reference x) const { return &x; }
		const_pointer address (const_reference x) const { return x; }
		pointer allocate (size_type n, const_pointer = 0) 
			void* p = NULL;
			//Symbol existence verification requires NULL != -like comparison.
			if (scanned_memory_allocator_env::allocate_scanned)
				p = NSAllocateCollectable (n * sizeof (T), NSScannedOption | NSCollectorDisabledOption);
				p = malloc (n * sizeof (T));
			if (! p)
				throw std::bad_alloc ();
			return static_cast <pointer> (p);
		void deallocate (pointer p, size_type n) 
			free (p);
		size_type max_size () const 
			return std::numeric_limits <size_type>::max () / sizeof (T);
		void construct (pointer p, const value_type& x) 
			new (p) value_type (x); 
		void destroy (pointer p) 
			p->~value_type (); 
		void operator= (const scanned_memory_allocator&);
	template <> class scanned_memory_allocator <void>
		typedef void        value_type;
		typedef void*       pointer;
		typedef const void* const_pointer;
		template <typename U> 
		struct rebind { typedef scanned_memory_allocator <U> other; };
	template <typename T> inline bool 
	operator== (const scanned_memory_allocator <T> &, const scanned_memory_allocator <T> &)
		return true;
	template <typename T> inline bool 
	operator!= (const scanned_memory_allocator <T> &, const scanned_memory_allocator <T> &) 
		return false;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.