Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
AllocatorIntf.h File Reference
#include <TargConfig.h>
#include <stddef.h>
Include dependency graph for AllocatorIntf.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  AllocatorIntf
 Memory allocation and deallocation Interface class. More...
 

Macros

#define AllocatorIntf_constructor(o, m, r, f)
 Install allocator callbacks without allocating any memory. More...
 
#define AllocatorIntf_malloc(o, size)   (o)->mallocCB(o, size)
 Dispatch an allocation request. More...
 
#define AllocatorIntf_realloc(o, memblock, size)    ((o)->reallocCB ? (o)->reallocCB(o,memblock,size) : 0)
 Dispatch a resize request. More...
 
#define AllocatorIntf_free(o, memblock)   (o)->freeCB(o,memblock)
 Dispatch a deallocation request. More...
 

Typedefs

typedef void *(* AllocatorIntf_Malloc) (struct AllocatorIntf *o, size_t *size)
 Allocate a block using an allocator implementation. More...
 
typedef void *(* AllocatorIntf_Realloc) (struct AllocatorIntf *o, void *memblock, size_t *size)
 Resize a block using the same allocator that allocated it. More...
 
typedef void(* AllocatorIntf_Free) (struct AllocatorIntf *o, void *memblock)
 Release a block. More...
 
typedef struct AllocatorIntf AllocatorIntf
 Memory allocation and deallocation Interface class. More...
 

Functions

BA_API AllocatorIntfAllocatorIntf_getDefault (void)
 
BA_API char * baStrdup2 (struct AllocatorIntf *a, const char *str)
 Allocate a NUL-terminated copy of a string. More...
 

Macro Definition Documentation

◆ AllocatorIntf_constructor

#define AllocatorIntf_constructor (   o,
  m,
  r,
 
)
Value:
do { \
(o)->mallocCB=m; \
(o)->reallocCB=r; \
(o)->freeCB=f; \
} while(0)

Install allocator callbacks without allocating any memory.

Parameters
[out]oCaller-owned allocator object.
[in]mRequired allocation callback.
[in]rOptional resize callback; NULL disables resizing.
[in]fRequired deallocation callback.

◆ AllocatorIntf_free

#define AllocatorIntf_free (   o,
  memblock 
)    (o)->freeCB(o,memblock)

Dispatch a deallocation request.

Parameters
[in,out]oInitialized allocator.
[in]memblockBlock to release; see AllocatorIntf_Free.

◆ AllocatorIntf_malloc

#define AllocatorIntf_malloc (   o,
  size 
)    (o)->mallocCB(o, size)

Dispatch an allocation request.

Parameters
[in,out]oInitialized allocator.
[in,out]sizeRequested byte count; see AllocatorIntf_Malloc.
Returns
New storage or NULL on failure.

◆ AllocatorIntf_realloc

#define AllocatorIntf_realloc (   o,
  memblock,
  size 
)     ((o)->reallocCB ? (o)->reallocCB(o,memblock,size) : 0)

Dispatch a resize request.

Parameters
[in,out]oInitialized allocator.
[in]memblockExisting block or NULL; see AllocatorIntf_Realloc.
[in,out]sizeRequested byte count; see AllocatorIntf_Realloc.
Returns
Replacement block or NULL. An absent callback returns NULL and leaves the original block and size unchanged.

Typedef Documentation

◆ AllocatorIntf

typedef struct AllocatorIntf AllocatorIntf

Memory allocation and deallocation Interface class.

This abstract interface class is used by some of the Barracuda classes when allocating memory. The reason for using an interface class and not directly calling the global functions baMalloc(), baRealloc() and baFree() is to provide a finer control of allocated memory. For example, an implementation of the AllocatorIntf can work with blocks of memory allocated from static memory. Implementing realloc is optional and can be set to NULL if not implemented.

◆ AllocatorIntf_Free

typedef void(* AllocatorIntf_Free) (struct AllocatorIntf *o, void *memblock)

Release a block.

Parameters
[in,out]oAllocator instance supplied to the callback.
[in]memblockLive block obtained from this allocator. NULL handling depends on the implementation; FixedSizeAllocator requires a non-NULL block. The pointer must not be used after the callback returns.

◆ AllocatorIntf_Malloc

typedef void *(* AllocatorIntf_Malloc) (struct AllocatorIntf *o, size_t *size)

Allocate a block using an allocator implementation.

Parameters
[in,out]oAllocator instance supplied to the callback.
[in,out]sizeRequired pointer to the requested byte count. On success the implementation may increase it to the actual allocation size. Its value on failure, and the handling of a zero request, depend on the implementation.
Returns
Uninitialized storage of at least the requested size, or NULL on failure. Release successful allocations with the same allocator's free callback. The allocator object must outlive its allocated blocks.

◆ AllocatorIntf_Realloc

typedef void *(* AllocatorIntf_Realloc) (struct AllocatorIntf *o, void *memblock, size_t *size)

Resize a block using the same allocator that allocated it.

Parameters
[in,out]oAllocator instance supplied to the callback.
[in]memblockExisting allocation, or NULL to request a new block.
[in,out]sizeRequired requested byte count. On success it may be increased to the actual size. Use a positive size for portable behavior; zero-size behavior depends on the allocator implementation.
Returns
Replacement block preserving the smaller of the old and new byte counts, or NULL on failure. For a positive request, failure leaves memblock owned by the caller; success invalidates the old pointer. New bytes are not initialized. This callback is optional in AllocatorIntf.

Function Documentation

◆ AllocatorIntf_getDefault()

BA_API AllocatorIntf * AllocatorIntf_getDefault ( void  )
Returns
Borrowed process-lifetime allocator backed by baMalloc, baRealloc and baFree. Do not free it or change its callbacks.

◆ baStrdup2()

BA_API char * baStrdup2 ( struct AllocatorIntf a,
const char *  str 
)

Allocate a NUL-terminated copy of a string.

Parameters
[in,out]aRequired initialized allocator, used only when str is non-NULL.
[in]strNUL-terminated source string, or NULL. The source is not modified.
Returns
Independent copy, or NULL when str is NULL or allocation fails. Release a successful result through a. The allocation includes the NUL byte.