cu/memory.h: Memory Allocation
[Basics]

Supplementary Definitions



typedef struct cu_hidden_ptr * cu_hidden_ptr_t
#define CU_GWIPE(ptr_lvalue)   ((ptr_lvalue) = NULL)
#define CU_GCLEAR_INT(lvalue)   (0? (void)((lvalue) = 0) : (void)0)
#define CU_GCLEAR_PTR(lvalue)   (0? (void)((lvalue) = NULL) : (void)0)
#define cu_hide_ptr(ptr)   ((cu_hidden_ptr_t)~(cu_uintptr_t)(ptr))
#define cu_reveal_ptr(hptr)   ((void*)~(cu_uintptr_t)CU_MARG(cu_hidden_ptr_t, hptr))
#define cu_gc_base   GC_base
#define cu_gc_register_finaliser   GC_register_finalizer
#define cu_gc_register_finaliser_no_order   GC_register_finalizer_no_order
#define cu_gc_ptr_assign(p, q)   (*(p) = (q))

Dynamic Memory Allocation

Most of these functions call the Boehm-Demers-Weiser conservative garbage collector to do the real work, but also asserts that the result is non-NULL.



void * cu_galloc (size_t size)
void * cu_galloc_atomic (size_t size)
void * cu_gallocz (size_t size)
void * cu_gallocz_atomic (size_t size)
void * cu_ualloc (size_t size)
void * cu_ualloc_atomic (size_t size)
void * cu_uallocz (size_t size)
void * cu_uallocz_atomic (size_t size)
#define cu_gfree   GC_free
#define cu_gfree_atomic   cu_gfree
#define cu_ufree   cu_gfree
#define cu_ufree_atomic   free

Stack Allocation



#define cu_salloc(size)   (alloca(size))
#define cu_sfree(p)   ((void)(p))

Typed Memory Allocation Macros

These are straight-forward convenience macros which passes the size of the given type to the corresponding allocation functions, and cast the result to the given type.



#define cu_snew(type, n)   ((type *)cu_salloc(sizeof(type)))
#define cu_gnew(type)   ((type*)cu_galloc(sizeof(type)))
#define cu_gnew_atomic(type)   ((type *)cu_galloc_atomic(sizeof(type)))
#define cu_unew(type)   ((type *)cu_ualloc(sizeof(type)))
#define cu_unew_atomic(type)   ((type *)cu_ualloc_atomic(sizeof(type)))
#define cu_gnewz(type)   ((type *)cu_gallocz(sizeof(type)))
#define cu_gnewz_atomic(type)   ((type *)cu_gallocz_atomic(sizeof(type)))
#define cu_unewz(type)   ((type *)cu_uallocz(sizeof(type)))
#define cu_unewz_atomic(type)   ((type *)cu_uallocz_atomic(sizeof(type)))
#define cu_snewarr(type, n)   ((type *)cu_salloc(sizeof(type)*(n)))
#define cu_gnewarr(type, n)   ((type *)cu_galloc(sizeof(type)*(n)))
#define cu_gnewarr_atomic(type, n)   ((type *)cu_galloc_atomic(sizeof(type)*(n)))
#define cu_unewarr(type, n)   ((type *)cu_ualloc(sizeof(type)*(n)))
#define cu_unewarr_atomic(type, n)   ((type *)cu_ualloc_atomic(sizeof(type)*(n)))
#define cu_gnewarrz(type, n)   ((type *)cu_gallocz(sizeof(type)*(n)))
#define cu_gnewarrz_atomic(type, n)   ((type *)cu_gallocz_atomic(sizeof(type)*(n)))
#define cu_unewarrz(type, n)   ((type *)cu_uallocz(sizeof(type)*(n)))
#define cu_unewarrz_atomic(type, n)   ((type *)cu_uallocz_atomic(sizeof(type)*(n)))

Define Documentation

#define cu_gfree   GC_free

Free traceable collectable memory (optional).

#define cu_gfree_atomic   cu_gfree

Free atomic collectable memory (optional).

#define cu_gnew ( type   )     ((type*)cu_galloc(sizeof(type)))

Shortcut to allocate an object of type type using cu_galloc.

#define cu_gnew_atomic ( type   )     ((type *)cu_galloc_atomic(sizeof(type)))

Shortcut to allocate an object of type type using cu_galloc_atomic.

#define CU_GWIPE ( ptr_lvalue   )     ((ptr_lvalue) = NULL)

Notify that the pointer ptr_lvalue in traceable memory is no longer needed. It will be left as a tuning option whether to zero the pointer or not. Clients may want to use their own analogous macro for local tuning.

#define cu_hide_ptr ( ptr   )     ((cu_hidden_ptr_t)~(cu_uintptr_t)(ptr))

Hide a pointer from the garbage collector.

#define cu_reveal_ptr ( hptr   )     ((void*)~(cu_uintptr_t)CU_MARG(cu_hidden_ptr_t, hptr))

Reveal a pointer that was hidden with cu_hide_ptr. If you don't know how determine if the pointer is still valid, don't use this.

#define cu_salloc ( size   )     (alloca(size))

Allocate memory on the stack. (This is just an alias for alloca used in the library for quick replacement when debugging stack-related issues.)

#define cu_sfree (  )     ((void)(p))

The counterpart of cu_salloc, which is a noop.

#define cu_snew ( type,
 )     ((type *)cu_salloc(sizeof(type)))

Shortcut to allocate an object of type type using cu_salloc.

#define cu_ufree   cu_gfree

Free traceable uncollectable memory.

#define cu_ufree_atomic   free

Free atomic uncollectable memory.

#define cu_unew ( type   )     ((type *)cu_ualloc(sizeof(type)))

Shortcut to allocate an object of type type using cu_ualloc.

#define cu_unew_atomic ( type   )     ((type *)cu_ualloc_atomic(sizeof(type)))

Shortcut to allocate an object of type type using cu_ualloc_atomic.


Typedef Documentation

typedef struct cu_hidden_ptr* cu_hidden_ptr_t

A type which represents a pointer that is hidden to the garbage collector.


Function Documentation

void* cu_galloc ( size_t  size  ) 

Returns size bytes of traced an collectable memory. GC_malloc with a check for NULL.

void* cu_galloc_atomic ( size_t  size  ) 

Allocate a memory region of size bytes of untraced but collectable memory. GC_malloc_atomic with a check for NULL.

void* cu_gallocz ( size_t  size  ) 

Allocate cleared traceable collectable memory.

void* cu_gallocz_atomic ( size_t  size  ) 

Allocate cleared atomic collectable memory.

void* cu_ualloc ( size_t  size  ) 

Returns size bytes of traced but uncollectable memory. GC_malloc_uncollectable with a check for NULL.

void* cu_ualloc_atomic ( size_t  size  ) 

Returns size bytes of untraced and uncollectable memory. May be implemented as GC_malloc_atomic_uncollectable, if available or malloc, so use cu_ufree_atomic to release it.

void* cu_uallocz ( size_t  size  ) 

Allocate cleared traceable uncollectable memory.

void* cu_uallocz_atomic ( size_t  size  ) 

Allocate cleared atomic uncollectable memory.

Generated 2009-11-23 for culibs-0.25 using Doxygen. Maintained by Petter Urkedal.