forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Ringbuf: fix PacketBuffer; clean up ringbuf implementation and use #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
38ec3bc
further ringbuf cleanup
dhalbert 77cd93a
merge from adafruit
dhalbert fbc8719
ringbuf tested
dhalbert 84cee1a
rename and improve PacketBuffer packet length property
dhalbert 3d62f87
back to '.packet_size' for compatiblity
dhalbert 180f5c6
Merge remote-tracking branch 'adafruit/master' into ringbuf-fixes
dhalbert f307851
further cleanup and bug fixing
dhalbert 241d7e2
change many ifndefs to ?=
dhalbert 46298dd
PacketBuffer doc fixes
dhalbert 7337ea4
fix support_matrix.py to handle ?=
dhalbert b0383f4
make translate; remove PO-Revision-Date to reduce merge changes in .p…
dhalbert d6c6f9f
add PacketBuffer .incoming_ and .outgoing_packet_length
dhalbert bae7a5e
make translate again
dhalbert b7836ae
address review comments
dhalbert c0af9f6
make translate
dhalbert 0105acb
Merge remote-tracking branch 'adafruit/master' into ringbuf-fixes
dhalbert b23ff41
merge from upstream and make translate again (sv.po)
dhalbert e1366d7
trivial change to force rebuild
dhalbert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
further ringbuf cleanup
- Loading branch information
commit 38ec3bc57476c6fde45cd7d722370a5408fd8e10
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,109 @@ | ||
| /* | ||
| * This file is part of the MicroPython project, http://micropython.org/ | ||
| * | ||
| * The MIT License (MIT) | ||
| * | ||
| * Copyright (c) 2016 Paul Sokolovsky | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
|
|
||
| #include "ringbuf.h" | ||
|
|
||
| // Dynamic initialization. This should be accessible from a root pointer. | ||
| // capacity is the number of bytes the ring buffer can hold. The actual | ||
| // size of the buffer is one greater than that, due to how the buffer | ||
| // handles empty and full statuses. | ||
| bool ringbuf_alloc(ringbuf_t *r, size_t capacity, bool long_lived) { | ||
| r->buf = gc_alloc(capacity + 1, false, long_lived); | ||
| r->size = capacity + 1; | ||
| r->iget = r->iput = 0; | ||
| return r->buf != NULL; | ||
| } | ||
|
|
||
| size_t ringbuf_capacity(ringbuf_t *r) { | ||
| return r->size - 1; | ||
| } | ||
|
|
||
| // Returns -1 if buffer is empty, else returns byte fetched. | ||
| int ringbuf_get(ringbuf_t *r) { | ||
| if (r->iget == r->iput) { | ||
| return -1; | ||
| } | ||
| uint8_t v = r->buf[r->iget++]; | ||
| if (r->iget >= r->size) { | ||
| r->iget = 0; | ||
| } | ||
| return v; | ||
| } | ||
|
|
||
| // Returns -1 if no room in buffer, else returns 0. | ||
| int ringbuf_put(ringbuf_t *r, uint8_t v) { | ||
| uint32_t iput_new = r->iput + 1; | ||
| if (iput_new >= r->size) { | ||
| iput_new = 0; | ||
| } | ||
| if (iput_new == r->iget) { | ||
| return -1; | ||
| } | ||
| r->buf[r->iput] = v; | ||
| r->iput = iput_new; | ||
| return 0; | ||
| } | ||
|
|
||
| void ringbuf_clear(ringbuf_t *r) { | ||
| r->iput = r->iget = 0; | ||
| } | ||
|
|
||
| // Number of free slots that can be written. | ||
| size_t ringbuf_free(ringbuf_t *r) { | ||
| return (r->size + r->iget - r->iput - 1) % r->size; | ||
| } | ||
|
|
||
| // Number of bytes available to read. | ||
| size_t ringbuf_avail(ringbuf_t *r) { | ||
| return (r->size + r->iput - r->iget) % r->size; | ||
| } | ||
|
|
||
| // If the ring buffer fills up, not all bytes will be written. | ||
| // Returns how many bytes were successfully written. | ||
| size_t ringbuf_put_n(ringbuf_t* r, uint8_t* buf, size_t bufsize) | ||
| { | ||
| for(size_t i=0; i < bufsize; i++) { | ||
| if ( ringbuf_put(r, buf[i]) < 0 ) { | ||
| // If ringbuf is full, give up and return how many bytes | ||
| // we wrote so far. | ||
| return i; | ||
| } | ||
| } | ||
| return bufsize; | ||
| } | ||
|
|
||
| // Returns how many bytes were fetched. | ||
| size_t ringbuf_get_n(ringbuf_t* r, uint8_t* buf, size_t bufsize) | ||
| { | ||
| for(size_t i=0; i < bufsize; i++) { | ||
| int b = ringbuf_get(r); | ||
| if (b < 0) { | ||
| return i; | ||
| } | ||
| buf[i] = b; | ||
| } | ||
| return bufsize; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.