1 typedef struct cxr_abuffer cxr_abuffer
;
11 #ifdef CXR_DEBUG_ALLOCATORS
12 #define CXR_STR_PRE(x) #x
13 #define CXR_STR(x) CXR_STR_PRE(x)
14 #define cxr_ab_ptr(b,i) __cxr_ab_ptr(b,i, __FILE__ ":L" CXR_STR(__LINE__) )
16 #define cxr_ab_ptr(b,i) __cxr_ab_ptr(b,i)
19 static void *__cxr_ab_ptr( struct cxr_abuffer
*buffer
, u32 index
21 #ifdef CXR_DEBUG_ALLOCATORS
22 ,const char *debug_str
28 #ifdef CXR_DEBUG_ALLOCATORS
29 if( index
>= buffer
->capacity
|| index
< 0 )
31 printf( "index out of capactity (%d /: [0->%d (cap)]) (%s)\n", index
, buffer
->capacity
, debug_str
);
36 return buffer
->arr
+ buffer
->esize
*index
;
39 static void cxr_ab_reserve( struct cxr_abuffer
*buffer
, u32 count
)
41 if( buffer
->count
+ count
> buffer
->capacity
)
43 buffer
->capacity
= cxr_max(buffer
->capacity
*2, buffer
->capacity
+count
);
44 buffer
->arr
= realloc( buffer
->arr
, buffer
->capacity
*buffer
->esize
);
48 static void *cxr_ab_empty( struct cxr_abuffer
*buffer
)
50 cxr_ab_reserve( buffer
, 1 );
51 return cxr_ab_ptr( buffer
, buffer
->count
++ );
54 static void *cxr_ab_empty_at( struct cxr_abuffer
*buffer
, int at
)
56 cxr_ab_reserve( buffer
, 1 );
58 if( at
== buffer
->count
)
61 return cxr_ab_ptr( buffer
, at
);
64 /* Shift buffer to make room */
67 cxr_ab_ptr( buffer
, at
+1 ),
68 cxr_ab_ptr( buffer
, at
),
69 (buffer
->count
-at
)*buffer
->esize
73 return cxr_ab_ptr( buffer
, at
);
76 static void cxr_ab_push( struct cxr_abuffer
*buffer
, void *em
)
78 cxr_ab_reserve( buffer
, 1 );
80 memcpy( buffer
->arr
+buffer
->count
*buffer
->esize
, em
, buffer
->esize
);
84 static void cxr_ab_init( struct cxr_abuffer
*buffer
, u32 esize
, u32 cap
)
86 buffer
->esize
= esize
;
87 buffer
->capacity
= cxr_max(1,cap
);
90 buffer
->arr
= malloc( buffer
->esize
*buffer
->capacity
);
93 static void cxr_ab_clear( struct cxr_abuffer
*buffer
)
98 static void cxr_ab_free( struct cxr_abuffer
*buffer
)