#include "sslalloc.h"
#include "sslctx.h"
#include "sslDebug.h"
#ifdef _APPLE_CDSA_
#include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
#pragma mark *** CF Allocators ***
static void* cfAllocate(CFIndex size, CFOptionFlags hint, void *info)
{
return sslMalloc((Size)size);
}
static void* cfReallocate(void *ptr, CFIndex newsize, CFOptionFlags hint, void *info)
{
return sslRealloc(ptr, (Size)newsize, (Size)newsize);
}
static void cfDeallocate(void *ptr, void *info)
{
sslFree(ptr);
}
OSStatus cfSetUpAllocators(SSLContext *ctx)
{
CFAllocatorGetContext(kCFAllocatorSystemDefault, &ctx->lCFAllocatorContext);
ctx->lCFAllocatorContext.allocate = cfAllocate;
ctx->lCFAllocatorContext.reallocate = cfReallocate;
ctx->lCFAllocatorContext.deallocate = cfDeallocate;
ctx->cfAllocatorRef = CFAllocatorCreate(kCFAllocatorUseContext,
&ctx->lCFAllocatorContext);
if (!ctx->cfAllocatorRef)
return memFullErr;
return noErr;
}
void cfTearDownAllocators(SSLContext *ctx)
{
if (ctx->cfAllocatorRef != NULL)
CFRelease(ctx->cfAllocatorRef);
}
#pragma mark *** Basic low-level malloc/free ***
#include <string.h>
#include <stdlib.h>
void *
sslMalloc(UInt32 length)
{
return malloc(length);
}
void
sslFree(void *p)
{
if(p != nil) {
free(p);
}
}
void *
sslRealloc(void *oldPtr, UInt32 oldLen, UInt32 newLen)
{
return realloc(oldPtr, newLen);
}
#endif
#pragma mark *** SSLBuffer-level alloc/free ***
SSLErr
SSLAllocBuffer(SSLBuffer *buf, UInt32 length, const SystemContext *ctx)
{
buf->data = sslMalloc(length);
if(buf->data == NULL) {
buf->length = 0;
return SSLMemoryErr;
}
buf->length = length;
return SSLNoErr;
}
SSLErr
SSLFreeBuffer(SSLBuffer *buf, const SystemContext *ctx)
{
if(buf == NULL) {
errorLog0("SSLFreeBuffer: NULL buf!\n");
return SSLInternalError;
}
sslFree(buf->data);
buf->data = NULL;
buf->length = 0;
return SSLNoErr;
}
SSLErr
SSLReallocBuffer(SSLBuffer *buf, UInt32 newSize, const SystemContext *ctx)
{
buf->data = sslRealloc(buf->data, buf->length, newSize);
if(buf->data == NULL) {
buf->length = 0;
return SSLMemoryErr;
}
buf->length = newSize;
return SSLNoErr;
}
#pragma mark *** Convenience routines ***
UInt8 *sslAllocCopy(
const UInt8 *src,
UInt32 len)
{
UInt8 *dst;
dst = sslMalloc(len);
if(dst == NULL) {
return NULL;
}
memmove(dst, src, len);
return dst;
}