summaryrefslogtreecommitdiffstats
path: root/buf.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2020-06-29 21:21:06 -0700
committerKaz Kylheku <kaz@kylheku.com>2020-06-29 21:21:06 -0700
commit9557a79765457c2cd1a7214f0d8d51d2ea62ed70 (patch)
treef01f0ef6ffe860d2ccabb053d937b24e058ebc04 /buf.c
parent7006ede97348c57aff89d6af7d19bb6411d9b3f6 (diff)
downloadtxr-9557a79765457c2cd1a7214f0d8d51d2ea62ed70.tar.gz
txr-9557a79765457c2cd1a7214f0d8d51d2ea62ed70.tar.bz2
txr-9557a79765457c2cd1a7214f0d8d51d2ea62ed70.zip
c_num: now takes self argument.
The c_num and c_unum functions now take a self argument for identifying the calling function. This requires changes in a large number of places. In a few places, additional functions acquire a self argument. The ffi module has the most extensive example of this. Some functions mention their name in a larger string, or have scattered literals giving their name; with the introduction of the self local variable, these are replaced by references to self. In the following changelog, the notation TS stands for "take self argument", meaning that the functions acquires a new "val self" argument. The notation DS means "define self": the functions in question defines a self variable, which they pass down. The notation PS means that the functions pass down an existing self variable to functions that now require it. * args.h (args_count): TS. * arith.c (c_unum, c_num): TS. (toint, exptv): DS. * buf.c (buf_check_len, buf_check_alloc_size, buf_check_index, buf_do_set_len, replace_buf, buf_put_buf, buf_put_i8, buf_put_u8, buf_put_char, buf_put_uchar, buf_get_bytes, buf_get_i8, buf_get_u8, buf_get_cptr, buf_strm_get_byte_callback, buf_strm_unget_byte, buf_swap32, str_buf, buf_int, buf_uint, int_buf, uint_buf): PS. (make_duplicate_buf, buf_shrink, sub_buf, buf_print, buf_pprint): DS. * chskum.c (sha256_stream_impl, sha256_buf, crc32_buf, md5_stream_impl, md5_buf): TS. (chksum_ensure_buf, sha256_stream, sha256, sha256_hash, md5_stream, md5, md5_hash): PS. (crc32_stream): DS. * combi.c (perm_while_fun, perm_gen_fun_common, perm_str_gen_fun, rperm_gen_fun, comb_vec_gen_fun, comb_str_gen_fun, rcomb_vec_gen_fun, rcomb_str_gen_fun): DS. * diff.c (dbg_clear, dbg_set, dbg_restore): DS. * eval.c (do_eval, gather_free_refs, maprodv, maprendv, maprodo, do_args_apf, do_args_ipf): DS. (op_dwim, me_op, map_common): PS. (prod_common): TS. * ffi.c (struct txr_ffi_type): release member TS. (make_ffi_type_pointer): PS and release argument TS. (ffi_varray_dynsize, ffi_array_in, ffi_array_put_common, ffi_array_get_common, ffi_varray_in, ffi_varray_null_term): PS. (ffi_simple_release, ffi_ptr_in_release, ffi_struct_release, ffi_wchar_array_get, ffi_array_release_common, ffi_array_release, ffi_varray_release): TS. (ffi_float_put, double_put, ffi_be_i16_put, ffi_be_u16_put, ffi_le_i16_put, ffi_le_u16_put, ffi_be_i32_put, ffi_be_u32_put, ffi_le_i32_put, ffi_sbit_put, ffi_ubit_put, ffi_buf_d_put, make_ffi_type_array, make_ffi_type_enum, ffi_type_compile, make_ffi_type_desc, ffi_make_call_desc, ffi_call_wrap, ffi_closure_dispatch_save, ffi_put_into, ffi_in, ffi_get, ffi_put, carray_set_length, carray_blank, carray_buf, carray_buf_sync, carray_cptr, carray_refset, carray_sub, carray_replace, carray_uint, carray_int): PS. (carray_vec, carray_list): DS. * filter.c (url_encode, url_decode, base64_stream_enc_impl): DS. * ftw.c (ftw_callback, ftw_wrap): DS. * gc.c (mark_obj, gc_set_delta): DS. * glob.c (glob_wrap): DS. * hash.c (equal_hash, eql_hash, eq_hash, do_make_hash, hash_equal, set_hash_traversal_limit, gen_hash_seed): DS. * itypes.c (c_i8, c_u8, c_i16, c_u16, c_i32, c_u32, c_i64, c_u64, c_short, c_ushort, c_int, c_uint, c_long, c_ulong): PS. * lib.c (seq_iter_rewind): TS and becomes internal. (seq_iter_init_with_info, seq_setpos, replace_str, less, replace_vec, diff, isec, obj_print_impl): PS. (nthcdr, equal, mkstring, mkustring, upcase_str, downcase_str, search_str, sub_str, cat_str, scat2, scat3, fmt_join, split_str_keep, split_str_set, trim_str, int_str, chr_int, chr_str, chr_str_set, vector, vecref, vecref_l, list_vec, copy_vec, sub_vec, cat_vec, lazy_str_put, lazy_str_gt, length_str_ge, length_str_lt, length_str_le, cptr_size_hint, cptr_int, out_lazy_str, out_quasi_str, time_string_local_time, time_string_utc, time_fields_local_time, time_fields_utc, time_struct_local, time_struct_utc, make_time, time_meth, time_parse_meth): DS. (init_str, cat_str_init, cat_str_measure, cat_str_append, vscat, time_fields_to_tm, time_struct_to_tm, make_time_impl): TS. * lib.h (seq_iter_rewind): Declaration removed. (c_num, c_unum, init_str): Declarations updated. * match.c (LOG_MISMATCH, LOG_MATCH): PS. (h_skip, h_coll, do_output_line, do_output, v_skip, v_fuzz, v_collect): DS. * parser.c (parser, circ_backpatch, report_security_problem, hist_save, repl, lino_fileno, lino_getch, lineno_getl, lineno_gets, lineno_open): DS. (parser_set_lineno, lisp_parse_impl): PS. * parser.l (YY_INPUT): PS. * rand.c (make_random_state): PS. * regex.c (print_rec): DS. (search_regex): PS. * signal.c (kill_wrap, raise_wrap, get_sig_handler, getitimer_wrap, setitimer_wrap): DS. * socket.c (addrinfo_in, sockaddr_pack, fd_timeout, to_connect, open_sockfd, sock_mark_connected, sock_timeout): TS. (getaddrinfo_wrap, dgram_set_sock_peer, sock_bind, sock_connect, sock_listen, sock_accept, sock_shutdown, sock_send_timeout, sock_recv_timeout, socketpair_wrap): DS. * stream.c (generic_fill_buf, errno_to_string, stdio_truncate, string_out_put_string, open_fileno, open_command, base_name, dir-name): DS. (unget_byte, put_buf, fill_buf, fill_buf_adjust, get_line_as_buf, formatv, put_byte, test_set_indent_mode, test_neq_set_indent_mode, set_indent_mode, set_indent, inc_indent, set_max_length, set_max_depth, open_subprocess, run ): PS. (fds_subst, fds_swizzle): TS. * struct.c (make_struct_type, super, umethod_args_fun): PS. (method_args_fun): DS. * strudel.c (strudel_put_buf, strudel_fill_buf): DS. * sysif.c (errno_wrap, exit_wrap, usleep_wrap, mkdir_wrap, ensure_dir, makedev_wrap, minor_wrap, major_wrap, mknod_wrap, mkfifo_wrap, wait_wrap, wifexited, wexitstatus, wifsignaled, wtermsig, wcoredump, wifstopped, wstopsig, wifcontinued, dup_wrap, close_wrap, exit_star_wrap, umask_wrap, setuid_wrap, seteuid_wrap, setgid_wrap, setegid_wrap, simulate_setuid_setgid, getpwuid_wrap, fnmatch_wrap, dlopen_wrap): DS. (chmod_wrap, do_chown, flock_pack, do_utimes, poll_wrap, setgroups_wrap, setresuid_wrap, setresgid_wrap, getgrgid_wrap): PS. (c_time): TS. * sysif.h (c_time): Declaration updated. * syslog.c (openlog_wrap, syslog_wrap): DS. * termios.c (termios_pack): TS. (tcgetattr_wrap, tcsetattr_wrap, tcsendbreak_wrap, tcdrain_wrap, tcflush_wrap, tcflow_rap, encode_speeds, decode_speeds): DS. * txr.c (compato, array_dim, gc_delta): DS. * unwind.c (uw_find_frames_by_mask): DS. * vm.c (vm_make_desc): PS. (vm_make_closure, vm_swtch): DS.
Diffstat (limited to 'buf.c')
-rw-r--r--buf.c85
1 files changed, 45 insertions, 40 deletions
diff --git a/buf.c b/buf.c
index 3cd050ef..9a9d8ff6 100644
--- a/buf.c
+++ b/buf.c
@@ -47,7 +47,7 @@
static cnum buf_check_len(val len, val self)
{
- cnum l = c_num(len);
+ cnum l = c_num(len, self);
if (l < 0)
uw_throwf(error_s, lit("~a: negative length ~s specified"),
self, len, nao);
@@ -56,7 +56,7 @@ static cnum buf_check_len(val len, val self)
static cnum buf_check_alloc_size(val alloc_size, cnum len, val self)
{
- cnum ah = c_num(alloc_size);
+ cnum ah = c_num(alloc_size, self);
if (ah < len)
uw_throwf(error_s, lit("~a: alloc size size ~s lower than length"),
self, alloc_size, nao);
@@ -65,9 +65,9 @@ static cnum buf_check_alloc_size(val alloc_size, cnum len, val self)
static cnum buf_check_index(struct buf *b, val index, val self)
{
- cnum ix = c_num(index);
+ cnum ix = c_num(index, self);
if (ix < 0)
- ix = c_num(plus(b->len, index));
+ ix = c_num(plus(b->len, index), self);
if (ix < 0)
uw_throwf(error_s, lit("~a: negative byte index ~s specified"),
self, index, nao);
@@ -119,10 +119,11 @@ val make_borrowed_buf(val len, mem_t *data)
val make_duplicate_buf(val len, mem_t *data)
{
+ val self = lit("make-duplicate-buf");
val obj = make_obj();
obj->b.type = BUF;
- obj->b.data = chk_copy_obj(data, c_num(len));
+ obj->b.data = chk_copy_obj(data, c_num(len, self));
obj->b.len = len;
obj->b.size = len;
@@ -154,13 +155,14 @@ val copy_buf(val buf)
static void buf_shrink(struct buf *b)
{
+ val self = lit("buf-trim");
val len = b->len;
if (len == zero)
len = succ(len); /* avoid reallocing to zero length; i.e. freeing */
if (len != b->size) {
- b->data = chk_realloc(b->data, c_unum(len));
+ b->data = chk_realloc(b->data, c_unum(len, self));
b->size = b->len;
}
}
@@ -181,8 +183,8 @@ static val buf_do_set_len(val buf, struct buf *b, val newlen,
val init_val, val self)
{
val oldlen = b->len;
- cnum olen = c_num(oldlen), len = c_num(newlen);
- cnum oldsize = c_num(b->size), size = oldsize;
+ cnum olen = c_num(oldlen, self), len = c_num(newlen, self);
+ cnum oldsize = c_num(b->size, self), size = oldsize;
cnum iv = c_u8(default_arg(init_val, zero), self);
if (!b->size)
@@ -242,6 +244,7 @@ mem_t *buf_get(val buf, val self)
val sub_buf(val buf, val from, val to)
{
+ val self = lit("sub-buf");
struct buf *b = buf_handle(buf, lit("sub"));
val len = b->len;
@@ -268,7 +271,7 @@ val sub_buf(val buf, val from, val to)
} else if (from == 0 && to == len) {
return buf;
} else {
- return make_duplicate_buf(minus(to, from), b->data + c_num(from));
+ return make_duplicate_buf(minus(to, from), b->data + c_num(from, self));
}
}
@@ -319,10 +322,10 @@ val replace_buf(val buf, val items, val from, val to)
if (gt(len_rep, len_it)) {
val len_diff = minus(len_rep, len_it);
- cnum t = c_num(to);
- cnum l = c_num(len);
+ cnum t = c_num(to, self);
+ cnum l = c_num(len, self);
- memmove(buf->b.data + t - c_num(len_diff),
+ memmove(buf->b.data + t - c_num(len_diff, self),
buf->b.data + t,
l - t);
@@ -330,12 +333,12 @@ val replace_buf(val buf, val items, val from, val to)
to = plus(from, len_it);
} else if (lt(len_rep, len_it)) {
val len_diff = minus(len_it, len_rep);
- cnum t = c_num(to);
- cnum l = c_num(len);
+ cnum t = c_num(to, self);
+ cnum l = c_num(len, self);
buf_set_length(buf, plus(len, len_diff), zero);
- memmove(buf->b.data + t + c_num(len_diff),
+ memmove(buf->b.data + t + c_num(len_diff, self),
buf->b.data + t,
l - t);
to = plus(from, len_it);
@@ -344,12 +347,12 @@ val replace_buf(val buf, val items, val from, val to)
if (zerop(len_it))
return buf;
if (bufp(items)) {
- memmove(buf->b.data + c_num(from), items->b.data, c_num(len_it));
+ memmove(buf->b.data + c_num(from, self), items->b.data, c_num(len_it, self));
} else {
seq_iter_t item_iter;
seq_iter_init(self, &item_iter, items);
- cnum f = c_num(from);
- cnum t = c_num(to);
+ cnum f = c_num(from, self);
+ cnum t = c_num(to, self);
for (; f != t; f++) {
val item = seq_geti(&item_iter);
@@ -392,7 +395,7 @@ val buf_put_buf(val dbuf, val sbuf, val pos)
{
val self = lit("buf-put-buf");
struct buf *sb = buf_handle(sbuf, self);
- buf_move_bytes(dbuf, pos, sb->data, c_num(sb->len), self);
+ buf_move_bytes(dbuf, pos, sb->data, c_num(sb->len, self), self);
return sbuf;
}
@@ -413,7 +416,7 @@ val buf_put_i8(val buf, val pos, val num)
struct buf *b = buf_handle(buf, self);
cnum p = buf_check_index(b, pos, self);
i8_t v = c_i8(num, self);
- if (p >= c_num(b->len))
+ if (p >= c_num(b->len, self))
buf_do_set_len(buf, b, succ(pos), nil, self);
b->data[p] = v;
return num;
@@ -425,7 +428,7 @@ val buf_put_u8(val buf, val pos, val num)
struct buf *b = buf_handle(buf, self);
cnum p = buf_check_index(b, pos, self);
cnum v = c_u8(num, self);
- if (p >= c_num(b->len))
+ if (p >= c_num(b->len, self))
buf_do_set_len(buf, b, succ(pos), nil, self);
b->data[p] = v;
return num;
@@ -492,7 +495,7 @@ val buf_put_char(val buf, val pos, val num)
struct buf *b = buf_handle(buf, self);
cnum p = buf_check_index(b, pos, self);
char v = c_char(num, self);
- if (p >= c_num(b->len))
+ if (p >= c_num(b->len, self))
buf_do_set_len(buf, b, succ(pos), nil, self);
b->data[p] = v;
return num;
@@ -504,7 +507,7 @@ val buf_put_uchar(val buf, val pos, val num)
struct buf *b = buf_handle(buf, self);
cnum p = buf_check_index(b, pos, self);
unsigned char v = c_uchar(num, self);
- if (p >= c_num(b->len))
+ if (p >= c_num(b->len, self))
buf_do_set_len(buf, b, succ(pos), nil, self);
b->data[p] = v;
return num;
@@ -593,7 +596,7 @@ void buf_get_bytes(val buf, val pos, mem_t *ptr, cnum size, val self)
struct buf *b = buf_handle(buf, self);
cnum p = buf_check_index(b, pos, self);
cnum e = p + size;
- cnum l = c_num(b->len);
+ cnum l = c_num(b->len, self);
if (e > l || e < 0)
uw_throwf(error_s, lit("~a: attempted read past buffer end"), self, nao);
@@ -607,7 +610,7 @@ val buf_get_i8(val buf, val pos)
val self = lit("buf-get-i8");
struct buf *b = buf_handle(buf, self);
cnum p = buf_check_index(b, pos, self);
- if (p >= c_num(b->len))
+ if (p >= c_num(b->len, self))
uw_throwf(error_s, lit("~a: attempted read past buffer end"), self, nao);
return num_fast(convert(i8_t, b->data[p]));
}
@@ -617,7 +620,7 @@ val buf_get_u8(val buf, val pos)
val self = lit("buf-get-u8");
struct buf *b = buf_handle(buf, self);
cnum p = buf_check_index(b, pos, self);
- if (p >= c_num(b->len))
+ if (p >= c_num(b->len, self))
uw_throwf(error_s, lit("~a: attempted read past buffer end"), self, nao);
return num_fast(convert(u8_t, b->data[p]));
}
@@ -787,9 +790,10 @@ val buf_get_cptr(val buf, val pos)
val buf_print(val buf, val stream_in)
{
+ val self = lit("buf-print");
val stream = default_arg(stream_in, std_output);
- struct buf *b = buf_handle(buf, lit("buf-print"));
- cnum len = c_num(b->len), count = 0;
+ struct buf *b = buf_handle(buf, self);
+ cnum len = c_num(b->len, self), count = 0;
mem_t *data = b->data;
val save_mode = test_neq_set_indent_mode(stream, num_fast(indent_foff),
num_fast(indent_data));
@@ -818,9 +822,10 @@ val buf_print(val buf, val stream_in)
val buf_pprint(val buf, val stream_in)
{
+ val self = lit("buf-pprint");
val stream = default_arg(stream_in, std_output);
- struct buf *b = buf_handle(buf, lit("buf-print"));
- cnum len = c_num(b->len);
+ struct buf *b = buf_handle(buf, self);
+ cnum len = c_num(b->len, self);
mem_t *data = b->data;
while (len-- > 0)
@@ -889,7 +894,7 @@ static int buf_strm_get_byte_callback(mem_t *ctx)
struct buf *b = buf_handle(s->buf, self);
cnum p = buf_check_index(b, s->pos, self);
s->pos = num(p + 1);
- return (p >= c_num(b->len)) ? EOF : b->data[p];
+ return (p >= c_num(b->len, self)) ? EOF : b->data[p];
}
static val buf_strm_get_char(val stream)
@@ -933,7 +938,7 @@ static val buf_strm_unget_byte(val stream, int byte)
val self = lit("unget-byte");
struct buf_strm *s = coerce(struct buf_strm *, stream->co.handle);
struct buf *b = buf_handle(s->buf, self);
- cnum p = c_num(s->pos);
+ cnum p = c_num(s->pos, self);
if (p <= 0) {
uw_throwf(file_error_s,
@@ -1090,7 +1095,7 @@ void buf_swap32(val buf)
{
val self = lit("buf-swap32");
struct buf *b = buf_handle(buf, self);
- mem_t *data = b->data, *end = data + c_num(b->len);
+ mem_t *data = b->data, *end = data + c_num(b->len, self);
for (; data + 3 < end; data += 4) {
u32_t sw32 = *coerce(u32_t *, data);
@@ -1113,7 +1118,7 @@ static val str_buf(val buf, val null_term)
val self = lit("str-buf");
struct buf *b = buf_handle(buf, self);
val nt = default_null_arg(null_term);
- size_t blen = c_unum(b->len);
+ size_t blen = c_unum(b->len, self);
size_t len = (nt && blen > 0 && !b->data[blen-1]) ? blen - 1 : blen;
wchar_t *str = utf8_dup_from_buf(coerce(const char *, b->data), len);
return string_own(str);
@@ -1125,7 +1130,7 @@ static val buf_int(val num)
switch (type(num)) {
case NUM: case CHR:
- num = bignum(c_num(num));
+ num = bignum(c_num(num, self));
/* fallthrough */
case BGNUM:
{
@@ -1134,10 +1139,10 @@ static val buf_int(val num)
val bytes = ash(plus(bits, num_fast(7)), num_fast(-3));
val bitsround = ash(bytes, num_fast(3));
val un = logtrunc(num, bitsround);
- val ube = if3(bignump(un), un, bignum(c_num(un)));
+ val ube = if3(bignump(un), un, bignum(c_num(un, self)));
mp_int *m = mp(ube);
size_t numsize = mp_unsigned_bin_size(m);
- size_t bufsize = c_unum(bytes);
+ size_t bufsize = c_unum(bytes, self);
mem_t *data = chk_malloc(bufsize);
data[0] = 0;
mp_to_unsigned_bin(m, data + (bufsize - numsize));
@@ -1155,7 +1160,7 @@ static val buf_uint(val num)
switch (type(num)) {
case NUM: case CHR:
- num = bignum(c_num(num));
+ num = bignum(c_num(num, self));
/* fallthrough */
case BGNUM:
{
@@ -1179,7 +1184,7 @@ static val int_buf(val buf)
{
val self = lit("int-buf");
struct buf *b = buf_handle(buf, self);
- ucnum size = c_unum(b->size);
+ ucnum size = c_unum(b->size, self);
ucnum bits = size * 8;
val ubn = make_bignum();
mp_err mpe = mp_read_unsigned_bin(mp(ubn), b->data, size);
@@ -1192,7 +1197,7 @@ static val uint_buf(val buf)
{
val self = lit("int-buf");
struct buf *b = buf_handle(buf, self);
- ucnum size = c_unum(b->size);
+ ucnum size = c_unum(b->size, self);
val ubn = make_bignum();
mp_err mpe = mp_read_unsigned_bin(mp(ubn), b->data, size);
if (mpe != MP_OKAY)