//
// $Id: Memory.m,v 1.24 2005/05/18 06:46:53 will_mason Exp $
//
// vi: set ft=objc:

/*
 * ObjectiveLib - a library of containers and algorithms for Objective-C
 *
 * Copyright (c) 2004
 * Will Mason
 *
 * Portions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1997
 * Moscow Center for SPARC Technology
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the author at will_mason@users.sourceforge.net.
 */

#import "Memory.h"
#import "Macros.h"
#import <stddef.h>
#import <limits.h>

static OLAllocator* __defaultAllocator = nil;

@interface OLDefaultZoneAllocator : OLAllocator
{
@private
    NSZone* defaultZone;
}

- (id) init;
- (void*) allocateBytes: (unsigned)count;
- (id) allocateObject: (Class)cls;
- (id) autorelease: (id)object;
- (void) deallocateBytes: (void*)ptr;
- (void) deallocateObject: (id)object;
- (unsigned) maxSize;
- (BOOL) supportsAutorelease;

@end

@implementation OLAllocator

+ (void) initialize
{
    if (__defaultAllocator == nil)
        __defaultAllocator = [[OLDefaultZoneAllocator alloc] init];
}

+ (OLAllocator*) defaultAllocator
{
    return __defaultAllocator;
}

+ (void) setDefaultAllocator: (OLAllocator*)allocator
{
    if (allocator != __defaultAllocator)
    {
        OBJ_RELEASE(__defaultAllocator);
        __defaultAllocator = OBJ_RETAIN(allocator);
    }
}

- (void*) allocateBytes: (unsigned)count
{
    return NULL;
}

- (id) allocateObject: (Class)cls
{
    return nil;
}

- (id) autorelease: (id)object
{
    return object;
}

- (void) deallocateBytes: (void*)ptr
{
}

- (void) deallocateObject: (id)object
{
}

- (BOOL) isEqual: (id)object
{
    return [object isMemberOfClass: [self class]];
}

- (unsigned) maxSize
{
    return 0;
}

- (BOOL) supportsAutorelease
{
    return NO;
}

@end

@implementation OLDefaultZoneAllocator

- (id) init
{
    [super init];
    defaultZone = NSDefaultMallocZone();
    return self;
}

- (void*) allocateBytes: (unsigned)count
{
    return NSZoneMalloc(defaultZone, count);
}

- (id) allocateObject: (Class)cls
{
    return [cls alloc];
}

- (id) autorelease: (id)object
{
    return [object autorelease];
}

- (void) deallocateBytes: (void*)ptr
{
    NSZoneFree(defaultZone, ptr);
}

- (void) deallocateObject: (id)object
{
    [object release];
}

- (unsigned) maxSize
{
    return UINT_MAX / sizeof(id);
}

- (BOOL) supportsAutorelease
{
    return YES;
}

@end
