aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2008-06-16 23:19:29 +0000
committerAris Adamantiadis <aris@0xbadc0de.be>2008-06-16 23:19:29 +0000
commit1fce8c72e79a69b0478b2f3bf84d058ebaf5211c (patch)
tree7f772c0db157a780888b97bd0983c4f99b0173df
parent77603dbc5a5c55ecfa8d583c133db844673fb690 (diff)
downloadlibssh-1fce8c72e79a69b0478b2f3bf84d058ebaf5211c.tar.gz
libssh-1fce8c72e79a69b0478b2f3bf84d058ebaf5211c.tar.xz
libssh-1fce8c72e79a69b0478b2f3bf84d058ebaf5211c.zip
more efficient allocation algorithm for buffer.c
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@170 7dcaeef0-15fb-0310-b436-a5af3365683c
-rw-r--r--libssh/buffer.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/libssh/buffer.c b/libssh/buffer.c
index 3dea65d1..12d43c78 100644
--- a/libssh/buffer.c
+++ b/libssh/buffer.c
@@ -52,6 +52,18 @@ void buffer_free(BUFFER *buffer){
free(buffer);
}
+static void realloc_buffer(BUFFER *buffer,int needed){
+ int smallest=1;
+ // find the smallest power of two which is greater or equal to needed
+ while(smallest<=needed)
+ smallest <<= 1;
+ needed=smallest;
+// printf("buffer %p : realloc(%x,%d)=",buffer,buffer->data,needed);
+ buffer->data=realloc(buffer->data,needed);
+// printf("%p\n",buffer->data);
+ buffer->allocated=needed;
+}
+
/* \internal
* \brief reinitialize a buffer
* \param buffer buffer
@@ -60,15 +72,11 @@ void buffer_reinit(BUFFER *buffer){
memset(buffer->data,0,buffer->used);
buffer->used=0;
buffer->pos=0;
+ if(buffer->allocated > 127){
+ realloc_buffer(buffer,127);
+ }
}
-static void realloc_buffer(BUFFER *buffer,int needed){
- needed=(needed+0x7f) & ~0x7f;
-// printf("buffer %p : realloc(%x,%d)=",buffer,buffer->data,needed);
- buffer->data=realloc(buffer->data,needed);
-// printf("%p\n",buffer->data);
- buffer->allocated=needed;
-}
/** \internal
* \brief add data at tail of the buffer
* \param buffer buffer