Fun API Documentation 0.42.1
The programming language that makes you have fun!
Loading...
Searching...
No Matches
xml2.c File Reference

libxml2 handle registries and helper utilities for the Fun VM extension. More...

#include <libxml/parser.h>
#include <libxml/tree.h>
Include dependency graph for xml2.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  XmlDocSlot
 Slot describing a registered XML document. More...
struct  XmlNodeSlot
 Slot describing a registered XML node. More...

Functions

static int xml_doc_alloc (xmlDocPtr d)
 Allocate a document handle for the given xmlDoc pointer.
static xmlDocPtr xml_doc_get (int h)
 Retrieve a registered xmlDoc by handle.
static int xml_doc_free_handle (int h)
 Free a document handle and the underlying xmlDoc.
static int xml_node_alloc (xmlNodePtr n)
 Allocate a node handle for the given xmlNode pointer.
static xmlNodePtr xml_node_get (int h)
 Retrieve a registered xmlNode by handle.
static int xml_node_free_handle (int h)
 Free a node handle without freeing the underlying node.

Variables

static XmlDocSlot g_xml_docs [64]
 Fixed-size registry storage for documents.
static XmlNodeSlot g_xml_nodes [256]
 Fixed-size registry storage for nodes.

Detailed Description

libxml2 handle registries and helper utilities for the Fun VM extension.

Overview

This translation unit implements compact, fixed-size registries that assign small positive integer handles to libxml2 objects. VM opcodes under src/vm/xml2/*.c and higher-level helpers can exchange these integer handles across the VM stack without exposing raw pointers.

Build-time feature flag

Code in this file is compiled only when the CMake option FUN_WITH_XML2 is enabled (i.e., the preprocessor symbol FUN_WITH_XML2 is defined). When disabled, this file contributes no symbols and the corresponding VM opcodes should be compiled out or provide a graceful fallback.

Ownership and lifetime

  • Document registry (XmlDocSlot) TAKES OWNERSHIP of the registered xmlDocPtr. Releasing the document handle will call xmlFreeDoc() for the stored pointer.
  • Node registry (XmlNodeSlot) DOES NOT take ownership of xmlNodePtr values. Nodes are owned by their document. Releasing a node handle only clears the registry slot; it does not free the underlying xmlNode memory.
  • When a document is freed, the libxml2 tree below it is destroyed by libxml2, thereby invalidating any node pointers previously returned from that document. The small node handle registry in this file does not track which document owns which node. Callers must ensure they do not use node handles after their owning document has been released; they should clear those node handles explicitly if necessary.

Limits and handle ranges

  • Document handles are allocated from a fixed-size array of 64 slots. Valid handles are in the inclusive range [1, 63].
  • Node handles are allocated from a fixed-size array of 256 slots. Valid handles are in the inclusive range [1, 255].
  • The value 0 is reserved and indicates failure or an invalid handle.

Error handling

  • Allocation requests fail with a return value of 0 when no free slot is available.
  • Lookups return NULL for invalid or unused handles.
  • Free functions return 1 when a valid in-use handle was released, and 0 otherwise. Document release also frees the underlying xmlDoc via xmlFreeDoc(). Node release does not free the xmlNode memory.

Thread-safety

  • The registries are simple, unsynchronized arrays. They are NOT thread-safe. If the VM uses libxml2 handles across multiple threads, external synchronization is required around all calls into this module.

Example

// Assume xmlInitParser() was called by the embedding application.
xmlDocPtr d = xmlReadMemory("<root><x/></root>", 20, "-", NULL, 0);
int dh = xml_doc_alloc(d); // takes ownership of d
xmlDocPtr same = xml_doc_get(dh);
// ... use 'same' with libxml2 APIs ...
// When done, free the document handle; this calls xmlFreeDoc(same).
static int xml_doc_free_handle(int h)
Free a document handle and the underlying xmlDoc.
Definition xml2.c:171
static int xml_doc_alloc(xmlDocPtr d)
Allocate a document handle for the given xmlDoc pointer.
Definition xml2.c:133
static xmlDocPtr xml_doc_get(int h)
Retrieve a registered xmlDoc by handle.
Definition xml2.c:152

Definition in file xml2.c.

Function Documentation

◆ xml_doc_alloc()

int xml_doc_alloc ( xmlDocPtr d)
static

Allocate a document handle for the given xmlDoc pointer.

Allocates a free slot in the document registry and stores the supplied xmlDocPtr. Ownership is transferred to the registry; releasing the handle will call xmlFreeDoc() for the stored pointer.

Parameters
dValid xmlDocPtr to register. The caller must not free the document directly after a successful registration.
Returns
Positive handle in the range [1, 63] on success; 0 if no slot is available.
Note
Passing NULL is undefined behavior for this helper in the sense that it will simply not find a slot and return 0 if no slot is free; callers should validate inputs before calling.

Definition at line 133 of file xml2.c.

◆ xml_doc_free_handle()

int xml_doc_free_handle ( int h)
static

Free a document handle and the underlying xmlDoc.

If the handle is valid and in use, calls xmlFreeDoc() for the stored document pointer, clears the slot, and returns 1.

Parameters
hHandle to release.
Returns
1 if the handle was valid and has been released; 0 otherwise.
Note
Freeing a document invalidates any xmlNodePtr previously obtained from that document. The node registry in this file does not automatically clear entries that reference nodes from the freed document; callers are responsible for avoiding use-after-free by releasing or ignoring such node handles.

Definition at line 171 of file xml2.c.

◆ xml_doc_get()

xmlDocPtr xml_doc_get ( int h)
static

Retrieve a registered xmlDoc by handle.

Performs bounds and state checks on the registry and returns the stored xmlDocPtr for the handle if present.

Parameters
hHandle previously returned by xml_doc_alloc().
Returns
xmlDocPtr if the handle is valid and in use; NULL otherwise.

Definition at line 152 of file xml2.c.

◆ xml_node_alloc()

int xml_node_alloc ( xmlNodePtr n)
static

Allocate a node handle for the given xmlNode pointer.

Allocates a free slot in the node registry and stores the supplied pointer. Ownership is not transferred; the memory is managed by the owning document.

Parameters
nValid xmlNodePtr to register. Must remain valid for as long as the handle is in use and the owning document is alive.
Returns
Positive handle in the range [1, 255] on success; 0 if no slot is available.

Definition at line 190 of file xml2.c.

◆ xml_node_free_handle()

int xml_node_free_handle ( int h)
static

Free a node handle without freeing the underlying node.

Clears the registry slot associated with the given handle. The underlying xmlNode memory is not freed because nodes are owned by their document.

Parameters
hHandle to release.
Returns
1 if the handle was valid and has been released; 0 otherwise.
Note
Releasing or freeing a document invalidates any nodes originating from that document. Callers should avoid using node handles after their document has been released.

Definition at line 226 of file xml2.c.

◆ xml_node_get()

xmlNodePtr xml_node_get ( int h)
static

Retrieve a registered xmlNode by handle.

Performs bounds and state checks on the registry and returns the stored xmlNodePtr for the handle if present.

Parameters
hHandle previously returned by xml_node_alloc().
Returns
xmlNodePtr if the handle is valid and in use; NULL otherwise.

Definition at line 209 of file xml2.c.

Variable Documentation

◆ g_xml_docs

XmlDocSlot g_xml_docs[64]
static

Fixed-size registry storage for documents.

Index 0 is reserved. Valid document handle indices are in [1, 63].

Definition at line 109 of file xml2.c.

◆ g_xml_nodes

XmlNodeSlot g_xml_nodes[256]
static

Fixed-size registry storage for nodes.

Index 0 is reserved. Valid node handle indices are in [1, 255].

Definition at line 115 of file xml2.c.