HTML Tidy 5.8.0
The HTACG Tidy HTML Project
 
Loading...
Searching...
No Matches
Memory Allocation

Detailed Description

Tidy can use a user-provided allocator for all memory allocations. If this allocator is not provided, then a default allocator is used which simply wraps standard C malloc()/free() calls. These wrappers call the panic() function upon any failure. The default panic function prints an out of memory message to stderr, and calls exit(2).

For applications in which it is unacceptable to abort in the case of memory allocation, then the panic function can be replaced with one which longjmps() out of the LibTidy code. For this to clean up completely, you should be careful not to use any Tidy methods that open files as these will not be closed before panic() is called.

Calling the xxxWithAllocator() family (tidyCreateWithAllocator, tidyBufInitWithAllocator, tidyBufAllocWithAllocator) allow setting custom allocators.

All parts of the document use the same allocator. Calls that require a user-provided buffer can optionally use a different allocator.

For reference in designing a plug-in allocator, most allocations made by LibTidy are less than 100 bytes, corresponding to attribute names and values, etc.

There is also an additional class of much larger allocations which are where most of the data from the lexer is stored. It is not currently possible to use a separate allocator for the lexer; this would be a useful extension.

In general, approximately 1/3rd of the memory used by LibTidy is freed during the parse, so if memory usage is an issue then an allocator that can reuse this memory is a good idea.

To create your own allocator, do something like the following:

typedef struct _MyAllocator {
TidyAllocator base;
// ...other custom allocator state...
} MyAllocator;
void* MyAllocator_alloc(TidyAllocator *base, void *block, size_t nBytes) {
MyAllocator *self = (MyAllocator*)base;
// ...
}
// etc.
static const TidyAllocatorVtbl MyAllocatorVtbl = {
MyAllocator_alloc,
MyAllocator_realloc,
MyAllocator_free,
MyAllocator_panic
};
myAllocator allocator;
TidyDoc doc;
allocator.base.vtbl = &MyAllocatorVtbl;
//...initialise allocator specific state...
doc = tidyCreateWithAllocator(&allocator);
TidyDoc TIDY_CALL tidyCreateWithAllocator(TidyAllocator *allocator)
Create a document supplying your own, custom TidyAllocator instead of using the built-in default.
Instances of this represent a Tidy document, which encapsulates everything there is to know about a s...

Although this looks slightly long-winded, the advantage is that to create a custom allocator you simply need to set the vtbl pointer correctly. The vtbl itself can reside in static/global data, and hence does not need to be initialised each time an allocator is created, and furthermore the memory is shared amongst all created allocators.

Data Structures

struct  TidyAllocator
 Tidy's built-in default allocator. More...
 
struct  TidyAllocatorVtbl
 This structure is the function table for an allocator. More...
 

Typedefs

typedef void(TIDY_CALLTidyFree) (void *buf)
 Callback for free replacement.
 
typedef void *(TIDY_CALLTidyMalloc) (size_t len)
 Callback for malloc replacement.
 
typedef void(TIDY_CALLTidyPanic) (ctmbstr mssg)
 Callback for out of memory panic state.
 
typedef void *(TIDY_CALLTidyRealloc) (void *buf, size_t len)
 Callback for realloc replacement.
 

Functions

Bool TIDY_CALL tidySetFreeCall (TidyFree ffree)
 Give Tidy a free() replacement.
 
Bool TIDY_CALL tidySetMallocCall (TidyMalloc fmalloc)
 Give Tidy a malloc() replacement.
 
Bool TIDY_CALL tidySetPanicCall (TidyPanic fpanic)
 Give Tidy an "out of memory" handler.
 
Bool TIDY_CALL tidySetReallocCall (TidyRealloc frealloc)
 Give Tidy a realloc() replacement.
 

Data Structure Documentation

◆ _TidyAllocator

struct _TidyAllocator

◆ _TidyAllocatorVtbl

struct _TidyAllocatorVtbl

Typedef Documentation

◆ TidyFree

typedef void(TIDY_CALL * TidyFree) (void *buf)

Callback for free replacement.

◆ TidyMalloc

typedef void *(TIDY_CALL * TidyMalloc) (size_t len)

Callback for malloc replacement.

◆ TidyPanic

typedef void(TIDY_CALL * TidyPanic) (ctmbstr mssg)

Callback for out of memory panic state.

◆ TidyRealloc

typedef void *(TIDY_CALL * TidyRealloc) (void *buf, size_t len)

Callback for realloc replacement.

Function Documentation

◆ tidySetFreeCall()

Bool TIDY_CALL tidySetFreeCall ( TidyFree ffree)

Give Tidy a free() replacement.

◆ tidySetMallocCall()

Bool TIDY_CALL tidySetMallocCall ( TidyMalloc fmalloc)

Give Tidy a malloc() replacement.

◆ tidySetPanicCall()

Bool TIDY_CALL tidySetPanicCall ( TidyPanic fpanic)

Give Tidy an "out of memory" handler.

◆ tidySetReallocCall()

Bool TIDY_CALL tidySetReallocCall ( TidyRealloc frealloc)

Give Tidy a realloc() replacement.