/*
 * The contents of this file are subject to the Netscape Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/NPL/

 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.

 * The Original Code is Rhino code, released
 * May 6, 1999.

 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
 * Rights Reserved.

 * Contributor(s): 

 * Alternatively, the contents of this file may be used under the
 * terms of the GNU Public License (the "GPL"), in which case the
 * provisions of the GPL are applicable instead of those above.
 * If you wish to allow use of your version of this file only
 * under the terms of the GPL and not to allow others to use your
 * version of this file under the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL.  If you do not delete
 * the provisions above, a recipient may use your version of this
 * file under either the NPL or the GPL.
 */
#ifndef _JS_HEAP_H_
#define _JS_HEAP_H_

#include <stdlib.h>
#ifdef LINUX
#include <malloc.h>
#endif
#ifdef VX
 #include "ewb-mem.h"
#endif

#if defined(SUNOS5) || defined(LINUX)
extern int jsGCHeapSize;
extern int jsByteCodeHeapSize;
extern int jsZoneHeapSize;

extern int jsBytecodeMemsize;
extern int jsBytecodeMemsizeMax;
extern int jsGCMemsize;
extern int jsZoneMemsize;
extern int jsZoneMemsizeMax;
#endif

/* Classes */
class JSHeap {
    public:
	void *memalign(size_t alignment, size_t size) {
#if defined(LINUX)
	    return ::memalign(alignment, size);
#elif defined(SUNOS5)
	    return (void *) ::memalign(alignment, size);
#elif defined(VX)
	    return EwbMAlignedAlloc( size, alignment );
#else
	    ->
#endif
	}
	void *malloc(size_t size) {
#if defined(LINUX)
	    return ::malloc(size);
#elif defined(SUNOS5)
	    return ::malloc(size);
#elif defined(VX)
	    return EwbMalloc( size );
#else
	    ->
#endif
	}
	void free(void *ptr) {
#if defined(SUNOS5)
	    ::free(ptr);
#elif defined(LINUX)
	    ::free(ptr);
#elif defined(VX)
	    if( ptr != NULL ) EwbMFree( ptr );
#else
	    ->
#endif
	}
} ;


/* 2004.04.22 ichino */
class ByteCodeHeap {
    public:
	void *memalign(size_t alignment, size_t size) {
#if defined(LINUX) || defined(SUNOS5)
	    return ::memalign(alignment, size);
#elif defined(VX)
	    return ByteCodeMAlignedAlloc( size, alignment );
#else
	    ->
#endif
	}
	void *malloc(size_t size) {
#if defined(LINUX) || defined(SUNOS5)
	    return ::malloc(size);
#elif defined(VX)
	    return ByteCodeMalloc( size );
#else
	    ->
#endif
	}
	void free(void *ptr) {
#if defined(LINUX) || defined(SUNOS5)
	    ::free(ptr);
#elif defined(VX)
	    if( ptr != NULL ) ByteCodeMFree( ptr );
#else
	    ->
#endif
	}
#if defined(SUNOS5)
	static void show() {
	  printf("jsBytecodeMemsize:%d\n",jsBytecodeMemsize);
	}
#endif

} ;

/* 2004.02.09 ichino JSLocalHeap is memory allocate class for JS GC */
class JSLocalHeap {
    public:
	void *memalign(size_t alignment, size_t size) {
#if defined(LINUX) || defined(SUNOS5)
	    return ::memalign(alignment, size);
#elif defined(VX)
	    return JsMAlignedAlloc( size, alignment );
#else
	    ->
#endif
	}
	void *malloc(size_t size) {
#if defined(LINUX) || defined(SUNOS5)
	    return ::malloc(size);
#elif defined(VX)
	    return JsMalloc( size );
#else
	    ->
#endif
	}
	void free(void *ptr) {
#if defined(LINUX) || defined(SUNOS5)
	    ::free(ptr);
#elif defined(VX)
	    if( ptr != NULL ) JsMFree( ptr );
#else
	    ->
#endif
	}
};

/* 2004.02.23 ichino */
class JSZoneLocalHeap {
    public:
	void *memalign(size_t alignment, size_t size) {
#if defined(LINUX) || defined(SUNOS5)
	    return ::memalign(alignment, size);
#elif defined(VX)
	    return JsZoneMAlignedAlloc( size, alignment );
#else
	    ->
#endif
	}
	void *malloc(size_t size) {
#if defined(LINUX) || defined(SUNOS5)
	    return ::malloc(size);
#elif defined(VX)
	    return JsZoneMalloc( size );
#else
	    ->
#endif
	}
	void free(void *ptr) {
#if defined(LINUX) || defined(SUNOS5)
	    ::free(ptr);
#elif defined(VX)
	    if( ptr != NULL ) JsZoneMFree( ptr );
#else
	    ->
#endif
	}
};

#endif /* _JS_HEAP_H_ */

