Skip to content

Commit

Permalink
ccan: update.
Browse files Browse the repository at this point in the history
This was from a different series, so I just cherry-picked it.

It adds ccan/membuf as a depenency of ccan/rbuf, though we don't use
it directly yet.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Oct 19, 2018
1 parent 6af8f29 commit acc01e6
Show file tree
Hide file tree
Showing 18 changed files with 660 additions and 167 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ CCAN_OBJS := \
ccan-isaac64.o \
ccan-list.o \
ccan-mem.o \
ccan-membuf.o \
ccan-noerr.o \
ccan-opt-helpers.o \
ccan-opt-parse.o \
Expand Down Expand Up @@ -129,6 +130,7 @@ CCAN_HEADERS := \
$(CCANDIR)/ccan/likely/likely.h \
$(CCANDIR)/ccan/list/list.h \
$(CCANDIR)/ccan/mem/mem.h \
$(CCANDIR)/ccan/membuf/membuf.h \
$(CCANDIR)/ccan/noerr/noerr.h \
$(CCANDIR)/ccan/opt/opt.h \
$(CCANDIR)/ccan/opt/private.h \
Expand Down Expand Up @@ -601,3 +603,5 @@ ccan-str-base32.o: $(CCANDIR)/ccan/str/base32/base32.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-utf8.o: $(CCANDIR)/ccan/utf8/utf8.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-membuf.o: $(CCANDIR)/ccan/membuf/membuf.c
$(CC) $(CFLAGS) -c -o $@ $<
2 changes: 1 addition & 1 deletion ccan/README
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CCAN imported from http://ccodearchive.net.

CCAN version: init-2442-ga8722345
CCAN version: init-2446-g1b4ed377
5 changes: 4 additions & 1 deletion ccan/ccan/crypto/shachain/tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CFLAGS=-Wall -Werror -O3 -I$(CCANDIR) -DSHACHAIN_BITS=48
#CFLAGS=-Wall -Werror -g3 -I$(CCANDIR) -DSHACHAIN_BITS=48

# 48 bit index for shachain. This is what lightning uses.
CCAN_OBJS:=ccan-str.o ccan-err.o ccan-hex.o ccan-shachain.o ccan-sha256.o ccan-rbuf.o
CCAN_OBJS:=ccan-str.o ccan-err.o ccan-hex.o ccan-shachain.o ccan-sha256.o ccan-rbuf.o ccan-membuf.o

all: shachain48

Expand All @@ -15,6 +15,7 @@ shachain48.o: $(CCANDIR)/ccan/crypto/shachain/shachain.h \
$(CCANDIR)/ccan/str/hex/hex.h \
$(CCANDIR)/ccan/str/str.h \
$(CCANDIR)/ccan/err/err.h \
$(CCANDIR)/ccan/membuf/membuf.h \
$(CCANDIR)/ccan/rbuf/rbuf.h

shachain48.o $(CCAN_OBJS): $(CCANDIR)/config.h
Expand All @@ -37,3 +38,5 @@ ccan-sha256.o: $(CCANDIR)/ccan/crypto/sha256/sha256.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-rbuf.o: $(CCANDIR)/ccan/rbuf/rbuf.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-membuf.o: $(CCANDIR)/ccan/membuf/membuf.c
$(CC) $(CFLAGS) -c -o $@ $<
4 changes: 2 additions & 2 deletions ccan/ccan/crypto/shachain/tools/shachain48.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ int main(int argc, char *argv[])
char *p;

shachain_init(&s);
rbuf_init(&rbuf, STDIN_FILENO, malloc(size), size);
rbuf_init(&rbuf, STDIN_FILENO, malloc(size), size, membuf_realloc);

while ((p = rbuf_read_str(&rbuf, '\n', realloc)) != NULL) {
while ((p = rbuf_read_str(&rbuf, '\n')) != NULL) {
struct sha256 hash;
unsigned long long idx;

Expand Down
1 change: 1 addition & 0 deletions ccan/ccan/membuf/LICENSE
51 changes: 51 additions & 0 deletions ccan/ccan/membuf/_info
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "config.h"
#include <stdio.h>
#include <string.h>

/**
* membuf - simple linear memory buffer routines.
*
* It's common to want a linear memory buffer, where you can get memory on
* the end, and consume memory from the start. The details of actually
* when to enlarge or move the buffer are slightly nontrivial, so they're
* encapsulated here.
*
* License: BSD-MIT
* Author: Rusty Russell <[email protected]>
*
* Example:
* #include <ccan/membuf/membuf.h>
* #include <string.h>
* #include <stdio.h>
*
* // Given "hello world" outputs helloworld
* // Given "hello there world" outputs hellothereworld
* int main(int argc, char *argv[])
* {
* MEMBUF(char) charbuf;
*
* membuf_init(&charbuf, malloc(10), 10, membuf_realloc);
*
* for (int i = 1; i < argc; i++)
* strcpy(membuf_add(&charbuf, strlen(argv[i])), argv[i]);
*
* // This is dumb, we could do all at once, but shows technique.
* while (membuf_num_elems(&charbuf) > 0)
* printf("%c", *(char *)membuf_consume(&charbuf, 1));
* printf("\n");
* return 0;
* }
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;

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

return 1;
}
60 changes: 60 additions & 0 deletions ccan/ccan/membuf/membuf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* MIT (BSD) license - see LICENSE file for details */
#include <ccan/membuf/membuf.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

void membuf_init_(struct membuf *mb,
void *elems, size_t num_elems, size_t elemsize,
void *(*expandfn)(struct membuf *, void *, size_t))
{

mb->start = mb->end = 0;
mb->max_elems = num_elems;
mb->elems = elems;
mb->expandfn = expandfn;
}

size_t membuf_prepare_space_(struct membuf *mb,
size_t num_extra, size_t elemsize)
{
char *oldstart = membuf_elems_(mb, elemsize);

/* Always reset in the trivial empty case. */
if (mb->start == mb->end)
mb->start = mb->end = 0;

if (membuf_num_space_(mb) >= num_extra)
return 0;

/* There are two ways to make space: enlarge buffer, and memmove
* down. We use a simple heuristic: if we are using less than half
* the buffer, and memmove would get us sufficient space, do that. */
if (membuf_num_elems_(mb) <= mb->max_elems / 2
&& membuf_num_elems_(mb) + num_extra <= mb->max_elems) {
memmove(mb->elems, oldstart, (mb->end - mb->start) * elemsize);
mb->end -= mb->start;
mb->start = 0;
} else {
void *expand;

/* Since we're going to expand, at least double. */
if (num_extra < mb->max_elems)
num_extra = mb->max_elems;

expand = mb->expandfn(mb, mb->elems,
(mb->max_elems + num_extra) * elemsize);
if (!expand) {
errno = ENOMEM;
} else {
mb->max_elems += num_extra;
mb->elems = expand;
}
}
return (char *)membuf_elems_(mb, elemsize) - oldstart;
}

void *membuf_realloc(struct membuf *mb, void *rawelems, size_t newsize)
{
return realloc(rawelems, newsize);
}
Loading

0 comments on commit acc01e6

Please sign in to comment.