12 |
#define _DIDEROT_DYN_SEQ_H_ |
#define _DIDEROT_DYN_SEQ_H_ |
13 |
|
|
14 |
typedef struct { |
typedef struct { |
15 |
unsigned int base; |
unsigned int base; //!< data[base] is the first element in the sequence |
16 |
size_t nElems; //!< number of elements in the sequence |
unsigned int nElems; //!< number of elements in the sequence |
17 |
size_t size; //!< size of data buffer in elements |
unsigned int size; //!< size of data buffer in elements |
18 |
void *data; //!< pointer to underlying data buffer |
void *data; //!< pointer to underlying data buffer |
19 |
} Diderot_DynSeq_t; |
} Diderot_DynSeq_t; |
20 |
|
|
21 |
#define DYNSEQ_SUB(ty, seq, i) (*((ty *)Diderot_DynSeqAddr(sizeof(ty), seq, i))) |
#define DYNSEQ_SUB(ty, seq, i) (*((ty *)Diderot_DynSeqAddr(sizeof(ty), seq, i))) |
22 |
#define DYNSEQ_MK(ty, n, data) Diderot_DynSeqMk(sizeof(ty), n, data) |
#define DYNSEQ_MK(ty, n, data) Diderot_DynSeqMk(sizeof(ty), n, data) |
23 |
|
|
24 |
inline unsigned int Diderot_DynSeqLength (Diderot_DynSeq_t *seq) |
STATIC_INLINE unsigned int Diderot_DynSeqLength (Diderot_DynSeq_t *seq) |
25 |
{ |
{ |
26 |
return seq->nElems; |
return seq->nElems; |
27 |
} |
} |
28 |
|
|
29 |
inline Diderot_DynSeq_t *Diderot_DynSeqAlloc (size_t elemSz, int initialSz) |
STATIC_INLINE Diderot_DynSeq_t *Diderot_DynSeqAlloc (size_t elemSz, int initialSz) |
30 |
{ |
{ |
31 |
Diderot_DynSeq_t *seq = NEW(Diderot_DynSeq_t); |
Diderot_DynSeq_t *seq = NEW(Diderot_DynSeq_t); |
32 |
seq->base = 0; |
seq->base = 0; |
33 |
seq->nElems = initialSz; |
seq->nElems = initialSz; |
34 |
seq->size = initialSz; // FIXME: allow some padding |
seq->size = initialSz + 8; // FIXME: be smarter about padding |
35 |
seq->data = CheckedAlloc (elemSz * seq->size); |
seq->data = CheckedAlloc (elemSz * seq->size); |
36 |
return seq; |
return seq; |
37 |
} |
} |
38 |
|
|
39 |
inline Diderot_DynSeq_t *Diderot_DynSeqMk (size_t elemSz, int nElems, void *elems) |
STATIC_INLINE Diderot_DynSeq_t *Diderot_DynSeqMk (size_t elemSz, int nElems, void *elems) |
40 |
{ |
{ |
41 |
Diderot_DynSeq_t *seq = Diderot_DynSeqAlloc(elemSz, nElems); |
Diderot_DynSeq_t *seq = Diderot_DynSeqAlloc(elemSz, nElems); |
42 |
unsigned char *p = (unsigned char *)seq->data; |
unsigned char *p = (unsigned char *)seq->data; |
49 |
return seq; |
return seq; |
50 |
} |
} |
51 |
|
|
52 |
inline void *Diderot_DynSeqAddr (size_t elemSz, Diderot_DynSeq_t *seq, int i) |
STATIC_INLINE void *Diderot_DynSeqAddr (size_t elemSz, Diderot_DynSeq_t *seq, int i) |
53 |
{ |
{ |
54 |
i = (seq->base + i) % seq->size; |
i = (seq->base + i) % seq->size; |
55 |
return ((char *)(seq->data) + elemSz * i); |
return ((char *)(seq->data) + elemSz * i); |