public virtual int alloc(int size, int alignment) { int allocatedAddr = 0; switch (allocType) { case PSP_SMEM_Low: allocatedAddr = freeMemoryChunks.allocLow(size, alignment - 1); break; case PSP_SMEM_High: allocatedAddr = freeMemoryChunks.allocHigh(size, alignment - 1); break; } if (allocatedAddr == 0) { return(0); } MemoryChunk memoryChunk = new MemoryChunk(allocatedAddr, size); allocatedMemoryChunks[allocatedAddr] = memoryChunk; return(allocatedAddr); }
// Allocates to 256-byte alignment public virtual SysMemInfo malloc(int partitionid, string name, int type, int size, int addr) { if (freeMemoryChunks == null) { return(null); } int allocatedAddress = 0; int allocatedSize = 0; if (isValidPartitionId(partitionid)) { MemoryChunkList freeMemoryChunk = freeMemoryChunks[partitionid]; int alignment = defaultSizeAlignment - 1; // The allocated size has not to be aligned to the requested alignment // (for PSP_SMEM_LowAligned or PSP_SMEM_HighAligned), // it is only aligned to the default size alignment. allocatedSize = Utilities.alignUp(size, alignment); if (type == PSP_SMEM_LowAligned || type == PSP_SMEM_HighAligned) { // Use the alignment provided in the addr parameter alignment = addr - 1; } switch (type) { case PSP_SMEM_Low: case PSP_SMEM_LowAligned: allocatedAddress = freeMemoryChunk.allocLow(allocatedSize, alignment); break; case PSP_SMEM_High: case PSP_SMEM_HighAligned: allocatedAddress = freeMemoryChunk.allocHigh(allocatedSize, alignment); break; case PSP_SMEM_Addr: allocatedAddress = freeMemoryChunk.alloc(addr & Memory.addressMask, allocatedSize); break; default: Console.WriteLine(string.Format("malloc: unknown type {0}", getTypeName(type))); break; } } SysMemInfo sysMemInfo; if (allocatedAddress == 0) { Console.WriteLine(string.Format("malloc cannot allocate partition={0:D}, name='{1}', type={2}, size=0x{3:X}, addr=0x{4:X8}, maxFreeMem=0x{5:X}, totalFreeMem=0x{6:X}", partitionid, name, getTypeName(type), size, addr, maxFreeMemSize(partitionid), totalFreeMemSize(partitionid))); if (log.TraceEnabled) { log.trace("Free list: " + DebugFreeMem); log.trace("Allocated blocks:\n" + DebugAllocatedMem + "\n"); } sysMemInfo = null; } else { sysMemInfo = new SysMemInfo(partitionid, name, type, size, allocatedSize, allocatedAddress); //if (log.DebugEnabled) { Console.WriteLine(string.Format("malloc partition={0:D}, name='{1}', type={2}, size=0x{3:X}, addr=0x{4:X8}: returns 0x{5:X8}", partitionid, name, getTypeName(type), size, addr, allocatedAddress)); if (log.TraceEnabled) { log.trace("Free list after malloc: " + DebugFreeMem); log.trace("Allocated blocks after malloc:\n" + DebugAllocatedMem + "\n"); } } } return(sysMemInfo); }