├── LICENSE ├── README.md ├── array.c └── array.h /LICENSE: -------------------------------------------------------------------------------- 1 | array.h is a fork from zauonlok's (github.com/zauonlok) darray.h file with 2 | small syntax changes. 3 | 4 | ------------------------------------------------------------------------------ 5 | 6 | darray.h is under the following license: 7 | 8 | MIT License 9 | 10 | Copyright (c) 2020 Zhou Le 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy 13 | of this software and associated documentation files (the "Software"), to deal 14 | in the Software without restriction, including without limitation the rights 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | copies of the Software, and to permit persons to whom the Software is 17 | furnished to do so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C Dynamic Array 2 | 3 | An extremely simple (and not type-safe) dynamic array implementation in C. 4 | 5 | This code is a fork from [zaunlok](https://github.com/zauonlok)'s [darray.h](https://github.com/zauonlok/renderer/blob/master/renderer/core/darray.h) and [darray.c](https://github.com/zauonlok/renderer/blob/master/renderer/core/darray.c) with some minimal syntax and name changes. 6 | -------------------------------------------------------------------------------- /array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "array.h" 4 | 5 | #define ARRAY_RAW_DATA(array) ((int*)(array) - 2) 6 | #define ARRAY_CAPACITY(array) (ARRAY_RAW_DATA(array)[0]) 7 | #define ARRAY_OCCUPIED(array) (ARRAY_RAW_DATA(array)[1]) 8 | 9 | void* array_hold(void* array, int count, int item_size) { 10 | if (array == NULL) { 11 | int raw_size = (sizeof(int) * 2) + (item_size * count); 12 | int* base = (int*)malloc(raw_size); 13 | base[0] = count; // capacity 14 | base[1] = count; // occupied 15 | return base + 2; 16 | } else if (ARRAY_OCCUPIED(array) + count <= ARRAY_CAPACITY(array)) { 17 | ARRAY_OCCUPIED(array) += count; 18 | return array; 19 | } else { 20 | int needed_size = ARRAY_OCCUPIED(array) + count; 21 | int double_curr = ARRAY_CAPACITY(array) * 2; 22 | int capacity = needed_size > double_curr ? needed_size : double_curr; 23 | int occupied = needed_size; 24 | int raw_size = sizeof(int) * 2 + item_size * capacity; 25 | int* base = (int*)realloc(ARRAY_RAW_DATA(array), raw_size); 26 | base[0] = capacity; 27 | base[1] = occupied; 28 | return base + 2; 29 | } 30 | } 31 | 32 | int array_length(void* array) { 33 | return (array != NULL) ? ARRAY_OCCUPIED(array) : 0; 34 | } 35 | 36 | void array_free(void* array) { 37 | if (array != NULL) { 38 | free(ARRAY_RAW_DATA(array)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /array.h: -------------------------------------------------------------------------------- 1 | #ifndef ARRAY_H 2 | #define ARRAY_H 3 | 4 | #define array_push(array, value) \ 5 | do { \ 6 | (array) = array_hold((array), 1, sizeof(*(array))); \ 7 | (array)[array_length(array) - 1] = (value); \ 8 | } while (0); 9 | 10 | void* array_hold(void* array, int count, int item_size); 11 | int array_length(void* array); 12 | void array_free(void* array); 13 | 14 | #endif 15 | --------------------------------------------------------------------------------