Error while trying to allocate memory (malloc)

CrudOMatic crudomatic at gmail.com
Mon Aug 6 06:01:07 PDT 2012


The following code:

import std.stdio, std.cstream, std.c.stdlib;

private {
	//import rescache.cache;
	//import mm.mempool;
}

void main(string[] args)
{
	// press <Return> before program begins
	din.getc();
	
	memoryPool memPool = new memoryPool(1024);
	writeln("Block Allocated!");
	
	// press <Return> to end program
	din.getc();
	//return cast(int)0;
}

	// Growable/shrinkable memory pool, for speedy memory allocation
	class memoryPool {
		private:
			// Allocation header, count and keep track of allocs and keep 
first, last info
			struct s_allocHeader {
				int m_allocCount; // count number of allocations from pool
				s_allocInfo* p_first; // pointer to first allocation info 
struct
				s_allocInfo* p_last; // pointer to last alloc info struct 
(should be null if only one)
			}
			// Keep track of allocations from the memory pool
			struct s_allocInfo {
				align(4) // align on 4-byte boundaries
				void* p_data; // pointer to data chunk allocated out
				void* p_inBlock; // pointer to block this allocation was made 
in
				int m_dataSize; // size of allocated chunk
				s_allocInfo* p_next; // pointer to next allocation list entry
			}
			
			// Block header, count and keep track of system allocs and 
keep first, last info
			struct s_blockHeader {
				int m_blockCount; // count number of allocations from system
				s_blockInfo* p_first; // pointer to first block info struct
				s_blockInfo* p_last; // pointer to last block info struct 
(should be null if only one)
			}
			// Keep track of block allocations
			struct s_blockInfo {
				align(4) // align on 4-byte boundaries
				void* p_block; // pointer to the memory allocated from the 
system
				int m_blockSize; // size of block allocated from the system
				bool m_poolMember; // whether or not the block was allocated 
for the memory pool
				s_blockInfo* p_next; // pointer to next block list entry
			}
			
			int m_CHUNK_SIZE = 128; // n-byte allocations from the pool
			int m_BLOCK_SIZE = 4096; // pool block size
		
		public:	
			// Allocator
			//void* alloc(int numChunks = 1, bool useMemPool = true) { }
			// Deallocator
			void free() { }
			// Grow the memory pool
			void growPool(int numBlocks = 1) { }
			// Shrink the memory pool
			void shrinkPool(int numBlocks = 1) { }
			
			// Constructor; pass it the initial memory pool size in MB
			this(int initialPoolSize) {
				// set up allocation list
				s_allocHeader* m_allocList;
				// set up block list
				s_blockHeader* m_blockList;
				
				// create initial block info reference, and set block count 
to 1
				m_blockList.p_first = malloc(s_blockInfo.sizeof);
				//m_blockList.m_blockCount = 1;
				// allocate initial block of memory and store it in block 
info reference
				//s_blockInfo* tmp = m_blockList.p_first;
				//tmp.p_block = malloc(cast(size_t)initialPoolSize);
				//tmp.m_blockSize = initialPoolSize;
				//tmp.m_poolMember = true;
				//tmp.p_next = null;
			}
			// Deconstructor
			~this() { }
	} // END of c_memoryPool class

when compiled, gives the error:

Error: cannot implicitly convert expression (malloc(16u)) of type 
void* to s_blockInfo*

on line (75):

m_blockList.p_first = malloc(s_blockInfo.sizeof);

What is going on here?

I even tried using new, but got an Access Violation

Any help would be appreciated.


More information about the Digitalmars-d-learn mailing list