forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
6af8f29
commit acc01e6
Showing
18 changed files
with
660 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../licenses/BSD-MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.