1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/* CFBase.c
Copyright (C) 2010-2011 Free Software Foundation, Inc.
Written by: Stefan Bidigaray
Date: January, 2010
This file is part of GNUstep CoreBase Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "CoreFoundation/CFBase.h"
#include "CoreFoundation/CFRuntime.h"
#include "GSPrivate.h"
#include <stdlib.h>
#include <string.h>
const double kCFCoreFoundationVersionNumber = 550.13;
struct __CFAllocator
{
CFRuntimeBase _parent;
CFAllocatorContext _context;
};
/* this will hold the default zone if set with CFAllocatorSetDefault () */
static CFTypeID _kCFAllocatorTypeID = 0;
static CFAllocatorRef _kCFDefaultAllocator = NULL;
static CFRuntimeClass CFAllocatorClass =
{
0,
"CFAllocator",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
void CFAllocatorInitialize (void)
{
_kCFAllocatorTypeID = _CFRuntimeRegisterClass (&CFAllocatorClass);
_kCFDefaultAllocator = kCFAllocatorSystemDefault;
/* These are already semi-initialized by INIT_CFRUNTIME_BASE() */
GSRuntimeConstantInit (kCFAllocatorSystemDefault, _kCFAllocatorTypeID);
GSRuntimeConstantInit (kCFAllocatorMalloc, _kCFAllocatorTypeID);
GSRuntimeConstantInit (kCFAllocatorMallocZone, _kCFAllocatorTypeID);
GSRuntimeConstantInit (kCFAllocatorNull, _kCFAllocatorTypeID);
}
static void *
malloc_alloc (CFIndex allocSize, CFOptionFlags hint, void *info)
{
return malloc (allocSize);
}
static void *
malloc_realloc (void *ptr, CFIndex newsize, CFOptionFlags hint, void *info)
{
return realloc (ptr, newsize);
}
static void
malloc_dealloc (void *ptr, void *info)
{
free (ptr);
}
static void *
null_alloc (CFIndex allocSize, CFOptionFlags hint, void *info)
{
return NULL;
}
static void *
null_realloc (void *ptr, CFIndex newsize, CFOptionFlags hint, void *info)
{
return NULL;
}
static void
null_dealloc (void *ptr, void *info)
{
}
static struct __CFAllocator _kCFAllocatorSystemDefault =
{
INIT_CFRUNTIME_BASE(),
{ 0, NULL, NULL, NULL, NULL, malloc_alloc, malloc_realloc, malloc_dealloc, NULL }
};
static struct __CFAllocator _kCFAllocatorNull =
{
INIT_CFRUNTIME_BASE(),
{ 0, NULL, NULL, NULL, NULL, null_alloc, null_realloc, null_dealloc, NULL }
};
CFAllocatorRef kCFAllocatorDefault = NULL;
/* Just use the default system allocator everywhere! */
CFAllocatorRef kCFAllocatorSystemDefault = &_kCFAllocatorSystemDefault;
CFAllocatorRef kCFAllocatorMalloc = &_kCFAllocatorSystemDefault;
CFAllocatorRef kCFAllocatorMallocZone = &_kCFAllocatorSystemDefault;
CFAllocatorRef kCFAllocatorNull = &_kCFAllocatorNull;
CFAllocatorRef kCFAllocatorUseContext = (CFAllocatorRef)0x01;
CFAllocatorRef
CFAllocatorCreate(CFAllocatorRef allocator, CFAllocatorContext *context)
{
struct __CFAllocator *new;
if (allocator == kCFAllocatorUseContext)
{
/* Chicken and egg problem... */
return NULL; /* FIXME */
}
else
{
new = (struct __CFAllocator*)_CFRuntimeCreateInstance (allocator,
_kCFAllocatorTypeID,
sizeof(struct __CFAllocator) - sizeof(CFRuntimeBase),
0);
memcpy (&(new->_context), context, sizeof(CFAllocatorContext));
}
return (CFAllocatorRef)new;
}
void *
CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint)
{
if (NULL == allocator)
allocator = _kCFDefaultAllocator;
return allocator->_context.allocate(size, hint, allocator->_context.info);
}
void
CFAllocatorDeallocate(CFAllocatorRef allocator, void *ptr)
{
if (NULL == allocator)
allocator = _kCFDefaultAllocator;
allocator->_context.deallocate(ptr, allocator->_context.info);
}
CFIndex
CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex size,
CFOptionFlags hint)
{
if (allocator == NULL)
allocator = _kCFDefaultAllocator;
if (allocator->_context.preferredSize)
return allocator->_context.preferredSize (size, hint,
allocator->_context.info);
return size;
}
void *
CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize, CFOptionFlags hint)
{
if (NULL == allocator)
allocator = _kCFDefaultAllocator;
return allocator->_context.reallocate(ptr, newsize, hint,
allocator->_context.info);
}
CFAllocatorRef
CFAllocatorGetDefault(void)
{
return _kCFDefaultAllocator;
}
void
CFAllocatorSetDefault(CFAllocatorRef allocator)
{
CFAllocatorRef current = _kCFDefaultAllocator;
if (allocator == NULL)
return;
CFRetain (allocator);
_kCFDefaultAllocator = allocator;
CFRelease (current);
}
void
CFAllocatorGetContext(CFAllocatorRef allocator, CFAllocatorContext *context)
{
memcpy (context, &(allocator->_context), sizeof(CFAllocatorContext));
}
CFTypeID
CFAllocatorGetTypeID(void)
{
return _kCFAllocatorTypeID;
}
static CFTypeID _kCFNullTypeID;
static const CFRuntimeClass CFNullClass =
{
0,
"CFNUll",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
struct __CFNull
{
CFRuntimeBase _parent;
};
static struct __CFNull _kCFNull =
{
INIT_CFRUNTIME_BASE()
};
CFNullRef kCFNull = &_kCFNull;
void CFNullInitialize (void)
{
_kCFNullTypeID = _CFRuntimeRegisterClass (&CFNullClass);
_CFRuntimeSetInstanceTypeID (&_kCFNull, _kCFNullTypeID);
GSRuntimeConstantInit (kCFNull, _kCFNullTypeID);
}
CFTypeID
CFNullGetTypeID (void)
{
return _kCFNullTypeID;
}
|