2 * Dealing with identifiers
4 * Copyright (C) 2014 Red Hat, Inc.
7 * Markus Armbruster <armbru@redhat.com>,
9 * This work is licensed under the terms of the GNU LGPL, version 2.1
10 * or later. See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
17 bool id_wellformed(const char *id
)
21 if (!qemu_isalpha(id
[0])) {
24 for (i
= 1; id
[i
]; i
++) {
25 if (!qemu_isalnum(id
[i
]) && !strchr("-._", id
[i
])) {
32 #define ID_SPECIAL_CHAR '#'
34 static const char *const id_subsys_str
[ID_MAX
] = {
40 * Generates an ID of the form PREFIX SUBSYSTEM NUMBER
43 * - PREFIX is the reserved character '#'
44 * - SUBSYSTEM identifies the subsystem creating the ID
45 * - NUMBER is a decimal number unique within SUBSYSTEM.
47 * Example: "#block146"
49 * Note that these IDs do not satisfy id_wellformed().
51 * The caller is responsible for freeing the returned string with g_free()
53 char *id_generate(IdSubSystems id
)
55 static uint64_t id_counters
[ID_MAX
];
58 assert(id
< ARRAY_SIZE(id_subsys_str
));
59 assert(id_subsys_str
[id
]);
61 rnd
= g_random_int_range(0, 100);
63 return g_strdup_printf("%c%s%" PRIu64
"%02" PRId32
, ID_SPECIAL_CHAR
,