Skip to content

Commit d2c6383

Browse files
authored
Update to C API 0.99.14 (#165)
1 parent e0ae4c1 commit d2c6383

File tree

13 files changed

+3032
-1822
lines changed

13 files changed

+3032
-1822
lines changed

subprojects/tskit/tskit/convert.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* pointless. */
4343

4444
typedef struct {
45-
size_t precision;
45+
unsigned int precision;
4646
tsk_flags_t options;
4747
char *newick;
4848
tsk_id_t *traversal_stack;
@@ -98,7 +98,7 @@ tsk_newick_converter_run(
9898
}
9999
/* We do this for ms-compatability. This should be a configurable option
100100
* via the flags attribute */
101-
label = v + 1;
101+
label = (int) v + 1;
102102
r = snprintf(buffer + s, buffer_size - s, "%d", label);
103103
if (r < 0) {
104104
ret = TSK_ERR_IO;
@@ -145,16 +145,17 @@ tsk_newick_converter_run(
145145

146146
static int
147147
tsk_newick_converter_init(tsk_newick_converter_t *self, const tsk_tree_t *tree,
148-
size_t precision, tsk_flags_t options)
148+
unsigned int precision, tsk_flags_t options)
149149
{
150150
int ret = 0;
151151

152-
memset(self, 0, sizeof(tsk_newick_converter_t));
152+
tsk_memset(self, 0, sizeof(tsk_newick_converter_t));
153153
self->precision = precision;
154154
self->options = options;
155155
self->tree = tree;
156-
self->traversal_stack = malloc(tsk_treeseq_get_num_nodes(self->tree->tree_sequence)
157-
* sizeof(*self->traversal_stack));
156+
self->traversal_stack
157+
= tsk_malloc(tsk_treeseq_get_num_nodes(self->tree->tree_sequence)
158+
* sizeof(*self->traversal_stack));
158159
if (self->traversal_stack == NULL) {
159160
ret = TSK_ERR_NO_MEMORY;
160161
goto out;
@@ -171,7 +172,7 @@ tsk_newick_converter_free(tsk_newick_converter_t *self)
171172
}
172173

173174
int
174-
tsk_convert_newick(const tsk_tree_t *tree, tsk_id_t root, size_t precision,
175+
tsk_convert_newick(const tsk_tree_t *tree, tsk_id_t root, unsigned int precision,
175176
tsk_flags_t options, size_t buffer_size, char *buffer)
176177
{
177178
int ret = 0;

subprojects/tskit/tskit/convert.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern "C" {
3232

3333
#include <tskit/trees.h>
3434

35-
int tsk_convert_newick(const tsk_tree_t *tree, tsk_id_t root, size_t precision,
35+
int tsk_convert_newick(const tsk_tree_t *tree, tsk_id_t root, unsigned int precision,
3636
tsk_flags_t options, size_t buffer_size, char *buffer);
3737

3838
#ifdef __cplusplus

subprojects/tskit/tskit/core.c

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2019 Tskit Developers
4+
* Copyright (c) 2019-2021 Tskit Developers
55
* Copyright (c) 2015-2018 University of Oxford
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -175,6 +175,9 @@ tsk_strerror_internal(int err)
175175
case TSK_ERR_BOTH_COLUMNS_REQUIRED:
176176
ret = "Both columns in a related pair must be provided";
177177
break;
178+
case TSK_ERR_BAD_COLUMN_TYPE:
179+
ret = "An incompatible type for a column was found in the file";
180+
break;
178181

179182
/* Out of bounds errors */
180183
case TSK_ERR_BAD_OFFSET:
@@ -400,6 +403,9 @@ tsk_strerror_internal(int err)
400403
case TSK_ERR_BAD_GENOTYPE:
401404
ret = "Bad genotype value provided";
402405
break;
406+
case TSK_ERR_BAD_ANCESTRAL_STATE:
407+
ret = "Bad ancestral state specified";
408+
break;
403409

404410
/* Genotype decoding errors */
405411
case TSK_ERR_TOO_MANY_ALLELES:
@@ -541,11 +547,11 @@ void
541547
tsk_blkalloc_print_state(tsk_blkalloc_t *self, FILE *out)
542548
{
543549
fprintf(out, "Block allocator%p::\n", (void *) self);
544-
fprintf(out, "\ttop = %d\n", (int) self->top);
545-
fprintf(out, "\tchunk_size = %d\n", (int) self->chunk_size);
546-
fprintf(out, "\tnum_chunks = %d\n", (int) self->num_chunks);
547-
fprintf(out, "\ttotal_allocated = %d\n", (int) self->total_allocated);
548-
fprintf(out, "\ttotal_size = %d\n", (int) self->total_size);
550+
fprintf(out, "\ttop = %lld\n", (long long) self->top);
551+
fprintf(out, "\tchunk_size = %lld\n", (long long) self->chunk_size);
552+
fprintf(out, "\tnum_chunks = %lld\n", (long long) self->num_chunks);
553+
fprintf(out, "\ttotal_allocated = %lld\n", (long long) self->total_allocated);
554+
fprintf(out, "\ttotal_size = %lld\n", (long long) self->total_size);
549555
}
550556

551557
int TSK_WARN_UNUSED
@@ -564,7 +570,7 @@ tsk_blkalloc_init(tsk_blkalloc_t *self, size_t chunk_size)
564570
{
565571
int ret = 0;
566572

567-
memset(self, 0, sizeof(tsk_blkalloc_t));
573+
tsk_memset(self, 0, sizeof(tsk_blkalloc_t));
568574
if (chunk_size < 1) {
569575
ret = TSK_ERR_BAD_PARAM_VALUE;
570576
goto out;
@@ -642,8 +648,8 @@ tsk_blkalloc_free(tsk_blkalloc_t *self)
642648

643649
/* Mirrors the semantics of numpy's searchsorted function. Uses binary
644650
* search to find the index of the closest value in the array. */
645-
size_t
646-
tsk_search_sorted(const double *restrict array, size_t size, double value)
651+
tsk_size_t
652+
tsk_search_sorted(const double *restrict array, tsk_size_t size, double value)
647653
{
648654
int64_t upper = (int64_t) size;
649655
int64_t lower = 0;
@@ -663,7 +669,7 @@ tsk_search_sorted(const double *restrict array, size_t size, double value)
663669
}
664670
}
665671
offset = (int64_t)(array[lower] < value);
666-
return (size_t)(lower + offset);
672+
return (tsk_size_t)(lower + offset);
667673
}
668674

669675
/* Rounds the specified double to the closest multiple of 10**-num_digits. If
@@ -703,3 +709,70 @@ tsk_is_unknown_time(double val)
703709
nan_union.f = val;
704710
return nan_union.i == TSK_UNKNOWN_TIME_HEX;
705711
}
712+
713+
void *
714+
tsk_malloc(tsk_size_t size)
715+
{
716+
/* Avoid malloc(0) as it's not portable */
717+
if (size == 0) {
718+
size = 1;
719+
}
720+
#if TSK_MAX_SIZE > SIZE_MAX
721+
if (size > SIZE_MAX) {
722+
return NULL;
723+
}
724+
#endif
725+
return malloc((size_t) size);
726+
}
727+
728+
void *
729+
tsk_realloc(void *ptr, tsk_size_t size)
730+
{
731+
/* We shouldn't ever realloc to a zero size in tskit */
732+
tsk_bug_assert(size > 0);
733+
return realloc(ptr, (size_t) size);
734+
}
735+
736+
/* We keep the size argument here as a size_t because we'd have to
737+
* cast the outputs of sizeof() otherwise, which would lead to
738+
* less readable code. We need to be careful to use calloc within
739+
* the library accordingly, so that size can't overflow on 32 bit.
740+
*/
741+
void *
742+
tsk_calloc(tsk_size_t n, size_t size)
743+
{
744+
/* Avoid calloc(0) as it's not portable */
745+
if (n == 0) {
746+
n = 1;
747+
}
748+
#if TSK_MAX_SIZE > SIZE_MAX
749+
if (n > SIZE_MAX) {
750+
return NULL;
751+
}
752+
#endif
753+
return calloc((size_t) n, size);
754+
}
755+
756+
void *
757+
tsk_memset(void *ptr, int fill, tsk_size_t size)
758+
{
759+
return memset(ptr, fill, (size_t) size);
760+
}
761+
762+
void *
763+
tsk_memcpy(void *dest, const void *src, tsk_size_t size)
764+
{
765+
return memcpy(dest, src, (size_t) size);
766+
}
767+
768+
void *
769+
tsk_memmove(void *dest, const void *src, tsk_size_t size)
770+
{
771+
return memmove(dest, src, (size_t) size);
772+
}
773+
774+
int
775+
tsk_memcmp(const void *s1, const void *s2, tsk_size_t size)
776+
{
777+
return memcmp(s1, s2, (size_t) size);
778+
}

subprojects/tskit/tskit/core.h

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,59 @@ __tsk_nan_f(void)
109109
}
110110
#define TSK_UNKNOWN_TIME __tsk_nan_f()
111111

112+
/**
113+
@brief Tskit Object IDs.
114+
115+
@rst
116+
All objects in tskit are referred to by integer IDs corresponding to the
117+
row they occupy in the relevant table. The ``tsk_id_t`` type should be used
118+
when manipulating these ID values. The reserved value ``TSK_NULL`` (-1) defines
119+
missing data.
120+
@endrst
121+
*/
122+
#ifdef _TSK_BIG_TABLES
123+
/* Allow tables to have more than 2^31 rows. This is an EXPERIMENTAL feature
124+
* and is not supported in any way. This typedef is only included for
125+
* future-proofing purposes, so that we can be sure that we don't make any
126+
* design decisions that are incompatible with big tables by building the
127+
* library in 64 bit mode in CI. See the discussion here for more background:
128+
129+
* https://github.com/tskit-dev/tskit/issues/343
130+
*
131+
* If you need big tables, please open an issue on GitHub to discuss, or comment
132+
* on the thread above.
133+
*/
134+
typedef int64_t tsk_id_t;
135+
#define TSK_MAX_ID INT64_MAX
136+
#define TSK_ID_STORAGE_TYPE KAS_INT64
137+
#else
138+
typedef int32_t tsk_id_t;
139+
#define TSK_MAX_ID INT32_MAX
140+
#define TSK_ID_STORAGE_TYPE KAS_INT32
141+
#endif
142+
143+
/**
144+
@brief Tskit sizes.
145+
146+
@rst
147+
The ``tsk_size_t`` type is an unsigned integer used for any size or count value.
148+
@endrst
149+
*/
150+
typedef uint64_t tsk_size_t;
151+
#define TSK_MAX_SIZE UINT64_MAX
152+
#define TSK_SIZE_STORAGE_TYPE KAS_UINT64
153+
154+
/**
155+
@brief Container for bitwise flags.
156+
157+
@rst
158+
Bitwise flags are used in tskit as a column type and also as a way to
159+
specify options to API functions.
160+
@endrst
161+
*/
162+
typedef uint32_t tsk_flags_t;
163+
#define TSK_FLAGS_STORAGE_TYPE KAS_UINT32
164+
112165
// clang-format off
113166
/**
114167
@defgroup API_VERSION_GROUP API version macros.
@@ -129,7 +182,7 @@ to the API or ABI are introduced, i.e., the addition of a new function.
129182
The library patch version. Incremented when any changes not relevant to the
130183
to the API or ABI are introduced, i.e., internal refactors of bugfixes.
131184
*/
132-
#define TSK_VERSION_PATCH 12
185+
#define TSK_VERSION_PATCH 14
133186
/** @} */
134187

135188
/* Node flags */
@@ -144,7 +197,7 @@ to the API or ABI are introduced, i.e., internal refactors of bugfixes.
144197
#define TSK_FILE_FORMAT_NAME "tskit.trees"
145198
#define TSK_FILE_FORMAT_NAME_LENGTH 11
146199
#define TSK_FILE_FORMAT_VERSION_MAJOR 12
147-
#define TSK_FILE_FORMAT_VERSION_MINOR 4
200+
#define TSK_FILE_FORMAT_VERSION_MINOR 5
148201

149202
/**
150203
@defgroup GENERAL_ERROR_GROUP General errors.
@@ -208,6 +261,12 @@ not found in the file.
208261
*/
209262
#define TSK_ERR_BOTH_COLUMNS_REQUIRED -104
210263

264+
/**
265+
An unsupported type was provided for a column in the file.
266+
*/
267+
#define TSK_ERR_BAD_COLUMN_TYPE -105
268+
269+
211270
/* Out of bounds errors */
212271
#define TSK_ERR_BAD_OFFSET -200
213272
#define TSK_ERR_OUT_OF_BOUNDS -201
@@ -291,6 +350,7 @@ not found in the file.
291350
/* Mutation mapping errors */
292351
#define TSK_ERR_GENOTYPES_ALL_MISSING -1000
293352
#define TSK_ERR_BAD_GENOTYPE -1001
353+
#define TSK_ERR_BAD_ANCESTRAL_STATE -1002
294354

295355
/* Genotype decoding errors */
296356
#define TSK_ERR_MUST_IMPUTE_NON_SAMPLES -1100
@@ -402,14 +462,24 @@ extern int tsk_blkalloc_init(tsk_blkalloc_t *self, size_t chunk_size);
402462
extern void *tsk_blkalloc_get(tsk_blkalloc_t *self, size_t size);
403463
extern void tsk_blkalloc_free(tsk_blkalloc_t *self);
404464

405-
size_t tsk_search_sorted(const double *array, size_t size, double value);
465+
tsk_size_t tsk_search_sorted(const double *array, tsk_size_t size, double value);
406466
double tsk_round(double x, unsigned int ndigits);
407467

408468
bool tsk_is_unknown_time(double val);
409469

410470
#define TSK_UUID_SIZE 36
411471
int tsk_generate_uuid(char *dest, int flags);
412472

473+
/* TODO most of these can probably be macros so they compile out as no-ops.
474+
* Lets do the 64 bit tsk_size_t switch first though. */
475+
void *tsk_malloc(tsk_size_t size);
476+
void *tsk_realloc(void *ptr, tsk_size_t size);
477+
void *tsk_calloc(tsk_size_t n, size_t size);
478+
void *tsk_memset(void *ptr, int fill, tsk_size_t size);
479+
void *tsk_memcpy(void *dest, const void *src, tsk_size_t size);
480+
void *tsk_memmove(void *dest, const void *src, tsk_size_t size);
481+
int tsk_memcmp(const void *s1, const void *s2, tsk_size_t size);
482+
413483
#ifdef __cplusplus
414484
}
415485
#endif

0 commit comments

Comments
 (0)