5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2008 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silcutil/Global Variable Interface
24 * The Global Variable API can be used to set variables as global in the
25 * process or in current thread. On some platforms, like Symbian, global
26 * variables cannot be used directly. This API can be used to write
27 * portable code that can use global variables on all supported platforms.
31 * // Initialize global buffer
32 * unsigned char buffer[256];
33 * silc_global_set_va("somebuf", sizeof(buffer), NULL, FALSE);
35 * // Retrieve the buffer
36 * unsigned char *buf = silc_global_get_var("somebuf", FALSE);
38 * // Set integer and its value global in the current thread
39 * SilcUInt32 integer = 100;
40 * silc_global_set_var("someint", 4, &integer, TRUE);
42 * // Retrieve the integer and change its value
43 * SilcUInt32 *intptr = silc_global_get_var("someint", TRUE);
51 /****f* silcutil/silc_global_set_var
55 * void *silc_global_set_var(const char *name, SilcUInt32 variable_size,
56 * void *initial_value, SilcBool tls);
60 * Sets a variable of size of `variable_size' bytes as a global variable
61 * under the name of `name'. If `initial_value' is non-NULL it is
62 * considered to be the initial value of the stored variable. If it is
63 * NULL this will initialize the variable as all zeroes.
65 * If variable with `name' already exists it will be replaced with the
66 * `initial_value' or as all zeroes, and the old pointer will become
69 * If `tls' is FALSE the variable is visible to all threads in the process.
70 * If it is TRUE the variable is visible only in the current thread. The
71 * variable can be retrieved using the same name by calling the
72 * silc_thread_global_get_var.
74 * Returns NULL and sets silc_errno if the variable could not be added, or
75 * a pointer to the added variable otherwise.
79 * // Initialize global buffer
80 * unsigned char buffer[256];
81 * silc_global_set_va("somebuf", sizeof(buffer), NULL, FALSE);
83 * // Retrieve the buffer
84 * unsigned char *buf = silc_global_get_var("somebuf", FALSE);
86 * // Set integer global
87 * SilcUInt32 integer = 100;
88 * silc_global_set_var("someint", 4, &integer, FALSE);
90 * // Retrieve the integer and change its value
91 * SilcUInt32 *intptr = silc_global_get_var("someint", FALSE);
94 * // Set structure as global in the thread
95 * silc_global_set_va("somestruct", sizeof(*context), NULL, TRUE);
97 * // Retrieve the context
98 * context = silc_global_get_var("somestruct", TRUE);
101 void *silc_global_set_var(const char *name, SilcUInt32 variable_size,
102 void *initial_value, SilcBool tls);
104 /****f* silcutil/silc_global_get_var
108 * void *silc_global_get_var(const char *name, SilcBool tls);
112 * Returns the variable from the global variable storage that has been
113 * stored under the name `name'. If `tls' is FALSE this fetches the
114 * variable from global storage, if it is TRUE it is fetched from the
115 * Thread-local storage. Returns NULL if such variable does not exist
116 * and sets silc_errno.
119 void *silc_global_get_var(const char *name, SilcBool tls);
121 /****f* silcutil/silc_global_del_var
125 * SilcBool silc_global_del_var(const char *name, SilcBool tls);
129 * Deletes the global variable named `name'. Returns TRUE if it was
130 * deleted, FALSE if such variable does not exist and sets silc_errno.
132 * If variable is not deleted before the process or thread is destroyed
133 * it will be deleted and freed automatically.
136 SilcBool silc_global_del_var(const char *name, SilcBool tls);
138 #endif /* SILCGLOBAL_H */