File:  [parser3project] / parser3 / src / include / pa_memory.h
Revision 1.1.2.9.2.2: download - view: text, annotated - select for diffs - revision graph
Wed Mar 19 13:16:56 2003 UTC (23 years, 3 months ago) by paf
string compiled
todo: test it

/** @file
	Parser: memory reference counting classes decls.

	Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)

	Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
*/

#ifndef PA_MEMORY_H
#define PA_MEMORY_H

static const char* IDENT_MEMORY_H="$Date: 2003/03/19 13:16:56 $";

#define PA_DEBUG_REFERENCES

// include

#include "pa_config_includes.h"
#include "gc.h"


#ifdef XML
#	include "gdome.h"
// for xmlChar
#	include "libxml/tree.h"
#endif

// forwards

void *pa_fail_alloc(const char *what, size_t size);

// inlines

inline void *pa_malloc(size_t size) {
	if(void *result=GC_MALLOC(size))
		return result;

	return pa_fail_alloc("allocate", size);
}

inline void *pa_malloc_atomic(size_t size) {
	if(void *result=GC_MALLOC_ATOMIC(size))
		return result;

	return pa_fail_alloc("allocate clean", size);
}
inline void pa_free(void *ptr) {
	GC_FREE(ptr);
}

inline void *pa_realloc(void *ptr, size_t size) {
	if(void *result=GC_REALLOC(ptr, size))
		return result;

	return pa_fail_alloc("reallocate to", size);
}

#define DECLARE_OBJECT_PTR(name) \
	typedef name* name##Ptr

#define DECLARE_SMART_PTR(name) \
	typedef name* name##Ptr

// defines

#define override
#define rethrow throw

// memory

inline void *operator new[] (size_t size) {
	return pa_malloc_atomic(size);
}
inline void operator delete[] (void *ptr) {
	pa_free(ptr);
}
inline void operator delete (void *ptr) {
	abort();
}

/// memory allocation/dallocation goes via pa_malloc/pa_free.
class PA_Allocated {
public:
	/// the sole: instances allocated using our functions
	static void *operator new(size_t size) { 
		return pa_malloc(size);
	}
	static void operator delete(void *ptr) {
		pa_free(ptr);
	}
	static void *malloc(size_t size) {
		return pa_malloc(size);
	}
	static void *malloc_atomic(size_t size) {
		return pa_malloc_atomic(size);
	}
	static void free(void *ptr) {
		pa_free(ptr);
	}
	static void *realloc(void *ptr, size_t size) {
		return pa_realloc(ptr, size);
	}

private: // disabled from accidental use

	/// use malloc instead [GC clears malloc result]
	static void *calloc(size_t size);

};

/// Base for all Parser classes
typedef PA_Allocated PA_Object;

// convinient types

#endif

E-mail: