Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
xml2.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file xml2.c
12 * @brief Lightweight libxml2 handle registry for Fun VM extension helpers.
13 *
14 * This module provides small fixed-size registries for libxml2 objects when
15 * compiled with FUN_WITH_XML2. Documents and nodes can be associated with
16 * small integer handles to make them easier to pass around inside the
17 * interpreter and its C API boundaries. The document registry owns the
18 * underlying xmlDoc, while the node registry does not own the xmlNode — nodes
19 * are freed when their owning document is freed.
20 *
21 * Limits: The registries are fixed-size (docs: 64, nodes: 256). Handle value 0
22 * is reserved and indicates failure/invalid.
23 *
24 * Thread-safety: Not thread-safe. Coordinate access externally if needed.
25 */
26#ifdef FUN_WITH_XML2
27#include <libxml/parser.h>
28#include <libxml/tree.h>
29
30/**
31 * @brief Slot describing a registered XML document.
32 *
33 * The registry owns the xmlDocPtr and will free it when the handle is
34 * released via xml_doc_free_handle().
35 */
36typedef struct {
37 xmlDocPtr doc;
38 int in_use;
40/**
41 * @brief Slot describing a registered XML node.
42 *
43 * The registry does not own the node; it is managed by its document. Releasing
44 * a node handle does not free the node memory.
45 */
46typedef struct {
47 xmlNodePtr node;
48 int in_use;
50
53
54/**
55 * @brief Allocate a document handle for the given xmlDoc pointer.
56 *
57 * @param d Valid xmlDocPtr to register. Ownership is transferred to the
58 * registry, which will xmlFreeDoc() it when the handle is freed.
59 * @return Positive handle in the range [1, 63] on success; 0 if no slot is
60 * available.
61 */
62static int xml_doc_alloc(xmlDocPtr d) {
63 for (int i = 1; i < (int)(sizeof(g_xml_docs) / sizeof(g_xml_docs[0])); ++i) {
64 if (!g_xml_docs[i].in_use) {
65 g_xml_docs[i].in_use = 1;
66 g_xml_docs[i].doc = d;
67 return i;
68 }
69 }
70 return 0;
71}
72/**
73 * @brief Retrieve a registered xmlDoc by handle.
74 *
75 * @param h Handle previously returned by xml_doc_alloc().
76 * @return xmlDocPtr if the handle is valid and in use; NULL otherwise.
77 */
78static xmlDocPtr xml_doc_get(int h) {
79 if (h > 0 && h < (int)(sizeof(g_xml_docs) / sizeof(g_xml_docs[0])) && g_xml_docs[h].in_use) return g_xml_docs[h].doc;
80 return NULL;
81}
82/**
83 * @brief Free a document handle and the underlying xmlDoc.
84 *
85 * @param h Handle to release.
86 * @return 1 if the handle was valid and has been released; 0 otherwise.
87 */
88static int xml_doc_free_handle(int h) {
89 if (h <= 0 || h >= (int)(sizeof(g_xml_docs) / sizeof(g_xml_docs[0])) || !g_xml_docs[h].in_use) return 0;
90 if (g_xml_docs[h].doc) xmlFreeDoc(g_xml_docs[h].doc);
91 g_xml_docs[h].doc = NULL;
92 g_xml_docs[h].in_use = 0;
93 return 1;
94}
95
96/**
97 * @brief Allocate a node handle for the given xmlNode pointer.
98 *
99 * @param n Valid xmlNodePtr to register. Ownership is NOT transferred;
100 * nodes are managed by their owning document.
101 * @return Positive handle in the range [1, 255] on success; 0 if no slot is
102 * available.
103 */
104static int xml_node_alloc(xmlNodePtr n) {
105 for (int i = 1; i < (int)(sizeof(g_xml_nodes) / sizeof(g_xml_nodes[0])); ++i) {
106 if (!g_xml_nodes[i].in_use) {
107 g_xml_nodes[i].in_use = 1;
108 g_xml_nodes[i].node = n;
109 return i;
110 }
111 }
112 return 0;
113}
114/**
115 * @brief Retrieve a registered xmlNode by handle.
116 *
117 * @param h Handle previously returned by xml_node_alloc().
118 * @return xmlNodePtr if the handle is valid and in use; NULL otherwise.
119 */
120static xmlNodePtr xml_node_get(int h) {
121 if (h > 0 && h < (int)(sizeof(g_xml_nodes) / sizeof(g_xml_nodes[0])) && g_xml_nodes[h].in_use) return g_xml_nodes[h].node;
122 return NULL;
123}
124/**
125 * @brief Free a node handle without freeing the underlying node.
126 *
127 * Nodes are owned by their document; releasing the document invalidates any
128 * associated node handles.
129 *
130 * @param h Handle to release.
131 * @return 1 if the handle was valid and has been released; 0 otherwise.
132 */
133static int xml_node_free_handle(int h) {
134 if (h <= 0 || h >= (int)(sizeof(g_xml_nodes) / sizeof(g_xml_nodes[0])) || !g_xml_nodes[h].in_use) return 0;
135 /* nodes are owned by their document; do not free here */
136 g_xml_nodes[h].node = NULL;
137 g_xml_nodes[h].in_use = 0;
138 return 1;
139}
140#endif
ce in_use
Definition disconnect.c:41
CURL * h
Definition download.c:59
dictionary * d
Definition get_bool.c:43
int n
Definition insert.c:41
Slot describing a registered XML document.
Definition xml2.c:36
xmlDocPtr doc
Definition xml2.c:37
int in_use
Definition xml2.c:38
Slot describing a registered XML node.
Definition xml2.c:46
int in_use
Definition xml2.c:48
xmlNodePtr node
Definition xml2.c:47
static int xml_node_alloc(xmlNodePtr n)
Allocate a node handle for the given xmlNode pointer.
Definition xml2.c:104
static int xml_doc_free_handle(int h)
Free a document handle and the underlying xmlDoc.
Definition xml2.c:88
static XmlDocSlot g_xml_docs[64]
Definition xml2.c:51
static xmlNodePtr xml_node_get(int h)
Retrieve a registered xmlNode by handle.
Definition xml2.c:120
static int xml_doc_alloc(xmlDocPtr d)
Allocate a document handle for the given xmlDoc pointer.
Definition xml2.c:62
static int xml_node_free_handle(int h)
Free a node handle without freeing the underlying node.
Definition xml2.c:133
static xmlDocPtr xml_doc_get(int h)
Retrieve a registered xmlDoc by handle.
Definition xml2.c:78
static XmlNodeSlot g_xml_nodes[256]
Definition xml2.c:52
xmlDocPtr doc
Definition parse.c:50