Skip to content

Commit

Permalink
cleanup/simplify bug fix for PS2Request allocation Snow Leopard start
Browse files Browse the repository at this point in the history
  • Loading branch information
RehabMan committed Mar 1, 2013
1 parent 794134f commit 5f6b235
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
14 changes: 6 additions & 8 deletions VoodooPS2Controller/ApplePS2Device.h
Expand Up @@ -401,27 +401,25 @@ struct PS2Request
friend ApplePS2Controller;

protected:
inline PS2Request() { };
void init(int max);
PS2Request();
static void* operator new(size_t); // "hide" it
static inline void* operator new(size_t, int max)
{ return ::operator new(sizeof(PS2Request) + sizeof(PS2Command)*max); }
static inline void operator delete(void*p)
{ ::operator delete(p); }

public:
UInt8 commandsCount;
private:
UInt8 commandsAllocated;
public:
void * completionTarget;
PS2CompletionAction completionAction;
void * completionParam;
private:
queue_chain_t chain;
public:
PS2Command commands[];
};

template<int max = kMaxCommands> struct TPS2Request : public PS2Request
{
public:
inline TPS2Request() { init(max); }
PS2Command commands[max];
};

Expand Down
22 changes: 11 additions & 11 deletions VoodooPS2Controller/VoodooPS2Controller.cpp
Expand Up @@ -475,8 +475,7 @@ bool ApplePS2Controller::start(IOService * provider)
#endif
if (debugFlag) _debuggingEnabled = true;

_keyboardQueueAlloc = (KeyboardQueueElement *)
IOMallocAligned(kKeyboardQueueSize*sizeof(KeyboardQueueElement), sizeof(void*));
_keyboardQueueAlloc = new KeyboardQueueElement[kKeyboardQueueSize];
if (!_keyboardQueueAlloc) goto fail;

// Add the allocated keyboard queue entries to "unused" queue.
Expand Down Expand Up @@ -687,7 +686,10 @@ void ApplePS2Controller::stop(IOService * provider)
#if DEBUGGER_SUPPORT
// Free the keyboard queue allocation space (after disabling interrupt).
if (_keyboardQueueAlloc)
IOFreeAligned(_keyboardQueueAlloc,kKeyboardQueueSize*sizeof(KeyboardQueueElement));
{
delete[] _keyboardQueueAlloc;
_keyboardQueueAlloc = 0;
}
#endif //DEBUGGER_SUPPORT

super::stop(provider);
Expand Down Expand Up @@ -826,16 +828,14 @@ PS2Request * ApplePS2Controller::allocateRequest(int max)
// Allocate a request structure. Blocks until successful.
// Most of request structure is guaranteed to be zeroed.
//
PS2Request* request = (PS2Request*)IOMalloc(sizeof(PS2Request) + sizeof(PS2Command)*max);
if (request)
request->init(max);
return request;
assert(max > 0);

return new(max) PS2Request;
}

void PS2Request::init(int max)
PS2Request::PS2Request()
{
commandsAllocated = max;

commandsCount = 0;
completionTarget = 0;
completionAction = 0;
Expand All @@ -857,7 +857,7 @@ void ApplePS2Controller::freeRequest(PS2Request * request)
// Deallocate a request structure.
//

IOFree(request, sizeof(PS2Request) + sizeof(PS2Command)*request->commandsAllocated);
delete request;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down
30 changes: 26 additions & 4 deletions new_kext.cpp
Expand Up @@ -11,15 +11,35 @@

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

void* operator_new(size_t size)
//
// Note: Originally these helper functions were exported as C++ functions
// instead of extern "C", and they had much longer names.
//
// But that causes problems for Snow Leopard (crash using Kernel Cache at startup).
// Not sure if it is related to the C++ names, or just the long names, but
// this does work with the shorter extern "C" names, even on Snow Leopard.
//
// Also realize that the calls to IOMallocAligned/IOFreeAligned are mapped to
// IOMalloc/IOFree because there appears to be a bug in the aligned variants.
// See new_kext.h for the macros...
//
// Note: For now we are not using this code, as it is easier to just use the
// built-in operator new/delete. I'm keeping it here, just in case it becomes
// useful in the future.
//

extern "C"
{

void* _opnew(size_t size)
{
size_t* p = (size_t*)IOMallocAligned(sizeof(size_t) + size, sizeof(void*));
if (p)
*p++ = size;
return p;
}

void operator_delete(void* p)
void _opdel(void* p)
{
assert(p);
if (p)
Expand All @@ -31,15 +51,15 @@ void operator_delete(void* p)

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

void* operator_new_array(size_t size)
void* _opnewa(size_t size)
{
size_t* p = (size_t*)IOMallocAligned(sizeof(size_t) + size, sizeof(void*));
if (p)
*p++ = size;
return p;
}

void operator_delete_array(void* p)
void _opdela(void* p)
{
assert(p);
if (p)
Expand All @@ -48,6 +68,8 @@ void operator_delete_array(void* p)
IOFreeAligned(t, *t);
}
}

} // extern "C"

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

22 changes: 12 additions & 10 deletions new_kext.h
Expand Up @@ -10,23 +10,25 @@

#include <IOKit/IOLib.h>

// helper functions for export
void* operator_new(size_t size);
void operator_delete(void* p);
void* operator_new_array(size_t size);
void operator_delete_array(void *p);
extern "C"
{
// helper functions for export
void* _opnew(size_t size);
void _opdel(void* p);
void* _opnewa(size_t size);
void _opdela(void *p);
} // extern "C"

// placement new
inline void* operator new(size_t, void* where) { return where; }

// global scope new/delete
inline void* operator new(size_t size) { return ::operator_new(size); }
inline void operator delete(void* p) { return ::operator_delete(p); }
inline void* operator new(size_t size) { return ::_opnew(size); }
inline void operator delete(void* p) { return ::_opdel(p); }

// global scope array new/delete
inline void* operator new[](size_t size) { return ::operator_new_array(size); }
inline void operator delete[](void *p) { return ::operator_delete_array(p); }

inline void* operator new[](size_t size) { return ::_opnewa(size); }
inline void operator delete[](void *p) { return ::_opdela(p); }

//REVIEW: seems that IOMallocAligned is broken in OS X... don't use it for now!
#define IOMallocAligned(x,y) IOMalloc(x)
Expand Down

0 comments on commit 5f6b235

Please sign in to comment.