void lcpp::LispRuntimeState::shutdown() { EZ_ASSERT(m_stats.m_initializationCount > 0, "LCPP_pRuntime was never initialized."); EZ_ASSERT(m_stats.m_initializationCount > m_stats.m_shutdownCount, "The LCPP_pRuntime was not shut down often enough!"); ++m_stats.m_shutdownCount; m_pGC->removeRoot(m_pReaderState->m_pMacroEnv.get()); m_pGC->removeRoot(m_pGlobalEnvironment); m_pGC->removeRoot(m_pSyntaxEnvironment); m_pReaderState->m_pMacroEnv = nullptr; EZ_DELETE(defaultAllocator(), m_pReaderState); m_pPrinterState->m_pOutStream = nullptr; EZ_DELETE(defaultAllocator(), m_pPrinterState); m_pGlobalEnvironment = nullptr; m_pSyntaxEnvironment = nullptr; // TODO Once we have a per-runtime garbage collector system running, uncomment the following line. //m_pGC->clear(); m_pGC->collect(0); }
ShaderProgram::ShaderProgram() : handle{0} , isLinked{false} , errorLog{} , m_attribLocations{defaultAllocator()} , m_uniformLocations{defaultAllocator()} { }
VectorType NativeArrayToMappedVector(Datum inDatum, bool inNeedMutableClone) { typedef typename VectorType::Scalar Scalar; ArrayType* array = reinterpret_cast<ArrayType*>( madlib_DatumGetArrayTypeP(inDatum)); size_t arraySize = ARR_NDIM(array) == 1 ? ARR_DIMS(array)[0] : ARR_DIMS(array)[0] * ARR_DIMS(array)[1]; if (!(ARR_NDIM(array) == 1 || (ARR_NDIM(array) == 2 && (ARR_DIMS(array)[0] == 1 || ARR_DIMS(array)[1] == 1)))) { std::stringstream errorMsg; errorMsg << "Invalid type conversion to matrix. Expected one-" "dimensional array but got " << ARR_NDIM(array) << " dimensions."; throw std::invalid_argument(errorMsg.str()); } Scalar* origData = reinterpret_cast<Scalar*>(ARR_DATA_PTR(array)); Scalar* data; if (inNeedMutableClone) { data = reinterpret_cast<Scalar*>( defaultAllocator().allocate<dbal::FunctionContext, dbal::DoNotZero, dbal::ThrowBadAlloc>(sizeof(Scalar) * arraySize)); std::copy(origData, origData + arraySize, data); } else { data = reinterpret_cast<Scalar*>(ARR_DATA_PTR(array)); } return VectorType(data, arraySize); }
b32 ShaderProgram::attachShaderFromMemory(ShaderType type, const String& shaderSource) { if (!handle) handle = glCreateProgram(); u32 shader = 0; if (type == ShaderType::Vertex) shader = glCreateShader(GL_VERTEX_SHADER); else if (type == ShaderType::Fragment) shader = glCreateShader(GL_FRAGMENT_SHADER); else panic("Unknown shader type."); const char* cStrSource = cString(shaderSource); glShaderSource(shader, 1, &cStrSource, nullptr); glCompileShader(shader); s32 status; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (status == false) { String msg; if (type == ShaderType::Vertex) msg = "Compile failure in vertex shader: \n"; else if (type == ShaderType::Fragment) msg = "Compile failure in fragment shader: \n"; else panic("Unknown shader type."); s32 infoLogLength; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); Allocator& a = defaultAllocator(); char* strInfoLog = (char*)a.allocate((infoLogLength + 1) * sizeof(char)); defer(a.deallocate(strInfoLog)); glGetShaderInfoLog(shader, infoLogLength, nullptr, strInfoLog); append(msg, strInfoLog); append(msg, '\n'); append(errorLog, msg); glDeleteShader(shader); return false; } glAttachShader(handle, shader); return true; }
void lcpp::LispRuntimeState::initialize(ezAllocatorBase* pAllocator) { EZ_ASSERT(m_stats.m_shutdownCount <= m_stats.m_initializationCount, "LCPP_pRuntime was shut down more often than it was initialized!"); if (m_stats.m_shutdownCount < m_stats.m_initializationCount) { shutdown(); } EZ_ASSERT(m_stats.m_initializationCount == m_stats.m_shutdownCount, "LCPP_pRuntime initialization and shutdown count must be balanced!"); ++m_stats.m_initializationCount; m_pAllocator = pAllocator ? pAllocator : defaultAllocator(); m_pGC = lcpp::getGarbageCollector(); #if EZ_ENABLED(EZ_COMPILE_FOR_DEBUG) g_pGC = m_pGC.get(); #endif m_pSyntaxEnvironment = env::createTopLevel(symbol::create("syntax")).get(); m_pGC->addRoot(m_pSyntaxEnvironment); m_pGlobalEnvironment = env::create(getSyntaxEnvironment(), symbol::create("global")).get(); m_pGC->addRoot(m_pGlobalEnvironment); ////////////////////////////////////////////////////////////////////////// m_pReaderState = EZ_NEW(defaultAllocator(), reader::State); m_pReaderState->m_pMacroEnv = env::createTopLevel(symbol::create("reader-macros")); m_pGC->addRoot(m_pReaderState->m_pMacroEnv.get()); m_pPrinterState = EZ_NEW(defaultAllocator(), printer::State); ////////////////////////////////////////////////////////////////////////// registerBuiltIns(); }
ArrayType* VectorToNativeArray(const Eigen::MatrixBase<Derived>& inVector) { typedef typename Derived::Scalar T; typedef typename Derived::Index Index; MutableArrayHandle<T> arrayHandle = defaultAllocator().allocateArray<T>(inVector.size()); T* ptr = arrayHandle.ptr(); for (Index el = 0; el < inVector.size(); ++el) *(ptr++) = inVector(el); return arrayHandle.array(); }
ArrayType* MatrixToNativeArray(const Eigen::MatrixBase<Derived>& inMatrix) { typedef typename Derived::Scalar T; typedef typename Derived::Index Index; MutableArrayHandle<T> arrayHandle = defaultAllocator().allocateArray<T>( inMatrix.cols(), inMatrix.rows()); T* ptr = arrayHandle.ptr(); for (Index row = 0; row < inMatrix.rows(); ++row) for (Index col = 0; col < inMatrix.cols(); ++col) *(ptr++) = inMatrix(row, col); return arrayHandle.array(); }
b32 ShaderProgram::link() { if (!handle) handle = glCreateProgram(); if (!isLinked) { glLinkProgram(handle); s32 status; glGetProgramiv(handle, GL_LINK_STATUS, &status); if (!status) { String msg{"ShaderProgram linking failure: \n"}; s32 infoLogLength; glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength); Allocator& a = defaultAllocator(); char* strInfoLog = (char*)a.allocate((infoLogLength + 1) * sizeof(char)); defer(a.deallocate(strInfoLog)); glGetProgramInfoLog(handle, infoLogLength, nullptr, strInfoLog); append(msg, strInfoLog); append(msg, "\n"); append(errorLog, msg); glDeleteProgram(handle); handle = 0; isLinked = false; return isLinked; } isLinked = true; } return isLinked; }
void ByteStreamHandleBuf<Storage, CharType, Mutable>::resize( size_t inSize, size_t inPivot) { if (inSize == this->size()) return; char_type* oldPtr = this->ptr(); size_t secondChunkStart = inPivot > this->size() ? this->size() : inPivot; size_t oldSize = this->size(); ptrdiff_t delta = inSize - oldSize; // FIXME: Deallocate old memory! this->mStorage = defaultAllocator().allocateByteString< dbal::FunctionContext, dbal::DoZero, dbal::ThrowBadAlloc>(inSize); std::copy(oldPtr, oldPtr + secondChunkStart, this->ptr()); std::copy(oldPtr + secondChunkStart, oldPtr + oldSize, this->ptr() + secondChunkStart + delta); std::fill(this->ptr() + secondChunkStart, this->ptr() + secondChunkStart + delta, 0); }
const Array<VideoMode>& VideoMode::getFullscreenModes() { LOCAL_PERSIST Array<VideoMode> modes{defaultAllocator()}; if (len(modes) == 0) { s32 displayModeCount; SDL_DisplayMode dm; displayModeCount = SDL_GetNumDisplayModes(0); if (displayModeCount < 1) { std::cerr << "SDL_GetNumDisplayModes failed: " << SDL_GetError() << std::endl; return modes; } for (s32 i = 0; i < displayModeCount; i++) { if (SDL_GetDisplayMode(0, i, &dm) != 0) { std::cerr << "SDL_GetNumDisplayModes failed: " << SDL_GetError() << std::endl; continue; } append( modes, VideoMode{(u32)dm.w, (u32)dm.h, SDL_BITSPERPIXEL(dm.format)}); } std::sort(begin(modes), end(modes), std::greater<VideoMode>()); } return modes; }
bool createGBuffer(GBuffer& b, u32 w, u32 h) { if (w == b.width && h == b.height) // GBuffer already exists return true; b.width = w; b.height = h; glGenFramebuffers(1, &b.fbo); glBindFramebuffer(GL_FRAMEBUFFER, b.fbo); defer(glBindFramebuffer(GL_FRAMEBUFFER, 0)); glEnable(GL_TEXTURE_2D); defer(glBindTexture(GL_TEXTURE_2D, 0)); Array<GLenum> drawBuffers{defaultAllocator()}; auto addRT = [&drawBuffers, w, h](Texture& tex, GLenum attachment, s32 internalFormat, GLenum format, GLenum type) { glGenTextures(1, &tex.handle); glBindTexture(GL_TEXTURE_2D, tex.handle); glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, (s32)w, (s32)h, 0, format, type, nullptr); tex.width = w; tex.height = h; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glFramebufferTexture2D( GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex.handle, 0); if (attachment != GL_DEPTH_ATTACHMENT) append(drawBuffers, attachment); }; addRT(b.textures[GBuffer::Diffuse], GL_COLOR_ATTACHMENT0, GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE); addRT(b.textures[GBuffer::Specular], GL_COLOR_ATTACHMENT1, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE); addRT(b.textures[GBuffer::Normal], GL_COLOR_ATTACHMENT2, GL_RGB10_A2, GL_RGBA, GL_FLOAT); addRT(b.textures[GBuffer::Depth], GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_FLOAT); glDrawBuffers((u32)len(drawBuffers), &drawBuffers[0]); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) return false; return true; }
/* * a few things to note about this function; the debug messages are not operating in the limited memory space * which would be nice, but we would have to override the allocation tendencies of not only std::string * but also std::ostringstream and its like, which would be rather a tedious and error prone process. * * any who. In this function we allocate a 1GB chunk of memory which we have exclusive control over and * we use the Free List Allocation method to divvy it up. we then allocate a multimap of pair<int,double>'s * multimap is good for testing for two reasons, it has horrible times in deallocation with the FreeLitAllocator * and it deallocates ALL of its memory when multimap<>::clear() is called. which the unordered_map variants * do not since they keep their hash tables until their destructors are called. */ void memoryStuff( evq::EventQueue * eventQueue ) { // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Creating DefaultAllocator.") ); // wrapper for ::operator new() and ::operator delete() alloc::DefaultAllocator defaultAllocator( 0, nullptr ); // pointer to our memory pool void * _mem_pool; // size of our memory pool: 1GB std::size_t _mem_size = 1024*1024*1024; // size of our memory pool: 0.5MB //std::size_t _mem_size = 1024*1024*0.5f; // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Allocating Memory Pool of size " + to_string(_mem_size) + "bytes.") ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Allocating FreeListAllocator, for Memory Pool.") ); // allocate a free list allocator and allocate the memory pool and pass it to the free list allocator. alloc::SafeFreeListAllocator * fla = new( (alloc::SafeFreeListAllocator*)defaultAllocator ) alloc::SafeFreeListAllocator( _mem_size, _mem_pool = defaultAllocator.allocateBlock(_mem_size,0) ); { alloc::ptr::weak_ptr<int> test_weak_ptr_outer; alloc::ptr::shared_ptr<int> test_shared_ptr_outer; { //alloc::ptr::shared_ptr<int> test_shared_ptr( new( (int*)*fla ) int(1), fla ); alloc::ptr::shared_ptr<int> test_shared_ptr( alloc::ptr::allocate_shared<int>(fla, 1) ); std::cout << *test_shared_ptr << std::endl; test_weak_ptr_outer = test_shared_ptr; test_shared_ptr_outer = test_shared_ptr; } if( auto t = test_weak_ptr_outer.lock() ) { std::cout << *t << std::endl; } alloc::ptr::unique_ptr<int> test_unique_ptr_outer; { alloc::ptr::unique_ptr<int> test_unique_ptr( new( (int*)*fla ) int(1000), fla ); test_unique_ptr_outer.swap( test_unique_ptr ); { alloc::ptr::unique_ptr<int[]> test_array_unique_ptr( new( fla->allocateArray<int>( 100 ) ) int[100], fla ); for( std::size_t i = 0; i < 100; ++i ) { test_array_unique_ptr[i] = i; } } } std::cout << *test_unique_ptr_outer << std::endl; } { //alloc::LockAllocator< alloc::LockAllocator< alloc::DefaultAllocator > > llalt(); // FIXME(dean): this line should not compile //llalt.printDebugInfo(); } //std::ostringstream stream; // we don't have control over this memory std::basic_ostringstream<char> stream( std::basic_string<char>( alloc::stl_adapter<char>( fla ) ), std::ios_base::out ); { // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Allocating IntMap from FreeListAllocator.") ); // allocate a std::multimap<int,double> with our custom allocator adapters alloc::stl::map< int, double > * intmap = new( (alloc::stl::map< int, double >*)*fla ) alloc::stl::map< int, double >( fla ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("") ); { // make 10000 pairs! for( std::size_t i = 0; i < 100000; ++i ) { // insert a pair uint32_t key = Rand::Int( 0u, ~0u ); double value = sqrt( double(key) ); // here multimap will allocate space for a node that contains a pair<int,double> in it. //intmap->insert( std::pair<int,double>(key,value) ); (*intmap)[key] = value; // print progress if( (i) % 1000 == 0 ) { // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Inserted " + to_string(i) + " pairs into IntMap on FreeListAllocator. Last pair: " + to_string(key) + "," + to_string(value) + ".") ); } } } // clear the stringstream buffer stream.str( std::basic_string< char >( alloc::stl_adapter<char>(fla) ) ); stream.clear(); // put allocator debug info into stringstream buffer fla->printDebugInfo( stream ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( std::string( stream.str().c_str() ) ) ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Deallocating all memory associated with IntMap in FreeListAllocator.") ); // deallocate and destruct the multimap<int,double> fla->deallocate( intmap ); } // clear the stringstream buffer stream.str( std::basic_string< char >( alloc::stl_adapter<char>(fla) ) ); stream.clear(); // put allocator debug info into stringstream buffer fla->printDebugInfo( stream ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( std::string( stream.str().c_str() ) ) ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( "cleaning up ... fla" ) ); // destruct the FreeListAllocator defaultAllocator.deallocate( fla ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( "cleaning up ... _mem_pool" ) ); // deallocate the memory block defaultAllocator.deallocateBlock( _mem_pool ); // clear the stringstream buffer //stream.str( std::basic_string< char >( alloc::stl_adapter<char>(fla) ) ); //stream.clear(); // put allocator debug info into stringstream buffer defaultAllocator.printDebugInfo( stream ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( std::string( stream.str().c_str() ) ) ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( "complete." ) ); }
void Room::generate(bool northDoor, bool eastDoor, bool southDoor, bool westDoor) { if (m_generated) return; if (!m_mesh) m_mesh = defaultAllocator().makeNew<Mesh>(); std::vector<std::vector<TileId>> mapGrid(size.x, std::vector<TileId>(size.y)); TileId emptyTile = {-1, -1}; TileId lightWoodTile = {0, 11}; TileId darkWoodTile = {0, 0}; RandomTileSet stoneTiles; for (int i = 1; i < 3; i++) stoneTiles.emplace_back(i, 15); RandomTileSet darkRockTiles; for (int i = 0; i < 4; i++) darkRockTiles.emplace_back(i, 0); RandomTileSet colorfulTiles; for (int i = 3; i < 12; i++) colorfulTiles.emplace_back(i, 15); for (int i = 0; i < size.x; i++) { for (int j = 0; j < size.y; j++) mapGrid[i][j] = emptyTile; } TileId tile = colorfulTiles[m_random.getInt(0, (u32)len(colorfulTiles) - 1)]; const Room::Size s = size; for (int i = 0; i < s.x; i++) { for (int j = 0; j < s.y; j++) mapGrid[i][j] = tile; } const s32 h = Height; for (int i = 0; i < size.x; i++) { for (int j = 0; j < size.y; j++) { if (mapGrid[i][j] != emptyTile) { addTileSurface( Vector3{i, 0, j}, TileSurfaceFace::Up, mapGrid[i][j]); addTileSurface( Vector3{i, 3, j}, TileSurfaceFace::Down, stoneTiles); } #if 1 // Build Walls else { addTileSurface( Vector3{i, h, j}, TileSurfaceFace::Up, stoneTiles); } for (int k = 0; k < h; k++) { if (mapGrid[i][j] == emptyTile) { if (i > 0) { if (mapGrid[i - 1][j] != emptyTile) addTileSurface(Vector3{i, k, j}, TileSurfaceFace::Left, stoneTiles); } if (i < size.x - 1) { if (mapGrid[i + 1][j] != emptyTile) addTileSurface(Vector3{i + 1, k, j}, TileSurfaceFace::Right, stoneTiles); } if (j > 0) { if (mapGrid[i][j - 1] != emptyTile) addTileSurface(Vector3{i, k, j}, TileSurfaceFace::Back, stoneTiles); } if (j < size.y - 1) { if (mapGrid[i][j + 1] != emptyTile) addTileSurface(Vector3{i, k, j + 1}, TileSurfaceFace::Front, stoneTiles); } } else { if (i == 0) { if (westDoor) { if (j != size.y / 2) addTileSurface(Vector3{i, k, j}, TileSurfaceFace::Right, stoneTiles); } else { addTileSurface(Vector3{i, k, j}, TileSurfaceFace::Right, stoneTiles); } } else if (i == size.x - 1) { if (eastDoor) { if (j != size.y / 2) addTileSurface(Vector3{i + 1, k, j}, TileSurfaceFace::Left, stoneTiles); } else { addTileSurface(Vector3{i + 1, k, j}, TileSurfaceFace::Left, stoneTiles); } } if (j == 0) { if (northDoor) { if (i != size.x / 2) addTileSurface(Vector3{i, k, j}, TileSurfaceFace::Front, stoneTiles); } else { addTileSurface(Vector3{i, k, j}, TileSurfaceFace::Front, stoneTiles); } } else if (j == size.y - 1) { if (southDoor) { if (i != size.x / 2) addTileSurface(Vector3{i, k, j + 1}, TileSurfaceFace::Back, stoneTiles); } else { addTileSurface(Vector3{i, k, j + 1}, TileSurfaceFace::Back, stoneTiles); } } } } #endif } } m_meshData.generateNormals(); m_mesh->addData(m_meshData); addComponent<MeshRenderer>(m_mesh, material); m_generated = true; }
Room::~Room() { if (m_mesh) defaultAllocator().makeDelete(m_mesh); }
ByteStreamHandleBuf<Storage, CharType, IsMutable>::ByteStreamHandleBuf( size_t inSize) : mStorage(defaultAllocator().allocateByteString< dbal::FunctionContext, dbal::DoZero, dbal::ThrowBadAlloc>(inSize)), mPos(0) { }