+
+#ifdef KERNEL
+
+/* BSD Malloc */
+#include <sys/malloc.h>
+#include <IOKit/IOLib.h>
+#include <libkern/libkern.h>
+
+/* Define this for debugging sslMalloc and sslFree */
+//#define SSL_CANARIS
+
+void *
+sslMalloc(size_t length)
+{
+ void *p;
+
+#ifdef SSL_CANARIS
+ length+=8;
+#endif
+
+ p = _MALLOC(length, M_TEMP, M_WAITOK);
+ check(p);
+
+ if(p==NULL)
+ return p;
+
+#ifdef SSL_CANARIS
+ *(uint32_t *)p=(uint32_t)length-8;
+ printf("sslMalloc @%p of 0x%08lx bytes\n", p, length-8);
+ *(uint32_t *)(p+length-4)=0xdeadbeed;
+ p+=4;
+#endif
+
+ return p;
+}
+
+void
+sslFree(void *p)
+{
+ if(p != NULL) {
+
+#ifdef SSL_CANARIS
+ p=p-4;
+ uint32_t len=*(uint32_t *)p;
+ uint32_t marker=*(uint32_t *)(p+4+len);
+ printf("sslFree @%p len=0x%08x\n", p, len);
+ if(marker!=0xdeadbeef)
+ panic("Buffer overflow in SSL!\n");
+#endif
+
+ _FREE(p, M_TEMP);
+ }
+}
+
+void *
+sslRealloc(void *oldPtr, size_t oldLen, size_t newLen)
+{
+ /* _REALLOC is in sys/malloc.h but is only exported in debug kernel */
+ /* return _REALLOC(oldPtr, newLen, M_TEMP, M_NOWAIT); */
+
+ /* FIXME */
+ void *newPtr;
+ if(newLen>oldLen) {
+ newPtr=sslMalloc(newLen);
+ if(newPtr) {
+ memcpy(newPtr, oldPtr, oldLen);
+ sslFree(oldPtr);
+ }
+ } else {
+ newPtr=oldPtr;
+ }
+ return newPtr;
+}
+
+#else
+