summaryrefslogtreecommitdiffstats
path: root/runtime/docs/html/string_8c-source.html
blob: 462913e3dcbb547609ab6272704a56e784272358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>SystemTap: string.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<h1>string.c</h1><a href="string_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="preprocessor">#ifndef _STRING_C_ </span><span class="comment">/* -*- linux-c -*- */</span>
00002 <span class="preprocessor">#define _STRING_C_</span>
00003 <span class="preprocessor"></span>
00004 <span class="preprocessor">#include &lt;linux/config.h&gt;</span>
00005 <span class="comment"></span>
00006 <span class="comment">/** @file string.c</span>
00007 <span class="comment"> * @brief Implements String type.</span>
00008 <span class="comment"> */</span><span class="comment"></span>
00009 <span class="comment">/** @addtogroup string String Functions</span>
00010 <span class="comment"> *</span>
00011 <span class="comment"> * One of the biggest restrictions the library has is that it cannot allocate things like strings off the stack.</span>
00012 <span class="comment"> * It is also not a good idea to dynamically allocate space for strings with kmalloc().  That leaves us with </span>
00013 <span class="comment"> * statically allocated space for strings. This is what is implemented in the String module.  Strings use</span>
00014 <span class="comment"> * preallocated per-cpu buffers and are safe to use (unlike C strings).</span>
00015 <span class="comment"> * @{</span>
00016 <span class="comment"> */</span>
00017 <span class="comment"></span>
00018 <span class="comment">/** Maximum string size allowed in Strings */</span>
00019 <span class="preprocessor">#ifndef STP_STRING_SIZE</span>
<a name="l00020"></a><a class="code" href="group__string.html#ga8">00020</a> <span class="preprocessor"></span><span class="preprocessor">#define STP_STRING_SIZE 2048</span>
00021 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
00022 <span class="preprocessor"></span>
00023 <span class="keyword">struct </span>string {
00024         <span class="keywordtype">short</span> len;
00025         <span class="keywordtype">short</span> global;
00026         <span class="keywordtype">char</span> buf[<a class="code" href="group__string.html#ga8">STP_STRING_SIZE</a>];
00027 };
00028 
00029 <span class="keyword">static</span> <span class="keyword">struct </span>string _stp_string[STP_NUM_STRINGS][NR_CPUS];
00030 
00031 <span class="keyword">typedef</span> <span class="keyword">struct </span>string *String;
00032 <span class="comment"></span>
00033 <span class="comment">/** Initialize a String for our use.</span>
00034 <span class="comment"> * This grabs one of the global Strings for our use.</span>
00035 <span class="comment"> *</span>
00036 <span class="comment"> * @param num Number of the preallocated String to use. </span>
00037 <span class="comment"> * #STP_NUM_STRINGS are statically allocated for our use. The</span>
00038 <span class="comment"> * translator (or author) should be sure to grab a free one.</span>
00039 <span class="comment"> * @todo Global (and static) Strings not implemented yet. </span>
00040 <span class="comment"> */</span>
00041 
<a name="l00042"></a><a class="code" href="group__string.html#ga2">00042</a> String <a class="code" href="group__string.html#ga2">_stp_string_init</a> (<span class="keywordtype">int</span> num)
00043 {
00044         <span class="keywordtype">int</span> global = 0;
00045         String str;
00046 
00047         <span class="keywordflow">if</span> (num  &lt; 0) {
00048                 num = -num;
00049                 global = 1;
00050         }
00051         
00052         <span class="keywordflow">if</span> (num &gt;= STP_NUM_STRINGS) {
00053                 <a class="code" href="group__io.html#ga2">_stp_log</a> (<span class="stringliteral">"_stp_string_init internal error: requested string exceeded allocated number"</span>);
00054                 <span class="keywordflow">return</span> NULL;
00055         }
00056 
00057         <span class="keywordflow">if</span> (global)
00058                 str = &amp;_stp_string[num][0];
00059         <span class="keywordflow">else</span>
00060                 str = &amp;_stp_string[num][smp_processor_id()];
00061 
00062         str-&gt;global = global;
00063         str-&gt;len = 0;
00064         <span class="keywordflow">return</span> str;
00065 }
00066 
00067 <span class="comment"></span>
00068 <span class="comment">/** Sprintf into a String.</span>
00069 <span class="comment"> * Like printf, except output goes into a String.</span>
00070 <span class="comment"> * Safe because overflowing the buffer is not allowed.</span>
00071 <span class="comment"> * Size is limited by length of String, #STP_STRING_SIZE.</span>
00072 <span class="comment"> *</span>
00073 <span class="comment"> * @param str String</span>
00074 <span class="comment"> * @param fmt A printf-style format string followed by a </span>
00075 <span class="comment"> * variable number of args.</span>
00076 <span class="comment"> */</span>
<a name="l00077"></a><a class="code" href="group__string.html#ga3">00077</a> <span class="keywordtype">void</span> <a class="code" href="group__string.html#ga3">_stp_sprintf</a> (String str, <span class="keyword">const</span> <span class="keywordtype">char</span> *fmt, ...)
00078 {
00079         <span class="keywordtype">int</span> num;
00080         va_list args;
00081         va_start(args, fmt);
00082         num = vscnprintf(str-&gt;buf + str-&gt;len, <a class="code" href="group__string.html#ga8">STP_STRING_SIZE</a> - str-&gt;len - 1, fmt, args);
00083         va_end(args);
00084         <span class="keywordflow">if</span> (num &gt; 0)
00085                 str-&gt;len += num;
00086 }
00087 <span class="comment"></span>
00088 <span class="comment">/** Vsprintf into a String</span>
00089 <span class="comment"> * Use this if your function already has a va_list.</span>
00090 <span class="comment"> * You probably want _stp_sprintf().</span>
00091 <span class="comment"> */</span>
<a name="l00092"></a><a class="code" href="group__string.html#ga4">00092</a> <span class="keywordtype">void</span> <a class="code" href="group__string.html#ga4">_stp_vsprintf</a> (String str, <span class="keyword">const</span> <span class="keywordtype">char</span> *fmt, va_list args)
00093 {
00094         <span class="keywordtype">int</span> num;
00095         num = vscnprintf(str-&gt;buf + str-&gt;len, <a class="code" href="group__string.html#ga8">STP_STRING_SIZE</a> - str-&gt;len - 1, fmt, args);
00096         <span class="keywordflow">if</span> (num &gt; 0)
00097                 str-&gt;len += num;
00098 }
00099 <span class="comment"></span>
00100 <span class="comment">/** ConCATenate (append) a C string to a String.</span>
00101 <span class="comment"> * Like strcat().</span>
00102 <span class="comment"> * @param str1 String</span>
00103 <span class="comment"> * @param str2 C string (char *)</span>
00104 <span class="comment"> * @sa _stp_string_cat</span>
00105 <span class="comment"> */</span>
<a name="l00106"></a><a class="code" href="group__string.html#ga5">00106</a> <span class="keywordtype">void</span> <a class="code" href="group__string.html#ga5">_stp_string_cat_cstr</a> (String str1, <span class="keyword">const</span> <span class="keywordtype">char</span> *str2)
00107 {
00108         <span class="keywordtype">int</span> num = strlen (str2);
00109         <span class="keywordflow">if</span> (num &gt; <a class="code" href="group__string.html#ga8">STP_STRING_SIZE</a> - str1-&gt;len - 1)
00110                 num = <a class="code" href="group__string.html#ga8">STP_STRING_SIZE</a> - str1-&gt;len - 1;
00111         strncpy (str1-&gt;buf + str1-&gt;len, str2, num+1);
00112         str1-&gt;len += num;
00113 }
00114 <span class="comment"></span>
00115 <span class="comment">/** ConCATenate (append) a String to a String.</span>
00116 <span class="comment"> * Like strcat().</span>
00117 <span class="comment"> * @param str1 String</span>
00118 <span class="comment"> * @param str2 String</span>
00119 <span class="comment"> * @sa _stp_string_cat</span>
00120 <span class="comment"> */</span>
<a name="l00121"></a><a class="code" href="group__string.html#ga6">00121</a> <span class="keywordtype">void</span> <a class="code" href="group__string.html#ga6">_stp_string_cat_string</a> (String str1, String str2)
00122 {
00123         <span class="keywordtype">int</span> num = str2-&gt;len;
00124         <span class="keywordflow">if</span> (num &gt; <a class="code" href="group__string.html#ga8">STP_STRING_SIZE</a> - str1-&gt;len - 1)
00125                 num = <a class="code" href="group__string.html#ga8">STP_STRING_SIZE</a> - str1-&gt;len - 1;
00126         strncpy (str1-&gt;buf + str1-&gt;len, str2-&gt;buf, num);
00127         str1-&gt;len += num;
00128 }
00129 <span class="comment"></span>
00130 <span class="comment">/** Get a pointer to String's buffer</span>
00131 <span class="comment"> * For rare cases when a C string is needed and you have a String.</span>
00132 <span class="comment"> * One example is when you want to print a String with _stp_printf().</span>
00133 <span class="comment"> * @param str String</span>
00134 <span class="comment"> * @returns A C string (char *)</span>
00135 <span class="comment"> * @note Readonly. Don't write to this pointer or it will mess up</span>
00136 <span class="comment"> * the internal String state and probably mess up your output or crash something.</span>
00137 <span class="comment"> */</span>
<a name="l00138"></a><a class="code" href="group__string.html#ga7">00138</a> <span class="keywordtype">char</span> * <a class="code" href="group__string.html#ga7">_stp_string_ptr</a> (String str)
00139 {
00140         <span class="keywordflow">return</span> str-&gt;buf;
00141 }
00142 
00143 <span class="comment"></span>
00144 <span class="comment">/** ConCATenate (append) a String or C string to a String.</span>
00145 <span class="comment"> * This macro selects the proper function to call.</span>
00146 <span class="comment"> * @param str1 A String</span>
00147 <span class="comment"> * @param str2 A String or C string (char *)</span>
00148 <span class="comment"> * @sa _stp_string_cat_cstr _stp_string_cat_string</span>
00149 <span class="comment"> */</span>
<a name="l00150"></a><a class="code" href="group__string.html#ga9">00150</a> <span class="preprocessor">#define _stp_string_cat(str1, str2)                                     \</span>
00151 <span class="preprocessor">  ({                                                            \</span>
00152 <span class="preprocessor">          if (__builtin_types_compatible_p (typeof (str2), char[])) {   \</span>
00153 <span class="preprocessor">                  char *x = (char *)str2;                               \</span>
00154 <span class="preprocessor">                  _str_string_cat_cstr(str1,x);                         \</span>
00155 <span class="preprocessor">          } else {                                                      \</span>
00156 <span class="preprocessor">                  String x = (String)str2;                              \</span>
00157 <span class="preprocessor">                  _str_string_cat_string(str1,x);                       \</span>
00158 <span class="preprocessor">          }                                                             \</span>
00159 <span class="preprocessor">  })</span>
00160 <span class="preprocessor"></span><span class="comment"></span>
00161 <span class="comment">/** @} */</span>
00162 <span class="preprocessor">#endif </span><span class="comment">/* _STRING_C_ */</span>
</pre></div></body></html>