|
| 1 | +// Copyright (c) 2020-2024 ByteDance, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in all |
| 11 | +// copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | +// |
| 21 | + |
| 22 | +// Created by Kelun Cai ([email protected]) on 2024-09-24. |
| 23 | + |
| 24 | +#include "bh_array.h" |
| 25 | + |
| 26 | +#include <stdlib.h> |
| 27 | +#include <string.h> |
| 28 | + |
| 29 | +int bh_array_push(bh_array_t *self, uintptr_t value) { |
| 30 | + if (__predict_false(self->count >= self->cap)) { |
| 31 | + self->cap *= 2; |
| 32 | + uintptr_t *heap = NULL; |
| 33 | + if (self->data == self->stack) { |
| 34 | + heap = malloc(sizeof(uintptr_t) * self->cap); |
| 35 | + if (NULL == heap) return -1; |
| 36 | + memcpy(heap, self->stack, sizeof(self->stack)); |
| 37 | + } else { |
| 38 | + heap = realloc(self->data, sizeof(uintptr_t) * self->cap); |
| 39 | + if (NULL == heap) { |
| 40 | + free(self->data); |
| 41 | + return -1; |
| 42 | + } |
| 43 | + } |
| 44 | + self->data = heap; |
| 45 | + } |
| 46 | + |
| 47 | + self->data[self->count] = value; |
| 48 | + self->count++; |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +void bh_array_free(bh_array_t *self) { |
| 53 | + if (__predict_false(self->data != self->stack)) free(self->data); |
| 54 | +} |
0 commit comments