PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/visualizations/Vortex/angelscript/angelscript/source/as_memory.cpp

http://github.com/xbmc/xbmc
C++ | 163 lines | 84 code | 35 blank | 44 comment | 8 complexity | 77a8a3a978e2c462f3983dd6f07152bc MD5 | raw file
  1/*
  2   AngelCode Scripting Library
  3   Copyright (c) 2003-2009 Andreas Jonsson
  4
  5   This software is provided 'as-is', without any express or implied
  6   warranty. In no event will the authors be held liable for any
  7   damages arising from the use of this software.
  8
  9   Permission is granted to anyone to use this software for any
 10   purpose, including commercial applications, and to alter it and
 11   redistribute it freely, subject to the following restrictions:
 12
 13   1. The origin of this software must not be misrepresented; you
 14      must not claim that you wrote the original software. If you use
 15      this software in a product, an acknowledgment in the product
 16      documentation would be appreciated but is not required.
 17
 18   2. Altered source versions must be plainly marked as such, and
 19      must not be misrepresented as being the original software.
 20
 21   3. This notice may not be removed or altered from any source
 22      distribution.
 23
 24   The original version of this library can be located at:
 25   http://www.angelcode.com/angelscript/
 26
 27   Andreas Jonsson
 28   andreas@angelcode.com
 29*/
 30
 31
 32//
 33// as_memory.cpp
 34//
 35// Overload the default memory management functions so that we
 36// can let the application decide how to do it.
 37//
 38
 39#include <stdlib.h>
 40
 41#include "as_config.h"
 42#include "as_memory.h"
 43#include "as_scriptnode.h"
 44#include "as_bytecode.h"
 45
 46BEGIN_AS_NAMESPACE
 47
 48// By default we'll use the standard memory management functions
 49asALLOCFUNC_t userAlloc = malloc;
 50asFREEFUNC_t  userFree  = free;
 51
 52extern "C"
 53{
 54
 55int asSetGlobalMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc)
 56{
 57	userAlloc = allocFunc;
 58	userFree  = freeFunc;
 59
 60	return 0;
 61}
 62
 63int asResetGlobalMemoryFunctions()
 64{
 65	asThreadCleanup();
 66
 67	userAlloc = malloc;
 68	userFree  = free;
 69
 70	return 0;
 71}
 72
 73} // extern "C"
 74
 75asCMemoryMgr::asCMemoryMgr()
 76{
 77}
 78
 79asCMemoryMgr::~asCMemoryMgr()
 80{
 81	FreeUnusedMemory();
 82}
 83
 84void asCMemoryMgr::FreeUnusedMemory()
 85{
 86	// It's necessary to protect the scriptNodePool from multiple 
 87	// simultaneous accesses, as the parser is used by several methods
 88	// that can be executed simultaneously.
 89	ENTERCRITICALSECTION(cs);
 90
 91	int n;
 92	for( n = 0; n < (signed)scriptNodePool.GetLength(); n++ )
 93		userFree(scriptNodePool[n]);
 94	scriptNodePool.Allocate(0, false);
 95
 96	LEAVECRITICALSECTION(cs);
 97
 98	// The engine already protects against multiple threads 
 99	// compiling scripts simultaneously so this pool doesn't have 
100	// to be protected again.
101	for( n = 0; n < (signed)byteInstructionPool.GetLength(); n++ )
102		userFree(byteInstructionPool[n]);
103	byteInstructionPool.Allocate(0, false);
104}
105
106void *asCMemoryMgr::AllocScriptNode()
107{
108	ENTERCRITICALSECTION(cs);
109
110	if( scriptNodePool.GetLength() )
111	{
112		void *tRet = scriptNodePool.PopLast();
113		LEAVECRITICALSECTION(cs);
114		return tRet;
115	}
116
117	LEAVECRITICALSECTION(cs);
118
119#if defined(AS_DEBUG) 
120	return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(asCScriptNode), __FILE__, __LINE__);
121#else
122	return userAlloc(sizeof(asCScriptNode));
123#endif
124}
125
126void asCMemoryMgr::FreeScriptNode(void *ptr)
127{
128	ENTERCRITICALSECTION(cs);
129
130	// Pre allocate memory for the array to avoid slow growth
131	if( scriptNodePool.GetLength() == 0 )
132		scriptNodePool.Allocate(100, 0);
133
134	scriptNodePool.PushLast(ptr);
135
136	LEAVECRITICALSECTION(cs);
137}
138
139void *asCMemoryMgr::AllocByteInstruction()
140{
141	if( byteInstructionPool.GetLength() )
142		return byteInstructionPool.PopLast();
143
144#if defined(AS_DEBUG) 
145	return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(cByteInstruction), __FILE__, __LINE__);
146#else
147	return userAlloc(sizeof(cByteInstruction));
148#endif
149}
150
151void asCMemoryMgr::FreeByteInstruction(void *ptr)
152{
153	// Pre allocate memory for the array to avoid slow growth
154	if( byteInstructionPool.GetLength() == 0 )
155		byteInstructionPool.Allocate(100, 0);
156
157	byteInstructionPool.PushLast(ptr);
158}
159
160END_AS_NAMESPACE
161
162
163