Skip to content

Commit

Permalink
ccan: add local copy.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jun 12, 2015
1 parent 92ec2f8 commit c91d2b5
Show file tree
Hide file tree
Showing 336 changed files with 54,774 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CCAN_OBJS := ccan-crypto-sha256.o ccan-crypto-shachain.o ccan-err.o ccan-tal.o c

HEADERS := $(wildcard *.h)

CCANDIR := ../ccan/
CCANDIR := ccan/
CFLAGS := -g -Wall -I $(CCANDIR)
LDLIBS := -lcrypto -lprotobuf-c
$(PROGRAMS): CFLAGS+=-I.
Expand All @@ -30,8 +30,13 @@ lightning.pb-c.c lightning.pb-c.h: lightning.proto
$(PROGRAMS): % : %.o $(HELPER_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS)
$(PROGRAMS:=.o) $(HELPER_OBJS): $(HEADERS)

$(CCAN_OBJS) $(HELPER_OBJS) $(PROGRAM_OBJS) $(BITCOIN_OBJS): ccan/config.h

ccan/config.h: ccan/tools/configurator/configurator
$< > $@

distclean: clean
$(RM) lightning.pb-c.c lightning.pb-c.h
$(RM) lightning.pb-c.c lightning.pb-c.h ccan/config.h

clean:
$(RM) $(PROGRAMS)
Expand Down
3 changes: 3 additions & 0 deletions ccan/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CCAN imported from http://ccodearchive.net.

CCAN version: init-1956-ged95d86
1 change: 1 addition & 0 deletions ccan/ccan/alignof/LICENSE
51 changes: 51 additions & 0 deletions ccan/ccan/alignof/_info
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "config.h"
#include <stdio.h>
#include <string.h>

/**
* alignof - ALIGNOF() macro to determine alignment of a type.
*
* Many platforms have requirements that certain types must be aligned
* to certain address boundaries, such as ints needing to be on 4-byte
* boundaries. Attempting to access variables with incorrect
* alignment may cause performance loss or even program failure (eg. a
* bus signal).
*
* There are times which it's useful to be able to programatically
* access these requirements, such as for dynamic allocators.
*
* Example:
* #include <stdio.h>
* #include <stdlib.h>
* #include <ccan/alignof/alignof.h>
*
* // Output contains "ALIGNOF(char) == 1"
* // Will also print out whether an onstack char array can hold a long.
* int main(int argc, char *argv[])
* {
* char arr[sizeof(int)];
*
* printf("ALIGNOF(char) == %zu\n", ALIGNOF(char));
* if ((unsigned long)arr % ALIGNOF(int)) {
* printf("arr %p CANNOT hold an int\n", arr);
* exit(1);
* } else {
* printf("arr %p CAN hold an int\n", arr);
* exit(0);
* }
* }
*
* License: CC0 (Public domain)
* Author: Rusty Russell <[email protected]>
*/
int main(int argc, char *argv[])
{
if (argc != 2)
return 1;

if (strcmp(argv[1], "depends") == 0) {
return 0;
}

return 1;
}
20 changes: 20 additions & 0 deletions ccan/ccan/alignof/alignof.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_ALIGNOF_H
#define CCAN_ALIGNOF_H
#include "config.h"

/**
* ALIGNOF - get the alignment of a type
* @t: the type to test
*
* This returns a safe alignment for the given type.
*/
#if HAVE_ALIGNOF
/* A GCC extension. */
#define ALIGNOF(t) __alignof__(t)
#else
/* Alignment by measuring structure padding. */
#define ALIGNOF(t) ((char *)(&((struct { char c; t _h; } *)0)->_h) - (char *)0)
#endif

#endif /* CCAN_ALIGNOF_H */
61 changes: 61 additions & 0 deletions ccan/ccan/alignof/test/run.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <ccan/alignof/alignof.h>
#include <stdlib.h>
#include <stddef.h>
#include <ccan/tap/tap.h>

/* Alignment is remarkably difficult to test. The rules may be more
* complex than ALIGNOF() can know: eg. on i386 __alignof__(double) == 8, but
* __alignof__(struct containing double) == 4.
*
* Technically, we can only test that we give *at least* the alignment which
* naturally occurs, and that accesses work.
*
* For the moment, we work around double. */
struct lots_of_types
{
char c;
short s;
char c2;
int i;
char c3;
float f;
char c4;
double d;
char c5;
};

int main(int argc, char *argv[])
{
struct lots_of_types lots_of_types, *lp = malloc(sizeof(*lp));
char c;
short s;
char c2;
int i;
char c3;
float f;
char c4;
double d;

/* Make sure we use all the variables. */
c = c2 = c3 = c4 = 0;

plan_tests(15);
ok1((unsigned long)&c % ALIGNOF(char) == 0);
ok1((unsigned long)&s % ALIGNOF(short) == 0);
ok1((unsigned long)&i % ALIGNOF(int) == 0);
ok1((unsigned long)&f % ALIGNOF(float) == 0);
ok1((unsigned long)&d % ALIGNOF(double) == 0);

ok1((unsigned long)&lots_of_types.c % ALIGNOF(char) == 0);
ok1((unsigned long)&lots_of_types.s % ALIGNOF(short) == 0);
ok1((unsigned long)&lots_of_types.i % ALIGNOF(int) == 0);
ok1((unsigned long)&lots_of_types.f % ALIGNOF(float) == 0);
ok1(offsetof(struct lots_of_types, d) % ALIGNOF(double) == 0);

ok1((unsigned long)&lp->c % ALIGNOF(char) == 0);
ok1((unsigned long)&lp->s % ALIGNOF(short) == 0);
ok1((unsigned long)&lp->i % ALIGNOF(int) == 0);
ok1((unsigned long)&lp->f % ALIGNOF(float) == 0);
ok1((unsigned long)&lp->d % ALIGNOF(double) == 0);
exit(exit_status());
}
1 change: 1 addition & 0 deletions ccan/ccan/array_size/LICENSE
46 changes: 46 additions & 0 deletions ccan/ccan/array_size/_info
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "config.h"
#include <stdio.h>
#include <string.h>

/**
* array_size - routine for safely deriving the size of a visible array.
*
* This provides a simple ARRAY_SIZE() macro, which (given a good compiler)
* will also break compile if you try to use it on a pointer.
*
* This can ensure your code is robust to changes, without needing a gratuitous
* macro or constant.
*
* Example:
* // Outputs "Initialized 32 values"
* #include <ccan/array_size/array_size.h>
* #include <stdlib.h>
* #include <stdio.h>
*
* // We currently use 32 random values.
* static unsigned int vals[32];
*
* int main(void)
* {
* unsigned int i;
* for (i = 0; i < ARRAY_SIZE(vals); i++)
* vals[i] = random();
* printf("Initialized %u values\n", i);
* return 0;
* }
*
* License: CC0 (Public domain)
* Author: Rusty Russell <[email protected]>
*/
int main(int argc, char *argv[])
{
if (argc != 2)
return 1;

if (strcmp(argv[1], "depends") == 0) {
printf("ccan/build_assert\n");
return 0;
}

return 1;
}
26 changes: 26 additions & 0 deletions ccan/ccan/array_size/array_size.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_ARRAY_SIZE_H
#define CCAN_ARRAY_SIZE_H
#include "config.h"
#include <ccan/build_assert/build_assert.h>

/**
* ARRAY_SIZE - get the number of elements in a visible array
* @arr: the array whose size you want.
*
* This does not work on pointers, or arrays declared as [], or
* function parameters. With correct compiler support, such usage
* will cause a build error (see build_assert).
*/
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))

#if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
/* Two gcc extensions.
* &a[0] degrades to a pointer: a different type from an array */
#define _array_size_chk(arr) \
BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \
typeof(&(arr)[0])))
#else
#define _array_size_chk(arr) 0
#endif
#endif /* CCAN_ALIGNOF_H */
24 changes: 24 additions & 0 deletions ccan/ccan/array_size/test/compile_fail-function-param.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <ccan/array_size/array_size.h>
#include <stdlib.h>

struct foo {
unsigned int a, b;
};

int check_parameter(const struct foo array[4]);
int check_parameter(const struct foo array[4])
{
#ifdef FAIL
return (ARRAY_SIZE(array) == 4);
#if !HAVE_TYPEOF || !HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if _array_size_chk is a noop."
#endif
#else
return sizeof(array) == 4 * sizeof(struct foo);
#endif
}

int main(int argc, char *argv[])
{
return check_parameter(NULL);
}
14 changes: 14 additions & 0 deletions ccan/ccan/array_size/test/compile_fail.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <ccan/array_size/array_size.h>

int main(int argc, char *argv[8])
{
char array[100];
#ifdef FAIL
return ARRAY_SIZE(argv) + ARRAY_SIZE(array);
#if !HAVE_TYPEOF || !HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if _array_size_chk is a noop."
#endif
#else
return ARRAY_SIZE(array);
#endif
}
33 changes: 33 additions & 0 deletions ccan/ccan/array_size/test/run.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <ccan/array_size/array_size.h>
#include <ccan/tap/tap.h>

static char array1[1];
static int array2[2];
static unsigned long array3[3][5];
struct foo {
unsigned int a, b;
char string[100];
};
static struct foo array4[4];

/* Make sure they can be used in initializers. */
static int array1_size = ARRAY_SIZE(array1);
static int array2_size = ARRAY_SIZE(array2);
static int array3_size = ARRAY_SIZE(array3);
static int array4_size = ARRAY_SIZE(array4);

int main(int argc, char *argv[])
{
plan_tests(8);
ok1(array1_size == 1);
ok1(array2_size == 2);
ok1(array3_size == 3);
ok1(array4_size == 4);

ok1(ARRAY_SIZE(array1) == 1);
ok1(ARRAY_SIZE(array2) == 2);
ok1(ARRAY_SIZE(array3) == 3);
ok1(ARRAY_SIZE(array4) == 4);

return exit_status();
}
1 change: 1 addition & 0 deletions ccan/ccan/asort/LICENSE
70 changes: 70 additions & 0 deletions ccan/ccan/asort/_info
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "config.h"
#include <stdio.h>
#include <string.h>

/**
* asort - typesafe array sort (qsort)
*
* qsort() is the standard routine for sorting an array of objects.
* Unfortunately, it has two problems:
* 1) It isn't typesafe,
* 2) The comparison function doesn't take a context pointer.
*
* asort does both.
*
* License: LGPL (v2.1 or any later version)
* Author: Rusty Russell <[email protected]>
*
* Example:
* #include <ccan/asort/asort.h>
* #include <stdio.h>
* #include <string.h>
*
* static int cmp(char *const *a, char *const *n, bool *casefold)
* {
* if (*casefold)
* return strcasecmp(*a, *n);
* else
* return strcmp(*a, *n);
* }
*
* int main(int argc, char *argv[])
* {
* bool casefold = false;
* unsigned int i;
*
* if (argc < 2) {
* fprintf(stderr, "Usage: %s [-i] <list>...\n"
* "Sort arguments (-i = ignore case)\n",
* argv[0]);
* exit(1);
* }
*
* if (strcmp(argv[1], "-i") == 0) {
* casefold = true;
* argc--;
* argv++;
* }
* asort(&argv[1], argc-1, cmp, &casefold);
* for (i = 1; i < argc; i++)
* printf("%s ", argv[i]);
* printf("\n");
* return 0;
* }
*/
int main(int argc, char *argv[])
{
if (argc != 2)
return 1;

if (strcmp(argv[1], "depends") == 0) {
printf("ccan/typesafe_cb\n");
return 0;
}
if (strcmp(argv[1], "testdepends") == 0) {
printf("ccan/array_size\n");
return 0;
}

return 1;
}
Loading

0 comments on commit c91d2b5

Please sign in to comment.