Last active
January 16, 2016 18:17
-
-
Save prideout/1bb39a0fd564d20c3b6b to your computer and use it in GitHub Desktop.
dynamic array of uint16_t in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1)) | |
| #define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * N)) | |
| #define PAR_FREE(BUF) free(BUF) | |
| typedef struct { | |
| uint16_t* values; | |
| size_t count; | |
| size_t capacity; | |
| } par__uint16list; | |
| static par__uint16list* par__uint16list_create() | |
| { | |
| par__uint16list* list = PAR_CALLOC(par__uint16list, 1); | |
| list->count = 0; | |
| list->capacity = 32; | |
| list->values = PAR_CALLOC(uint16_t, list->capacity); | |
| return list; | |
| } | |
| static void par__uint16list_add3(par__uint16list* list, | |
| uint16_t a, uint16_t b, uint16_t c) | |
| { | |
| if (list->count + 3 > list->capacity) { | |
| list->capacity *= 2; | |
| list->values = PAR_REALLOC(uint16_t, list->values, list->capacity); | |
| } | |
| list->values[list->count++] = a; | |
| list->values[list->count++] = b; | |
| list->values[list->count++] = c; | |
| } | |
| static void par__uint16list_free(par__uint16list* list) | |
| { | |
| if (list) { | |
| PAR_FREE(list->values); | |
| PAR_FREE(list); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment