File:  [parser3project] / parser3 / src / include / pa_memory.h
Revision 1.1.2.9.2.11: download - view: text, annotated - select for diffs - revision graph
Mon Mar 24 17:00:49 2003 UTC (23 years, 3 months ago) by paf
size_t pos everywhere checks changed to check for eq STRING_NOT_FOUND

/** @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/24 17:00:49 $";

#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);
}
/// @a length may be null, which mean "autocalc it"
inline char *pa_strdup(const char* auto_variable_never_null, size_t length=0) {
	if(!length)
		length=strlen(auto_variable_never_null);

	size_t size=length+1;
	if(char *result=static_cast<char*>(GC_MALLOC_ATOMIC(size))) {
		memcpy(result, auto_variable_never_null, length);
		result[length]=0;
		return result;
	}

	return static_cast<char*>(pa_fail_alloc("allocate clean", size));
}
char* pa_format_integer(int value);

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);
}

//{@ these operators are disabled, one should explicitely specify either new(UseGC) or new(PointerFreeGC)
inline void *operator new(size_t size) { abort(); } // disabled
inline void operator delete (void *ptr) { abort(); } // disabled
inline void *operator new[] (size_t size) { abort(); } // disabled
//}@

#define UseGC ((int)1)
#define PointerFreeGC (true)

inline void *operator new[] (size_t size, int) { // UseGC
	return pa_malloc(size);
}
inline void *operator new[] (size_t size, bool) { // PointerFreeGC
	return pa_malloc_atomic(size);
}
inline void operator delete[] (void *ptr) {
	pa_free(ptr);
}

/// 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 char *strdup(const char* auto_variable_never_null, size_t length=0) {
		return pa_strdup(auto_variable_never_null, length);
	}
	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/malloc_atomic instead [GC clears result of those]
	static void *calloc(size_t size);

};

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

// defines

#define override
#define rethrow throw


#endif

E-mail: