/*
 *	dmem_if.h -- dynamic memory public interface
 *	Copyright (C) 2002-2005 Fred Barnes <frmb@kent.ac.uk>
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program 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 General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __DMEM_IF_H
#define __DMEM_IF_H

/* DM_DEBUG moved to configure */

#ifdef __cplusplus
extern "C" {
#endif

#if defined(DM_DEBUG) && (DM_DEBUG == 1)
#define ADDR_INVALID	0x00

#define ADDR_TARGET	0x01
#define ADDR_SOURCE	0x02

#define MODE_WRITE	0x01
#define MODE_READ	0x02
#define MODE_BALLOC	0x04
#define MODE_TRASHED	0x08

extern void extadd_ord_mem (void *, int, int);
extern void extdel_ord_mem (void *);
extern void extadd_elf_mem (char *);
extern void extdel_elf_mem (char *);
extern void dm_debug_dump (void);
#endif

typedef struct {
	const char *name;
	void *priv;
	void *(*alloc)(int);			/* bytes */
	void (*alloc2)(void **);		/* pointer to single size/result */
	void (*release)(void *);		/* pointer */
	void *(*realloc)(void *, int, int);	/* pointer, old-size, new-size */
} dmem_allocator_t;

extern void dmem_init (void);
extern void dmem_shutdown (void);

extern dmem_allocator_t *default_allocator;


extern int dmem_slot_to_size (int idx);

#define dmem_alloc(S) default_allocator->alloc((S))
#define dmem_alloc2(PP) default_allocator->alloc2((PP))
#define dmem_release(P) default_allocator->release((P))
#define dmem_realloc(P,S1,S2) default_allocator->realloc((P),(S1),(S2))

extern void dmem_usagedump (void);
extern void dmem_chunkdump (void);

#if !defined(OOS_BUILD)
extern void *dmem_locked_malloc (const int nbytes);
extern void dmem_locked_free (const void *ptr);
#endif

extern void *dmem_mtnew (unsigned int *typedesc);
extern void dmem_mtfree (unsigned int *typedesc, void *ptr);
extern void *dmem_mtclone (unsigned int *typedesc, void *ptr);


#ifdef __cplusplus
}
#endif

/* if blocking syscalls are enabled, malloc() and free() must be protected */
#if defined(BLOCKING_SYSCALLS)
	#define locked_malloc(X) dmem_locked_malloc((X))
	#define locked_free(X) dmem_locked_free((X))
#else
	#define locked_malloc(X) malloc((X))
	#define locked_free(X) free((X))
#endif	/* !defined(BLOCKING_SYSCALLS) */

#endif	/* !__DMEM_IF_H */

