Files

79 lines
1.9 KiB
C
Raw Permalink Normal View History

2021-11-10 11:27:58 -05:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
2019-05-09 10:38:17 -07:00
*
2021-11-10 11:27:58 -05:00
* (c)2019-2021 ZeroTier, Inc.
* https://www.zerotier.com/
2018-11-01 10:17:12 -07:00
*/
2018-11-09 21:06:52 -08:00
#ifndef ZTLF_VECTOR_H
#define ZTLF_VECTOR_H
2018-11-01 10:17:12 -07:00
#include "common.h"
2018-12-10 15:16:48 -08:00
/* Vector for pointers */
2018-11-16 16:37:27 -08:00
struct ZTLF_Vector
{
2018-12-10 15:16:48 -08:00
void **v;
2018-11-16 16:37:27 -08:00
unsigned long size;
unsigned long cap;
};
2018-12-10 15:16:48 -08:00
static inline void ZTLF_Vector_Init(struct ZTLF_Vector *const vec,const unsigned long initialCapacity)
2018-11-16 16:37:27 -08:00
{
if (initialCapacity > 0) {
2018-12-10 15:16:48 -08:00
ZTLF_MALLOC_CHECK(vec->v = (void **)malloc(sizeof(void *) * initialCapacity));
2018-11-16 16:37:27 -08:00
} else {
vec->v = NULL;
}
vec->size = 0;
vec->cap = initialCapacity;
}
2018-12-10 15:16:48 -08:00
#define ZTLF_Vector_Append(vec,i) { \
2018-11-16 16:37:27 -08:00
if (unlikely((vec)->size >= (vec)->cap)) { \
(vec)->cap = ((vec)->cap) ? ((vec)->cap << 1) : 1024; \
2018-12-10 15:16:48 -08:00
ZTLF_MALLOC_CHECK((vec)->v = (void **)realloc((vec)->v,sizeof(void *) * (vec)->cap)); \
2018-11-16 16:37:27 -08:00
} \
2018-12-10 15:16:48 -08:00
(vec)->v[(vec)->size++] = (i); \
2018-11-16 16:37:27 -08:00
}
2018-12-10 15:16:48 -08:00
#define ZTLF_Vector_Clear(vec) (vec)->size = 0
2018-11-16 16:37:27 -08:00
2018-12-10 15:16:48 -08:00
#define ZTLF_Vector_Free(vec) if ((vec)->v) { free((vec)->v); }
2018-12-04 16:42:10 -08:00
2018-11-16 16:37:27 -08:00
/* Vector for 64-bit ints */
2018-11-09 21:06:52 -08:00
struct ZTLF_Vector_i64
2018-11-01 10:17:12 -07:00
{
2018-11-09 21:06:52 -08:00
int64_t *v;
unsigned long size;
unsigned long cap;
};
2018-11-01 10:17:12 -07:00
2018-12-10 15:16:48 -08:00
static inline void ZTLF_Vector_i64_Init(struct ZTLF_Vector_i64 *const vec,const unsigned long initialCapacity)
2018-11-09 21:06:52 -08:00
{
if (initialCapacity > 0) {
ZTLF_MALLOC_CHECK(vec->v = (int64_t *)malloc(sizeof(int64_t) * initialCapacity));
} else {
vec->v = NULL;
2018-11-01 10:17:12 -07:00
}
2018-11-09 21:06:52 -08:00
vec->size = 0;
vec->cap = initialCapacity;
2018-11-01 10:17:12 -07:00
}
2018-12-10 15:16:48 -08:00
#define ZTLF_Vector_i64_Append(vec,i) { \
2018-11-09 21:06:52 -08:00
if (unlikely((vec)->size >= (vec)->cap)) { \
2018-11-14 16:53:17 -08:00
(vec)->cap = ((vec)->cap) ? ((vec)->cap << 1) : 1024; \
ZTLF_MALLOC_CHECK((vec)->v = (int64_t *)realloc((vec)->v,sizeof(int64_t) * (vec)->cap)); \
2018-11-09 21:06:52 -08:00
} \
(vec)->v[(vec)->size++] = (i); \
}
2018-12-10 15:16:48 -08:00
#define ZTLF_Vector_i64_Clear(vec) (vec)->size = 0
2018-11-09 21:06:52 -08:00
2018-12-10 15:16:48 -08:00
#define ZTLF_Vector_i64_Free(vec) if ((vec)->v) { free((vec)->v); }
2018-11-09 21:06:52 -08:00
2018-11-01 10:17:12 -07:00
#endif