/* 
   NSDefaultZone.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <config.h>

#if HAVE_STRING_H
# include <string.h>
#endif

#if HAVE_MEMORY_H
# include <memory.h>
#endif

#if !HAVE_MEMCPY
# define memcpy(d, s, n)       bcopy((s), (d), (n))
# define memmove(d, s, n)      bcopy((s), (d), (n))
#endif

#if HAVE_STDLIB_H
# include <stdlib.h>
#else
extern void* malloc();
extern void* calloc();
extern void* realloc();
extern void free();
extern atoi();
extern atol();
#endif

#include <Foundation/common.h>
#include <Foundation/NSString.h>
#include <Foundation/NSException.h>
#include <Foundation/exceptions/GeneralExceptions.h>

#include <extensions/objc-runtime.h>

#include "NSDefaultZone.h"

@implementation NSDefaultZone

+ alloc
{
    return [self allocZoneInstance];
}

- init
{
    return [self initForSize:0 granularity:0 canFree:YES];
}

- initForSize:(unsigned)startSize granularity:(unsigned)granularity
    canFree:(BOOL)canFree
{
    [name release];
    name = @"Default zone";
    return self;
}

- (void*)malloc:(unsigned)size
{
    void* p = malloc(size);
    if(!p)
	THROW([[memoryExhaustedException retain]
			setPointer:&p memorySize:size]);
    
    return p;
}

- (void*)calloc:(unsigned)count byteSize:(unsigned)size
{
    void* p = calloc(count, size);
    if(!p) {
	THROW([[memoryExhaustedException retain]
					setPointer:&p memorySize:size]);
	memset(p, 0, size);
    }
    return p;
}

- (void*)realloc:(void*)p size:(unsigned)size
{
    void* new_p = realloc(p, size);
    if(!new_p) {
	THROW([[memoryExhaustedException retain]
					setPointer:&new_p memorySize:size]);
	memmove(new_p, p, size);
	free(p);
    }
    return new_p;
}

- (void)recycle
{
}

- (void)freePointer:(void*)p
{
    if(p)
	free(p);
}

- (BOOL)checkZone
{
    return YES;
}

- (BOOL)pointerInZone:(void*)pointer
{
    return YES;
}

@end /* NSDefaultZone */
