Esempio n. 1
0
StandardMemoryPool :: StandardMemoryPool(uint64 sizeInBytes, uint32 boundsCheck)
{
  if(!sizeInBytes)
  {
    mem_error_log("Can not create memory pool with size 0");
  }

  m_poolSize = sizeInBytes;

  m_boundsCheck = boundsCheck;

  m_poolMemory = new uint8[sizeInBytes];

  //Log
  mem_debug_log("StandardPool Constructor initialization in address %p with size %lu\n", m_poolMemory, sizeInBytes);
  mem_debug_log("StandardPool created with m_trashOnCreation:%d m_trashOnAlloc: %d  m_trashOnFree :%d m_boundsCheck %d", m_trashOnCreation, m_trashOnAlloc, m_trashOnFree, m_boundsCheck);

  m_freePoolSize = sizeInBytes - sizeof(Chunk);
  m_totalPoolSize = sizeInBytes;

  // Trash it if required
  if(m_trashOnCreation)
  {
    memset(m_poolMemory, s_trashOnCreation, sizeInBytes);
  }

  if(m_boundsCheck)
  {
    m_freePoolSize -= s_boundsCheckSize * 2;
    Chunk freeChunk(sizeInBytes - sizeof(Chunk) - 2 * s_boundsCheckSize);
    freeChunk.name_set(MEMORY_POOL_BLOCK_NAME);
    freeChunk.write(m_poolMemory + s_boundsCheckSize);
    memcpy(m_poolMemory, s_startBound, s_boundsCheckSize);
    memcpy(m_poolMemory + sizeInBytes - s_boundsCheckSize, s_endBound, s_boundsCheckSize);
    freeChunk.m_next = NULL;
    freeChunk.m_prev = NULL;
  }
  else
  {
    Chunk freeChunk(sizeInBytes - sizeof(Chunk));
    freeChunk.name_set(MEMORY_POOL_BLOCK_NAME);
    freeChunk.write(m_poolMemory);
    freeChunk.m_next = NULL;
    freeChunk.m_prev = NULL;
  }

#ifdef MEM_DEBUG_ON
    dumpToStdOut(DUMP_ELEMENT_PER_LINE, DUMP_HEX);
#endif
}
Esempio n. 2
0
void ObjectMemory::deallocate(OTE* ote)
{
	#ifdef _DEBUG
		ASSERT(!ote->isFree());
		if (Interpreter::executionTrace)
		{
			tracelock lock(TRACESTREAM);
			TRACESTREAM << ote << " (" << hex << (UINT)ote << "), refs " << dec << (int)ote->m_count << ", is being deallocated" << endl;
		}
	#endif

	ASSERT(!isPermanent(ote));
	// We can have up to 256 different destructors (8 bits)
	switch (ote->heapSpace())
	{
		case OTEFlags::NormalSpace:
			freeChunk(ote->m_location);
 			releasePointer(ote);
			break;

		case OTEFlags::VirtualSpace:
			::VirtualFree(static_cast<VirtualObject*>(ote->m_location)->getHeader(), 0, MEM_RELEASE);
 			releasePointer(ote);
			break;

		case OTEFlags::BlockSpace:
			Interpreter::m_otePools[Interpreter::BLOCKPOOL].deallocate(ote);
			break;

		case OTEFlags::ContextSpace:
			// Return it to the interpreter's free list of contexts
			Interpreter::m_otePools[Interpreter::CONTEXTPOOL].deallocate(ote);
			break;

		case OTEFlags::DWORDSpace:
			Interpreter::m_otePools[Interpreter::DWORDPOOL].deallocate(ote);
			break;

		case OTEFlags::HeapSpace:
			//_asm int 3;
			HARDASSERT(FALSE);
			break;
		
		case OTEFlags::FloatSpace:
			Interpreter::m_otePools[Interpreter::FLOATPOOL].deallocate(ote);
			break;

		case OTEFlags::PoolSpace:
		{
			MWORD size = ote->sizeOf();
			HARDASSERT(size <= MaxSmallObjectSize);
			freeSmallChunk(ote->m_location, size);
			releasePointer(ote);
		}
		break;

		default:
			ASSERT(false);
	}
}
bool MySoundEffect::loadChunk(const char * path)
{
	freeChunk();
	chunk = Mix_LoadWAV(path);

	return chunk != NULL;
}
Esempio n. 4
0
void CachingReader::process() {
    ReaderStatusUpdate status;
    while (m_readerStatusFIFO.read(&status, 1) == 1) {
        CachingReaderChunkForOwner* pChunk = static_cast<CachingReaderChunkForOwner*>(status.chunk);
        if (pChunk) {
            // Take over control of the chunk from the worker.
            // This has to be done before freeing all chunks
            // after a new track has been loaded (see below)!
            pChunk->takeFromWorker();
            if (status.status != CHUNK_READ_SUCCESS) {
                // Discard chunks that are empty (EOF) or invalid
                freeChunk(pChunk);
            }
        }
        if (status.status == TRACK_NOT_LOADED) {
            m_readerStatus = status.status;
        } else if (status.status == TRACK_LOADED) {
            m_readerStatus = status.status;
            // Reset the max. readable frame index
            m_maxReadableFrameIndex = status.maxReadableFrameIndex;
            // Free all chunks with sample data from a previous track
            freeAllChunks();
        }
        // Adjust the max. readable frame index
        if (m_readerStatus == TRACK_LOADED) {
            m_maxReadableFrameIndex = math_min(status.maxReadableFrameIndex, m_maxReadableFrameIndex);
        } else {
            m_maxReadableFrameIndex = Mixxx::AudioSource::getMinFrameIndex();
        }
    }
}
Esempio n. 5
0
void CMemoryManager::clearMemory(void) {
	SMemoryBlock* block = nullptr;

	for (block = m_headBlock; block; block = m_headBlock) {
		freeChunk(&block->memChunk);
	}

	m_totalMemorySize = 0;
}
Esempio n. 6
0
void generateASM(ANTLR3_BASE_TREE *node) {
  debug(DEBUG_GENERATION, "\033[22;93mGenerate ASM\033[0m");
  initRegisters();

  program = initChunk();

  addInstruction(program, "SP EQU R15");
  addInstruction(program, "FP EQU R14");
  addInstruction(program, "DISPLAY EQU R13");
  // TODO - dynamise load adr
  addInstruction(program, "ORG 0xE000");
  addInstruction(program, "START MAIN");


  chunk *stack = stackEnvironement();
  chunk *instructionASM = computeInstruction(node);
  chunk *unstack = unstackEnvironement();


  addInstruction(program, "\n\n\n// PRGM");
  addEtiquette(program, "MAIN");
  addInstruction(program, "STACKBASE 0x1000");
  addInstruction(program, "LDW FP, SP");
  addInstruction(program, "LDW DISPLAY, #0x2000");
  addInstruction(program, "// STACK FACK RETURN");
  addInstruction(program, "ADQ -2, SP");
  appendChunks(program, stack);
  appendChunks(program, instructionASM);
  appendChunks(program, unstack);
  addInstruction(program, "// UNSTACK FACK RETURN");
  addInstruction(program, "ADQ 2, SP");


  FILE *file = fopen("a.asm", "w");
  fprintf(file, program->string);
  fclose(file);

  // printChunk(program);

  freeChunk(instructionASM);
  freeChunk(stack);
  freeChunk(unstack);
  freeChunk(program);
}
Esempio n. 7
0
void freeWorld(World *world) {
    int i;

    for (i = 0; i < world->num_chunks; i++) {
        freeChunk(world->chunks[i]);
    }

    free(world->chunks);

    free(world);
}
Esempio n. 8
0
 void Allocator::reset()
 {
     Chunk *c = current_chunk;
     while (c) {
         Chunk *prev = c->prev;
         freeChunk(c);
         c = prev;
     }
     current_chunk = NULL;
     current_top = NULL;
     current_limit = NULL;
     postReset();
 }
Esempio n. 9
0
CachingReaderChunkForOwner* CachingReader::allocateChunkExpireLRU(SINT chunkIndex) {
    CachingReaderChunkForOwner* pChunk = allocateChunk(chunkIndex);
    if (pChunk == nullptr) {
        if (m_lruCachingReaderChunk == nullptr) {
            qDebug() << "ERROR: No LRU chunk to free in allocateChunkExpireLRU.";
            return nullptr;
        }
        freeChunk(m_lruCachingReaderChunk);
        pChunk = allocateChunk(chunkIndex);
    }
    //qDebug() << "allocateChunkExpireLRU" << chunk << pChunk;
    return pChunk;
}
Esempio n. 10
0
Chunk* CachingReader::allocateChunkExpireLRU() {
    Chunk* chunk = allocateChunk();
    if (chunk == NULL) {
        if (m_lruChunk == NULL) {
            qDebug() << "ERROR: No LRU chunk to free in allocateChunkExpireLRU.";
            return NULL;
        }
        //qDebug() << "Expiring LRU" << m_lruChunk << m_lruChunk->chunk_number;
        freeChunk(m_lruChunk);
        chunk = allocateChunk();
    }
    //qDebug() << "allocateChunkExpireLRU" << chunk;
    return chunk;
}
Esempio n. 11
0
static BOOL GT2_Load(BOOL curious)
{
	GT_CHUNK *tmp;

	_mm_fseek(modreader, 0, SEEK_SET);
	while ((tmp = loadChunk()) != NULL) {
#ifdef MIKMOD_DEBUG
		/* FIXME: to be completed */
		fprintf(stderr, "%c%c%c%c\n", tmp->id[0], tmp->id[1], tmp->id[2], tmp->id[3]);
#endif
		freeChunk(tmp);
	}
	_mm_errno = MMERR_LOADING_HEADER;
	return 0;
}
Esempio n. 12
0
void CachingReader::process() {
    ReaderStatusUpdate status;
    while (m_readerStatusFIFO.read(&status, 1) == 1) {
        // qDebug() << "Got ReaderStatusUpdate:" << status.status
        //          << (status.chunk ? status.chunk->chunk_number : -1);
        if (status.status == TRACK_NOT_LOADED) {
            m_readerStatus = status.status;
        } else if (status.status == TRACK_LOADED) {
            freeAllChunks();
            m_readerStatus = status.status;
            m_iTrackNumSamplesCallbackSafe = status.trackNumSamples;
        } else if (status.status == CHUNK_READ_SUCCESS) {
            Chunk* pChunk = status.chunk;
            if (pChunk == NULL) {
                qDebug() << "ERROR: status.chunk is NULL in CHUNK_READ_SUCCESS ReaderStatusUpdate. Ignoring update.";
                continue;
            }
            Chunk* pChunk2 = m_chunksBeingRead.take(pChunk->chunk_number);
            if (pChunk2 != pChunk) {
                qDebug() << "Mismatch in requested chunk to read!";
            }

            Chunk* pAlreadyExisting = lookupChunk(pChunk->chunk_number);
            // If this chunk is already in the cache, then we just freshened
            // it. Free this chunk.
            if (pAlreadyExisting != NULL) {
                qDebug() << "CHUNK" << pChunk->chunk_number << "ALREADY EXISTS!";
                freeChunk(pChunk);
            } else {
                //qDebug() << "Inserting chunk" << pChunk << pChunk->chunk_number;
                m_allocatedChunks.insert(pChunk->chunk_number, pChunk);

                // Insert the chunk into the LRU list
                m_mruChunk = insertIntoLRUList(pChunk, m_mruChunk);

                // If this chunk has no next LRU then it is the LRU. This only
                // happens if this is the first allocated chunk.
                if (pChunk->next_lru == NULL) {
                    m_lruChunk = pChunk;
                }
            }
        } else if (status.status == CHUNK_READ_EOF) {
            Chunk* pChunk = status.chunk;
            if (pChunk == NULL) {
                qDebug() << "ERROR: status.chunk is NULL in CHUNK_READ_EOF ReaderStatusUpdate. Ignoring update.";
                continue;
            }
            Chunk* pChunk2 = m_chunksBeingRead.take(pChunk->chunk_number);
            if (pChunk2 != pChunk) {
                qDebug() << "Mismatch in requested chunk to read!";
            }
            freeChunk(pChunk);
        } else if (status.status == CHUNK_READ_INVALID) {
            qDebug() << "WARNING: READER THREAD RECEIVED INVALID CHUNK READ";
            Chunk* pChunk = status.chunk;
            if (pChunk == NULL) {
                qDebug() << "ERROR: status.chunk is NULL in CHUNK_READ_INVALID ReaderStatusUpdate. Ignoring update.";
                continue;
            }
            Chunk* pChunk2 = m_chunksBeingRead.take(pChunk->chunk_number);
            if (pChunk2 != pChunk) {
                qDebug() << "Mismatch in requested chunk to read!";
            }
            freeChunk(pChunk);
        }
    }
}
Esempio n. 13
0
void ChunkCache::setChunk(PositionI chunkBasePosition, const std::vector<std::uint8_t> &buffer)
{
    getDebugLog() << "stored chunk at " << chunkBasePosition << " with buffer of length " << buffer.size() << postnl;
    std::unique_lock<std::mutex> lockIt(theLock);
    static_assert(NullChunk == std::size_t(), "invalid value for NullChunk");
    std::size_t &startingChunkIndex = startingChunksMap[chunkBasePosition];
    std::size_t prevChunkIndex = NullChunk;
    std::size_t bufferPos = 0;
    while(bufferPos < buffer.size())
    {
        std::size_t chunkIndex;
        if(prevChunkIndex == NullChunk)
            chunkIndex = startingChunkIndex;
        else
            chunkIndex = fileChunks[prevChunkIndex].nextChunk;
        if(chunkIndex == NullChunk)
        {
            chunkIndex = allocChunk();
            if(prevChunkIndex == NullChunk)
                startingChunkIndex = chunkIndex;
            else
                fileChunks[prevChunkIndex].nextChunk = chunkIndex;
        }
        prevChunkIndex = chunkIndex;
        std::size_t currentChunkSize = chunkSize;
        if(currentChunkSize > buffer.size() - bufferPos)
            currentChunkSize = buffer.size() - bufferPos;
        fileChunks[chunkIndex].usedSize = currentChunkSize;
        bufferPos += currentChunkSize;
    }
    std::size_t freeChunkIndex;
    if(prevChunkIndex == NullChunk)
    {
        freeChunkIndex = startingChunkIndex;
        startingChunkIndex = NullChunk;
    }
    else
    {
        freeChunkIndex = fileChunks[prevChunkIndex].nextChunk;
        fileChunks[prevChunkIndex].nextChunk = NullChunk;
    }
    while(freeChunkIndex != NullChunk)
    {
        std::size_t nextChunkIndex = fileChunks[freeChunkIndex].nextChunk;
        freeChunk(freeChunkIndex);
        freeChunkIndex = nextChunkIndex;
    }
    bufferPos = 0;
    std::size_t chunkIndex = startingChunkIndex;
    while(bufferPos < buffer.size())
    {
        assert(chunkIndex != NullChunk);
        std::size_t currentChunkSize = chunkSize;
        if(currentChunkSize > buffer.size() - bufferPos)
            currentChunkSize = buffer.size() - bufferPos;
        fileChunks[chunkIndex].usedSize = currentChunkSize;
        try
        {
            static_assert(NullChunk == 0, "invalid value for NullChunk");
            std::int64_t filePosition = static_cast<std::int64_t>(chunkIndex - 1) * chunkSize; // skip null chunk
            writer->seek(filePosition, stream::SeekPosition::Start);
            writer->writeBytes(&buffer[bufferPos], currentChunkSize);
        }
        catch(stream::IOException &e)
        {
            try
            {
                writer->flush();
            }
            catch(stream::IOException &)
            {
            }
            throw e;
        }
        bufferPos += currentChunkSize;
        chunkIndex = fileChunks[chunkIndex].nextChunk;
    }
    assert(chunkIndex == NullChunk);
    writer->flush();
}
Esempio n. 14
0
chunk *computeInstruction(ANTLR3_BASE_TREE *node) {
  debug(DEBUG_GENERATION, "\033[22;93mCompute instruction\033[0m");

  chunk *chunk, *tmp_chunk;
  int i, scope_tmp;

  static int scope = 0;


  switch (node->getType(node)) {

    case INTEGER :
    case STRING :
    case ID :
    case FUNC_CALL :
    case SUP    :
    case INF    :
    case SUP_EQ :
    case INF_EQ :
    case EQ     :
    case DIFF   :
    case AND :
    case OR :
    case MINUS :
    case PLUS :
    case DIV :
    case MULT :
    case NEG :
    case ASSIGNE :
      return computeExpr(node);


    case IF:
      return computeIf(node);

    case WHILE :
      return computeWhile(node);

    case FOR :
      return computeFor(node);


    case VAR_DECLARATION :
      return computeVarDeclaration(node);

    case FUNC_DECLARATION :
    case LET :
      enterScopeN(scope);

      scope_tmp = scope;
      scope = 0;

      switch (node->getType(node)) {
        case LET:
          chunk = computeLet(node);
          break;
        case FUNC_DECLARATION:
          chunk = computeFuncDeclaration(node);
          break;
      }

      scope = scope_tmp;
      scope++;

      leaveScope();

      return chunk;


    default:
      chunk = initChunk();

      // Compute all children
      for (i = 0; i < node->getChildCount(node); i++) {
        tmp_chunk = computeInstruction(node->getChild(node, i));
        appendChunks(chunk, tmp_chunk);
        // appendChunks replace chunk->registre with tmp_chunk's one
        // but freeChunk free the registre of tmp_chunk
        // we don't want chunk->registre to be overwritten !
        if (tmp_chunk != NULL)
          tmp_chunk->registre = -1;
        freeChunk(tmp_chunk);
      }

      return chunk;
  }
}
MySoundEffect::~MySoundEffect()
{
	freeMusic();
	freeChunk();
}
Esempio n. 16
0
/* Reallocate memory. When we need more memory
 * we allocate a piece of memory with an appropriate size,
 * copy an old memory and then free it.
 */
void* reallocateMemory(
    void*                  self,
	MemoryContainer        container, 
	void*                  old_mem, 
	size_t                 new_size)
{
	IMemContainerManager  _    = (IMemContainerManager)self;
	IErrorLogger          elog = _->errorLogger;
    
	MemorySet    set   = (MemorySet)container;
    MemoryChunk	 chunk = (MemoryChunk)((char*)old_mem - MEM_CHUNK_SIZE);  

	size_t		 oldsize = chunk->size;   
	void*        chunkPtr;
	void*        new_mem;

	/* Always return when a requested size is a decrease */
	if (oldsize >= new_size)
	    return old_mem;

	/* Check if for the chunk there was allocate a block */
	if (oldsize > set->chunkMaxSize)
	{
        /* Try to find the corresponding block first 
		 */
		MemoryBlock   block     = set->blockList;
        MemoryBlock   prevblock = NULL;

        size_t		  chunk_size;
		size_t		  block_size;
		char*         expected_block_end;

		while (block != NULL)
		{
            if (chunk == (MemoryChunk)((char*)block + MEM_BLOCK_SIZE))
				break;

			prevblock = block;
			block     = block->next;
		}

		/* Could not find the block. We should report an error. */
		if (block == NULL)
		{
			 elog->log(LOG_ERROR, 
		          ERROR_CODE_BLOCK_NOT_FOUND, 
				  "Could not find block containing chunk %p", 
				  chunk);

			 return NULL;
		}

        /* We should check that the chunk is only one 
		 * on the block.
		 */
		expected_block_end = (char*)block + 
			chunk->size +
            MEM_BLOCK_SIZE + 
			MEM_CHUNK_SIZE;

		ASSERT(elog, block->freeEnd == expected_block_end, NULL); 

		/* Do the realloc */
		chunk_size = AlignDefault(new_size);
		block_size = chunk_size + MEM_BLOCK_SIZE + MEM_CHUNK_SIZE;

		ASSERT(elog, funcRealloc != NULL, NULL); 
		block      = (MemoryBlock)funcRealloc(block, block_size);

		if (block == NULL)
		{
            showMemStat(_, topMemCont, 0);

	        elog->log(LOG_ERROR, 
		          ERROR_CODE_OUT_OF_MEMORY, 
				  "Out of memory. Failed request size: %lu", 
				  block_size);

		    return NULL;
		}

		block->freeStart = block->freeEnd = (char*)block + block_size;
        chunk = (MemoryChunk)((char*)block + MEM_BLOCK_SIZE);

		/* Change block pointer to newly allocated block. */
		if (prevblock == NULL)
			set->blockList = block;
		else
			prevblock->next = block;

		chunk->size = chunk_size;

		chunkPtr = MemoryChunkGetPointer(chunk);
	    return chunkPtr;
	}

	/* If we are here that this small block
	 * was taken from the free list. We allocate
	 * a new free chunk and the old chunk add to
	 * the free list. 
	 */
    new_mem = allocateMemory(_, container, new_size);

	/* copy existing memory to a new memory. */
	memcpy(new_mem, old_mem, oldsize);

	/* free old chunk */
	freeChunk(self, old_mem);
	return new_mem;
}