htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
darray.h File Reference
#include <h2o/memory.h>
#include <htils/assert.h>
#include <htils/basictypes.h>

Go to the source code of this file.

Data Structures

struct  h2o_h2o_da_header
 The header of a dynamic array. More...
 

Macros

#define H2O_DA_HEADER_SIZE
 
#define h2o_da_len(darray)   ((darray) ? h2o_da__hdr(darray)->len : 0)
 
#define h2o_da_cap(darray)   ((darray) ? h2o_da__hdr(darray)->cap : 0)
 
#define h2o_da_new(pool, darray, intitial_capacity)
 Initialize a dynamic array.
 
#define h2o_da_append(pool, darray, item)
 Append an item to a dynamic array.
 
#define h2o_da_pop(darray)
 Pop an item from a dynamic array.
 
#define h2o_da_last(darray)
 Substitute with the last item in a dynamic array.
 
#define h2o_da_clear(darray)
 Clears a dynamic array.
 

Typedefs

typedef struct h2o_h2o_da_header h2o_da_header_t
 The header of a dynamic array.
 

Functions

static h2o_da_header_th2o_da__hdr (void *h2o_da)
 Get the dynamic array header.
 

Macro Definition Documentation

◆ h2o_da_append

#define h2o_da_append (   pool,
  darray,
  item 
)
Value:
do { \
htils_assert(pool != null && "Pool cannot be null."); \
htils_assert(darray != null && "Darray cannot be null."); \
htils_assert(item != null && "Item cannot be null."); \
\
if (!(darray) || h2o_da__hdr(darray)->len >= h2o_da__hdr(darray)->cap) { \
u64 old_capacity = h2o_da__hdr(darray)->cap; \
u64 new_capacity = old_capacity ? old_capacity * 2 : 8; \
u64 old_len = h2o_da__hdr(darray)->len; \
u64 alloc_size = H2O_DA_HEADER_SIZE + new_capacity; \
\
h2o_da_header_t *header = \
h2o_mem_alloc_pool((pool), darray, alloc_size); \
\
header->cap = new_capacity; \
header->len = old_len; \
\
if (darray) \
memcpy(header + 1, (darray), old_len * sizeof(*(darray))); \
\
(darray) = (void *)(header + 1); \
} \
\
(darray)[h2o_da__hdr(darray)->len++] = (item); \
} while (0)
uint64_t u64
Definition basictypes.h:17
#define null
Definition basictypes.h:47
#define H2O_DA_HEADER_SIZE
Definition darray.h:33
static h2o_da_header_t * h2o_da__hdr(void *h2o_da)
Get the dynamic array header.
Definition darray.h:53
The header of a dynamic array.
Definition darray.h:23
u64 len
Definition darray.h:25
u64 cap
Definition darray.h:24

Append an item to a dynamic array.

By incrementing the len, has automatic capacity growing functionality.

Parameters
poolThe memory pool to allocate the dynamic array from.
darrayThe dynamic array to append to.
itemThe item to append to the dynamic array.
Precondition
pool, darray, and item must be valid and cannot be null.

◆ h2o_da_cap

#define h2o_da_cap (   darray)    ((darray) ? h2o_da__hdr(darray)->cap : 0)

Gets the capacity of the current dynamic array

◆ h2o_da_clear

#define h2o_da_clear (   darray)
Value:
do { \
if (darray) \
h2o_da__hdr(darray)->len = 0; \
} while (0)

Clears a dynamic array.

By setting its length to 0, if the dynamic array is null, nothing happens.

Parameters
darrayThe dynamic array to clear.

◆ H2O_DA_HEADER_SIZE

#define H2O_DA_HEADER_SIZE
Value:
((sizeof(h2o_da_header_t) + _Alignof(max_align_t) - 1) & \
~(_Alignof(max_align_t) - 1))
struct h2o_h2o_da_header h2o_da_header_t
The header of a dynamic array.

The size of the header of a dynamic array.

◆ h2o_da_last

#define h2o_da_last (   darray)
Value:
(htils_assert(h2o_da__hdr(darray)->len > 0), \
(darray)[da__hdr(darray)->len - 1])
#define htils_assert(expr)
Definition assert.h:58
static da_header_t * da__hdr(void *da)
Get the da_header.
Definition darray.h:44
u64 len
Definition darray.h:16

Substitute with the last item in a dynamic array.

Asserts that the length of the dynamic array is larger than 0, then simply gets the last entry.

Parameters
darrayThe dynamic array to get the last item from.

◆ h2o_da_len

#define h2o_da_len (   darray)    ((darray) ? h2o_da__hdr(darray)->len : 0)

Gets the length of the current dynamic array

◆ h2o_da_new

#define h2o_da_new (   pool,
  darray,
  intitial_capacity 
)
Value:
do { \
htils_assert(pool != null && "pool cannot be null."); \
htils_assert(intitial_capacity > 0 && \
"Initial capacity must be greater than 0"); \
\
u64 capacity = (intitial_capacity); \
u64 alloc_size = H2O_DA_HEADER_SIZE + capacity; \
\
h2o_da_header_t *header = h2o_mem_alloc_pool((pool), darray, alloc_size); \
header->cap = capacity; \
(darray) = (void *)(header + 1); \
} while (0)

Initialize a dynamic array.

Caluclates the size for the header with H2O_DA_HEADER_SIZE, and the capacity; Allocates the header with initial_capacity, then sets the dynamic array to be the header + 1 offsetting the header.

Parameters
poolThe memory pool to allocate the dynamic array from.
darrayThe dynamic array to initialize.
intitial_capacityThe initial capacity of the dynamic array.
Precondition
  • pool, and darray must be valid and cannot be null.
  • intitial_capacity must be greater than 0.

◆ h2o_da_pop

#define h2o_da_pop (   darray)
Value:
do { \
if (darray && h2o_da__hdr(darray)->len > 0) \
h2o_da__hdr(darray)->len--; \
} while (0)

Pop an item from a dynamic array.

By simply decrementing its length, if the dynamic array is empty, nothing happens.

Parameters
darrayThe dynamic array to pop from.

Typedef Documentation

◆ h2o_da_header_t

The header of a dynamic array.

Parameters
capThe capacity of the dynamic array.
lenThe length of the dynamic array.

Function Documentation

◆ h2o_da__hdr()

static h2o_da_header_t * h2o_da__hdr ( void *  h2o_da)
inlinestatic

Get the dynamic array header.

Casts the da to cstr * from a dynamic array, by then offsetting by the size of it.

Parameters
daThe dynamic array to get the header from.
Precondition
da must be valid and cannot be null.
Returns
The header of the dynamic array.