2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
22 Contains: memory allocator implementation
24 Written by: Doug Mitchell, based on Netscape RSARef 3.0
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
29 /* *********************************************************************
32 SSLRef 3.0 Final -- 11/19/96
34 Copyright (c)1996 by Netscape Communications Corp.
36 By retrieving this software you are bound by the licensing terms
37 disclosed in the file "LICENSE.txt". Please read it, and if you don't
38 accept the terms, delete this software.
40 SSLRef 3.0 was developed by Netscape Communications Corp. of Mountain
41 View, California <http://home.netscape.com/> and Consensus Development
42 Corporation of Berkeley, California <http://www.consensus.com/>.
44 *********************************************************************
46 File: sslalloc.c Utility functions for doing allocation
48 These functions call the user-supplied callbacks to
49 allocate/free/reallocate memory
51 ****************************************************************** */
59 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
61 #pragma mark *** CF Allocators ***
63 /* copied from CSSMCFUtilities in the AppleCSP:CSPLib project.... */
65 static void* cfAllocate(CFIndex size
, CFOptionFlags hint
, void *info
)
67 return sslMalloc((Size
)size
);
70 static void* cfReallocate(void *ptr
, CFIndex newsize
, CFOptionFlags hint
, void *info
)
72 return sslRealloc(ptr
, (Size
)newsize
, (Size
)newsize
);
75 static void cfDeallocate(void *ptr
, void *info
)
81 * Set up/tear down CF allocators.
83 OSStatus
cfSetUpAllocators(SSLContext
*ctx
)
85 /* Initialize gCFAllocatorContext with the system default
87 CFAllocatorGetContext(kCFAllocatorSystemDefault
, &ctx
->lCFAllocatorContext
);
89 ctx
->lCFAllocatorContext
.allocate
= cfAllocate
;
90 ctx
->lCFAllocatorContext
.reallocate
= cfReallocate
;
91 ctx
->lCFAllocatorContext
.deallocate
= cfDeallocate
;
93 ctx
->cfAllocatorRef
= CFAllocatorCreate(kCFAllocatorUseContext
,
94 &ctx
->lCFAllocatorContext
);
95 if (!ctx
->cfAllocatorRef
)
101 void cfTearDownAllocators(SSLContext
*ctx
)
103 if (ctx
->cfAllocatorRef
!= NULL
)
104 CFRelease(ctx
->cfAllocatorRef
);
107 #pragma mark *** Basic low-level malloc/free ***
110 * For now, all allocs/frees go thru here.
112 #include <string.h> /* memset */
116 sslMalloc(UInt32 length
)
118 return malloc(length
);
130 sslRealloc(void *oldPtr
, UInt32 oldLen
, UInt32 newLen
)
132 return realloc(oldPtr
, newLen
);
137 #pragma mark *** SSLBuffer-level alloc/free ***
140 SSLAllocBuffer(SSLBuffer
*buf
, UInt32 length
, const SystemContext
*ctx
)
142 buf
->data
= sslMalloc(length
);
143 if(buf
->data
== NULL
) {
147 buf
->length
= length
;
152 SSLFreeBuffer(SSLBuffer
*buf
, const SystemContext
*ctx
)
155 errorLog0("SSLFreeBuffer: NULL buf!\n");
156 return SSLInternalError
;
165 SSLReallocBuffer(SSLBuffer
*buf
, UInt32 newSize
, const SystemContext
*ctx
)
167 buf
->data
= sslRealloc(buf
->data
, buf
->length
, newSize
);
168 if(buf
->data
== NULL
) {
172 buf
->length
= newSize
;
176 #pragma mark *** Convenience routines ***
184 dst
= sslMalloc(len
);
188 memmove(dst
, src
, len
);