ccan: remove autodata.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2021-09-21 16:53:10 +09:30 committed by Christian Decker
parent 24536c5561
commit 2736e997f6
9 changed files with 1 additions and 424 deletions

View File

@ -93,7 +93,6 @@ FEATURES :=
CCAN_OBJS := \
ccan-asort.o \
ccan-autodata.o \
ccan-bitmap.o \
ccan-bitops.o \
ccan-breakpoint.o \
@ -147,7 +146,6 @@ CCAN_HEADERS := \
$(CCANDIR)/ccan/alignof/alignof.h \
$(CCANDIR)/ccan/array_size/array_size.h \
$(CCANDIR)/ccan/asort/asort.h \
$(CCANDIR)/ccan/autodata/autodata.h \
$(CCANDIR)/ccan/bitmap/bitmap.h \
$(CCANDIR)/ccan/bitops/bitops.h \
$(CCANDIR)/ccan/breakpoint/breakpoint.h \
@ -773,8 +771,6 @@ ccan-list.o: $(CCANDIR)/ccan/list/list.c
@$(call VERBOSE, "cc $<", $(CC) $(CFLAGS) -c -o $@ $<)
ccan-asort.o: $(CCANDIR)/ccan/asort/asort.c
@$(call VERBOSE, "cc $<", $(CC) $(CFLAGS) -c -o $@ $<)
ccan-autodata.o: $(CCANDIR)/ccan/autodata/autodata.c
@$(call VERBOSE, "cc $<", $(CC) $(CFLAGS) -c -o $@ $<)
ccan-ptr_valid.o: $(CCANDIR)/ccan/ptr_valid/ptr_valid.c
@$(call VERBOSE, "cc $<", $(CC) $(CFLAGS) -c -o $@ $<)
ccan-read_write_all.o: $(CCANDIR)/ccan/read_write_all/read_write_all.c

View File

@ -1 +0,0 @@
../../licenses/BSD-MIT

View File

@ -1,111 +0,0 @@
#include "config.h"
#include <stdio.h>
#include <string.h>
/**
* autodata - stash pointers in your binary for automatic registration
*
* This code allows declarations in your source which you can gather
* together at runtime to form tables. This is often used in place of
* having a central registration function or table.
*
* Note that this technique does not work in general for shared libaries,
* only for code compiled into a binary.
*
* License: BSD-MIT
*
* Example:
* // Distributed commandline option registration (note: ccan/opt is better!)
* #include <ccan/autodata/autodata.h>
* #include <stdio.h>
* #include <unistd.h>
* #include <stdbool.h>
* #include <err.h>
*
* static bool verbose = false;
*
* // This would normally be in a header, so any C file can use it.
* struct option {
* char c;
* bool takes_arg;
* bool (*cb)(char *optarg);
* };
* AUTODATA_TYPE(options, struct option);
* #define REGISTER_OPTION(optstruct) \
* AUTODATA(options, (optstruct))
*
* // Now a few examples (could be anywhere in source)
* static bool verbose_cb(char *unused)
* {
* verbose = true;
* return true;
* }
* static struct option dash_v = { 'v', false, verbose_cb };
* REGISTER_OPTION(&dash_v);
*
* static bool chdir_cb(char *dir)
* {
* if (verbose)
* printf("chdir to %s. ", dir);
* if (chdir(dir) != 0)
* return false;
* return true;
* }
* static struct option dash_C = { 'C', true, chdir_cb };
* REGISTER_OPTION(&dash_C);
*
* int main(int argc, char *argv[])
* {
* struct option **opts;
* size_t i, num;
* int o;
* char *optstring, *p;
*
* // Gather together all the registered options.
* opts = autodata_get(options, &num);
*
* // Make pretty string for getopt().
* p = optstring = malloc(num * 2 + 1);
* for (i = 0; i < num; i++) {
* *(p++) = opts[i]->c;
* if (opts[i]->takes_arg)
* *(p++) = ':';
* }
* *p = '\0';
*
* while ((o = getopt(argc, argv, optstring)) != -1) {
* if (o == '?')
* exit(1);
* // Call callback in matching option.
* for (i = 0; i < num; i++) {
* if (opts[i]->c == o) {
* if (!opts[i]->cb(optarg))
* err(1, "parsing -%c", o);
* break;
* }
* }
* }
* // free up gathered option table.
* autodata_free(opts);
*
* if (verbose)
* printf("verbose mode on\n");
* return 0;
* }
* // Given "-v" outputs "verbose mode on\n"
* // Given "-v -C /" outputs "chdir to /. verbose mode on\n"
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/compiler\n");
printf("ccan/ptr_valid\n");
return 0;
}
return 1;
}

View File

@ -1,80 +0,0 @@
// Licensed under BSD-MIT: See LICENSE.
#include "autodata.h"
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#if HAVE_SECTION_START_STOP
void *autodata_get_section(void *start, void *stop, size_t *nump)
{
*nump = (void **)(stop) - (void **)(start);
return start;
}
void autodata_free(void *table UNNEEDED)
{
}
#else
#include <ccan/ptr_valid/ptr_valid.h>
void *autodata_make_table(const void *example, const char *name, size_t *nump)
{
const char *start, *end, *tag;
struct ptr_valid_batch batch;
const void *const magic = (void *)AUTODATA_MAGIC;
void **table = NULL;
char first_magic;
if (!ptr_valid_batch_start(&batch))
return NULL;
/* Get range to search. */
for (start = (char *)((intptr_t)example & ~(getpagesize() - 1));
ptr_valid_batch(&batch, start-getpagesize(), 1, sizeof(void *),
false);
start -= getpagesize());
for (end = (char *)((intptr_t)example & ~(getpagesize() - 1));
ptr_valid_batch(&batch, end, 1, sizeof(void *), false);
end += getpagesize());
*nump = 0;
first_magic = *(char *)&magic;
for (tag = memchr(start, first_magic, end - start);
tag;
tag = memchr(tag+1, first_magic, end - (tag + 1))) {
void *adata[4];
/* We can read 4 void *'s here? */
if (tag + sizeof(adata) > end)
continue;
memcpy(adata, tag, sizeof(adata));
/* False match? */
if (adata[0] != (void *)AUTODATA_MAGIC || adata[1] != tag)
continue;
/* OK, check name. */
if (!ptr_valid_batch_string(&batch, adata[3])
|| strcmp(name, adata[3]) != 0)
continue;
if (!ptr_valid_batch_read(&batch, (char *)adata[2]))
continue;
table = realloc(table, sizeof(void *) * (*nump + 1));
if (!table)
break;
table[*nump] = adata[2];
(*nump)++;
}
ptr_valid_batch_end(&batch);
return table;
}
void autodata_free(void *table)
{
free(table);
}
#endif

View File

@ -1,109 +0,0 @@
// Licensed under BSD-MIT: See LICENSE.
#ifndef CCAN_AUTODATA_H
#define CCAN_AUTODATA_H
#include "config.h"
#include <ccan/compiler/compiler.h>
#include <stdlib.h>
#if HAVE_SECTION_START_STOP
/**
* AUTODATA_TYPE - declare the type for a given autodata name.
* @name: the name for this set of autodata
* @type: the type this autodata points to
*
* This macro is usually placed in a header: it must precede any
* autodata functions in the file.
*
* Example:
* #include <ccan/autodata/autodata.h>
*
* // My set of char pointers.
* AUTODATA_TYPE(names, char);
*/
#define AUTODATA_TYPE(name, type) \
typedef type autodata_##name##_; \
extern type *__start_xautodata_##name[], *__stop_xautodata_##name[]
/**
* AUTODATA - add a pointer to this autodata set
* @name: the name of the set of autodata
* @ptr: the compile-time-known pointer
*
* This embeds @ptr into the binary, with the tag corresponding to
* @name (which must look like a valid identifier, no punctuation!).
* The type of @ptr must match that given by AUTODATA_TYPE. It is
* usually a file-level declaration.
*
* Example:
* // Put two char pointers into the names AUTODATA set.
* AUTODATA(names, "Arabella");
* AUTODATA(names, "Alex");
*/
#define AUTODATA(name, ptr) \
static const autodata_##name##_ *NEEDED \
__attribute__((section("xautodata_" #name))) \
AUTODATA_VAR_(name, __LINE__) = (ptr);
/**
* autodata_get - get an autodata set
* @name: the name of the set of autodata
* @nump: the number of items in the set.
*
* This extract the embedded pointers matching @name. It may fail
* if malloc() fails, or if there is no AUTODATA at all.
*
* The return will be a pointer to an array of @type pointers (from
* AUTODATA_TYPE).
*
* Example:
* static void print_embedded_names(void)
* {
* unsigned int i;
* size_t num;
* char **n = autodata_get(names, &num);
*
* for (i = 0; i < num; i++)
* printf("%s\n", n[i]);
* }
*/
#define autodata_get(name, nump) \
((autodata_##name##_ **) \
autodata_get_section(__start_xautodata_##name, \
__stop_xautodata_##name, (nump)))
#endif /* HAVE_SECTION_START_STOP */
/**
* autodata_free - free the table returned by autodata_get()
* @p: the table.
*/
void autodata_free(void *p);
/* Internal functions. */
#define AUTODATA_VAR__(name, line) autodata_##name##_##line
#define AUTODATA_VAR_(name, line) AUTODATA_VAR__(name, line)
#if HAVE_SECTION_START_STOP
void *autodata_get_section(void *start, void *stop, size_t *nump);
#else
#define AUTODATA_TYPE(name, type) \
typedef type autodata_##name##_; \
static const void *autodata_##name##_ex = &autodata_##name##_ex
#define AUTODATA_MAGIC ((long)0xFEEDA10DA7AF00D5ULL)
#define AUTODATA(name, ptr) \
static const autodata_##name##_ *NEEDED \
AUTODATA_VAR_(name, __LINE__)[4] = \
{ (void *)AUTODATA_MAGIC, \
(void *)&AUTODATA_VAR_(name, __LINE__), \
(ptr), \
(void *)#name }
#define autodata_get(name, nump) \
((autodata_##name##_ **) \
autodata_make_table(&autodata_##name##_ex, #name, (nump)))
void *autodata_make_table(const void *example, const char *name, size_t *nump);
#endif
#endif /* CCAN_AUTODATA_H */

View File

@ -1,6 +0,0 @@
/* Check that linking together works. */
#include <ccan/autodata/autodata.h>
AUTODATA_TYPE(autostrings, char);
AUTODATA(autostrings, "helper");

View File

@ -1,71 +0,0 @@
#include <ccan/autodata/autodata.h>
/* Include the C files directly. */
#include <ccan/autodata/autodata.c>
#include <ccan/tap/tap.h>
AUTODATA_TYPE(autostrings, char);
AUTODATA(autostrings, "genuine");
#if !HAVE_SECTION_START_STOP
/* These are all fake, to test the various failure paths. */
/* Hopefully fake_alpha or fake_omega will test run-past-end. */
static const void *NEEDED fake_alpha[] = { (void *)AUTODATA_MAGIC };
/* Wrong magic in the middle. */
static const void *NEEDED fake1[] = { (void *)(AUTODATA_MAGIC ^ 0x10000),
(void *)&fake1,
"fake1",
(void *)"autostrings" };
/* Wrong self pointer. */
static const void *NEEDED fake2[] = { (void *)AUTODATA_MAGIC,
(void *)&fake1,
"fake2",
(void *)"autostrings" };
/* Wrong name. */
static const void *NEEDED fake3[] = { (void *)AUTODATA_MAGIC,
(void *)&fake3,
"fake3",
(void *)"autostrings2" };
/* Invalid self-pointer. */
static const void *NEEDED fake4[] = { (void *)AUTODATA_MAGIC,
(void *)1UL,
"fake4",
(void *)"autostrings" };
/* Invalid name pointer */
static const void *NEEDED fake5[] = { (void *)AUTODATA_MAGIC,
(void *)&fake5,
"fake5",
(void *)1UL };
/* Invalid contents pointer */
static const void *NEEDED fake6[] = { (void *)AUTODATA_MAGIC,
(void *)&fake6,
(char *)1UL,
(void *)"autostrings" };
static const void *NEEDED fake_omega[] = { (void *)AUTODATA_MAGIC };
#endif
int main(void)
{
char **table;
size_t num;
/* This is how many tests you plan to run */
plan_tests(2);
table = autodata_get(autostrings, &num);
ok1(num == 2);
ok1((!strcmp(table[0], "genuine") && !strcmp(table[1], "helper"))
|| (!strcmp(table[1], "genuine") && !strcmp(table[0], "helper")));
autodata_free(table);
/* This exits depending on whether all tests passed */
return exit_status();
}

View File

@ -1,41 +0,0 @@
#include <ccan/autodata/autodata.h>
/* Include the C files directly. */
#include <ccan/autodata/autodata.c>
#include <ccan/tap/tap.h>
AUTODATA_TYPE(autostrings, char);
AUTODATA(autostrings, "hello");
AUTODATA(autostrings, "world");
int main(void)
{
char **table;
size_t num;
int i, hello = -1, world = -1, helper = -1;
/* This is how many tests you plan to run */
plan_tests(4);
table = autodata_get(autostrings, &num);
ok1(num == 3);
for (i = 0; i < num; i++) {
if (strcmp(table[i], "hello") == 0)
hello = i;
else if (strcmp(table[i], "world") == 0)
world = i;
else if (strcmp(table[i], "helper") == 0)
helper = i;
else
fail("Unknown entry %s", table[i]);
}
ok1(hello != -1);
ok1(world != -1);
ok1(helper != -1);
autodata_free(table);
/* This exits depending on whether all tests passed */
return exit_status();
}

View File

@ -87,7 +87,6 @@ sources = [
"bitcoin/signature.c",
"bitcoin/tx.c",
"bitcoin/varint.c",
"ccan/ccan/autodata/autodata.c",
"ccan/ccan/breakpoint/breakpoint.c",
"ccan/ccan/crypto/hkdf_sha256/hkdf_sha256.c",
"ccan/ccan/crypto/hmac_sha256/hmac_sha256.c",
@ -113,6 +112,7 @@ sources = [
"ccan/ccan/timer/timer.c",
"ccan/ccan/utf8/utf8.c",
"common/amount.c",
"common/autodata.c",
"common/bigsize.c",
"common/bip32.c",
"common/bolt12_merkle.c",