Added even better allocator codes for both expat & libxml

This commit is contained in:
Moriyoshi Koizumi 2003-05-07 15:21:31 +00:00
parent edac258751
commit 21661d3bde
3 changed files with 79 additions and 23 deletions

View File

@ -197,33 +197,61 @@ php_xml_compat_handlers = {
XML_Parser
XML_ParserCreate(const XML_Char *encoding)
{
XML_Parser parser;
parser = (XML_Parser) calloc(1, sizeof(struct _XML_Parser));
parser->parser = xmlCreatePushParserCtxt((xmlSAXHandlerPtr) &php_xml_compat_handlers, (void *) parser, NULL, 0, NULL);
if (parser->parser == NULL) {
return NULL;
}
parser->parser->encoding = xmlStrdup(encoding);
return parser;
return XML_ParserCreate_MM(encoding, NULL, NULL);
}
XML_Parser
XML_ParserCreateNS(const XML_Char *encoding, const XML_Char sep)
{
XML_Parser parser;
XML_Char tmp[2];
tmp[0] = sep;
tmp[1] = '\0';
return XML_ParserCreate_MM(encoding, NULL, tmp);
}
parser = (XML_Parser) calloc(1, sizeof(struct _XML_Parser));
static void *(*_expat_cpt_malloc_fcn)(size_t sz);
static char *_expat_cpt_intn_strdup(const chat *str)
{
size_t len;
char *retval;
len = strlen(str);
if ((retval = _expat_cpt_malloc_fcn(len + 1)) == NULL) {
return NULL;
}
memcpy(retval, str, len + 1);
return retval;
}
XML_Parser
XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *memsuite, const XML_Char *sep)
{
XML_Parser parser;
static XML_Memory_Handling_Suite mtemp_i = { malloc, realloc, free };
if (memsuite == NULL) {
memsuite = &mtemp_i;
}
_expat_cpt_malloc_fcn = memsuite->malloc_fcn; /* FIXME: not reentrant ! */
xmlMemSetup(memsuite->free_fcn, memsuite->malloc_fcn, memsuite->realloc_fcn, _expat_compat_intn_strdup); /* WHOCANFIXME: not reentrant ! */
parser = (XML_Parser) memsuite->malloc_fcn(sizeof(struct _XML_Parser));
parser->mem_hdlrs = *memsuite;
parser->parser = xmlCreatePushParserCtxt((xmlSAXHandlerPtr) &php_xml_compat_handlers, (void *) parser, NULL, 0, NULL);
if (parser->parser == NULL) {
parser->mem_hdlrs.free_fcn(parser->parser);
return NULL;
}
parser->parser->encoding = xmlStrdup(encoding);
parser->namespace = 1;
parser->_ns_map = xmlHashCreate(10);
parser->_reverse_ns_map = xmlHashCreate(10);
if (sep != NULL) {
parser->namespace = 1;
parser->_ns_map = xmlHashCreate(10);
parser->_reverse_ns_map = xmlHashCreate(10);
}
return parser;
}
@ -353,7 +381,7 @@ XML_ParserFree(XML_Parser parser)
xmlHashFree(parser->_reverse_ns_map, _free_ns_name);
}
xmlFreeParserCtxt(parser->parser);
free(parser);
parser->mem_hdlrs.free_fcn(parser);
}
#endif /* LIBXML_EXPAT_COMPAT */

View File

@ -43,6 +43,12 @@ typedef int (*XML_ExternalEntityRefHandler)(void *, const XML_Char *, const XML
typedef void (*XML_StartNamespaceDeclHandler)(void *, const XML_Char *, const XML_Char *);
typedef void (*XML_EndNamespaceDeclHandler)(void *, const XML_Char *);
typedef struct _XML_Memory_Handling_Suite {
void *(*malloc_fcn)(size_t size);
void *(*realloc_fcn)(void *ptr, size_t size);
void (*free_fcn)(void *ptr);
} XML_Memory_Handling_Suite;
typedef struct _XML_Parser {
int namespace;
@ -62,10 +68,12 @@ typedef struct _XML_Parser {
XML_ExternalEntityRefHandler h_external_entity_ref;
XML_StartNamespaceDeclHandler h_start_ns;
XML_EndNamespaceDeclHandler h_end_ns;
XML_Memory_Handling_Suite mem_hdlrs;
} *XML_Parser;
XML_Parser XML_ParserCreate(const XML_Char *);
XML_Parser XML_ParserCreateNS(const XML_Char *, const XML_Char);
XML_ParserCreate_MM(const XML_Char *, const XML_Memory_Handling_Suite *, const XML_Char *);
void XML_SetUserData(XML_Parser, void *);
void *XML_GetUserData(XML_Parser);
void XML_SetElementHandler(XML_Parser, XML_StartElementHandler, XML_EndElementHandler);

View File

@ -152,6 +152,8 @@ xml_encoding xml_encodings[] = {
{ NULL, NULL, NULL }
};
static XML_Memory_Handling_Suite php_xml_mem_hdlrs;
/* True globals, no need for thread safety */
static int le_xml_parser;
@ -165,6 +167,21 @@ static void php_xml_init_globals(php_xml_globals *xml_globals_p TSRMLS_DC)
}
#endif
static void *php_xml_malloc_wrapper(size_t sz)
{
return emalloc(sz);
}
static void *php_xml_realloc_wrapper(void *ptr, size_t sz)
{
return erealloc(ptr, sz);
}
static void php_xml_free_wrapper(void *ptr)
{
efree(ptr);
}
PHP_MINIT_FUNCTION(xml)
{
le_xml_parser = zend_register_list_destructors_ex(xml_parser_dtor, NULL, "xml", module_number);
@ -203,10 +220,13 @@ PHP_MINIT_FUNCTION(xml)
REGISTER_LONG_CONSTANT("XML_OPTION_SKIP_TAGSTART", PHP_XML_OPTION_SKIP_TAGSTART, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("XML_OPTION_SKIP_WHITE", PHP_XML_OPTION_SKIP_WHITE, CONST_CS|CONST_PERSISTENT);
#ifdef LIBXML_EXPAT_COMPAT
xmlMemSetup(_efree, _emalloc, _erealloc, _estrdup);
#endif
/* this object should not be pre-initialised at compile time,
as the order of members may vary */
php_xml_mem_hdlrs.malloc_fcn = php_xml_malloc_wrapper;
php_xml_mem_hdlrs.realloc_fcn = php_xml_realloc_wrapper;
php_xml_mem_hdlrs.free_fcn = php_xml_free_wrapper;
return SUCCESS;
}
@ -1032,7 +1052,7 @@ PHP_FUNCTION(xml_parser_create)
}
parser = ecalloc(sizeof(xml_parser), 1);
parser->parser = XML_ParserCreate(encoding);
parser->parser = XML_ParserCreate_MM(encoding, &php_xml_mem_hdlrs, NULL);
parser->target_encoding = encoding;
parser->case_folding = 1;
parser->object = NULL;
@ -1088,7 +1108,7 @@ PHP_FUNCTION(xml_parser_create_ns)
}
parser = ecalloc(sizeof(xml_parser), 1);
parser->parser = XML_ParserCreateNS(encoding, sep[0]);
parser->parser = XML_ParserCreate_MM(encoding, &php_xml_mem_hdlrs, sep);
parser->target_encoding = encoding;
parser->case_folding = 1;
parser->object = NULL;