~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

« back to all changes in this revision

Viewing changes to src/VBox/Runtime/common/string/straprintf.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-30 23:27:25 UTC
  • mfrom: (0.3.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20110130232725-2ouajjd2ggdet0zd
Tags: 4.0.2-dfsg-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add Apport hook.
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Drop *-source packages.
* Drop ubuntu-01-fix-build-gcc45.patch, fixed upstream.
* Drop ubuntu-02-as-needed.patch, added to the Debian package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: straprintf.cpp $ */
 
1
/* $Id: straprintf.cpp 33464 2010-10-26 12:27:50Z vboxsync $ */
2
2
/** @file
3
3
 * IPRT - Allocating String Formatters.
4
4
 */
5
5
 
6
6
/*
7
 
 * Copyright (C) 2006-2007 Oracle Corporation
 
7
 * Copyright (C) 2006-2010 Oracle Corporation
8
8
 *
9
9
 * This file is part of VirtualBox Open Source Edition (OSE), as
10
10
 * available from http://www.virtualbox.org. This file is free software;
42
42
typedef struct STRALLOCARG
43
43
{
44
44
    /** Pointer to current buffer position. */
45
 
    char   *psz;
 
45
    char       *psz;
46
46
    /** Number of bytes left in the buffer - not including the trailing zero. */
47
 
    size_t  cch;
 
47
    size_t      cch;
48
48
    /** Pointer to the start of the buffer. */
49
 
    char   *pszBuffer;
 
49
    char       *pszBuffer;
50
50
    /** The number of bytes in the buffer. */
51
 
    size_t  cchBuffer;
 
51
    size_t      cchBuffer;
52
52
    /** Set if the buffer was allocated using RTMemRealloc(). If clear
53
53
     * pszBuffer points to the initial stack buffer. */
54
 
    bool    fAllocated;
 
54
    bool        fAllocated;
 
55
    /** Allocation tag used for statistics and such. */
 
56
    const char *pszTag;
55
57
} STRALLOCARG;
56
58
/** Pointer to a strallocoutput() argument structure. */
57
59
typedef STRALLOCARG *PSTRALLOCARG;
99
101
            cbAdded = RT_ALIGN_Z(cbChars, _4K);
100
102
        if (cbAdded <= _1G)
101
103
        {
102
 
            char *pszBuffer = (char *)RTMemRealloc(pArg->fAllocated ? pArg->pszBuffer : NULL, cbAdded + pArg->cchBuffer);
 
104
            char *pszBuffer = (char *)RTMemReallocTag(pArg->fAllocated ? pArg->pszBuffer : NULL,
 
105
                                                      cbAdded + pArg->cchBuffer, pArg->pszTag);
103
106
            if (pszBuffer)
104
107
            {
105
108
                size_t off = pArg->psz - pArg->pszBuffer;
134
137
}
135
138
 
136
139
 
137
 
RTDECL(int) RTStrAPrintfV(char **ppszBuffer, const char *pszFormat, va_list args)
 
140
RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag)
138
141
{
 
142
#ifdef IN_RING3
139
143
    char            szBuf[2048];
 
144
#else
 
145
    char            szBuf[256];
 
146
#endif
140
147
    STRALLOCARG     Arg;
141
148
    Arg.fAllocated  = false;
142
149
    Arg.cchBuffer   = sizeof(szBuf);
143
150
    Arg.pszBuffer   = szBuf;
144
151
    Arg.cch         = sizeof(szBuf) - 1;
145
152
    Arg.psz         = szBuf;
 
153
    Arg.pszTag      = pszTag;
146
154
    szBuf[0] = '\0';
147
155
    int cbRet = (int)RTStrFormatV(strallocoutput, &Arg, NULL, NULL, pszFormat, args);
148
156
    if (Arg.psz)
151
159
        {
152
160
            /* duplicate the string in szBuf */
153
161
            Assert(Arg.pszBuffer == szBuf);
154
 
            char *psz = (char *)RTMemAlloc(cbRet + 1);
 
162
            char *psz = (char *)RTMemAllocTag(cbRet + 1, pszTag);
155
163
            if (psz)
156
164
                memcpy(psz, szBuf, cbRet + 1);
157
165
            *ppszBuffer = psz;
159
167
        else
160
168
        {
161
169
            /* adjust the allocated buffer */
162
 
            char *psz = (char *)RTMemRealloc(Arg.pszBuffer, cbRet + 1);
 
170
            char *psz = (char *)RTMemReallocTag(Arg.pszBuffer, cbRet + 1, pszTag);
163
171
            *ppszBuffer = psz ? psz : Arg.pszBuffer;
164
172
        }
165
173
    }
176
184
 
177
185
    return cbRet;
178
186
}
179
 
RT_EXPORT_SYMBOL(RTStrAPrintfV);
180
 
 
181
 
 
182
 
RTDECL(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
183
 
{
184
 
    va_list args;
185
 
    va_start(args, pszFormat);
186
 
    int cbRet = RTStrAPrintfV(ppszBuffer, pszFormat, args);
187
 
    va_end(args);
188
 
    return cbRet;
189
 
}
190
 
RT_EXPORT_SYMBOL(RTStrAPrintf);
191
 
 
192
 
 
193
 
RTDECL(char *) RTStrAPrintf2V(const char *pszFormat, va_list args)
 
187
RT_EXPORT_SYMBOL(RTStrAPrintfVTag);
 
188
 
 
189
 
 
190
RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag)
194
191
{
195
192
    char *pszBuffer;
196
 
    RTStrAPrintfV(&pszBuffer, pszFormat, args);
197
 
    return pszBuffer;
198
 
}
199
 
RT_EXPORT_SYMBOL(RTStrAPrintf2V);
200
 
 
201
 
 
202
 
RTDECL(char *) RTStrAPrintf2(const char *pszFormat, ...)
203
 
{
204
 
    va_list va;
205
 
    char   *pszBuffer;
206
 
 
207
 
    va_start(va, pszFormat);
208
 
    RTStrAPrintfV(&pszBuffer, pszFormat, va);
209
 
    va_end(va);
210
 
 
211
 
    return pszBuffer;
212
 
}
213
 
RT_EXPORT_SYMBOL(RTStrAPrintf2);
 
193
    RTStrAPrintfVTag(&pszBuffer, pszFormat, args, pszTag);
 
194
    return pszBuffer;
 
195
}
 
196
RT_EXPORT_SYMBOL(RTStrAPrintf2VTag);
214
197