VirtualBox

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

Last change on this file since 83886 was 83745, checked in by vboxsync, 5 years ago

IPRT: Sort IPRT status messages so we can look them up using binary searching. bugref:8489

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.4 KB
Line 
1/* $Id: errmsg.cpp 83745 2020-04-17 12:33:33Z 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.215389.xyz. 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* Global Variables *
41*********************************************************************************************************************************/
42/** Array of messages.
43 * The data is generated by a sed script.
44 */
45static const RTSTATUSMSG g_aStatusMsgs[] =
46{
47#if !defined(IPRT_NO_ERROR_DATA) && !defined(DOXYGEN_RUNNING)
48# include "errmsgdata-sorted.h"
49#else
50 { "Success.", "Success.", "VINF_SUCCESS", 0 },
51#endif
52};
53
54
55/** Temporary buffers to format unknown messages in.
56 * @{
57 */
58static char g_aszUnknownStr[8][64];
59static RTSTATUSMSG g_aUnknownMsgs[8] =
60{
61 { &g_aszUnknownStr[0][0], &g_aszUnknownStr[0][0], &g_aszUnknownStr[0][0], 0 },
62 { &g_aszUnknownStr[1][0], &g_aszUnknownStr[1][0], &g_aszUnknownStr[1][0], 0 },
63 { &g_aszUnknownStr[2][0], &g_aszUnknownStr[2][0], &g_aszUnknownStr[2][0], 0 },
64 { &g_aszUnknownStr[3][0], &g_aszUnknownStr[3][0], &g_aszUnknownStr[3][0], 0 },
65 { &g_aszUnknownStr[4][0], &g_aszUnknownStr[4][0], &g_aszUnknownStr[4][0], 0 },
66 { &g_aszUnknownStr[5][0], &g_aszUnknownStr[5][0], &g_aszUnknownStr[5][0], 0 },
67 { &g_aszUnknownStr[6][0], &g_aszUnknownStr[6][0], &g_aszUnknownStr[6][0], 0 },
68 { &g_aszUnknownStr[7][0], &g_aszUnknownStr[7][0], &g_aszUnknownStr[7][0], 0 },
69};
70/** Last used index in g_aUnknownMsgs. */
71static volatile uint32_t g_iUnknownMsgs;
72/** @} */
73
74
75/**
76 * Get the message corresponding to a given status code.
77 *
78 * @returns Pointer to read-only message description.
79 * @param rc The status code.
80 */
81RTDECL(PCRTSTATUSMSG) RTErrGet(int rc)
82{
83 /*
84 * Perform binary search (duplicate code in RTErrWinGet).
85 */
86 size_t iStart = 0;
87 size_t iEnd = RT_ELEMENTS(g_aStatusMsgs);
88 for (;;)
89 {
90 size_t i = iStart + (iEnd - iStart) / 2;
91 int const iCode = g_aStatusMsgs[i].iCode;
92 if (rc < iCode)
93 {
94 if (iStart < i)
95 iEnd = i;
96 else
97 break;
98 }
99 else if (rc > iCode)
100 {
101 i++;
102 if (i < iEnd)
103 iStart = i;
104 else
105 break;
106 }
107 else
108 return &g_aStatusMsgs[i];
109 }
110
111#ifdef RT_STRICT
112 for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
113 Assert(g_aStatusMsgs[i].iCode != rc);
114#endif
115
116 /*
117 * Need to use the temporary stuff.
118 */
119 int iMsg = ASMAtomicXchgU32(&g_iUnknownMsgs, (g_iUnknownMsgs + 1) % RT_ELEMENTS(g_aUnknownMsgs));
120 RTStrPrintf(&g_aszUnknownStr[iMsg][0], sizeof(g_aszUnknownStr[iMsg]), "Unknown Status %d (%#x)", rc, rc);
121 return &g_aUnknownMsgs[iMsg];
122}
123RT_EXPORT_SYMBOL(RTErrGet);
124
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