1
0
Fork 0

DBG: re-fixed some of the code (somehow wasn't committed)

This commit is contained in:
Mr. eXoDia 2015-04-06 21:59:11 +02:00
parent 7be4cf8cac
commit 30a6f4cd2c
3 changed files with 95 additions and 53 deletions

View File

@ -17,15 +17,20 @@ bool ModLoad(uint Base, uint Size, const char* FullPath)
MODINFO info;
// Copy the module path in the struct
strcpy_s(info.path, FullPath);
// Break the module path into a directory and file name
char dir[deflen];
char* file;
if(GetFullPathNameA(FullPath, ARRAYSIZE(dir), dir, &file) == 0)
return false;
// Make everything lowercase
char dir[MAX_PATH] = "";
char file[MAX_MODULE_SIZE] = "";
strcpy_s(dir, FullPath);
_strlwr(dir);
char* fileStart = strrchr(dir, '\\');
if(fileStart)
{
strcpy_s(file, fileStart + 1);
*fileStart = '\0';
}
// Copy the extension into the module struct
{

View File

@ -1,55 +1,89 @@
/**
@file msgqueue.cpp
@brief Implements the msgqueue class.
*/
#include "msgqueue.h"
#include <stdio.h>
// Allocate a message stack
//allocate a message (internal)
static MESSAGE* msgalloc()
{
return (MESSAGE*)emalloc(sizeof(MESSAGE), "msgalloc:msg");
}
//free a message (internal)
static void msgfree(MESSAGE* msg)
{
efree(msg, "msgfree:msg");
}
//allocate a message stack
MESSAGE_STACK* MsgAllocStack()
{
// Allocate memory for the structure
PVOID memoryBuffer = emalloc(sizeof(MESSAGE_STACK), "MsgAllocStack:memoryBuffer");
if(!memoryBuffer)
return nullptr;
// Use placement new to ensure all constructors are called correctly
return new(memoryBuffer) MESSAGE_STACK;
MESSAGE_STACK* msgstack = (MESSAGE_STACK*)emalloc(sizeof(MESSAGE_STACK), "msgallocstack:msgstack");
if(!msgstack)
return 0;
memset(msgstack, 0, sizeof(MESSAGE_STACK));
InitializeCriticalSection(&msgstack->cr);
return msgstack;
}
// Free a message stack and all messages in the queue
void MsgFreeStack(MESSAGE_STACK* Stack)
//free a message stack
void MsgFreeStack(MESSAGE_STACK* msgstack)
{
// Destructor must be called manually due to placement new
Stack->FIFOStack.~unbounded_buffer();
// Free memory
efree(Stack, "MsgFreeStack:Stack");
DeleteCriticalSection(&msgstack->cr);
int stackpos = msgstack->stackpos;
for(int i = 0; i < stackpos; i++) //free all messages left in stack
msgfree(msgstack->msg[i]);
efree(msgstack, "msgfreestack:msgstack");
}
// Add a message to the stack
bool MsgSend(MESSAGE_STACK* Stack, int Msg, uint Param1, uint Param2)
//add a message to the stack
bool MsgSend(MESSAGE_STACK* msgstack, int msg, uint param1, uint param2)
{
MESSAGE messageInfo;
messageInfo.msg = Msg;
messageInfo.param1 = Param1;
messageInfo.param2 = Param2;
// Asynchronous send. Return value doesn't matter.
concurrency::asend(Stack->FIFOStack, messageInfo);
CRITICAL_SECTION* cr = &msgstack->cr;
EnterCriticalSection(cr);
int stackpos = msgstack->stackpos;
if(stackpos >= MAX_MESSAGES)
{
LeaveCriticalSection(cr);
return false;
}
MESSAGE* newmsg = msgalloc();
if(!newmsg)
{
LeaveCriticalSection(cr);
return false;
}
newmsg->msg = msg;
newmsg->param1 = param1;
newmsg->param2 = param2;
msgstack->msg[stackpos] = newmsg;
msgstack->stackpos++; //increase stack pointer
LeaveCriticalSection(cr);
return true;
}
// Get a message from the stack (will return false when there are no messages)
bool MsgGet(MESSAGE_STACK* Stack, MESSAGE* Message)
//get a message from the stack (will return false when there are no messages)
bool MsgGet(MESSAGE_STACK* msgstack, MESSAGE* msg)
{
return concurrency::try_receive(Stack->FIFOStack, *Message);
CRITICAL_SECTION* cr = &msgstack->cr;
EnterCriticalSection(cr);
int stackpos = msgstack->stackpos;
if(!msgstack->stackpos) //no messages to process
{
LeaveCriticalSection(cr);
return false;
}
msgstack->stackpos--; //current message is at stackpos-1
stackpos--;
MESSAGE* stackmsg = msgstack->msg[stackpos];
memcpy(msg, stackmsg, sizeof(MESSAGE));
msgfree(stackmsg);
msgstack->msg[stackpos] = 0;
LeaveCriticalSection(cr);
return true;
}
// Wait for a message on the specified stack
void MsgWait(MESSAGE_STACK* Stack, MESSAGE* Message)
//wait for a message on the specified stack
void MsgWait(MESSAGE_STACK* msgstack, MESSAGE* msg)
{
*Message = concurrency::receive(Stack->FIFOStack);
while(!MsgGet(msgstack, msg))
Sleep(1);
}

View File

@ -3,9 +3,10 @@
#include "_global.h"
#include <windows.h>
#include <agents.h>
// Message structure
#define MAX_MESSAGES 256
//message structure
struct MESSAGE
{
int msg;
@ -13,17 +14,19 @@ struct MESSAGE
uint param2;
};
// Message stack structure.
// Supports an unlimited number of messages.
//message stack structure
struct MESSAGE_STACK
{
concurrency::unbounded_buffer<MESSAGE> FIFOStack;
CRITICAL_SECTION cr;
int stackpos;
MESSAGE* msg[MAX_MESSAGES];
};
//function definitions
MESSAGE_STACK* MsgAllocStack();
void MsgFreeStack(MESSAGE_STACK* Stack);
bool MsgSend(MESSAGE_STACK* Stack, int Msg, uint Param1, uint Param2);
bool MsgGet(MESSAGE_STACK* Stack, MESSAGE* Message);
void MsgWait(MESSAGE_STACK* Stack, MESSAGE* Message);
void MsgFreeStack(MESSAGE_STACK* msgstack);
bool MsgSend(MESSAGE_STACK* msgstack, int msg, uint param1, uint param2);
bool MsgGet(MESSAGE_STACK* msgstack, MESSAGE* msg);
void MsgWait(MESSAGE_STACK* msgstack, MESSAGE* msg);
#endif // _MSGQUEUE_H