Skip to content

Commit 1cbfe83

Browse files
committed
* **BREAKING** mulle_buffer_zero_to_length is now done in mulle_buffer_set_length
* **BREAKING** the length of the buffer before the overflow happened is now saved, so string and data extraction will provide the date before the overflow happened * **BREAKING** `mulle_buffer_set_seek` has change parameter order to match `lseek` and `fseek` * **BREAKING** mulle-flexbuffer is dead, use mulle-c11 mulle-alloca instead * **BREAKING** `mulle_init` now has a capacity parameter just like the container init functions, maybe use `mulle_init_default` instead * **BREAKING** `mulle-buffer-set-length` now has an `options` argument, which determines zerofilling and size fitting * new `_default` functions like `mulle_buffer_create_default` allow the user to leave out an allocator argument, lessening the cognitive load * `mulle-buffer-do` gains a default `alloca` so you have to do even less work to get better performing code * mulle-buffer can now be set to readonly or writeonly (in addition to the default read/write)
1 parent 6bc2c68 commit 1cbfe83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2292
-2131
lines changed

.mulle/share/env/tool-extension

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.mulle/share/sde/extension

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ It's easy, fast and safe. It can also be used as a stream and it is used to
88
implement NSMutableData. mulle-buffer has functions to create hexdumps and
99
quoted C string output.
1010

11-
There is a sibling project [mulle-utf32buffer](//mulle-c/mulle-utf32buffer)
12-
to construct UTF32 strings.
11+
With [mulle-fprintf](//github.com/mulle-core/mulle_fprintf), you can use
12+
`printf` style formatting and create string concatenations without
13+
having to worry about memory management.
1314

1415

1516

@@ -20,11 +21,10 @@ to construct UTF32 strings.
2021

2122
## API
2223

23-
| Data Structure | Description
24-
| ---------------------------------------------| ----------------------------------------
25-
| [`mulle-buffer`](dox/API_BUFFER.md) | A resizable byte buffer that can grow dynamically as needed
26-
27-
24+
| Data Structure | Description
25+
| ------------------------------------------------------| ----------------------------------------
26+
| [`mulle-buffer`](dox/API_BUFFER.md) | A resizable alloca buffer that grows to the heaep if needed
27+
| [`mulle-flushablebuffer`](dox/API_FLUSHABLEBUFFER.md) | Useful for dumps and other longer output
2828

2929

3030

@@ -59,11 +59,12 @@ As soon as the `mulle_buffer_do` block is exited, the buffer will be invalid.
5959
> A `break` outside of the block is OK and does not leak, but a return will
6060
6161
62-
### Use the stack for small strings
62+
### Use explict stack memory for small strings
6363
64-
If you expect the string to be small in most circumstances, you can use
65-
`mulle_buffer_do_flexible` to keep character storage on the stack as long
66-
as possible. If the stack storage is exhausted, the string will be copied
64+
`mulle_buffer_do` will create a default sized `alloca` ca. 100 bytes.
65+
66+
If you want to specify the amount of stack space yourself, you can use
67+
`mulle_buffer_do_flexible`. If the stack storage is exhausted, the string will be copied
6768
to dynamically allocated memory:
6869
6970
@@ -83,24 +84,30 @@ void test( void)
8384
}
8485
```
8586

87+
![pix/mulle-buffer-alloca.svg]
88+
89+
8690
If you don't want the string to ever exceed the initial storage length
8791
you can use `mulle_buffer_do_inflexible`:
8892

8993
``` c
9094
void test( void)
9195
{
92-
char tmp[ 4];
96+
char tmp[ 8]; // space for seven characters and trailing zero
9397

9498
mulle_buffer_do_inflexible( buffer, tmp)
9599
{
96-
mulle_buffer_add_string( buffer, "hello");
100+
mulle_buffer_add_string( buffer, "VfL_");
101+
mulle_buffer_add_string( buffer, "Bochum");
97102

98103
printf( "%s\n", mulle_buffer_get_string( buffer));
99104
}
100105
}
101106
```
102107
103-
This should print "hel", as a trailing zero will be needed for the last
108+
![pix/mulle-buffer-alloca.svg]
109+
110+
This should print "Vf_", as a trailing zero will be needed for the last
104111
character.
105112
106113
@@ -138,6 +145,24 @@ void test( void)
138145

139146
You will have to `mulle_free` the constructed string "s".
140147

148+
### Permanent mulle-buffer
149+
150+
If the mulle-buffer should live longer than the current function, create
151+
and destroy a buffer manually:
152+
153+
``` c
154+
buffer = mulle_buffer_create_default();
155+
...
156+
mulle_buffer_destroy( buffer);
157+
```
158+
159+
or
160+
161+
``` c
162+
mulle_buffer_init_default( &buffer);
163+
...
164+
mulle_buffer_done( buffer);
165+
```
141166

142167
> #### Tip
143168
>

clib.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" : "mulle-buffer",
3-
"version" : "3.5.2",
3+
"version" : "4.0.0",
44
"description" : "↗️ A growable C char array and also a stream",
55
"keywords" : [],
66
"license" : "BSD-3-Clause",
@@ -10,10 +10,8 @@
1010
"src/generic/include.h",
1111
"src/mulle--buffer.c",
1212
"src/mulle--buffer.h",
13-
"src/mulle-buffer-standalone.c",
1413
"src/mulle-buffer.c",
1514
"src/mulle-buffer.h",
16-
"src/mulle-flexbuffer.h",
1715
"src/mulle-flushablebuffer.c",
1816
"src/mulle-flushablebuffer.h",
1917
"src/reflect/_mulle-buffer-include-private.h",

cmake/reflect/_Headers.cmake

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmake/reflect/_Sources.cmake

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmake/share/CMakeTweaksC.cmake

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cola/api.md.bud

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
## API
22

3-
| Data Structure | Description
4-
| ---------------------------------------------| ----------------------------------------
5-
| [`mulle-buffer`](dox/API_BUFFER.md) | A resizable byte buffer that can grow dynamically as needed
6-
7-
3+
| Data Structure | Description
4+
| ------------------------------------------------------| ----------------------------------------
5+
| [`mulle-buffer`](dox/API_BUFFER.md) | A resizable alloca buffer that grows to the heaep if needed
6+
| [`mulle-flushablebuffer`](dox/API_FLUSHABLEBUFFER.md) | Useful for dumps and other longer output
87

cola/info.md.bud

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ As soon as the `mulle_buffer_do` block is exited, the buffer will be invalid.
2828
> A `break` outside of the block is OK and does not leak, but a return will
2929

3030

31-
### Use the stack for small strings
31+
### Use explict stack memory for small strings
3232

33-
If you expect the string to be small in most circumstances, you can use
34-
`mulle_buffer_do_flexible` to keep character storage on the stack as long
35-
as possible. If the stack storage is exhausted, the string will be copied
33+
`mulle_buffer_do` will create a default sized `alloca` ca. 100 bytes.
34+
35+
If you want to specify the amount of stack space yourself, you can use
36+
`mulle_buffer_do_flexible`. If the stack storage is exhausted, the string will be copied
3637
to dynamically allocated memory:
3738

3839

@@ -52,24 +53,30 @@ void test( void)
5253
}
5354
```
5455

56+
![pix/mulle-buffer-alloca.svg]
57+
58+
5559
If you don't want the string to ever exceed the initial storage length
5660
you can use `mulle_buffer_do_inflexible`:
5761

5862
``` c
5963
void test( void)
6064
{
61-
char tmp[ 4];
65+
char tmp[ 8]; // space for seven characters and trailing zero
6266

6367
mulle_buffer_do_inflexible( buffer, tmp)
6468
{
65-
mulle_buffer_add_string( buffer, "hello");
69+
mulle_buffer_add_string( buffer, "VfL_");
70+
mulle_buffer_add_string( buffer, "Bochum");
6671

6772
printf( "%s\n", mulle_buffer_get_string( buffer));
6873
}
6974
}
7075
```
7176

72-
This should print "hel", as a trailing zero will be needed for the last
77+
![pix/mulle-buffer-alloca.svg]
78+
79+
This should print "Vf_", as a trailing zero will be needed for the last
7380
character.
7481

7582

@@ -118,6 +125,13 @@ buffer = mulle_buffer_create_default();
118125
mulle_buffer_destroy( buffer);
119126
```
120127

128+
or
129+
130+
``` c
131+
mulle_buffer_init_default( &buffer);
132+
...
133+
mulle_buffer_done( buffer);
134+
```
121135

122136
> #### Tip
123137
>

0 commit comments

Comments
 (0)