47 #ifndef AA_ALLOC_STACK_MAX
48 #define AA_ALLOC_STACK_MAX (4096-64)
50 #endif //AA_ALLOC_STACK_MAX
77 void *p = malloc(size);
78 if(p) memset(p,0,size);
85 if( NULL != ptr ) free(ptr);
89 #define AA_NEW_AR(type,n) ( (type*) malloc(sizeof(type)*(n)) )
92 #define AA_NEW0_AR(type,n) ( (type*) aa_malloc0(sizeof(type)*(n)) )
95 #define AA_NEW(type) AA_NEW_AR(type,1)
98 #define AA_NEW0(type) AA_NEW0_AR(type,1)
102 #define AA_ALIGN2( val, align ) (((val) + (align) - 1) & ~(align-1))
104 static inline void aa_checked_free(
void * ptr )
123 #define AA_ALLOCAL(size) ({ size_t aa_$_allocal_size = (size); \
124 aa_$_allocal_size > AA_ALLOC_STACK_MAX ? \
125 malloc(aa_$_allocal_size) : alloca(aa_$_allocal_size); })
131 #define AA_NEW_LOCAL(type, n) ( (type*) AA_ALLOCAL( sizeof(type)*(n) ) )
143 #define AA_DEL_LOCAL(ptr, type, n) aa_frlocal( ptr, sizeof(type)*(n) )
162 #define AA_MEMREG_ALIGN 16
307 #define AA_MEM_REGION_NEW( reg, type ) ( (type*) aa_mem_region_alloc((reg), sizeof(type)) )
309 #define AA_MEM_REGION_NEW_CPY( reg, src, type ) ( (type*) aa_mem_region_dup((reg), (src), sizeof(type)) )
310 #define AA_MEM_REGION_NEW_N( reg, type, n ) ( (type*) aa_mem_region_alloc((reg), (n)*sizeof(type)) )
312 #define AA_MEM_REGION_LOCAL_NEW( type ) ( (type*) aa_mem_region_local_alloc( sizeof(type)) )
313 #define AA_MEM_REGION_LOCAL_NEW_N( type, n ) ( (type*) aa_mem_region_local_alloc( (n)*sizeof(type)) )
423 #define AA_RLIST_DEF( element_type, list_type ) \
425 aa_mem_rlist_t rlist; \
427 static inline list_type* \
428 list_type ## _alloc( aa_mem_region *reg ) \
430 return (list_type*)aa_mem_rlist_alloc(reg); \
441 #define AA_MEM_CPY(dst, src, n_elem) \
444 memcpy( (dst), (src), sizeof((dst)[0])*(n_elem) ); \
449 static inline void *
aa_mem_dup(
const void *src,
size_t size )
451 void *dst = malloc(size);
452 memcpy(dst,src,size);
459 #define AA_MEM_DUP( type, src, count ) \
460 (type*)aa_mem_dup( (src), sizeof(type)*(count) );
462 #define AA_MEM_DUPOP( refop, type, const_dst, dst_data, src, n_elem ) \
465 dst_data = AA_MEM_DUP(type, src, n_elem); \
466 const_dst = dst_data; \
470 const_dst = dst_data; \
472 case AA_MEM_BORROW: \
484 #define AA_MEM_SET(dst, val, n_elem) \
486 for( size_t aa_$_set_i = 0; \
487 aa_$_set_i < (n_elem); \
489 (dst)[aa_$_set_i] = (val); \
496 #define AA_MEM_ZERO(dst, n_elem) (memset((dst),0,(n_elem)*sizeof(*(dst))))
499 #define AA_FAR(...) ((double[]){__VA_ARGS__})
503 static inline void aa_fcpy(
double *dst,
const double *src,
size_t n ) {
509 static inline void aa_fset(
double *dst,
double val,
size_t n ) {
515 static inline void aa_zero(
void *p,
size_t n ) {
521 static inline void aa_fzero(
double *p,
size_t n ) {
526 #define AA_ZERO_AR( var ) aa_zero( var, sizeof(var) )
529 #define AA_SET_AR( var, val ) \
530 for( size_t aa_$_set_ar_i = 0; \
531 aa_$_set_ar_i < sizeof(var)/sizeof(var[0]); \
532 aa_$_set_ar_i ++ ) var[aa_$_set_ar_i] = val;
543 #define AA_BITS_BITS (8*sizeof(aa_bits))
550 if( words*
sizeof(
aa_bits)*8 < n ) words++;
561 #define AA_BITS_JK( i, j, k ) \
562 j = (i) / AA_BITS_BITS; \
563 k = (i) - (j)*AA_BITS_BITS;
574 return (b[j] >> k) & 0x1;
601 b[j] &= ~ ( 0x1<<k );
613 a[n_words] &= b[n_words];
625 a[n_words] |= b[n_words];
637 a[n_words] ^= b[n_words];
646 typedef long aa_memswap_type;
653 aa_memswap_type *la = (aa_memswap_type*)a, *lb = (aa_memswap_type*)b;
654 for(
size_t i = 0; i < size/
sizeof(*la); i++ ) {
655 aa_memswap_type tmp = la[i];
660 uint8_t *ca = (uint8_t*)a, *cb = (uint8_t*)b;
661 for(
size_t i = size - size%
sizeof(*la); i < size; i ++ ) {
670 #define AA_MEM_SWAP( a, b, n_elem ) (aa_memswap( (a), (b), n_elem*sizeof(*(a)) ))
688 #define AA_VECTOR_PUSH( max, size, ptr, value ) \
689 if( (size) >= (max) ) { \
690 (max) = 2*(size+1); \
691 (ptr) = (__typeof__(ptr))realloc(ptr,sizeof(*ptr)*max); \
693 ptr[(size)++] = (value);
695 #define AA_VECTOR_DEF( element_type, vector_type ) \
699 element_type *data; \
701 static inline void vector_type ## _init \
702 ( vector_type *vec, size_t max ) { \
703 vec->data = (element_type*)malloc(max*sizeof(*vec->data)); \
705 vec->size = (size_t)0; \
707 static inline void vector_type ## _push \
708 ( vector_type *vec, element_type value ) { \
709 AA_VECTOR_PUSH(vec->max, vec->size, vec->data, value); \
uint8_t * head
pointer to first free element of top chunk
AA_API void * aa_mem_rlist_pop(struct aa_mem_rlist *list)
Remove front element of the list and return its data pointer.
AA_API unsigned aa_mem_ref_inc(unsigned *count)
Atomically increment the reference count and return the previous count.
uint64_t dalign
dummy element for alignment
A linked list allocated out of a memory region.
static int aa_bits_get(aa_bits *b, size_t i)
Return the value of the i'th bit in bitset b.
AA_API void * aa_mem_region_alloc(aa_mem_region_t *region, size_t size)
Allocate size bytes from the region.
static void aa_fcpy(double *dst, const double *src, size_t n) AA_DEPRECATED
copy n double floats from src to dst
static void aa_fset(double *dst, double val, size_t n) AA_DEPRECATED
set n double floats to val
AA_API void aa_mem_region_local_destroy(void)
Destroy the thread-local memory region.
aa_mem_region_t region
memory region to allocate from
static void * aa_malloc0(size_t size)
Malloc and zero initialize size bytes.
AA_API int aa_circbuf_write(aa_circbuf_t *cb, int fd, size_t n)
untested
AA_API void aa_mem_rlist_push_ptr(struct aa_mem_rlist *list, void *p)
Push the pointer p to the front of the list.
size_t size
size of each element
AA_API void aa_mem_region_init(aa_mem_region_t *region, size_t size)
Initialize memory region with an initial chunk of size bytes.
struct aa_mem_region_node * node
linked list of chunks
AA_API void aa_mem_pool_init(aa_mem_pool_t *pool, size_t size, size_t count)
untested
static void * aa_mem_dup(const void *src, size_t size)
Copy n octets from src into freshly-allocated heap memory.
AA_API char * aa_mem_region_vprintf(aa_mem_region_t *reg, const char *fmt, va_list ap)
printf's into a buffer allocated from region
AA_API void aa_mem_rlist_enqueue_ptr(struct aa_mem_rlist *list, void *p)
Enqueue a the pointer p at the back of the list.
AA_API void * aa_mem_pool_alloc(aa_mem_pool_t *pool)
untested
AA_API void * aa_mem_region_dup(aa_mem_region_t *region, const void *p, size_t size)
Duplicate size bytes at p, allocated out of region.
AA_API char * aa_mem_region_printf(aa_mem_region_t *region, const char *fmt,...)
printf's into a buffer allocated from region
uint8_t * end
pointer to end of this chunk
AA_API void aa_mem_rlist_enqueue_cpy(struct aa_mem_rlist *list, void *p, size_t n)
Enqueue a copy of data at p at the back of the list.
struct aa_mem_cons aa_mem_cons_t
A "cons" cell.
static size_t aa_bits_words(size_t n)
Number of aa_bits words to store n bits.
static void aa_bits_set(aa_bits *b, size_t i, int val)
Set the value of the i'th bit in bitset b to val.
static size_t aa_bits_size(size_t n_bits)
Size of bit vector in octets.
AA_API struct aa_mem_rlist * aa_mem_rlist_alloc(struct aa_mem_region *reg)
Allocate rlist out of region.
AA_API void aa_mem_region_local_pop(void *ptr)
Pop ptr from thread-local memory region.
AA_API void aa_circbuf_create(aa_circbuf_t *cb, size_t n)
untested
static void aa_zero(void *p, size_t n) AA_DEPRECATED
set n bytes of p to zero
static void aa_memswap(void *AA_RESTRICT a, void *AA_RESTRICT b, size_t size)
Swap size bytes of memory at a and b.
Data Structure for Region-Based memory allocation.
AA_API aa_mem_region_t * aa_mem_region_local_get(void)
Return pointer to a thread-local memory region.
static void aa_fzero(double *p, size_t n) AA_DEPRECATED
zero array p of length n
AA_API void aa_mem_region_release(aa_mem_region_t *region)
Deallocates all allocated objects from the region.
static void aa_bits_xor(aa_bits *a, const aa_bits *b, size_t n_bits)
Compute the bitwise XOR of a and b, storing the result in a.
AA_API size_t aa_mem_region_chunk_count(aa_mem_region_t *region)
Number of chunks in the region.
AA_API void aa_mem_region_local_init(size_t size)
Initialize the thread-local memory region.
AA_API void aa_mem_pool_free(aa_mem_pool_t *pool, void *ptr)
untested
AA_API void aa_circbuf_destroy(aa_circbuf_t *cb, size_t n)
untested
AA_EXTERN const char *aa_verbf_prefix AA_DEPRECATED
don't use
Circular buffers use a fixed-size array that acts as if connected end-to end.
void * top
top of list of free elements
static void aa_bits_and(aa_bits *a, const aa_bits *b, size_t n_bits)
Compute the bitwise AND of a and b, storing the result in a.
AA_API void aa_mem_pool_release(aa_mem_pool_t *pool)
untested
AA_API int aa_circbuf_read(aa_circbuf_t *cb, int fd, size_t n)
untested
struct aa_mem_region aa_mem_region_t
Data Structure for Region-Based memory allocation.
int aa_bits
An integer type for bit vectors.
#define AA_RESTRICT
Defined restrict keyword based on language flavor.
AA_API void * aa_mem_region_ptr(aa_mem_region_t *region)
Pointer to start of free space in region.
AA_API void aa_mem_pool_destroy(aa_mem_pool_t *pool)
untested
AA_API size_t aa_mem_region_freesize(aa_mem_region_t *region)
Number of free contiguous bytes in region.
#define AA_API
calling and name mangling convention for functions
AA_API void * aa_mem_region_local_alloc(size_t size)
Allocate from thread-local memory region.
AA_API void aa_circbuf_realloc(aa_circbuf_t *cb, size_t n)
untested
size_t end
end of filled data
struct aa_mem_rlist aa_mem_rlist_t
A linked list allocated out of a memory region.
AA_API unsigned aa_mem_ref_dec(unsigned *count)
Atomically decrement the reference count and return the previous count.
AA_API void * aa_mem_region_local_tmpalloc(size_t size)
Temporary allocate from thread-local memory region.
static int aa_bits_getn(aa_bits *b, size_t n, size_t i)
Return the value of the i'th bit in bitset b of size n.
#define AA_ALLOC_STACK_MAX
maximum size of objects to stack allocate
AA_API void * aa_mem_region_tmpalloc(aa_mem_region_t *region, size_t size)
Temporary allocation.
AA_API void aa_mem_region_destroy(aa_mem_region_t *region)
Destroy memory region freeing all chunks.
static void aa_frlocal(void *ptr, size_t size)
Free the results of AA_ALLOCAL.
AA_API void * aa_mem_region_tmprealloc(aa_mem_region_t *region, size_t size)
Temporary allocation, ensuring that there is enough room for size bytes.
AA_API void aa_flexbuf_free(aa_flexbuf_t *p)
free buffer
AA_API size_t aa_circbuf_used(aa_circbuf_t *cb)
untested
static void aa_bits_or(aa_bits *a, const aa_bits *b, size_t n_bits)
Compute the bitwise OR of a and b, storing the result in a.
AA_API size_t aa_circbuf_space(aa_circbuf_t *cb)
untested
Data Structure for Object pools.
size_t start
start of filled data
AA_API void aa_circbuf_put(aa_circbuf_t *cb, void *buf, size_t n)
untested
A single block of memory to be parceled out by the region allocator.
AA_API void aa_mem_region_local_release(void)
Release all objects allocated from thread-local memory region.
size_t n
size of the circbuf
#define AA_MEM_CPY(dst, src, n_elem)
Copy n_elem elements from src to dst.
AA_API aa_flexbuf_t * aa_flexbuf_alloc(size_t n)
allocate buffer of n bytes
AA_API void aa_mem_rlist_push_cpy(struct aa_mem_rlist *list, void *p, size_t n)
Push a copy of data at p to the front of the list.
#define AA_MEM_SET(dst, val, n_elem)
Set n_elem elements at dst to val.
static void aa_free_if_valid(void *ptr)
Frees ptr unless NULL == ptr.
struct aa_mem_region_node * next
pointer to next chunk node
#define AA_BITS_BITS
Number of bits in a aa_bits word.
AA_API size_t aa_mem_region_topsize(aa_mem_region_t *region) AA_DEPRECATED
Size of top chunk in region.
AA_API void aa_mem_region_pop(aa_mem_region_t *region, void *ptr)
Deallocates ptr and all blocks allocated after ptr was allocated.