blob: 7d2a33103fe9fcfad48e05477e810d258befe57a (
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
|
<!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: alloc.h 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 Page</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div>
<h1>alloc.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* -*- linux-c -*- */</span>
00002
00003 <span class="keyword">enum</span> errorcode { ERR_NONE=0, ERR_NO_MEM };
00004 <span class="keyword">enum</span> errorcode _stp_error = ERR_NONE;
00005 <span class="comment"></span>
00006 <span class="comment">/** Allocates memory within a probe.</span>
00007 <span class="comment"> * This is used for small allocations from within a running</span>
00008 <span class="comment"> * probe where the process cannot sleep. </span>
00009 <span class="comment"> * @param len Number of bytes to allocate.</span>
00010 <span class="comment"> * @return a valid pointer on success or NULL on failure.</span>
00011 <span class="comment"> * @bug Currently uses kmalloc (GFP_ATOMIC).</span>
00012 <span class="comment"> */</span>
00013
00014 <span class="keywordtype">void</span> *_stp_alloc(size_t len)
00015 {
00016 <span class="keywordtype">void</span> *ptr = kmalloc(len, GFP_ATOMIC);
00017 <span class="keywordflow">if</span> (unlikely(ptr == NULL))
00018 _stp_error = ERR_NO_MEM;
00019 return ptr;
00020 }
00021 <span class="comment"></span>
00022 <span class="comment">/** Allocates and clears memory within a probe.</span>
00023 <span class="comment"> * This is used for small allocations from within a running</span>
00024 <span class="comment"> * probe where the process cannot sleep. </span>
00025 <span class="comment"> * @param len Number of bytes to allocate.</span>
00026 <span class="comment"> * @return a valid pointer on success or NULL on failure.</span>
00027 <span class="comment"> * @bug Currently uses kmalloc (GFP_ATOMIC).</span>
00028 <span class="comment"> */</span>
00029
00030 <span class="keywordtype">void</span> *_stp_calloc(size_t len)
00031 {
00032 <span class="keywordtype">void</span> *ptr = _stp_alloc(len);
00033 <span class="keywordflow">if</span> (likely(ptr))
00034 memset(ptr, 0, len);
00035 return ptr;
00036 }
00037 <span class="comment"></span>
00038 <span class="comment">/** Allocates and clears memory outside a probe.</span>
00039 <span class="comment"> * This is typically used in the module initialization to</span>
00040 <span class="comment"> * allocate new maps, lists, etc.</span>
00041 <span class="comment"> * @param len Number of bytes to allocate.</span>
00042 <span class="comment"> * @return a valid pointer on success or NULL on failure.</span>
00043 <span class="comment"> */</span>
00044
00045 <span class="keywordtype">void</span> *_stp_valloc(size_t len)
00046 {
00047 <span class="keywordtype">void</span> *ptr = vmalloc(len);
00048 <span class="keywordflow">if</span> (likely(ptr))
00049 memset(ptr, 0, len);
00050 else
00051 _stp_error = ERR_NO_MEM;
00052 return ptr;
00053 }
00054 <span class="comment"></span>
00055 <span class="comment">/** Frees memory allocated by _stp_alloc or _stp_calloc.</span>
00056 <span class="comment"> * @param ptr pointer to memory to free</span>
00057 <span class="comment"> */</span>
00058
00059 <span class="keywordtype">void</span> _stp_free(<span class="keywordtype">void</span> *ptr)
00060 {
00061 <span class="keywordflow">if</span> (likely(ptr))
00062 kfree(ptr);
00063 }
00064 <span class="comment"></span>
00065 <span class="comment">/** Frees memory allocated by _stp_valloc.</span>
00066 <span class="comment"> * @param ptr pointer to memory to free</span>
00067 <span class="comment"> */</span>
00068
00069 <span class="keywordtype">void</span> _stp_vfree(<span class="keywordtype">void</span> *ptr)
00070 {
00071 <span class="keywordflow">if</span> (likely(ptr))
00072 vfree(ptr);
00073 }
</pre></div><hr size="1"><address style="align: right;"><small>
Generated on Tue Mar 22 00:32:02 2005 for SystemTap.</small></body>
</html>
|