]> git.saurik.com Git - apple/security.git/blob - SecureTransport/sslalloc.c
46870116c0c309900d15cb7f172a8423cea4cefd
[apple/security.git] / SecureTransport / sslalloc.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 /*
20 File: sslalloc.c
21
22 Contains: memory allocator implementation
23
24 Written by: Doug Mitchell, based on Netscape RSARef 3.0
25
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
27
28 */
29 /* *********************************************************************
30 File: sslalloc.c
31
32 SSLRef 3.0 Final -- 11/19/96
33
34 Copyright (c)1996 by Netscape Communications Corp.
35
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.
39
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/>.
43
44 *********************************************************************
45
46 File: sslalloc.c Utility functions for doing allocation
47
48 These functions call the user-supplied callbacks to
49 allocate/free/reallocate memory
50
51 ****************************************************************** */
52
53 #include "sslalloc.h"
54 #include "sslctx.h"
55 #include "sslDebug.h"
56
57 #ifdef _APPLE_CDSA_
58
59 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
60
61 #pragma mark *** CF Allocators ***
62
63 /* copied from CSSMCFUtilities in the AppleCSP:CSPLib project.... */
64
65 static void* cfAllocate(CFIndex size, CFOptionFlags hint, void *info)
66 {
67 return sslMalloc((Size)size);
68 }
69
70 static void* cfReallocate(void *ptr, CFIndex newsize, CFOptionFlags hint, void *info)
71 {
72 return sslRealloc(ptr, (Size)newsize, (Size)newsize);
73 }
74
75 static void cfDeallocate(void *ptr, void *info)
76 {
77 sslFree(ptr);
78 }
79
80 /*
81 * Set up/tear down CF allocators.
82 */
83 OSStatus cfSetUpAllocators(SSLContext *ctx)
84 {
85 /* Initialize gCFAllocatorContext with the system default
86 allocator context. */
87 CFAllocatorGetContext(kCFAllocatorSystemDefault, &ctx->lCFAllocatorContext);
88
89 ctx->lCFAllocatorContext.allocate = cfAllocate;
90 ctx->lCFAllocatorContext.reallocate = cfReallocate;
91 ctx->lCFAllocatorContext.deallocate = cfDeallocate;
92
93 ctx->cfAllocatorRef = CFAllocatorCreate(kCFAllocatorUseContext,
94 &ctx->lCFAllocatorContext);
95 if (!ctx->cfAllocatorRef)
96 return memFullErr;
97
98 return noErr;
99 }
100
101 void cfTearDownAllocators(SSLContext *ctx)
102 {
103 if (ctx->cfAllocatorRef != NULL)
104 CFRelease(ctx->cfAllocatorRef);
105 }
106
107 #pragma mark *** Basic low-level malloc/free ***
108
109 /*
110 * For now, all allocs/frees go thru here.
111 */
112 #include <string.h> /* memset */
113 #include <stdlib.h>
114
115 void *
116 sslMalloc(UInt32 length)
117 {
118 return malloc(length);
119 }
120
121 void
122 sslFree(void *p)
123 {
124 if(p != nil) {
125 free(p);
126 }
127 }
128
129 void *
130 sslRealloc(void *oldPtr, UInt32 oldLen, UInt32 newLen)
131 {
132 return realloc(oldPtr, newLen);
133 }
134
135 #endif
136
137 #pragma mark *** SSLBuffer-level alloc/free ***
138
139 SSLErr
140 SSLAllocBuffer(SSLBuffer *buf, UInt32 length, const SystemContext *ctx)
141 {
142 buf->data = sslMalloc(length);
143 if(buf->data == NULL) {
144 buf->length = 0;
145 return SSLMemoryErr;
146 }
147 buf->length = length;
148 return SSLNoErr;
149 }
150
151 SSLErr
152 SSLFreeBuffer(SSLBuffer *buf, const SystemContext *ctx)
153 {
154 if(buf == NULL) {
155 errorLog0("SSLFreeBuffer: NULL buf!\n");
156 return SSLInternalError;
157 }
158 sslFree(buf->data);
159 buf->data = NULL;
160 buf->length = 0;
161 return SSLNoErr;
162 }
163
164 SSLErr
165 SSLReallocBuffer(SSLBuffer *buf, UInt32 newSize, const SystemContext *ctx)
166 {
167 buf->data = sslRealloc(buf->data, buf->length, newSize);
168 if(buf->data == NULL) {
169 buf->length = 0;
170 return SSLMemoryErr;
171 }
172 buf->length = newSize;
173 return SSLNoErr;
174 }
175
176 #pragma mark *** Convenience routines ***
177
178 UInt8 *sslAllocCopy(
179 const UInt8 *src,
180 UInt32 len)
181 {
182 UInt8 *dst;
183
184 dst = sslMalloc(len);
185 if(dst == NULL) {
186 return NULL;
187 }
188 memmove(dst, src, len);
189 return dst;
190 }