amino
Lightweight Robot Utility Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mem.h
Go to the documentation of this file.
1 /* -*- mode: C; c-basic-offset: 4 -*- */
2 /* ex: set shiftwidth=4 tabstop=4 expandtab: */
3 /*
4  * Copyright (c) 2010-2012, Georgia Tech Research Corporation
5  * All rights reserved.
6  *
7  * Author(s): Neil T. Dantam <ntd@gatech.edu>
8  * Georgia Tech Humanoid Robotics Lab
9  * Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
10  *
11  *
12  * This file is provided under the following "BSD-style" License:
13  *
14  *
15  * Redistribution and use in source and binary forms, with or
16  * without modification, are permitted provided that the following
17  * conditions are met:
18  *
19  * * Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  *
22  * * Redistributions in binary form must reproduce the above
23  * copyright notice, this list of conditions and the following
24  * disclaimer in the documentation and/or other materials provided
25  * with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
28  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
29  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  *
41  */
42 
43 #ifndef AA_MEM_H
44 #define AA_MEM_H
45 
46 
47 #ifndef AA_ALLOC_STACK_MAX
48 #define AA_ALLOC_STACK_MAX (4096-64)
50 #endif //AA_ALLOC_STACK_MAX
51 
56 enum aa_mem_refop {
57  /* Make a private copy */
58  AA_MEM_COPY,
59  /* Use the existing copy. Someone else will clean up */
60  AA_MEM_BORROW,
61  /* Take the reference and cleanup when we're done */
62  AA_MEM_STEAL
63 };
64 
65 /**************/
66 /* Allocation */
67 /**************/
68 
69 /*----------- General Allocation ------------------*/
70 
71 /* FIXME: we should do something reasonable when malloc() fails, but
72  since Linux lies about whether it can provide memory, there may be
73  no point. */
74 
76 static inline void *aa_malloc0( size_t size ) {
77  void *p = malloc(size);
78  if(p) memset(p,0,size);
79  return p;
80 }
81 
82 
84 static inline void aa_free_if_valid( void *ptr ) {
85  if( NULL != ptr ) free(ptr);
86 }
87 
89 #define AA_NEW_AR(type,n) ( (type*) malloc(sizeof(type)*(n)) )
90 
92 #define AA_NEW0_AR(type,n) ( (type*) aa_malloc0(sizeof(type)*(n)) )
93 
95 #define AA_NEW(type) AA_NEW_AR(type,1)
96 
98 #define AA_NEW0(type) AA_NEW0_AR(type,1)
99 
102 #define AA_ALIGN2( val, align ) (((val) + (align) - 1) & ~(align-1))
103 
104 static inline void aa_checked_free( void * ptr )
105 {
106  if( ptr ) free(ptr);
107 }
108 
109 /*----------- Local Allocation ------------------*/
110 
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); })
126 
131 #define AA_NEW_LOCAL(type, n) ( (type*) AA_ALLOCAL( sizeof(type)*(n) ) )
132 
138 static inline void aa_frlocal( void *ptr, size_t size ) {
139  if( size > AA_ALLOC_STACK_MAX) free(ptr);
140 }
141 
143 #define AA_DEL_LOCAL(ptr, type, n) aa_frlocal( ptr, sizeof(type)*(n) )
144 
146 typedef struct {
147  size_t n;
148  union {
149  uint64_t dalign;
150  uint8_t d[1];
151  };
152 } aa_flexbuf_t;
153 
158 
159 /*----------- Region Allocation ------------------*/
160 
162 #define AA_MEMREG_ALIGN 16
163 
169  //size_t n; ///< size of this chunk
170  uint8_t *end;
172  uint8_t d[];
173 };
174 
197 typedef struct aa_mem_region {
198  uint8_t *head;
201 
203 AA_API void aa_mem_region_init( aa_mem_region_t *region, size_t size );
204 
208 
209 
214 
219 AA_API void *aa_mem_region_tmpalloc( aa_mem_region_t *region, size_t size );
220 
225 AA_API void *aa_mem_region_alloc( aa_mem_region_t *region, size_t size );
226 
229 AA_API void *aa_mem_region_tmprealloc( aa_mem_region_t *region, size_t size );
230 
233 AA_API void *aa_mem_region_dup( aa_mem_region_t *region, const void *p, size_t size );
234 
235 
240 
243 AA_API char *aa_mem_region_printf( aa_mem_region_t *region, const char *fmt, ... );
244 
247 AA_API char* aa_mem_region_vprintf(aa_mem_region_t *reg, const char *fmt, va_list ap );
248 
251 AA_API void aa_mem_region_pop( aa_mem_region_t *region, void *ptr );
252 
253 
256 AA_API void *aa_mem_region_ptr( aa_mem_region_t *region );
257 
261 
265 
266 
271 AA_API void aa_mem_region_local_init( size_t size );
272 
278 
282 
287 AA_API void *aa_mem_region_local_alloc( size_t size );
288 
293 AA_API void *aa_mem_region_local_tmpalloc( size_t size );
294 
299 AA_API void aa_mem_region_local_pop( void *ptr );
300 
306 
307 #define AA_MEM_REGION_NEW( reg, type ) ( (type*) aa_mem_region_alloc((reg), sizeof(type)) )
308 
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)) )
311 
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)) )
314 
315 /*----------- Pooled Allocation ------------------*/
316 
325 typedef struct {
326  size_t size;
327  void *top;
329 } aa_mem_pool_t;
330 
332 AA_API void aa_mem_pool_init( aa_mem_pool_t *pool, size_t size, size_t count );
336 AA_API void *aa_mem_pool_alloc( aa_mem_pool_t *pool );
338 AA_API void aa_mem_pool_free( aa_mem_pool_t *pool, void *ptr );
341 
342 /*----------- Circular Buffers ------------------*/
346 typedef struct {
347  void *buf;
348  size_t n;
349  size_t start;
350  size_t end;
351 } aa_circbuf_t;
352 
354 AA_API void aa_circbuf_create( aa_circbuf_t *cb, size_t n );
356 AA_API void aa_circbuf_destroy( aa_circbuf_t *cb, size_t n );
358 AA_API void aa_circbuf_realloc( aa_circbuf_t *cb, size_t n );
360 AA_API size_t aa_circbuf_space( aa_circbuf_t *cb );
362 AA_API size_t aa_circbuf_used( aa_circbuf_t *cb );
364 AA_API void aa_circbuf_put( aa_circbuf_t *cb, void *buf, size_t n );
366 AA_API int aa_circbuf_read( aa_circbuf_t *cb, int fd, size_t n );
368 AA_API int aa_circbuf_write( aa_circbuf_t *cb, int fd, size_t n );
369 
370 
371 /***************/
372 /* Linked List */
373 /***************/
374 
377 typedef struct aa_mem_cons {
378  void *data;
379  struct aa_mem_cons *next;
380 } aa_mem_cons_t;
381 
386 typedef struct aa_mem_rlist {
387  struct aa_mem_region *reg;
388  struct aa_mem_cons *head;
389  struct aa_mem_cons **tailp;
391 
397 AA_API struct aa_mem_rlist *
398 aa_mem_rlist_alloc( struct aa_mem_region *reg );
399 
401 AA_API void
402 aa_mem_rlist_push_cpy( struct aa_mem_rlist *list, void *p, size_t n );
403 
405 AA_API void
406 aa_mem_rlist_push_ptr( struct aa_mem_rlist *list, void *p );
407 
409 AA_API void
410 aa_mem_rlist_enqueue_cpy( struct aa_mem_rlist *list, void *p, size_t n );
411 
413 AA_API void
414 aa_mem_rlist_enqueue_ptr( struct aa_mem_rlist *list, void *p );
415 
421 AA_API void *aa_mem_rlist_pop( struct aa_mem_rlist *list );
422 
423 #define AA_RLIST_DEF( element_type, list_type ) \
424  typedef struct { \
425  aa_mem_rlist_t rlist; \
426  } list_type; \
427  static inline list_type* \
428  list_type ## _alloc( aa_mem_region *reg ) \
429  { \
430  return (list_type*)aa_mem_rlist_alloc(reg); \
431  }
432 
433 /**********/
434 /* Arrays */
435 /**********/
436 
441 #define AA_MEM_CPY(dst, src, n_elem) \
442  { \
443  /* _Static_assert(sizeof(*dst) == sizeof(*src));*/ \
444  memcpy( (dst), (src), sizeof((dst)[0])*(n_elem) ); \
445  }
446 
449 static inline void *aa_mem_dup( const void *src, size_t size )
450 {
451  void *dst = malloc(size);
452  memcpy(dst,src,size);
453  return dst;
454 }
455 
459 #define AA_MEM_DUP( type, src, count ) \
460  (type*)aa_mem_dup( (src), sizeof(type)*(count) );
461 
462 #define AA_MEM_DUPOP( refop, type, const_dst, dst_data, src, n_elem ) \
463  switch(refop) { \
464  case AA_MEM_COPY: \
465  dst_data = AA_MEM_DUP(type, src, n_elem); \
466  const_dst = dst_data; \
467  break; \
468  case AA_MEM_STEAL: \
469  dst_data = src; \
470  const_dst = dst_data; \
471  break; \
472  case AA_MEM_BORROW: \
473  dst_data = NULL; \
474  const_dst = src; \
475  break; \
476  };
477 
478 
479 
484 #define AA_MEM_SET(dst, val, n_elem) \
485  { \
486  for( size_t aa_$_set_i = 0; \
487  aa_$_set_i < (n_elem); \
488  aa_$_set_i++ ) \
489  (dst)[aa_$_set_i] = (val); \
490  }
491 
496 #define AA_MEM_ZERO(dst, n_elem) (memset((dst),0,(n_elem)*sizeof(*(dst))))
497 
499 #define AA_FAR(...) ((double[]){__VA_ARGS__})
500 
502 static inline void aa_fcpy( double *dst, const double *src, size_t n ) AA_DEPRECATED;
503 static inline void aa_fcpy( double *dst, const double *src, size_t n ) {
504  AA_MEM_CPY( dst, src, n );
505 }
506 
508 static inline void aa_fset( double *dst, double val, size_t n ) AA_DEPRECATED;
509 static inline void aa_fset( double *dst, double val, size_t n ) {
510  AA_MEM_SET( dst, val, n );
511 }
512 
514 static inline void aa_zero( void *p, size_t n ) AA_DEPRECATED;
515 static inline void aa_zero( void *p, size_t n ) {
516  memset(p,0,n);
517 }
518 
520 static inline void aa_fzero( double *p, size_t n ) AA_DEPRECATED;
521 static inline void aa_fzero( double *p, size_t n ) {
522  AA_MEM_SET( p, 0, n );
523 }
524 
526 #define AA_ZERO_AR( var ) aa_zero( var, sizeof(var) )
527 
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;
533 
534 
535 /**************/
536 /* Bit Array */
537 /**************/
538 
540 typedef int aa_bits;
541 
543 #define AA_BITS_BITS (8*sizeof(aa_bits))
544 
546 static inline size_t
547 aa_bits_words( size_t n )
548 {
549  size_t words = n / AA_BITS_BITS;
550  if( words*sizeof(aa_bits)*8 < n ) words++;
551  return words;
552 }
553 
555 static inline size_t
556 aa_bits_size( size_t n_bits )
557 {
558  return aa_bits_words(n_bits) * sizeof(aa_bits);
559 }
560 
561 #define AA_BITS_JK( i, j, k ) \
562  j = (i) / AA_BITS_BITS; \
563  k = (i) - (j)*AA_BITS_BITS;
564 
568 static inline int
569 aa_bits_get( aa_bits *b, size_t i )
570 {
571  size_t j,k;
572  AA_BITS_JK(i,j,k);
573 
574  return (b[j] >> k) & 0x1;
575 }
576 
577 
583 static inline int
584 aa_bits_getn( aa_bits *b, size_t n, size_t i )
585 {
586  if( aa_bits_size(i+1) > n ) return 0;
587  else return aa_bits_get( b, i );
588 }
589 
593 static inline void
594 aa_bits_set( aa_bits *b, size_t i, int val )
595 {
596  size_t j,k;
597  AA_BITS_JK(i,j,k);
598  if( val ) {
599  b[j] |= 0x1<<k;
600  } else {
601  b[j] &= ~ ( 0x1<<k );
602  }
603 }
604 
608 static inline void
609 aa_bits_and( aa_bits *a, const aa_bits *b, size_t n_bits )
610 {
611  size_t n_words = aa_bits_words(n_bits);
612  while( n_words ) {
613  a[n_words] &= b[n_words];
614  }
615 }
616 
620 static inline void
621 aa_bits_or( aa_bits *a, const aa_bits *b, size_t n_bits )
622 {
623  size_t n_words = aa_bits_words(n_bits);
624  while( n_words ) {
625  a[n_words] |= b[n_words];
626  }
627 }
628 
632 static inline void
633 aa_bits_xor( aa_bits *a, const aa_bits *b, size_t n_bits )
634 {
635  size_t n_words = aa_bits_words(n_bits);
636  while( n_words ) {
637  a[n_words] ^= b[n_words];
638  }
639 }
640 
641 
642 /************/
643 /* Swapping */
644 /************/
645 
646 typedef long aa_memswap_type;
647 
649 static inline void aa_memswap( void *AA_RESTRICT a, void *AA_RESTRICT b, size_t size )
650 {
651 
652  // TODO: consider alignment
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];
656  la[i] = lb[i];
657  lb[i] = tmp;
658  }
659 
660  uint8_t *ca = (uint8_t*)a, *cb = (uint8_t*)b;
661  for( size_t i = size - size%sizeof(*la); i < size; i ++ ) {
662  uint8_t tmp = ca[i];
663  ca[i] = cb[i];
664  cb[i] = tmp;
665  }
666 
667 }
668 
670 #define AA_MEM_SWAP( a, b, n_elem ) (aa_memswap( (a), (b), n_elem*sizeof(*(a)) ))
671 
672 /***********/
673 /* VECTORS */
674 /***********/
675 
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); \
692  } \
693  ptr[(size)++] = (value);
694 
695 #define AA_VECTOR_DEF( element_type, vector_type ) \
696  typedef struct { \
697  size_t max; \
698  size_t size; \
699  element_type *data; \
700  } vector_type; \
701  static inline void vector_type ## _init \
702  ( vector_type *vec, size_t max ) { \
703  vec->data = (element_type*)malloc(max*sizeof(*vec->data)); \
704  vec->max = max; \
705  vec->size = (size_t)0; \
706  }; \
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); \
710  };
711 
712 
713 /***********************************/
714 /* Synchronized Reference Counting */
715 /***********************************/
716 
720 AA_API unsigned
721 aa_mem_ref_inc( unsigned *count );
722 
726 AA_API unsigned
727 aa_mem_ref_dec( unsigned *count );
728 
729 #endif //AA_MEM_H
uint8_t * head
pointer to first free element of top chunk
Definition: mem.h:198
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
Definition: mem.h:149
A linked list allocated out of a memory region.
Definition: mem.h:386
static int aa_bits_get(aa_bits *b, size_t i)
Return the value of the i'th bit in bitset b.
Definition: mem.h:569
AA_API void * aa_mem_region_alloc(aa_mem_region_t *region, size_t size)
Allocate size bytes from the region.
uint8_t d[]
data array
Definition: mem.h:172
static void aa_fcpy(double *dst, const double *src, size_t n) AA_DEPRECATED
copy n double floats from src to dst
Definition: mem.h:503
static void aa_fset(double *dst, double val, size_t n) AA_DEPRECATED
set n double floats to val
Definition: mem.h:509
AA_API void aa_mem_region_local_destroy(void)
Destroy the thread-local memory region.
A buffer struct.
Definition: mem.h:146
aa_mem_region_t region
memory region to allocate from
Definition: mem.h:328
static void * aa_malloc0(size_t size)
Malloc and zero initialize size bytes.
Definition: mem.h:76
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
Definition: mem.h:326
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
Definition: mem.h:199
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.
Definition: mem.h:449
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
Definition: mem.h:170
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.
Definition: mem.h:547
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.
Definition: mem.h:594
static size_t aa_bits_size(size_t n_bits)
Size of bit vector in octets.
Definition: mem.h:556
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
Definition: mem.h:515
static void aa_memswap(void *AA_RESTRICT a, void *AA_RESTRICT b, size_t size)
Swap size bytes of memory at a and b.
Definition: mem.h:649
Data Structure for Region-Based memory allocation.
Definition: mem.h:197
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
Definition: mem.h:521
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.
Definition: mem.h:633
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
void * buf
memory
Definition: mem.h:347
AA_EXTERN const char *aa_verbf_prefix AA_DEPRECATED
don't use
Definition: debug.h:101
Circular buffers use a fixed-size array that acts as if connected end-to end.
Definition: mem.h:346
void * top
top of list of free elements
Definition: mem.h:327
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.
Definition: mem.h:609
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.
Definition: mem.h:540
#define AA_RESTRICT
Defined restrict keyword based on language flavor.
Definition: amino.h:90
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
Definition: amino.h:86
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
Definition: mem.h:350
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.
Definition: mem.h:584
#define AA_ALLOC_STACK_MAX
maximum size of objects to stack allocate
Definition: mem.h:49
size_t n
size of buffer
Definition: mem.h:147
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.
Definition: mem.h:138
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.
Definition: mem.h:621
AA_API size_t aa_circbuf_space(aa_circbuf_t *cb)
untested
Data Structure for Object pools.
Definition: mem.h:325
size_t start
start of filled data
Definition: mem.h:349
AA_API void aa_circbuf_put(aa_circbuf_t *cb, void *buf, size_t n)
untested
A "cons" cell.
Definition: mem.h:377
A single block of memory to be parceled out by the region allocator.
Definition: mem.h:168
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
Definition: mem.h:348
#define AA_MEM_CPY(dst, src, n_elem)
Copy n_elem elements from src to dst.
Definition: mem.h:441
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.
Definition: mem.h:484
static void aa_free_if_valid(void *ptr)
Frees ptr unless NULL == ptr.
Definition: mem.h:84
struct aa_mem_region_node * next
pointer to next chunk node
Definition: mem.h:171
#define AA_BITS_BITS
Number of bits in a aa_bits word.
Definition: mem.h:543
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.