amino  1.0-beta2
Lightweight Robot Utility Library
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 
51 #ifndef AA_ALLOC_STACK_MAX
52 #define AA_ALLOC_STACK_MAX (4096-64)
54 #endif //AA_ALLOC_STACK_MAX
55 
56 
57 enum aa_mem_refop {
58  /* Make a private copy */
59  AA_MEM_COPY,
60  /* Use the existing copy. Someone else will clean up */
61  AA_MEM_BORROW,
62  /* Take the reference and cleanup when we're done */
63  AA_MEM_STEAL
64 };
65 
66 /**************/
67 /* Allocation */
68 /**************/
69 
70 /*----------- General Allocation ------------------*/
71 
72 /* FIXME: we should do something reasonable when malloc() fails, but
73  since Linux lies about whether it can provide memory, there may be
74  no point. */
75 
77 static inline void *aa_malloc0( size_t size ) {
78  void *p = malloc(size);
79  if(p) memset(p,0,size);
80  return p;
81 }
82 
83 
85 static inline void aa_free_if_valid( void *ptr ) {
86  if( NULL != ptr ) free(ptr);
87 }
88 
90 #define AA_NEW_AR(type,n) ( (type*) malloc(sizeof(type)*(n)) )
91 
93 #define AA_NEW0_AR(type,n) ( (type*) aa_malloc0(sizeof(type)*(n)) )
94 
96 #define AA_NEW(type) AA_NEW_AR(type,1)
97 
99 #define AA_NEW0(type) AA_NEW0_AR(type,1)
100 
103 #define AA_ALIGN2( val, align ) (((val) + (align) - 1) & ~(align-1))
104 
105 static inline void aa_checked_free( void * ptr )
106 {
107  if( ptr ) free(ptr);
108 }
109 
110 /*----------- Local Allocation ------------------*/
111 
124 #define AA_ALLOCAL(size) ({ size_t aa_$_allocal_size = (size); \
125  aa_$_allocal_size > AA_ALLOC_STACK_MAX ? \
126  malloc(aa_$_allocal_size) : alloca(aa_$_allocal_size); })
127 
132 #define AA_NEW_LOCAL(type, n) ( (type*) AA_ALLOCAL( sizeof(type)*(n) ) )
133 
139 static inline void aa_frlocal( void *ptr, size_t size ) {
140  if( size > AA_ALLOC_STACK_MAX) free(ptr);
141 }
142 
144 #define AA_DEL_LOCAL(ptr, type, n) aa_frlocal( ptr, sizeof(type)*(n) )
145 
147 typedef struct {
148  size_t n;
149  union {
150  uint64_t dalign;
151  uint8_t d[1];
152  };
153 } aa_flexbuf_t;
154 
159 
160 /*----------- Region Allocation ------------------*/
161 
163 #define AA_MEMREG_ALIGN 16
164 
170  //size_t n; ///< size of this chunk
171  uint8_t *end;
173  uint8_t d[];
174 };
175 
198 typedef struct aa_mem_region {
199  uint8_t *head;
201  union {
205  int reserved;
206  struct {
213  unsigned free_on_destroy;
214  };
215  };
217 
219 AA_API void aa_mem_region_init( aa_mem_region_t *region, size_t size );
220 
225 AA_API struct aa_mem_region *
226 aa_mem_region_create( size_t size );
227 
235 
236 
241 
246 AA_API void *aa_mem_region_tmpalloc( aa_mem_region_t *region, size_t size );
247 
252 AA_API void *aa_mem_region_alloc( aa_mem_region_t *region, size_t size );
253 
256 AA_API void *aa_mem_region_tmprealloc( aa_mem_region_t *region, size_t size );
257 
260 AA_API void *aa_mem_region_dup( aa_mem_region_t *region, const void *p, size_t size );
261 
264 AA_API char *aa_mem_region_strdup( aa_mem_region_t *region, const char *str );
265 
270 
273 AA_API char *aa_mem_region_printf( aa_mem_region_t *region, const char *fmt, ... );
274 
277 AA_API char* aa_mem_region_vprintf(aa_mem_region_t *reg, const char *fmt, va_list ap );
278 
281 AA_API void aa_mem_region_pop( aa_mem_region_t *region, void *ptr );
282 
283 
286 AA_API void *aa_mem_region_ptr( aa_mem_region_t *region );
287 
291 
295 
296 
301 AA_API void aa_mem_region_local_init( size_t size );
302 
308 
312 
317 AA_API void *aa_mem_region_local_alloc( size_t size );
318 
323 AA_API void *aa_mem_region_local_tmpalloc( size_t size );
324 
329 AA_API void aa_mem_region_local_pop( void *ptr );
330 
336 
337 
341 #define AA_MEM_REGION_NEW( reg, type ) ( (type*) aa_mem_region_alloc((reg), sizeof(type)) )
342 
346 #define AA_MEM_REGION_NEW_CPY( reg, src, type ) ( (type*) aa_mem_region_dup((reg), (src), sizeof(type)) )
347 
351 #define AA_MEM_REGION_DUP( reg, type, src, count ) ( (type*) aa_mem_region_dup((reg), (src), (count)*sizeof(type)) )
352 
356 #define AA_MEM_REGION_NEW_N( reg, type, n ) ( (type*) aa_mem_region_alloc((reg), (n)*sizeof(type)) )
357 
361 #define AA_MEM_REGION_LOCAL_NEW( type ) ( (type*) aa_mem_region_local_alloc( sizeof(type)) )
362 
366 #define AA_MEM_REGION_LOCAL_NEW_N( type, n ) ( (type*) aa_mem_region_local_alloc( (n)*sizeof(type)) )
367 
368 /*----------- Pooled Allocation ------------------*/
369 
378 typedef struct {
379  size_t size;
380  void *top;
382 } aa_mem_pool_t;
383 
385 AA_API void aa_mem_pool_init( aa_mem_pool_t *pool, size_t size, size_t count );
389 AA_API void *aa_mem_pool_alloc( aa_mem_pool_t *pool );
391 AA_API void aa_mem_pool_free( aa_mem_pool_t *pool, void *ptr );
394 
395 /*----------- Circular Buffers ------------------*/
399 typedef struct {
400  void *buf;
401  size_t n;
402  size_t start;
403  size_t end;
404 } aa_circbuf_t;
405 
407 AA_API void aa_circbuf_create( aa_circbuf_t *cb, size_t n );
409 AA_API void aa_circbuf_destroy( aa_circbuf_t *cb, size_t n );
411 AA_API void aa_circbuf_realloc( aa_circbuf_t *cb, size_t n );
413 AA_API size_t aa_circbuf_space( aa_circbuf_t *cb );
415 AA_API size_t aa_circbuf_used( aa_circbuf_t *cb );
417 AA_API void aa_circbuf_put( aa_circbuf_t *cb, void *buf, size_t n );
419 AA_API int aa_circbuf_read( aa_circbuf_t *cb, int fd, size_t n );
421 AA_API int aa_circbuf_write( aa_circbuf_t *cb, int fd, size_t n );
422 
423 
424 /***************/
425 /* Linked List */
426 /***************/
427 
430 typedef struct aa_mem_cons {
431  void *data;
432  struct aa_mem_cons *next;
433 } aa_mem_cons_t;
434 
441 typedef struct aa_mem_rlist {
442  struct aa_mem_region *reg;
443  struct aa_mem_cons *head;
444  struct aa_mem_cons **tailp;
446 
452 AA_API struct aa_mem_rlist *
453 aa_mem_rlist_alloc( struct aa_mem_region *reg );
454 
456 AA_API void
457 aa_mem_rlist_push_cpy( struct aa_mem_rlist *list, void *p, size_t n );
458 
460 AA_API void
461 aa_mem_rlist_push_ptr( struct aa_mem_rlist *list, void *p );
462 
464 AA_API void
465 aa_mem_rlist_enqueue_cpy( struct aa_mem_rlist *list, const void *p, size_t n );
466 
468 AA_API void
469 aa_mem_rlist_enqueue_ptr( struct aa_mem_rlist *list, void *p );
470 
472 AA_API void
473 aa_mem_rlist_map( struct aa_mem_rlist *list,
474  void (*function)(void *cx, void *element ),
475  void *cx );
476 
482 AA_API void *aa_mem_rlist_pop( struct aa_mem_rlist *list );
483 
484 #define AA_RLIST_DEF( element_type, list_type ) \
485  typedef struct { \
486  aa_mem_rlist_t rlist; \
487  } list_type; \
488  static inline list_type* \
489  list_type ## _alloc( aa_mem_region *reg ) \
490  { \
491  return (list_type*)aa_mem_rlist_alloc(reg); \
492  }
493 
494 /**********/
495 /* Arrays */
496 /**********/
497 
502 #define AA_MEM_CPY(dst, src, n_elem) \
503  { \
504  /* _Static_assert(sizeof(*dst) == sizeof(*src));*/ \
505  memcpy( (dst), (src), sizeof((dst)[0])*(n_elem) ); \
506  }
507 
510 static inline void *aa_mem_dup( const void *src, size_t size )
511 {
512  void *dst = malloc(size);
513  memcpy(dst,src,size);
514  return dst;
515 }
516 
520 #define AA_MEM_DUP( type, src, count ) \
521  ((type*)aa_mem_dup((src), sizeof(type)*(count)))
522 
523 #define AA_MEM_DUPOP( refop, type, const_dst, dst_data, src, n_elem ) \
524  switch(refop) { \
525  case AA_MEM_COPY: \
526  dst_data = AA_MEM_DUP(type, src, n_elem); \
527  const_dst = dst_data; \
528  break; \
529  case AA_MEM_STEAL: \
530  dst_data = src; \
531  const_dst = dst_data; \
532  break; \
533  case AA_MEM_BORROW: \
534  dst_data = NULL; \
535  const_dst = src; \
536  break; \
537  };
538 
539 
540 
545 #define AA_MEM_SET(dst, val, n_elem) \
546  { \
547  for( size_t aa_$_set_i = 0; \
548  aa_$_set_i < (n_elem); \
549  aa_$_set_i++ ) \
550  (dst)[aa_$_set_i] = (val); \
551  }
552 
557 #define AA_MEM_ZERO(dst, n_elem) (memset((dst),0,(n_elem)*sizeof(*(dst))))
558 
560 #define AA_FAR(...) ((double[]){__VA_ARGS__})
561 
563 static inline void aa_fcpy( double *dst, const double *src, size_t n ) AA_DEPRECATED;
564 static inline void aa_fcpy( double *dst, const double *src, size_t n ) {
565  AA_MEM_CPY( dst, src, n );
566 }
567 
569 static inline void aa_fset( double *dst, double val, size_t n ) AA_DEPRECATED;
570 static inline void aa_fset( double *dst, double val, size_t n ) {
571  AA_MEM_SET( dst, val, n );
572 }
573 
575 static inline void aa_zero( void *p, size_t n ) AA_DEPRECATED;
576 static inline void aa_zero( void *p, size_t n ) {
577  memset(p,0,n);
578 }
579 
581 static inline void aa_fzero( double *p, size_t n ) AA_DEPRECATED;
582 static inline void aa_fzero( double *p, size_t n ) {
583  AA_MEM_SET( p, 0, n );
584 }
585 
587 #define AA_ZERO_AR( var ) aa_zero( var, sizeof(var) )
588 
590 #define AA_SET_AR( var, val ) \
591  for( size_t aa_$_set_ar_i = 0; \
592  aa_$_set_ar_i < sizeof(var)/sizeof(var[0]); \
593  aa_$_set_ar_i ++ ) var[aa_$_set_ar_i] = val;
594 
595 
596 /**************/
597 /* Bit Array */
598 /**************/
599 
601 typedef int aa_bits;
602 
604 #define AA_BITS_BITS (8*sizeof(aa_bits))
605 
607 static inline size_t
608 aa_bits_words( size_t n )
609 {
610  size_t words = n / AA_BITS_BITS;
611  if( words*sizeof(aa_bits)*8 < n ) words++;
612  return words;
613 }
614 
616 static inline size_t
617 aa_bits_size( size_t n_bits )
618 {
619  return aa_bits_words(n_bits) * sizeof(aa_bits);
620 }
621 
622 #define AA_BITS_JK( i, j, k ) \
623  j = (i) / AA_BITS_BITS; \
624  k = (i) - (j)*AA_BITS_BITS;
625 
629 static inline int
630 aa_bits_get( aa_bits *b, size_t i )
631 {
632  size_t j,k;
633  AA_BITS_JK(i,j,k);
634 
635  return (b[j] >> k) & 0x1;
636 }
637 
638 
644 static inline int
645 aa_bits_getn( aa_bits *b, size_t n, size_t i )
646 {
647  if( aa_bits_size(i+1) > n ) return 0;
648  else return aa_bits_get( b, i );
649 }
650 
654 static inline void
655 aa_bits_set( aa_bits *b, size_t i, int val )
656 {
657  size_t j,k;
658  AA_BITS_JK(i,j,k);
659  if( val ) {
660  b[j] |= 0x1<<k;
661  } else {
662  b[j] &= ~ ( 0x1<<k );
663  }
664 }
665 
669 static inline void
670 aa_bits_and( aa_bits *a, const aa_bits *b, size_t n_bits )
671 {
672  size_t n_words = aa_bits_words(n_bits);
673  while( n_words ) {
674  a[n_words] &= b[n_words];
675  }
676 }
677 
681 static inline void
682 aa_bits_or( aa_bits *a, const aa_bits *b, size_t n_bits )
683 {
684  size_t n_words = aa_bits_words(n_bits);
685  while( n_words ) {
686  a[n_words] |= b[n_words];
687  }
688 }
689 
693 static inline void
694 aa_bits_xor( aa_bits *a, const aa_bits *b, size_t n_bits )
695 {
696  size_t n_words = aa_bits_words(n_bits);
697  while( n_words ) {
698  a[n_words] ^= b[n_words];
699  }
700 }
701 
702 
703 /************/
704 /* Swapping */
705 /************/
706 
710 typedef long aa_memswap_type;
711 
713 static inline void aa_memswap( void *AA_RESTRICT a, void *AA_RESTRICT b, size_t size )
714 {
715 
716  // TODO: consider alignment
717  aa_memswap_type *la = (aa_memswap_type*)a, *lb = (aa_memswap_type*)b;
718  for( size_t i = 0; i < size/sizeof(*la); i++ ) {
719  aa_memswap_type tmp = la[i];
720  la[i] = lb[i];
721  lb[i] = tmp;
722  }
723 
724  uint8_t *ca = (uint8_t*)a, *cb = (uint8_t*)b;
725  for( size_t i = size - size%sizeof(*la); i < size; i ++ ) {
726  uint8_t tmp = ca[i];
727  ca[i] = cb[i];
728  cb[i] = tmp;
729  }
730 
731 }
732 
734 #define AA_MEM_SWAP( a, b, n_elem ) (aa_memswap( (a), (b), n_elem*sizeof(*(a)) ))
735 
736 /***********/
737 /* VECTORS */
738 /***********/
739 
752 #define AA_VECTOR_PUSH( max, size, ptr, value ) \
753  if( (size) >= (max) ) { \
754  (max) = 2*(size+1); \
755  (ptr) = (__typeof__(ptr))realloc(ptr,sizeof(*ptr)*max); \
756  } \
757  ptr[(size)++] = (value);
758 
759 #define AA_VECTOR_DEF( element_type, vector_type ) \
760  typedef struct { \
761  size_t max; \
762  size_t size; \
763  element_type *data; \
764  } vector_type; \
765  static inline void vector_type ## _init \
766  ( vector_type *vec, size_t max ) { \
767  vec->data = (element_type*)malloc(max*sizeof(*vec->data)); \
768  vec->max = max; \
769  vec->size = (size_t)0; \
770  }; \
771  static inline void vector_type ## _push \
772  ( vector_type *vec, element_type value ) { \
773  AA_VECTOR_PUSH(vec->max, vec->size, vec->data, value); \
774  };
775 
776 
777 
778 #endif //AA_MEM_H
uint8_t * head
pointer to first free element of top chunk
Definition: mem.h:199
AA_API void * aa_mem_rlist_pop(struct aa_mem_rlist *list)
Remove front element of the list and return its data pointer.
int reserved
Reserve space for other bits.
Definition: mem.h:205
uint64_t dalign
dummy element for alignment
Definition: mem.h:150
A linked list allocated out of a memory region.
Definition: mem.h:441
static int aa_bits_get(aa_bits *b, size_t i)
Return the value of the i&#39;th bit in bitset b.
Definition: mem.h:630
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:173
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:564
AA_API void aa_mem_rlist_enqueue_cpy(struct aa_mem_rlist *list, const void *p, size_t n)
Enqueue a copy of data at p at the back of the list.
static void aa_fset(double *dst, double val, size_t n) AA_DEPRECATED
set n double floats to val
Definition: mem.h:570
AA_API void aa_mem_region_local_destroy(void)
Destroy the thread-local memory region.
A buffer struct.
Definition: mem.h:147
aa_mem_region_t region
memory region to allocate from
Definition: mem.h:381
static void * aa_malloc0(size_t size)
Malloc and zero initialize size bytes.
Definition: mem.h:77
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:379
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:200
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:510
AA_API char * aa_mem_region_vprintf(aa_mem_region_t *reg, const char *fmt, va_list ap)
printf&#39;s into a buffer allocated from region
AA_API char * aa_mem_region_strdup(aa_mem_region_t *region, const char *str)
Duplicate a string, allocating from the 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&#39;s into a buffer allocated from region
uint8_t * end
pointer to end of this chunk
Definition: mem.h:171
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:608
static void aa_bits_set(aa_bits *b, size_t i, int val)
Set the value of the i&#39;th bit in bitset b to val.
Definition: mem.h:655
static size_t aa_bits_size(size_t n_bits)
Size of bit vector in octets.
Definition: mem.h:617
AA_API struct aa_mem_rlist * aa_mem_rlist_alloc(struct aa_mem_region *reg)
Allocate rlist out of region.
AA_API struct aa_mem_region * aa_mem_region_create(size_t size)
Allocate and initialize memory region with an initial chunk of size bytes.
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:576
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:713
Data Structure for Region-Based memory allocation.
Definition: mem.h:198
AA_API aa_mem_region_t * aa_mem_region_local_get(void)
Return pointer to a thread-local memory region.
AA_API void aa_mem_rlist_map(struct aa_mem_rlist *list, void(*function)(void *cx, void *element), void *cx)
Apply function to each element of list.
static void aa_fzero(double *p, size_t n) AA_DEPRECATED
zero array p of length n
Definition: mem.h:582
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:694
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:400
AA_EXTERN const char *aa_verbf_prefix AA_DEPRECATED
don&#39;t use
Definition: debug.h:104
Circular buffers use a fixed-size array that acts as if connected end-to end.
Definition: mem.h:399
void * top
top of list of free elements
Definition: mem.h:380
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:670
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.
long aa_memswap_type
Type to use for memory swapping.
Definition: mem.h:710
int aa_bits
An integer type for bit vectors.
Definition: mem.h:601
#define AA_RESTRICT
Defined restrict keyword based on language flavor.
Definition: amino.h:99
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:95
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:403
struct aa_mem_rlist aa_mem_rlist_t
A linked list allocated out of a memory region.
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&#39;th bit in bitset b of size n.
Definition: mem.h:645
#define AA_ALLOC_STACK_MAX
maximum size of objects to stack allocate
Definition: mem.h:53
size_t n
size of buffer
Definition: mem.h:148
void * data
pointer to data
Definition: mem.h:431
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:139
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:682
AA_API size_t aa_circbuf_space(aa_circbuf_t *cb)
untested
Data Structure for Object pools.
Definition: mem.h:378
size_t start
start of filled data
Definition: mem.h:402
AA_API void aa_circbuf_put(aa_circbuf_t *cb, void *buf, size_t n)
untested
A "cons" cell.
Definition: mem.h:430
unsigned free_on_destroy
Free struct when calling aa_mem_region_destroy().
Definition: mem.h:213
A single block of memory to be parceled out by the region allocator.
Definition: mem.h:169
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:401
#define AA_MEM_CPY(dst, src, n_elem)
Copy n_elem elements from src to dst.
Definition: mem.h:502
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:545
static void aa_free_if_valid(void *ptr)
Frees ptr unless NULL == ptr.
Definition: mem.h:85
struct aa_mem_region_node * next
pointer to next chunk node
Definition: mem.h:172
#define AA_BITS_BITS
Number of bits in a aa_bits word.
Definition: mem.h:604
AA_API size_t aa_mem_region_topsize(aa_mem_region_t *region) AA_DEPRECATED
Size of top chunk in region.
struct aa_mem_cons * next
pointer to next list cell
Definition: mem.h:432
AA_API void aa_mem_region_pop(aa_mem_region_t *region, void *ptr)
Deallocates ptr and all blocks allocated after ptr was allocated.