HTML Tidy  4.9.32
The HTACG Tidy HTML Project
Memory Allocation

Tidy uses a user provided allocator for all memory allocations. More...

Data Structures

struct  _TidyAllocator
 An allocator. More...
struct  _TidyAllocatorVtbl
 An allocator's function table. More...


typedef struct _TidyAllocator TidyAllocator
 The allocator. More...
typedef struct _TidyAllocatorVtbl TidyAllocatorVtbl
 The allocators function table. More...
typedef void(TIDY_CALL * TidyFree) (void *buf)
 Callback for "free" replacement. More...
typedef void *(TIDY_CALL * TidyMalloc) (size_t len)
 Callback for "malloc" replacement. More...
typedef void(TIDY_CALL * TidyPanic) (ctmbstr mssg)
 Callback for "out of memory" panic state. More...
typedef void *(TIDY_CALL * TidyRealloc) (void *buf, size_t len)
 Callback for "realloc" replacement. More...


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

Detailed Description

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 tidy 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.

TODO: associate file handles with tidyDoc and ensure that tidyDocRelease() can close them all.

Calling the withAllocator() family ( tidyCreateWithAllocator, tidyBufInitWithAllocator, tidyBufAllocWithAllocator) allow settings 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 tidy are less than 100 bytes, corresponding to attribute names/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 tidy is freed during the parse, so if memory usage is an issue then an allocator that can reuse this memory is a good idea.

Data Structure Documentation

struct _TidyAllocator

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

typedef struct _MyAllocator {
...other custom allocator state...
} MyAllocator;
void* MyAllocator_alloc(TidyAllocator *base, void *block, size_t nBytes)
MyAllocator *self = (MyAllocator*)base;
static const TidyAllocatorVtbl MyAllocatorVtbl = {
myAllocator allocator;
TidyDoc doc;
allocator.base.vtbl = &MyAllocatorVtbl;
...initialise allocator specific state...
doc = tidyCreateWithAllocator(&allocator);

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.

Definition at line 224 of file tidy.h.

Data Fields
const TidyAllocatorVtbl * vtbl
struct _TidyAllocatorVtbl

All functions here must be provided.

Definition at line 166 of file tidy.h.

Public Member Functions

void *TIDY_CALL * alloc (TidyAllocator *self, size_t nBytes)
 Called to allocate a block of nBytes of memory. More...
void *TIDY_CALL * realloc (TidyAllocator *self, void *block, size_t nBytes)
 Called to resize (grow, in general) a block of memory. More...
 void (TIDY_CALL *free)(TidyAllocator *self
 Called to free a previously allocated block of memory. More...
 void (TIDY_CALL *panic)(TidyAllocator *self
 Called when a panic condition is detected. More...

Data Fields

ctmbstr msg

Member Function Documentation

void* TIDY_CALL* _TidyAllocatorVtbl::alloc ( TidyAllocator self,
size_t  nBytes 
void* TIDY_CALL* _TidyAllocatorVtbl::realloc ( TidyAllocator self,
void block,
size_t  nBytes 

Must support being called with NULL.

_TidyAllocatorVtbl::void ( TIDY_CALL *  free)
_TidyAllocatorVtbl::void ( TIDY_CALL *  panic)

Must support block == NULL. This function is not called if either alloc or realloc fails; it is up to the allocator to do this. Currently this function can only be called if an error is detected in the tree integrity via the internal function CheckNodeIntegrity(). This is a situation that can only arise in the case of a programming error in tidylib. You can turn off node integrity checking by defining the constant NO_NODE_INTEGRITY_CHECK during the build.

Field Documentation

void* _TidyAllocatorVtbl::block

Definition at line 174 of file tidy.h.

ctmbstr _TidyAllocatorVtbl::msg

Definition at line 185 of file tidy.h.

Typedef Documentation

typedef struct _TidyAllocator TidyAllocator

Definition at line 161 of file tidy.h.

Definition at line 156 of file tidy.h.

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

Definition at line 233 of file tidy.h.

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

Definition at line 229 of file tidy.h.

typedef void(TIDY_CALL * TidyPanic) (ctmbstr mssg)

Definition at line 235 of file tidy.h.

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

Definition at line 231 of file tidy.h.

Function Documentation

Bool TIDY_CALL tidySetFreeCall ( TidyFree  ffree)
Bool TIDY_CALL tidySetMallocCall ( TidyMalloc  fmalloc)
Bool TIDY_CALL tidySetPanicCall ( TidyPanic  fpanic)
Bool TIDY_CALL tidySetReallocCall ( TidyRealloc  frealloc)