xref: /netbsd-src/external/mpl/bind/dist/lib/isc/xml.c (revision 9689912e6b171cbda866ec33f15ae94a04e2c02d)
1 /*	$NetBSD: xml.c,v 1.1.1.1 2025/01/26 16:12:30 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 #include <isc/mem.h>
17 #include <isc/util.h>
18 #include <isc/xml.h>
19 
20 #ifdef HAVE_LIBXML2
21 #include <libxml/parser.h>
22 #include <libxml/xmlversion.h>
23 
24 static isc_mem_t *isc__xml_mctx = NULL;
25 
26 static void *
27 isc__xml_malloc(size_t size) {
28 	return isc_mem_allocate(isc__xml_mctx, size);
29 }
30 
31 static void *
32 isc__xml_realloc(void *ptr, size_t size) {
33 	return isc_mem_reallocate(isc__xml_mctx, ptr, size);
34 }
35 
36 static char *
37 isc__xml_strdup(const char *str) {
38 	return isc_mem_strdup(isc__xml_mctx, str);
39 }
40 
41 static void
42 isc__xml_free(void *ptr) {
43 	if (ptr == NULL) {
44 		return;
45 	}
46 	isc_mem_free(isc__xml_mctx, ptr);
47 }
48 
49 #endif /* HAVE_LIBXML2 */
50 
51 void
52 isc__xml_initialize(void) {
53 #ifdef HAVE_LIBXML2
54 	isc_mem_create(&isc__xml_mctx);
55 	isc_mem_setname(isc__xml_mctx, "libxml2");
56 	isc_mem_setdestroycheck(isc__xml_mctx, false);
57 
58 	RUNTIME_CHECK(xmlMemSetup(isc__xml_free, isc__xml_malloc,
59 				  isc__xml_realloc, isc__xml_strdup) == 0);
60 
61 	xmlInitParser();
62 #endif /* HAVE_LIBXML2 */
63 }
64 
65 void
66 isc__xml_shutdown(void) {
67 #ifdef HAVE_LIBXML2
68 	xmlCleanupParser();
69 	isc_mem_destroy(&isc__xml_mctx);
70 #endif /* HAVE_LIBXML2 */
71 }
72 
73 void
74 isc__xml_setdestroycheck(bool check) {
75 #if HAVE_LIBXML2
76 	isc_mem_setdestroycheck(isc__xml_mctx, check);
77 #else
78 	UNUSED(check);
79 #endif
80 }
81