VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/err/errmsg.cpp@ 84050

Last change on this file since 84050 was 83984, checked in by vboxsync, 5 years ago

IPRT: Reduce the amoung of strings in errmsg and errmsgwin in static builds (IN_RT_STATIC). The windows ones are almost never used anywhere anyways.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.2 KB
Line 
1/* $Id: errmsg.cpp 83984 2020-04-27 00:17:19Z vboxsync $ */
2/** @file
3 * IPRT - Status code messages.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/err.h>
32#include "internal/iprt.h"
33
34#include <iprt/asm.h>
35#include <iprt/string.h>
36#include <VBox/err.h>
37
38
39/*********************************************************************************************************************************
40* Defined Constants And Macros *
41*********************************************************************************************************************************/
42#ifdef IPRT_ERRMSG_DEFINES_ONLY
43# define ENTRY(a_pszMsgShort, a_pszMsgFull, a_pszDefine, a_iCode) \
44 { a_pszDefine, a_pszDefine, a_pszDefine, a_iCode }
45#elif defined(IN_RT_STATIC) /* Skip the full message in static builds to save space. */
46# define ENTRY(a_pszMsgShort, a_pszMsgFull, a_pszDefine, a_iCode) \
47 { a_pszMsgShort, a_pszMsgShort, a_pszDefine, a_iCode }
48#else
49# define ENTRY(a_pszMsgShort, a_pszMsgFull, a_pszDefine, a_iCode) \
50 { a_pszMsgShort, a_pszMsgFull, a_pszDefine, a_iCode }
51#endif
52
53
54/*********************************************************************************************************************************
55* Global Variables *
56*********************************************************************************************************************************/
57/** Array of messages.
58 * The data is generated by a sed script.
59 */
60static const RTSTATUSMSG g_aStatusMsgs[] =
61{
62#if !defined(IPRT_NO_ERROR_DATA) && !defined(DOXYGEN_RUNNING)
63# include "errmsgdata-sorted.h"
64#else
65 { "Success.", "Success.", "VINF_SUCCESS", 0 },
66#endif
67};
68
69
70/** Temporary buffers to format unknown messages in.
71 * @{
72 */
73static char g_aszUnknownStr[8][64];
74static RTSTATUSMSG g_aUnknownMsgs[8] =
75{
76 { &g_aszUnknownStr[0][0], &g_aszUnknownStr[0][0], &g_aszUnknownStr[0][0], 0 },
77 { &g_aszUnknownStr[1][0], &g_aszUnknownStr[1][0], &g_aszUnknownStr[1][0], 0 },
78 { &g_aszUnknownStr[2][0], &g_aszUnknownStr[2][0], &g_aszUnknownStr[2][0], 0 },
79 { &g_aszUnknownStr[3][0], &g_aszUnknownStr[3][0], &g_aszUnknownStr[3][0], 0 },
80 { &g_aszUnknownStr[4][0], &g_aszUnknownStr[4][0], &g_aszUnknownStr[4][0], 0 },
81 { &g_aszUnknownStr[5][0], &g_aszUnknownStr[5][0], &g_aszUnknownStr[5][0], 0 },
82 { &g_aszUnknownStr[6][0], &g_aszUnknownStr[6][0], &g_aszUnknownStr[6][0], 0 },
83 { &g_aszUnknownStr[7][0], &g_aszUnknownStr[7][0], &g_aszUnknownStr[7][0], 0 },
84};
85/** Last used index in g_aUnknownMsgs. */
86static volatile uint32_t g_iUnknownMsgs;
87/** @} */
88
89
90/**
91 * Get the message corresponding to a given status code.
92 *
93 * @returns Pointer to read-only message description.
94 * @param rc The status code.
95 */
96RTDECL(PCRTSTATUSMSG) RTErrGet(int rc)
97{
98 /*
99 * Perform binary search (duplicate code in RTErrWinGet).
100 */
101 size_t iStart = 0;
102 size_t iEnd = RT_ELEMENTS(g_aStatusMsgs);
103 for (;;)
104 {
105 size_t i = iStart + (iEnd - iStart) / 2;
106 int const iCode = g_aStatusMsgs[i].iCode;
107 if (rc < iCode)
108 {
109 if (iStart < i)
110 iEnd = i;
111 else
112 break;
113 }
114 else if (rc > iCode)
115 {
116 i++;
117 if (i < iEnd)
118 iStart = i;
119 else
120 break;
121 }
122 else
123 return &g_aStatusMsgs[i];
124 }
125
126#ifdef RT_STRICT
127 for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
128 Assert(g_aStatusMsgs[i].iCode != rc);
129#endif
130
131 /*
132 * Need to use the temporary stuff.
133 */
134 int iMsg = ASMAtomicXchgU32(&g_iUnknownMsgs, (g_iUnknownMsgs + 1) % RT_ELEMENTS(g_aUnknownMsgs));
135 RTStrPrintf(&g_aszUnknownStr[iMsg][0], sizeof(g_aszUnknownStr[iMsg]), "Unknown Status %d (%#x)", rc, rc);
136 return &g_aUnknownMsgs[iMsg];
137}
138RT_EXPORT_SYMBOL(RTErrGet);
139
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette