00001 #ifndef _STRING_C_
00002 #define _STRING_C_
00003
00004 #include <linux/config.h>
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef STP_STRING_SIZE
00020 #define STP_STRING_SIZE 2048
00021 #endif
00022
00023 struct string {
00024 short len;
00025 short global;
00026 char buf[STP_STRING_SIZE];
00027 };
00028
00029 static struct string _stp_string[STP_NUM_STRINGS][NR_CPUS];
00030
00031 typedef struct string *String;
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 String _stp_string_init (int num)
00043 {
00044 int global = 0;
00045 String str;
00046
00047 if (num < 0) {
00048 num = -num;
00049 global = 1;
00050 }
00051
00052 if (num >= STP_NUM_STRINGS) {
00053 _stp_log ("_stp_string_init internal error: requested string exceeded allocated number");
00054 return NULL;
00055 }
00056
00057 if (global)
00058 str = &_stp_string[num][0];
00059 else
00060 str = &_stp_string[num][smp_processor_id()];
00061
00062 str->global = global;
00063 str->len = 0;
00064 return str;
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 void _stp_sprintf (String str, const char *fmt, ...)
00078 {
00079 int num;
00080 va_list args;
00081 va_start(args, fmt);
00082 num = vscnprintf(str->buf + str->len, STP_STRING_SIZE - str->len - 1, fmt, args);
00083 va_end(args);
00084 if (num > 0)
00085 str->len += num;
00086 }
00087
00088
00089
00090
00091
00092 void _stp_vsprintf (String str, const char *fmt, va_list args)
00093 {
00094 int num;
00095 num = vscnprintf(str->buf + str->len, STP_STRING_SIZE - str->len - 1, fmt, args);
00096 if (num > 0)
00097 str->len += num;
00098 }
00099
00100
00101
00102
00103
00104
00105
00106 void _stp_string_cat_cstr (String str1, const char *str2)
00107 {
00108 int num = strlen (str2);
00109 if (num > STP_STRING_SIZE - str1->len - 1)
00110 num = STP_STRING_SIZE - str1->len - 1;
00111 strncpy (str1->buf + str1->len, str2, num+1);
00112 str1->len += num;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121 void _stp_string_cat_string (String str1, String str2)
00122 {
00123 int num = str2->len;
00124 if (num > STP_STRING_SIZE - str1->len - 1)
00125 num = STP_STRING_SIZE - str1->len - 1;
00126 strncpy (str1->buf + str1->len, str2->buf, num);
00127 str1->len += num;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 char * _stp_string_ptr (String str)
00139 {
00140 return str->buf;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150 #define _stp_string_cat(str1, str2) \
00151 ({ \
00152 if (__builtin_types_compatible_p (typeof (str2), char[])) { \
00153 char *x = (char *)str2; \
00154 _str_string_cat_cstr(str1,x); \
00155 } else { \
00156 String x = (String)str2; \
00157 _str_string_cat_string(str1,x); \
00158 } \
00159 })
00160
00161
00162 #endif