From 79f5751375353a80a57bd50c4aeba76077095f02 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 14 Jun 2020 17:40:48 -0500 Subject: [PATCH] Add array/trim and buffer/trim. --- CHANGELOG.md | 4 ++++ meson.build | 2 +- src/conf/janetconf.h | 6 +++--- src/core/array.c | 25 +++++++++++++++++++++++++ src/core/buffer.c | 26 ++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1623a17..00b4bba0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. +## Unreleased - ??? +- Add `array/trim` and `buffer/trim` to shrink the backing capacity of these types + to their current length. + ## 1.10.0 - 2020-06-14 - Hardcode default jpm paths on install so env variables are needed in fewer cases. - Add `:no-compile` to `create-executable` option for jpm. diff --git a/meson.build b/meson.build index 55e04f93..38e5d2a0 100644 --- a/meson.build +++ b/meson.build @@ -20,7 +20,7 @@ project('janet', 'c', default_options : ['c_std=c99', 'b_lundef=false', 'default_library=both'], - version : '1.10.0') + version : '1.10.1-dev') # Global settings janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet') diff --git a/src/conf/janetconf.h b/src/conf/janetconf.h index 100e2156..0808ad77 100644 --- a/src/conf/janetconf.h +++ b/src/conf/janetconf.h @@ -28,9 +28,9 @@ #define JANET_VERSION_MAJOR 1 #define JANET_VERSION_MINOR 10 -#define JANET_VERSION_PATCH 0 -#define JANET_VERSION_EXTRA "" -#define JANET_VERSION "1.10.0" +#define JANET_VERSION_PATCH 1 +#define JANET_VERSION_EXTRA "-dev" +#define JANET_VERSION "1.10.1-dev" /* #define JANET_BUILD "local" */ diff --git a/src/core/array.c b/src/core/array.c index 46014d9c..a7f92155 100644 --- a/src/core/array.c +++ b/src/core/array.c @@ -270,6 +270,26 @@ static Janet cfun_array_remove(int32_t argc, Janet *argv) { return argv[0]; } +static Janet cfun_array_trim(int32_t argc, Janet *argv) { + janet_fixarity(argc, 1); + JanetArray *array = janet_getarray(argv, 0); + if (array->count) { + if (array->count < array->capacity) { + Janet *newData = realloc(array->data, array->count * sizeof(Janet)); + if (NULL == newData) { + JANET_OUT_OF_MEMORY; + } + array->data = newData; + array->capacity = array->count; + } + } else { + array->capacity = 0; + free(array->data); + array->data = NULL; + } + return argv[0]; +} + static const JanetReg array_cfuns[] = { { "array/new", cfun_array_new, @@ -345,6 +365,11 @@ static const JanetReg array_cfuns[] = { "By default, n is 1. " "Returns the array.") }, + { + "array/trim", cfun_array_trim, + JDOC("(array/trim arr)\n\n" + "Set the backing capacity of an array to its current length. Returns the modified array.") + }, {NULL, NULL, NULL} }; diff --git a/src/core/buffer.c b/src/core/buffer.c index ee205600..6e040c00 100644 --- a/src/core/buffer.c +++ b/src/core/buffer.c @@ -197,6 +197,26 @@ static Janet cfun_buffer_fill(int32_t argc, Janet *argv) { return argv[0]; } +static Janet cfun_buffer_trim(int32_t argc, Janet *argv) { + janet_fixarity(argc, 1); + JanetBuffer *buffer = janet_getbuffer(argv, 0); + if (buffer->count) { + if (buffer->count < buffer->capacity) { + uint8_t *newData = realloc(buffer->data, buffer->count); + if (NULL == newData) { + JANET_OUT_OF_MEMORY; + } + buffer->data = newData; + buffer->capacity = buffer->count; + } + } else { + buffer->capacity = 0; + free(buffer->data); + buffer->data = NULL; + } + return argv[0]; +} + static Janet cfun_buffer_u8(int32_t argc, Janet *argv) { int32_t i; janet_arity(argc, 1, -1); @@ -379,6 +399,12 @@ static const JanetReg buffer_cfuns[] = { "Fill up a buffer with bytes, defaulting to 0s. Does not change the buffer's length. " "Returns the modified buffer.") }, + { + "buffer/trim", cfun_buffer_trim, + JDOC("(buffer/trim buffer)\n\n" + "Set the backing capacity of the buffer to the current length of the buffer. Returns the " + "modified buffer.") + }, { "buffer/push-byte", cfun_buffer_u8, JDOC("(buffer/push-byte buffer x)\n\n"