本文整理汇总了C++中MemoryPool::getSize方法的典型用法代码示例。如果您正苦于以下问题:C++ MemoryPool::getSize方法的具体用法?C++ MemoryPool::getSize怎么用?C++ MemoryPool::getSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemoryPool
的用法示例。
在下文中一共展示了MemoryPool::getSize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: VMFileWrite
TVMStatus VMFileWrite(int fileDescriptor, void* data, int *length){
TMachineSignalState sigState;
MachineSuspendSignals(&sigState); //suspend signals so we cannot be pre-empted while scheduling a new thread
ThreadStore* tStore = ThreadStore::getInstance();
MemoryPool* sharedMemory = tStore->findMemoryPoolByID((TVMMemoryPoolID)1);
TCB* currentThread = tStore->getCurrentThread();
uint8_t* sharedLocation;
TVMMemorySize allocSize = *length;
//Step 0 - validate data
if((data == NULL) || (length == NULL)){
MachineResumeSignals (&sigState);
return VM_STATUS_ERROR_INVALID_PARAMETER;
}
//Step 1 - initialize shared memory location, and wait if memory is not available from share pool
if(allocSize > 512){ //make sure thread never asks for > 512 bytes
allocSize = 512;
}
if(allocSize > sharedMemory->getSize()){
allocSize = sharedMemory->getSize();
}
sharedLocation = sharedMemory->allocateMemory(allocSize); //allocate a space in the shared memory pool
if(sharedLocation == NULL){
// printf("VMFileWrite(): shared location was null\n");
sharedLocation = tStore->waitCurrentThreadOnMemory(allocSize, 1);
}
//Step 2 - IO loop. If the data to transfer is > 512 bytes, loop. If it isn't, loop only runs once.
int bytesLeft = *length;
int chunkSize = bytesLeft;
void *dataLocation = data;
int bytesTransferred = 0;
if(bytesLeft > allocSize)
chunkSize = allocSize;
for(bytesLeft; bytesLeft > 0; bytesLeft -= chunkSize){
if(bytesLeft < chunkSize)
chunkSize = bytesLeft;
//printf("tid %d outputting\n", tStore->getCurrentThread()->getThreadID());
//copy chunkSize bytes of data from *data into the shared memory location, starting at dataLocation
memcpy((void*)sharedLocation, dataLocation, chunkSize*sizeof(uint8_t));
//step 3 - call MachineFileWrite with the pointer to the shared memory location
MachineFileWrite(fileDescriptor, sharedLocation, chunkSize, &machineFileIOCallback, (void*)currentThread);
tStore->waitCurrentThreadOnIO(); //switch to a new thread while waiting on IO
//update bytesLeft and dataLocation for the next iteration
bytesTransferred = bytesTransferred + currentThread->getFileIOResult();
dataLocation = ((uint8_t*)dataLocation + chunkSize);
}
//step 4 - Deallocate the shared memory location, do last error check, and return
sharedMemory->deallocate(sharedLocation);
if(bytesTransferred < 0){
MachineResumeSignals (&sigState);
return VM_STATUS_FAILURE;
}
MachineResumeSignals (&sigState);
return VM_STATUS_SUCCESS;
}
示例2: VMFileRead
TVMStatus VMFileRead(int filedescriptor, void *data, int *length){
TMachineSignalState sigState;
MachineSuspendSignals(&sigState); //suspend signals so we cannot be pre-empted while scheduling a new thread
ThreadStore* tStore = ThreadStore::getInstance();
MemoryPool* sharedMemory = tStore->findMemoryPoolByID((TVMMemoryPoolID)1);
TCB* currentThread = tStore->getCurrentThread();
uint8_t* sharedLocation;
TVMMemorySize allocSize = *length;
//Step 0 - validation
if ((data == NULL) || (length == NULL)){
MachineResumeSignals (&sigState);
return VM_STATUS_ERROR_INVALID_PARAMETER;
}
//Step 1 - initialize shared memory location, and wait if memory is not available from share pool
if(allocSize > 512){ //make sure thread never asks for > 512 bytes
allocSize = 512;
}
if(allocSize > sharedMemory->getSize()){
allocSize = sharedMemory->getSize();
}
sharedLocation = sharedMemory->allocateMemory(allocSize); //allocate a space in the shared memory pool
if(sharedLocation == NULL){
//if there isn't enough room in shared memory to do the IO, the current thread must wait until a chunk
//of size allocSize becomes avaliable. waitCurrentThread puts the thread to sleep until that happens,
//then returns the amount of shared memory requested.
sharedLocation = tStore->waitCurrentThreadOnMemory(allocSize, 1);
}
//Step 2 - Set up loop
int bytesLeft = *length;
int chunkSize = bytesLeft;
void *dataLocation = data;
int bytesTransferred = 0;
if(bytesLeft > allocSize)
chunkSize = allocSize;
for(bytesLeft; bytesLeft > 0; bytesLeft -= chunkSize){
if(bytesLeft < chunkSize)
chunkSize = bytesLeft;
//read the amount of data specified by chunkSize into the location specified by sharedLocation
MachineFileRead(filedescriptor, (void*)sharedLocation, chunkSize, &machineFileIOCallback, (void*)currentThread);
tStore->waitCurrentThreadOnIO(); //resume execution here after IO completes
//copy chunkSize bytes of data from sharedLocation into dataLocation, starting at sharedLocation
memcpy(dataLocation, (void*)sharedLocation, chunkSize*sizeof(uint8_t));
//update bytesLeft and dataLocation for the next iteration
bytesTransferred = bytesTransferred + currentThread->getFileIOResult();
dataLocation = ((uint8_t*)dataLocation + chunkSize);
}
if(currentThread->getFileIOResult() < 0){ //if call to open() failed
MachineResumeSignals (&sigState);
return VM_STATUS_FAILURE; //return failure state
}
*length = bytesTransferred;
MachineResumeSignals(&sigState);
return VM_STATUS_SUCCESS;
}