~ubuntu-branches/ubuntu/precise/crossroads/precise

« back to all changes in this revision

Viewing changes to xr/netbuffer/checkspace.cc

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ritter
  • Date: 2010-07-05 16:27:00 UTC
  • Revision ID: james.westby@ubuntu.com-20100705162700-0g08tfav8ee9y51u
Tags: upstream-2.65
ImportĀ upstreamĀ versionĀ 2.65

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "netbuffer"
 
2
 
 
3
void Netbuffer::check_space(unsigned extra) {
 
4
    PROFILE("Netbuffer::check_space");
 
5
 
 
6
    if (!buf_alloced) {
 
7
        buf_alloced = extra;
 
8
        // When the first network buffer is allocated in HTTP mode, get
 
9
        // twice as much. Most often that will be enough to fetch the whole
 
10
        // client request, so that one realloc() will be spared.
 
11
        if (extra == config.buffersize() &&
 
12
            config.stype() == Servertype::t_http)
 
13
            buf_alloced <<= 1;
 
14
        debugmsg (Mstr("Netbuffer: reserving ") + buf_alloced +
 
15
                  " bytes for network buffer\n");
 
16
        LOCK_MALLOC;
 
17
        buf_data = (char*)malloc(buf_alloced);
 
18
        UNLOCK_MALLOC;
 
19
        if (! buf_data)
 
20
            throw Error("Memory fault in Netbuffer::check_space");
 
21
    } else if (buf_sz + extra > buf_alloced) {
 
22
        debugmsg((Mstr("Netbuffer: reallocating net buffer from ") +
 
23
                  buf_alloced) +
 
24
                 (Mstr(" to ") + (buf_alloced + extra)) + " bytes\n");
 
25
        buf_alloced += extra;
 
26
        LOCK_MALLOC;
 
27
        buf_data = (char*)realloc(buf_data, buf_alloced);
 
28
        UNLOCK_MALLOC;
 
29
        if (! buf_data)
 
30
            throw Error("Memory fault in Netbuffer::check_space");
 
31
    }
 
32
}