<commit_before>/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is [Open Source Virtual Machine.].
 *
 * The Initial Developer of the Original Code is
 * Adobe System Incorporated.
 * Portions created by the Initial Developer are Copyright (C) 2004-2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Adobe AS3 Team
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#ifndef __FixedAlloc__
#define __FixedAlloc__

namespace MMgc
{
	/**
	 * This is a fast, fixed-size memory allocator for manually freed
	 * objects.
	 *
	 * Memory is allocated from the system in 4096 byte chunks
	 * via the GCHeap class.  Not that this won't work well for
	 * large objects (>400 bytes).  For that, we have the
	 * FixedAllocLarge allocator which will allocate multiple
	 * pages at a time to minimize waste.
	 */
	class FixedAlloc : public GCAllocObject
	{
		friend class FixedMalloc;
		friend class FastAllocator;
		friend class GC;
	public:
		FixedAlloc(int itemSize, GCHeap* heap);
		~FixedAlloc();

		void* Alloc(size_t size, FixedMallocOpts flags=kNone);
		static void Free(void *item);

		//This method returns the number bytes allocated by FixedAlloc
		size_t GetBytesInUse()
		{
			size_t totalAskSize, totalAllocated;  
			GetUsageInfo(totalAskSize, totalAllocated);
			return totalAllocated;			
		}
		
		//This method is for more fine grained allocation details
		//It reports the total number of bytes requested (i.e. ask size) and
		//the number of bytes actually allocated.  The latter is the same
		//number as reported by GetBytesInUse()
		void GetUsageInfo(size_t& totalAsk, size_t& totalAllocated);
		
		size_t GetItemSize() const;
		int GetMaxAlloc() const { return m_maxAlloc; }

		size_t GetNumChunks() { return m_maxAlloc / m_itemsPerBlock; }	

		static FixedAlloc *GetFixedAlloc(void *item)
		{
			return GetFixedBlock(item)->alloc;
		}

#ifndef VMCFG_SYMBIAN // Symbian SDK sees an error in GC.cpp if FixedBlock is private.
	private:
#endif //VMCFG_SYMBIAN

		struct FixedBlock
		{
			void*  firstFree;        // first item on free list
			void*  nextItem;         // next free item
			FixedBlock* next;
			FixedBlock* prev;
			uint16_t numAlloc;
			uint16_t size;
			FixedBlock *prevFree;
			FixedBlock *nextFree;
			FixedAlloc *alloc;
			char   items[1];
		};

#ifdef VMCFG_SYMBIAN		
	private:
#endif //VMCFG_SYMBIAN

		GCHeap *m_heap;
		unsigned int    m_itemsPerBlock;
		size_t    m_itemSize;

		// The list of chunk blocks
		FixedBlock* m_firstBlock; 
		FixedBlock* m_lastBlock;

		// The lowest priority block that has free items		
		FixedBlock* m_firstFree;

		int    m_maxAlloc;
	#ifdef MMGC_MEMORY_PROFILER
		size_t m_totalAskSize;
	#endif

		bool IsFull(FixedBlock *b) const { return b->numAlloc == m_itemsPerBlock; }
		void CreateChunk(bool canFail);
		void FreeChunk(FixedBlock* b);

		static inline FixedBlock *GetFixedBlock(const void *item)
		{
			return (FixedBlock*) ((uintptr_t)item & ~0xFFF);
		}

		static inline size_t Size(const void *item)
		{
			return GetFixedBlock(item)->size;
		}

#ifdef MMGC_MEMORY_INFO
		static void VerifyFreeBlockIntegrity(const void* item, uint32_t size);
#endif

	};

	class FixedAllocSafe : public FixedAlloc
	{
		friend class GCHeap;	
	public:
		FixedAllocSafe(int itemSize, GCHeap* heap) : 
			FixedAlloc(itemSize, heap)
		{
			VMPI_lockInit(&m_spinlock);
		}
		
		~FixedAllocSafe()
		{
			VMPI_lockDestroy(&m_spinlock);
		}

		void* Alloc(size_t size, FixedMallocOpts flags=0)
		{
			MMGC_LOCK(m_spinlock);
			return FixedAlloc::Alloc(size, flags); 
		}

		void Free(void *ptr)
		{
			MMGC_LOCK(m_spinlock);
			FixedAlloc::Free(ptr);
		}

		static FixedAllocSafe *GetFixedAllocSafe(void *item)
		{
			return (FixedAllocSafe*) FixedAlloc::GetFixedAlloc(item);
		}
<commit_msg>Fix typo causing strict compilers to fail, wish they were all equally strict<commit_after>/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is [Open Source Virtual Machine.].
 *
 * The Initial Developer of the Original Code is
 * Adobe System Incorporated.
 * Portions created by the Initial Developer are Copyright (C) 2004-2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Adobe AS3 Team
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#ifndef __FixedAlloc__
#define __FixedAlloc__

namespace MMgc
{
	/**
	 * This is a fast, fixed-size memory allocator for manually freed
	 * objects.
	 *
	 * Memory is allocated from the system in 4096 byte chunks
	 * via the GCHeap class.  Not that this won't work well for
	 * large objects (>400 bytes).  For that, we have the
	 * FixedAllocLarge allocator which will allocate multiple
	 * pages at a time to minimize waste.
	 */
	class FixedAlloc : public GCAllocObject
	{
		friend class FixedMalloc;
		friend class FastAllocator;
		friend class GC;
	public:
		FixedAlloc(int itemSize, GCHeap* heap);
		~FixedAlloc();

		void* Alloc(size_t size, FixedMallocOpts flags=kNone);
		static void Free(void *item);

		//This method returns the number bytes allocated by FixedAlloc
		size_t GetBytesInUse()
		{
			size_t totalAskSize, totalAllocated;  
			GetUsageInfo(totalAskSize, totalAllocated);
			return totalAllocated;			
		}
		
		//This method is for more fine grained allocation details
		//It reports the total number of bytes requested (i.e. ask size) and
		//the number of bytes actually allocated.  The latter is the same
		//number as reported by GetBytesInUse()
		void GetUsageInfo(size_t& totalAsk, size_t& totalAllocated);
		
		size_t GetItemSize() const;
		int GetMaxAlloc() const { return m_maxAlloc; }

		size_t GetNumChunks() { return m_maxAlloc / m_itemsPerBlock; }	

		static FixedAlloc *GetFixedAlloc(void *item)
		{
			return GetFixedBlock(item)->alloc;
		}

#ifndef VMCFG_SYMBIAN // Symbian SDK sees an error in GC.cpp if FixedBlock is private.
	private:
#endif //VMCFG_SYMBIAN

		struct FixedBlock
		{
			void*  firstFree;        // first item on free list
			void*  nextItem;         // next free item
			FixedBlock* next;
			FixedBlock* prev;
			uint16_t numAlloc;
			uint16_t size;
			FixedBlock *prevFree;
			FixedBlock *nextFree;
			FixedAlloc *alloc;
			char   items[1];
		};

#ifdef VMCFG_SYMBIAN		
	private:
#endif //VMCFG_SYMBIAN

		GCHeap *m_heap;
		unsigned int    m_itemsPerBlock;
		size_t    m_itemSize;

		// The list of chunk blocks
		FixedBlock* m_firstBlock; 
		FixedBlock* m_lastBlock;

		// The lowest priority block that has free items		
		FixedBlock* m_firstFree;

		int    m_maxAlloc;
	#ifdef MMGC_MEMORY_PROFILER
		size_t m_totalAskSize;
	#endif

		bool IsFull(FixedBlock *b) const { return b->numAlloc == m_itemsPerBlock; }
		void CreateChunk(bool canFail);
		void FreeChunk(FixedBlock* b);

		static inline FixedBlock *GetFixedBlock(const void *item)
		{
			return (FixedBlock*) ((uintptr_t)item & ~0xFFF);
		}

		static inline size_t Size(const void *item)
		{
			return GetFixedBlock(item)->size;
		}

#ifdef MMGC_MEMORY_INFO
		static void VerifyFreeBlockIntegrity(const void* item, uint32_t size);
#endif

	};

	class FixedAllocSafe : public FixedAlloc
	{
		friend class GCHeap;	
	public:
		FixedAllocSafe(int itemSize, GCHeap* heap) : 
			FixedAlloc(itemSize, heap)
		{
			VMPI_lockInit(&m_spinlock);
		}
		
		~FixedAllocSafe()
		{
			VMPI_lockDestroy(&m_spinlock);
		}

		void* Alloc(size_t size, FixedMallocOpts flags=kNone)
		{
			MMGC_LOCK(m_spinlock);
			return FixedAlloc::Alloc(size, flags); 
		}

		void Free(void *ptr)
		{
			MMGC_LOCK(m_spinlock);
			FixedAlloc::Free(ptr);
		}

		static FixedAllocSafe *GetFixedAllocSafe(void *item)
		{
			return (FixedAllocSafe*) FixedAlloc::GetFixedAlloc(item);
		}

{'fraction_non_alphanumeric': 0.0799106709389261, 'fraction_numerical': 0.006893873191571997, 'mean_word_length': 3.654315408947131, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5349246', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3130, 'n_words': 1222}