Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

alloc.h

00001 /* -*- linux-c -*- */
00002 
00003 enum errorcode { ERR_NONE=0, ERR_NO_MEM };
00004 enum errorcode _stp_error = ERR_NONE;
00005 
00006 /** Allocates memory within a probe.
00007  * This is used for small allocations from within a running
00008  * probe where the process cannot sleep. 
00009  * @param len Number of bytes to allocate.
00010  * @return a valid pointer on success or NULL on failure.
00011  * @bug Currently uses kmalloc (GFP_ATOMIC).
00012  */
00013 
00014 void *_stp_alloc(size_t len)
00015 {
00016         void *ptr = kmalloc(len, GFP_ATOMIC);
00017         if (unlikely(ptr == NULL))
00018                 _stp_error = ERR_NO_MEM;
00019         return ptr;
00020 }
00021 
00022 /** Allocates and clears memory within a probe.
00023  * This is used for small allocations from within a running
00024  * probe where the process cannot sleep. 
00025  * @param len Number of bytes to allocate.
00026  * @return a valid pointer on success or NULL on failure.
00027  * @bug Currently uses kmalloc (GFP_ATOMIC).
00028  */
00029 
00030 void *_stp_calloc(size_t len)
00031 {
00032         void *ptr = _stp_alloc(len);
00033         if (likely(ptr))
00034                 memset(ptr, 0, len);
00035         return ptr;
00036 }
00037 
00038 /** Allocates and clears memory outside a probe.
00039  * This is typically used in the module initialization to
00040  * allocate new maps, lists, etc.
00041  * @param len Number of bytes to allocate.
00042  * @return a valid pointer on success or NULL on failure.
00043  */
00044 
00045 void *_stp_valloc(size_t len)
00046 {
00047         void *ptr = vmalloc(len);
00048         if (likely(ptr))
00049                 memset(ptr, 0, len);
00050         else
00051                 _stp_error = ERR_NO_MEM;
00052         return ptr;
00053 }
00054 
00055 /** Frees memory allocated by _stp_alloc or _stp_calloc.
00056  * @param ptr pointer to memory to free
00057  */
00058 
00059 void _stp_free(void *ptr)
00060 {
00061         if (likely(ptr))
00062                 kfree(ptr);
00063 }
00064 
00065 /** Frees memory allocated by _stp_valloc.
00066  * @param ptr pointer to memory to free
00067  */
00068 
00069 void _stp_vfree(void *ptr)
00070 {
00071         if (likely(ptr))
00072                 vfree(ptr);
00073 }

Generated on Tue Mar 22 00:32:02 2005 for SystemTap.