10 typedef struct cxr_abuffer cxr_abuffer
;
20 #ifdef CXR_DEBUG_ALLOCATORS
21 #define CXR_STR_PRE(x) #x
22 #define CXR_STR(x) CXR_STR_PRE(x)
23 #define cxr_ab_ptr(b,i) __cxr_ab_ptr(b,i, __FILE__ ":L" CXR_STR(__LINE__) )
25 #define cxr_ab_ptr(b,i) __cxr_ab_ptr(b,i)
28 static void *__cxr_ab_ptr( struct cxr_abuffer
*buffer
, u32 index
30 #ifdef CXR_DEBUG_ALLOCATORS
31 ,const char *debug_str
37 #ifdef CXR_DEBUG_ALLOCATORS
38 if( index
>= buffer
->capacity
|| index
< 0 )
40 printf( "index out of capactity (%d /: [0->%d (cap)]) (%s)\n", index
,
41 buffer
->capacity
, debug_str
);
46 return buffer
->arr
+ buffer
->esize
*index
;
49 static void cxr_ab_reserve( struct cxr_abuffer
*buffer
, u32 count
)
51 if( buffer
->count
+ count
> buffer
->capacity
)
53 buffer
->capacity
= cxr_max(buffer
->capacity
*2, buffer
->capacity
+count
);
54 buffer
->arr
= realloc( buffer
->arr
, buffer
->capacity
*buffer
->esize
);
58 static void *cxr_ab_empty( struct cxr_abuffer
*buffer
)
60 cxr_ab_reserve( buffer
, 1 );
61 return cxr_ab_ptr( buffer
, buffer
->count
++ );
64 static void *cxr_ab_empty_at( struct cxr_abuffer
*buffer
, int at
)
66 cxr_ab_reserve( buffer
, 1 );
68 if( at
== buffer
->count
)
71 return cxr_ab_ptr( buffer
, at
);
74 /* Shift buffer to make room */
77 cxr_ab_ptr( buffer
, at
+1 ),
78 cxr_ab_ptr( buffer
, at
),
79 (buffer
->count
-at
)*buffer
->esize
83 return cxr_ab_ptr( buffer
, at
);
86 static void cxr_ab_push( struct cxr_abuffer
*buffer
, void *em
)
88 cxr_ab_reserve( buffer
, 1 );
90 memcpy( buffer
->arr
+buffer
->count
*buffer
->esize
, em
, buffer
->esize
);
94 static void cxr_ab_init( struct cxr_abuffer
*buffer
, u32 esize
, u32 cap
)
96 buffer
->esize
= esize
;
97 buffer
->capacity
= cap
;
101 buffer
->arr
= malloc( buffer
->esize
*buffer
->capacity
);
106 static void cxr_ab_clear( struct cxr_abuffer
*buffer
)
111 static void cxr_ab_free( struct cxr_abuffer
*buffer
)
116 #endif /* CXR_TYPES_H */