2018-01-05 16:17:55 -05:00
|
|
|
/*
|
2023-01-07 15:03:35 -06:00
|
|
|
* Copyright (c) 2023 Calvin Rose
|
2018-01-05 16:17:55 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 00:15:58 -05:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-30 19:06:15 -05:00
|
|
|
#include "features.h"
|
2018-07-03 23:07:35 -04:00
|
|
|
#include "vector.h"
|
2019-04-27 15:45:28 -04:00
|
|
|
#include "util.h"
|
2019-01-24 00:15:58 -05:00
|
|
|
#endif
|
2018-01-19 16:43:19 -05:00
|
|
|
|
|
|
|
/* Grow the buffer dynamically. Used for push operations. */
|
2018-09-05 22:18:42 -04:00
|
|
|
void *janet_v_grow(void *v, int32_t increment, int32_t itemsize) {
|
|
|
|
int32_t dbl_cur = (NULL != v) ? 2 * janet_v__cap(v) : 0;
|
|
|
|
int32_t min_needed = janet_v_count(v) + increment;
|
2018-01-19 16:43:19 -05:00
|
|
|
int32_t m = dbl_cur > min_needed ? dbl_cur : min_needed;
|
2019-10-10 22:59:43 -05:00
|
|
|
size_t newsize = ((size_t) itemsize) * m + sizeof(int32_t) * 2;
|
|
|
|
int32_t *p = (int32_t *) janet_srealloc(v ? janet_v__raw(v) : 0, newsize);
|
2019-06-01 23:31:39 -04:00
|
|
|
if (!v) p[1] = 0;
|
|
|
|
p[0] = m;
|
|
|
|
return p + 2;
|
2018-01-19 16:43:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a buffer to normal allocated memory (forget capacity) */
|
2018-09-05 22:18:42 -04:00
|
|
|
void *janet_v_flattenmem(void *v, int32_t itemsize) {
|
2023-06-01 10:52:34 -05:00
|
|
|
char *p;
|
2018-01-19 16:43:19 -05:00
|
|
|
if (NULL == v) return NULL;
|
2020-01-02 22:02:57 -06:00
|
|
|
size_t size = (size_t) itemsize * janet_v__cnt(v);
|
2021-03-23 23:00:48 +13:00
|
|
|
p = janet_malloc(size);
|
2018-01-19 16:43:19 -05:00
|
|
|
if (NULL != p) {
|
2020-01-28 23:38:52 -06:00
|
|
|
safe_memcpy(p, v, size);
|
2018-01-19 16:43:19 -05:00
|
|
|
return p;
|
|
|
|
} else {
|
2020-01-02 22:02:57 -06:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-01-19 16:43:19 -05:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 16:17:55 -05:00
|
|
|
|