I tried to use xmlMemSetup and libxml2 gives me an error.
Probably I'm missing something.
This is a simple main I wrote:

#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/encoding.h>
#include <stdlib.h>

const unsigned int mgc = 0x88DEFEA7;

static void *xml2_alloc(size_t sz)
{
   void *mem = malloc(sz + sizeof(int));
   const unsigned int *mark = mem;
   *mark = mgc;
   return (char *) mem + sizeof(int);
}
  static void xml2_free(void *emem)
{
   void *mem = (char *)emem - sizeof(int);
   const unsigned int *mark = mem;
   if (*mark == mgc) {
       free(mem);
   }
}

static void *xml2_realloc(void * emem, size_t sz)
{
   void *mem = (char *)emem - sizeof(int);
   void *mem_re = xmlMemRealloc(mem, sz);
   if (mem_re != NULL)
       return (char *) mem_re + sizeof(int);
   else
       return NULL;
}

xmlDocPtr
parseDoc(char *xmlfile)
{
   xmlDocPtr doc;
   xmlNodePtr root;

   doc = xmlParseFile(xmlfile);

   if (doc == NULL ) {
       fprintf(stderr,"Document not parsed successfully. \n");
       xmlFreeDoc(doc);
       return (NULL);
   }

   root = xmlDocGetRootElement(doc);

   if (root == NULL) {
       fprintf(stderr,"empty document\n");
       xmlFreeDoc(doc);
       return (NULL);
   }

   return doc;

}

int
main(int argc, char **argv) {
   xmlDocPtr doc;
   xmlChar *bufptr;
   int size = 0;
   char *xmlfile = "/usr/local/apache/conf/conf_prova.xml";
     xmlMemSetup(xml2_free, xml2_alloc, xml2_realloc, xmlMemoryStrdup);
   xmlInitParser();
     doc = parseDoc (xmlfile);
     if (doc == NULL) {
       fprintf(stderr,"[ERRORE] ParseDoc ha ritornato NULL\n");
       return -1;
   }
     fprintf(stderr,"[OK] ParseDoc Ok\n");
     xmlFreeDoc(doc);
   xmlCleanupParser();

   return 0;
}

The xml document is very simple:

<?xml version="1.0" encoding="ISO-8859-1"?>
<CONFIGURAZIONE>
       <Q>3</Q>
       <S>
               <H>e</H>
               <P>80</P>
               <L>p.l</L>
               <PAR>
                       <AD>on</AD>
                       <S>off</S>
                       <C>r</C>
                       <N>0</N>
                       <W>1</W>
               </PAR>
       </S>
       .....
</CONFIGURAZIONE>

this is the error I get:


[EMAIL PROTECTED] test]$ ./main
xmlMallocBreakpoint reached on block 0
Memory tag error occurs :0x805c140
        bye
Memory tag error occurs :0x805c158
        bye
/usr/local/apache/conf/conf_prova.xml:1: parser error : Memory 
allocation failed
<?xml version="1.0" encoding="ISO-8859-1"?>
                                      ^
Document not parsed successfully.
[ERRORE] ParseDoc ha ritornato NULL

What am I missing?

Marco



Daniel Veillard ha scritto:
> On Tue, Aug 07, 2007 at 12:09:05PM +0200, Marco Spinetti wrote:
>   
>> Reading the docs, I don't understand the difference between xmlMemSetup 
>> and xmlGcMemSetup.
>> I need to overwrite libxml2 memory allocation and I don't understand if 
>> I have to use xmlGcMemSetup or xmlMemSetup.
>> Any hints?
>>     
>
>   http://xmlsoft.org/html/libxml-xmlmemory.html#xmlGcMemSetup
> the later has an extra function allowing a special function for
> allocation of atomic area (i.e. areas which will never be grown).
> In doubt use xmlMemSetup()
>
>   
>> Other question: is xmlInitMemory(9 should be called before use 
>> xmlGcMemSetup or xmlMemSetup?
>>     
>
>   no, xmlMemSetup must be the very first call to libxml2 then call
> xmlInitParser(). Don't call xmlInitMemory by yourself, xmlInitParser
> will do it.
>
> Daniel
>
>   

_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml

Reply via email to