File:  [parser3project] / parser3 / src / targets / cgi / Attic / pa_pool.C
Revision 1.41.4.2: download - view: text, annotated - select for diffs - revision graph
Tue Mar 18 09:53:02 2003 UTC (23 years, 4 months ago) by paf
Diff to: branchpoint 1.41: preferred, unified
gc logging

/** @file
	Parser: CGI memory manager impl.

	Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
	Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
*/

static const char* IDENT_POOL_C="$Date: 2003/03/18 09:53:02 $";

#include "pa_pool.h"

#ifdef XML
#	include "libxml/xmlmemory.h"
#endif

#ifdef _DEBUG
#	define GC_DEBUG
#endif
#include "gc.h"


// debug switches

#define DEBUG_TOTAL_ALLOC_SIZE
#define DEBUG_DUMP

//

#ifdef DEBUG_TOTAL_ALLOC_SIZE
unsigned long total_alloc_size=0;
#endif

void Pool::log_high_watermark(const char *msg) {
	fprintf(stderr, "%s: malloced=%lu, heap_used=%lu, heap_free=%lu, bytes_since_gc=%lu, total_bytes=%lu\n",
		msg,
		total_alloc_size,
		GC_get_heap_size(),
		GC_get_free_bytes(),
		GC_get_bytes_since_gc(),
		GC_get_total_bytes()
		);
}

void Pool::collect_garbage() {
#ifdef DEBUG_DUMP
	log_high_watermark("before gc");
#endif

	//GC_dont_gc=false;
	//GC_collect_a_little();
	GC_gcollect();
	GC_gcollect();

#ifdef DEBUG_DUMP
	log_high_watermark("after gc");
#endif
}

#ifdef XML
char *pa_GC_strdup(const char *s) {
	if(!s)
		return 0;

	size_t size=strlen(s)+1;
	char *result=(char *)GC_MALLOC_ATOMIC(size);
	memcpy(result, s, size);
	return result;
}

#endif

void Pool::real_init() {
#ifdef DEBUG_DUMP
	log_high_watermark("init");
#endif
	//GC_dont_gc=true;
#ifdef XML
	static bool XML_memory_function_overloaded=false;
	if(!XML_memory_function_overloaded) {
		XML_memory_function_overloaded=true;
		xmlMemSetup(/*xmlFreeFunc */GC_free,
			 /*xmlMallocFunc */GC_malloc,
			 /*xmlReallocFunc */GC_realloc,
			 /*xmlStrdupFunc */pa_GC_strdup);
	}
#endif
}
void Pool::real_finit() {
	//collect_garbage();
}

void *Pool::real_malloc(size_t size, int place) {
#ifdef DEBUG_TOTAL_ALLOC_SIZE
	total_alloc_size+=size;
#endif
	//printf("%d,",size);

	return GC_MALLOC(size);//::calloc(size,1);//
}

void *Pool::real_malloc_atomic(size_t size, int place) {
#ifdef DEBUG_TOTAL_ALLOC_SIZE
	total_alloc_size+=size;
#endif

	return GC_MALLOC_ATOMIC(size);//::calloc(size,1);//
}

void *Pool::real_calloc(size_t size) {
#ifdef DEBUG_TOTAL_ALLOC_SIZE
	total_alloc_size+=size;
#endif

	return GC_MALLOC(size);//::calloc(size,1);//
}

bool Pool::real_register_cleanup(void (*cleanup) (void *, void*), void *data) {
	GC_REGISTER_FINALIZER(data, cleanup, 0, 0, 0);
	return true;
}

E-mail: