hw/arm/aspeed: add support for the Qualcomm DC-SCM v1 board
[qemu.git] / util / id.c
blobded41c5025e4a0a1c3422469ff723b91d8c210c2
1 /*
2 * Dealing with identifiers
4 * Copyright (C) 2014 Red Hat, Inc.
6 * Authors:
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/ctype.h"
15 #include "qemu/id.h"
17 bool id_wellformed(const char *id)
19 int i;
21 if (!qemu_isalpha(id[0])) {
22 return false;
24 for (i = 1; id[i]; i++) {
25 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
26 return false;
29 return true;
32 #define ID_SPECIAL_CHAR '#'
34 static const char *const id_subsys_str[ID_MAX] = {
35 [ID_QDEV] = "qdev",
36 [ID_BLOCK] = "block",
37 [ID_CHR] = "chr",
38 [ID_NET] = "net",
42 * Generates an ID of the form PREFIX SUBSYSTEM NUMBER
43 * where:
45 * - PREFIX is the reserved character '#'
46 * - SUBSYSTEM identifies the subsystem creating the ID
47 * - NUMBER is a decimal number unique within SUBSYSTEM.
49 * Example: "#block146"
51 * Note that these IDs do not satisfy id_wellformed().
53 * The caller is responsible for freeing the returned string with g_free()
55 char *id_generate(IdSubSystems id)
57 static uint64_t id_counters[ID_MAX];
58 uint32_t rnd;
60 assert(id < ARRAY_SIZE(id_subsys_str));
61 assert(id_subsys_str[id]);
63 rnd = g_random_int_range(0, 100);
65 return g_strdup_printf("%c%s%" PRIu64 "%02" PRId32, ID_SPECIAL_CHAR,
66 id_subsys_str[id],
67 id_counters[id]++,
68 rnd);