Skip to content

Commit

Permalink
Minor optimizations in RBuffer.bytes ##performance
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae authored Oct 20, 2024
1 parent 3e6f559 commit 6e7be05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 6 additions & 0 deletions libr/util/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,12 @@ R_API st64 r_buf_fwrite_at(RBuffer *b, ut64 addr, const ut8 *buf, const char *fm
return r;
}

#if R2_600
R_API ut64 r_buf_at(RBuffer *b) {
return r_buf_seek (b, 0, R_BUF_CUR);
}
#endif

R_API st64 r_buf_read_at(RBuffer *b, ut64 addr, ut8 *buf, ut64 len) {
R_RETURN_VAL_IF_FAIL (b && buf, -1);
st64 o_addr = r_buf_seek (b, 0, R_BUF_CUR);
Expand Down
15 changes: 9 additions & 6 deletions libr/util/buf_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ static st64 buf_bytes_read(RBuffer *b, ut8 *buf, ut64 len) {
return 0;
}
ut64 real_len = priv->length < priv->offset? 0: R_MIN (priv->length - priv->offset, len);
memmove (buf, priv->buf + priv->offset, real_len);
// memmove (buf, priv->buf + priv->offset, real_len);
memcpy (buf, priv->buf + priv->offset, real_len);
priv->offset += real_len;
return real_len;
}
Expand All @@ -115,7 +116,7 @@ static ut64 buf_bytes_get_size(RBuffer *b) {

static st64 buf_bytes_seek(RBuffer *b, st64 addr, int whence) {
struct buf_bytes_priv *priv = get_priv_bytes (b);
if (addr < 0) {
if (R_UNLIKELY (addr < 0)) {
if (addr > -UT48_MAX) {
if (-addr > (st64)priv->offset) {
return -1;
Expand All @@ -124,17 +125,19 @@ static st64 buf_bytes_seek(RBuffer *b, st64 addr, int whence) {
return -1;
}
}
ut64 po = priv->offset;
if (R_LIKELY (whence == R_BUF_SET)) {
// 50%
priv->offset = addr;
po = addr;
} else if (whence == R_BUF_CUR) {
// 20%
priv->offset += addr;
po += addr;
} else {
// 5%
priv->offset = priv->length + addr;
po = priv->length + addr;
}
return priv->offset;
priv->offset = po;
return po;
}

static ut8 *buf_bytes_get_whole_buf(RBuffer *b, ut64 *sz) {
Expand Down

0 comments on commit 6e7be05

Please sign in to comment.